diff --git a/packages/tx/src/__tests__/events.spec.ts b/packages/tx/src/__tests__/events.spec.ts index 9e4bb1fb15..1fc006ca41 100644 --- a/packages/tx/src/__tests__/events.spec.ts +++ b/packages/tx/src/__tests__/events.spec.ts @@ -63,6 +63,35 @@ describe("getSumTotalSpenderCoinsSpent", () => { expect(coins).toEqual([]); }); + + it("should only total the amounts for the specified sender", () => { + const coins = getSumTotalSpenderCoinsSpent( + "osmo1gtgx92pxk6hvhc3c3g0xlkrwqq6knymu0e0caw", + mockMultipleEvents + ); + + // Expected result is the sum of the amounts for the specified sender, not for osmo13vhcd3xllpvz8tql4dzp8yszxeas8zxpzptyvjttdy7m64kuyz5sv6caqq + const expectedResult = [ + { + denom: + "factory/osmo1z0qrq605sjgcqpylfl4aa6s90x738j7m58wyatt0tdzflg2ha26q67k743/wbtc", + amount: "58573", + }, + { + denom: + "ibc/EA1D43981D5C9A1C4AAEA9C23BB1D4FA126BA9BC7020A25E0AE4AA841EA25DC5", + amount: "21083680327000100", + }, + { + denom: + "ibc/F4A070A6D78496D53127EA85C094A9EC87DFC1F36071B8CCDDBD020F933D213D", + amount: "131211643845355500", + }, + { denom: "uosmo", amount: "95799380" }, + ]; + + expect(coins).toEqual(expectedResult); + }); }); describe("matchRawCoinValue", () => { @@ -225,4 +254,26 @@ const mockMultipleEvents = [ }, ], }, + { + type: "coin_spent", + attributes: [ + { + key: "spender", + value: + "osmo13vhcd3xllpvz8tql4dzp8yszxeas8zxpzptyvjttdy7m64kuyz5sv6caqq", + index: true, + }, + { + key: "amount", + value: + "2605ibc/498A0751C798A0D9A389AA3691123DADA57DAA4FE165D5C75894505B876BA6E4", + index: true, + }, + { + key: "msg_index", + value: "0", + index: true, + }, + ], + }, ]; diff --git a/packages/tx/src/events.ts b/packages/tx/src/events.ts index 6a31b2096f..54937dd1fe 100644 --- a/packages/tx/src/events.ts +++ b/packages/tx/src/events.ts @@ -15,7 +15,8 @@ export function getSumTotalSpenderCoinsSpent( if (type !== "coin_spent") return; if (attributes.length === 0) return; const spendAttribute = attributes.find((attr) => attr.key === "spender"); - if (spendAttribute && spendAttribute.value !== spenderBech32Address) return; + if (!spendAttribute) return; + if (spendAttribute.value !== spenderBech32Address) return; // a comma separated list of coins spent const coinsSpentRawAttribute = attributes.find( diff --git a/packages/tx/src/gas.ts b/packages/tx/src/gas.ts index e45df1bebb..8614bb0e8e 100644 --- a/packages/tx/src/gas.ts +++ b/packages/tx/src/gas.ts @@ -297,9 +297,15 @@ export async function getGasFeeAmount({ bech32Address, }), ]); - const feeBalances = balances.filter((balance) => - chainFeeDenoms.includes(balance.denom) - ); + const feeBalances: { denom: string; amount: string }[] = []; + + // iterate in order of fee denoms + chainFeeDenoms.forEach((denom) => { + const balance = balances.find((balance) => balance.denom === denom); + if (balance) { + feeBalances.push(balance); + } + }); if (!feeBalances.length) { throw new InsufficientFeeError(