From 15f8399f7957248fc3391cc4c4c12448ea76572e Mon Sep 17 00:00:00 2001 From: Tal Derei Date: Thu, 16 Jan 2025 15:26:27 -0800 Subject: [PATCH] LPs default to priority fee selection --- packages/services/src/view-service/fees.ts | 52 +++++++++++----------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/packages/services/src/view-service/fees.ts b/packages/services/src/view-service/fees.ts index f69efefa0..339ed1293 100644 --- a/packages/services/src/view-service/fees.ts +++ b/packages/services/src/view-service/fees.ts @@ -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,11 +99,9 @@ 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; } @@ -101,25 +109,17 @@ export const extractAltFee = async ( 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; + } } }