Skip to content

Commit

Permalink
LPs default to priority fee selection
Browse files Browse the repository at this point in the history
  • Loading branch information
TalDerei committed Jan 16, 2025
1 parent 0ae0173 commit 15f8399
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions packages/services/src/view-service/fees.ts
Original file line number Diff line number Diff line change
@@ -3,6 +3,16 @@ import { TransactionPlannerRequest } from '@penumbra-zone/protobuf/penumbra/view
import { assetIdFromBaseDenom } from '@penumbra-zone/wasm/asset';
import { IndexedDbInterface } from '@penumbra-zone/types/indexed-db';

// TODO: change other transaction planner request types to default to priority fee selection
// that's agonsitic to the underlying action.
const prioritySelection = (request: TransactionPlannerRequest): boolean => {
return (
request.positionOpens.length > 0 ||
request.positionCloses.length > 0 ||
request.positionWithdraws.length > 0
);
};

// Attempts to extract a fee token, with priority in descending order, from the assets used
// in the actions of the transaction planner request (TPR). If no fee token is found from the
// specified assets, it falls back to checking the gas prices table for an asset with a positive balance.
@@ -89,37 +99,27 @@ export const extractAltFee = async (
}
}

const positionOpen = request.positionOpens
.map(assetIn => assetIn.position?.phi?.pair?.asset1)
.find(Boolean);
const positionOpen = request.positionOpens.map(assetIn => assetIn.position).find(Boolean);
if (positionOpen) {
const assetId = await getAssetFromGasPriceTable(request, indexedDb, swapAsset);
const assetId = await getAssetFromGasPriceTable(request, indexedDb);
if (assetId) {
return assetId;
}
}

const positionClose = request.positionCloses.map(a => a.positionId).find(Boolean);
if (positionClose) {
const closePosition = await indexedDb.getPosition(positionClose);
if (closePosition?.phi?.pair?.asset1) {
const inputAssetId = closePosition.phi.pair.asset1;
const assetId = await getAssetFromGasPriceTable(request, indexedDb, inputAssetId);
if (assetId) {
return assetId;
}
const assetId = await getAssetFromGasPriceTable(request, indexedDb);
if (assetId) {
return assetId;
}
}

const positonWithdraw = request.positionWithdraws.map(a => a.positionId).find(Boolean);
if (positonWithdraw) {
const withdrawPosition = await indexedDb.getPosition(positonWithdraw);
if (withdrawPosition?.phi?.pair?.asset1) {
const inputAssetId = withdrawPosition.phi.pair.asset1;
const assetId = await getAssetFromGasPriceTable(request, indexedDb, inputAssetId);
if (assetId) {
return assetId;
}
const assetId = await getAssetFromGasPriceTable(request, indexedDb);
if (assetId) {
return assetId;
}
}

@@ -149,13 +149,15 @@ export const getAssetFromGasPriceTable = async (
const altGasPrices = await indexedDb.getAltGasPrices();

// If a specific asset ID is provided, extracted from the transaction request, check its balance is
// positive and GasPrices for that asset exist.
if (assetId) {
const balance = await indexedDb.hasTokenBalance(request.source.account, assetId);
// This check ensures that the alternative fee token is a valid fee token, for example, TestUSD is not.
const isInGasTable = altGasPrices.find(gp => gp.assetId?.equals(assetId));
if (balance && isInGasTable) {
return assetId;
// positive and GasPrices for that asset exist – valid only for 'spend' and 'output' transaction planner requests.
if (!prioritySelection(request)) {
if (assetId) {
const balance = await indexedDb.hasTokenBalance(request.source.account, assetId);
// This check ensures that the alternative fee token is a valid fee token, for example, TestUSD is not.
const isInGasTable = altGasPrices.find(gp => gp.assetId?.equals(assetId));
if (balance && isInGasTable) {
return assetId;
}
}
}

0 comments on commit 15f8399

Please sign in to comment.