Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
jonator committed May 31, 2024
1 parent 99c43da commit 796296b
Showing 1 changed file with 123 additions and 0 deletions.
123 changes: 123 additions & 0 deletions packages/tx/src/__tests__/gas.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,129 @@ describe("getGasFeeAmount", () => {
expect(gasAmount.isNeededForTx).toBe(false);
});

it("should the correct gas amount with an alternative fee token that is the lesser of the spent amounts", async () => {
const gasLimit = 1000;
const chainId = "osmosis-1";
const address = "osmo1...";
const baseFee = 0.04655;
const altTokenSpotPrice = 1;

(queryBalances as jest.Mock).mockResolvedValue({
balances: [
{
denom: "uosmo",
amount: "1",
},
{
// ATOM
denom:
"ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2",
amount: "1000",
},
{
denom: "uion",
amount: "1000000",
},
],
} as Awaited<ReturnType<typeof queryBalances>>);
(queryFeesBaseGasPrice as jest.Mock).mockResolvedValue({
base_fee: baseFee.toString(),
} as Awaited<ReturnType<typeof queryFeesBaseGasPrice>>);
(queryFeeTokens as jest.Mock).mockResolvedValue({
fee_tokens: [
{
// ATOM
denom:
"ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2",
poolID: 1,
},
{
denom: "uion",
poolID: 2,
},
],
} as Awaited<ReturnType<typeof queryFeeTokens>>);
(queryFeesBaseDenom as jest.Mock).mockResolvedValue({
base_denom: "uosmo",
} as Awaited<ReturnType<typeof queryFeesBaseDenom>>);
(queryFeeTokenSpotPrice as jest.Mock).mockImplementation(({ denom }) => {
// return the same spot price to isolate the differences in amounts
if (
denom === "uion" ||
denom ===
"ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2"
) {
return Promise.resolve({
pool_id: "2",
spot_price: altTokenSpotPrice.toString(),
} as Awaited<ReturnType<typeof queryFeeTokenSpotPrice>>);
}
throw new Error("Mocked implementation got an unexpected fee denom");
});

const gasMultiplier = 1.5;
const coinsSpent = [
{ denom: "uion", amount: "1000" },
{
// notice smaller amount is second
denom:
"ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2",
amount: "100",
},
];

const gasAmount = (
await getGasFeeAmount({
chainId,
chainList: MockChains,
gasLimit: gasLimit.toString(),
bech32Address: address,
gasMultiplier,
coinsSpent,
})
)[0];

const expectedGasAmount = new Dec(baseFee * gasMultiplier)
.quo(new Dec(altTokenSpotPrice))
.mul(new Dec(1.01))
.mul(new Dec(gasLimit))
.truncate()
.toString();

expect(queryBalances).toBeCalledWith({
chainId,
bech32Address: address,
chainList: MockChains,
});
expect(queryFeesBaseGasPrice).toBeCalledWith({
chainId,
chainList: MockChains,
});
expect(queryFeeTokens).toBeCalledWith({
chainId,
chainList: MockChains,
});
expect(queryFeesBaseDenom).toBeCalledWith({
chainId,
chainList: MockChains,
});
expect(queryFeeTokenSpotPrice).toBeCalledWith({
chainId,
chainList: MockChains,
denom: "uion",
});
expect(queryFeeTokenSpotPrice).toBeCalledWith({
chainId,
chainList: MockChains,
denom:
"ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2",
});

expect(gasAmount.denom).toBe("uion");
expect(gasAmount.amount).toBe(expectedGasAmount);
expect(gasAmount.isNeededForTx).toBe(false);
});

it("should throw InsufficientFeeError when balance is insufficient without Osmosis fee module — no balances", async () => {
const gasLimit = 1000;
const chainId = "cosmoshub-4";
Expand Down

0 comments on commit 796296b

Please sign in to comment.