Skip to content

Commit

Permalink
fix filter spent coins by sender
Browse files Browse the repository at this point in the history
  • Loading branch information
jonator committed Jun 4, 2024
1 parent e461487 commit 1e0f4ed
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
51 changes: 51 additions & 0 deletions packages/tx/src/__tests__/events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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,
},
],
},
];
3 changes: 2 additions & 1 deletion packages/tx/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
12 changes: 9 additions & 3 deletions packages/tx/src/gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 1e0f4ed

Please sign in to comment.