UNPKG

@ledgerhq/coin-aptos

Version:
280 lines 13 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const bignumber_js_1 = __importDefault(require("bignumber.js")); const bridge_fixture_1 = require("../../bridge/bridge.fixture"); const getFeesForTransaction = __importStar(require("../../bridge/getFeesForTransaction")); const network_1 = require("../../network"); let simulateTransaction = jest.fn(); jest.mock("../../network", () => { return { AptosAPI: function () { return { estimateGasPrice: jest.fn(() => ({ gas_estimate: 101 })), generateTransaction: jest.fn(() => "tx"), simulateTransaction, getAccount: jest.fn(() => ({ sequence_number: "123" })), }; }, }; }); jest.mock("@aptos-labs/ts-sdk", () => { return { Ed25519PublicKey: jest.fn(), }; }); const mockedGetTokenAccount = jest.fn(); describe("getFeesForTransaction Test", () => { beforeEach(() => { jest.mock("../../bridge/logic", () => ({ DEFAULT_GAS: 201, DEFAULT_GAS_PRICE: 101, ESTIMATE_GAS_MUL: 1, normalizeTransactionOptions: jest.fn(), getTokenAccount: mockedGetTokenAccount, })); }); describe("when using getFee", () => { describe("with vm_status as INSUFFICIENT_BALANCE", () => { it("should return a fee estimation object", async () => { simulateTransaction = jest.fn(() => [ { success: false, vm_status: ["INSUFFICIENT_BALANCE"], expiration_timestamp_secs: 5, gas_used: "202", gas_unit_price: "102", }, ]); mockedGetTokenAccount.mockReturnValue(undefined); const account = (0, bridge_fixture_1.createFixtureAccount)(); const transaction = (0, bridge_fixture_1.createFixtureTransaction)(); const aptosClient = new network_1.AptosAPI(account.currency.id); transaction.amount = new bignumber_js_1.default(1); account.xpub = "xpub"; account.spendableBalance = new bignumber_js_1.default(100000000); const result = await getFeesForTransaction.getFee(account, transaction, aptosClient); const expected = { fees: new bignumber_js_1.default(22644), estimate: { maxGasAmount: "222", gasUnitPrice: "102", }, errors: {}, }; expect(result).toEqual(expected); }); it("should return a fee estimation object for the token transaction", async () => { simulateTransaction = jest.fn(() => [ { success: true, vm_status: [], expiration_timestamp_secs: 5, gas_used: "202", gas_unit_price: "102", }, ]); mockedGetTokenAccount.mockReturnValue(undefined); const account = (0, bridge_fixture_1.createFixtureAccountWithSubAccount)("coin"); const transaction = (0, bridge_fixture_1.createFixtureTransactionWithSubAccount)(); const aptosClient = new network_1.AptosAPI(account.currency.id); transaction.amount = new bignumber_js_1.default(1); account.xpub = "xpub"; account.spendableBalance = new bignumber_js_1.default(100000000); const result = await getFeesForTransaction.getFee(account, transaction, aptosClient); const expected = { fees: new bignumber_js_1.default(22644), estimate: { maxGasAmount: "222", gasUnitPrice: "102", }, errors: {}, }; expect(result).toEqual(expected); }); }); describe("with vm_status as DUMMY_STATE", () => { it("should return a fee estimation object", () => { simulateTransaction = jest.fn(() => [ { success: false, vm_status: ["DUMMY_STATE"], expiration_timestamp_secs: 5, gas_used: "9", gas_unit_price: "100", }, ]); mockedGetTokenAccount.mockReturnValue(undefined); const account = (0, bridge_fixture_1.createFixtureAccount)(); const transaction = (0, bridge_fixture_1.createFixtureTransaction)(); const aptosClient = new network_1.AptosAPI(account.currency.id); transaction.amount = new bignumber_js_1.default(1); account.xpub = "xpub"; account.spendableBalance = new bignumber_js_1.default(100000000); expect(async () => { await getFeesForTransaction.getFee(account, transaction, aptosClient); }).rejects.toThrow("Simulation failed with following error: DUMMY_STATE"); }); }); describe("with vm_status as MAX_GAS_UNITS_BELOW_MIN_TRANSACTION_GAS_UNITS", () => { it("should return a fee estimation object with GasInsuficeinetBalance error", async () => { simulateTransaction = jest.fn(() => [ { success: false, vm_status: ["MAX_GAS_UNITS_BELOW_MIN_TRANSACTION_GAS_UNITS"], expiration_timestamp_secs: 5, gas_used: "0", gas_unit_price: "100", }, ]); mockedGetTokenAccount.mockReturnValue(undefined); const account = (0, bridge_fixture_1.createFixtureAccountWithSubAccount)("coin"); const transaction = (0, bridge_fixture_1.createFixtureTransactionWithSubAccount)(); const aptosClient = new network_1.AptosAPI(account.currency.id); transaction.amount = new bignumber_js_1.default(1); account.xpub = "xpub"; account.spendableBalance = new bignumber_js_1.default(100000000); const result = await getFeesForTransaction.getFee(account, transaction, aptosClient); const expected = { fees: new bignumber_js_1.default(0), estimate: { maxGasAmount: "0", gasUnitPrice: "100", }, errors: { maxGasAmount: "GasInsufficientBalance", }, }; expect(result).toEqual(expected); }); }); }); describe("when using getEstimatedGas", () => { describe("when key not in cache", () => { it("should return fee", async () => { simulateTransaction = jest.fn(() => [ { success: true, vm_status: [], expiration_timestamp_secs: 5, gas_used: "9", gas_unit_price: "102", }, ]); mockedGetTokenAccount.mockReturnValue(undefined); const account = (0, bridge_fixture_1.createFixtureAccount)(); const transaction = (0, bridge_fixture_1.createFixtureTransaction)(); const aptosClient = new network_1.AptosAPI(account.currency.id); transaction.amount = new bignumber_js_1.default(1); account.xpub = "xpub"; account.spendableBalance = new bignumber_js_1.default(100000000); const result = await getFeesForTransaction.getEstimatedGas(account, transaction, aptosClient); const expected = { errors: {}, estimate: { gasUnitPrice: "102", maxGasAmount: "10", }, fees: new bignumber_js_1.default(1020), }; expect(result).toEqual(expected); }); }); describe("when key is in cache", () => { beforeEach(() => { jest.clearAllMocks(); }); it("should return cached fee", async () => { simulateTransaction = jest.fn(() => [ { success: true, vm_status: [], expiration_timestamp_secs: 5, gas_used: "202", gas_unit_price: "102", }, ]); mockedGetTokenAccount.mockReturnValue(undefined); const account = (0, bridge_fixture_1.createFixtureAccount)(); account.xpub = "xpub"; const transaction = (0, bridge_fixture_1.createFixtureTransaction)(); const aptosClient = new network_1.AptosAPI(account.currency.id); transaction.amount = new bignumber_js_1.default(10); const result1 = await getFeesForTransaction.getEstimatedGas(account, transaction, aptosClient); const result2 = await getFeesForTransaction.getEstimatedGas(account, transaction, aptosClient); expect(simulateTransaction.mock.calls).toHaveLength(1); const expected = { errors: {}, estimate: { gasUnitPrice: "102", maxGasAmount: "222", }, fees: new bignumber_js_1.default(22644), }; expect(result1).toEqual(expected); expect(result2).toEqual(expected); }); }); }); describe("when key is in cache from a token account", () => { beforeEach(() => { jest.clearAllMocks(); }); it("should return cached fee", async () => { simulateTransaction = jest.fn(() => [ { success: true, vm_status: [], expiration_timestamp_secs: 5, gas_used: "202", gas_unit_price: "102", }, ]); mockedGetTokenAccount.mockReturnValue(undefined); const account = (0, bridge_fixture_1.createFixtureAccountWithSubAccount)("coin"); account.xpub = "xpub"; const transaction = (0, bridge_fixture_1.createFixtureTransactionWithSubAccount)(); const aptosClient = new network_1.AptosAPI(account.currency.id); transaction.amount = new bignumber_js_1.default(10); const result1 = await getFeesForTransaction.getEstimatedGas(account, transaction, aptosClient); const result2 = await getFeesForTransaction.getEstimatedGas(account, transaction, aptosClient); expect(simulateTransaction.mock.calls).toHaveLength(1); const expected = { errors: {}, estimate: { gasUnitPrice: "102", maxGasAmount: "222", }, fees: new bignumber_js_1.default(22644), }; expect(result1).toEqual(expected); expect(result2).toEqual(expected); }); }); }); //# sourceMappingURL=getFeesForTransaction.test.js.map