From 526b5db20d31032cafe7421c3b545caa09e31570 Mon Sep 17 00:00:00 2001 From: OleksiiM Date: Wed, 8 Jan 2025 23:45:14 +0000 Subject: [PATCH] Tests update to follow up cache update --- .../src/families/aptos/api/index.test.ts | 3 - .../families/aptos/bridge.integration.test.ts | 2 - .../families/aptos/buildTransaction.test.ts | 4 +- .../families/aptos/createTransaction.test.ts | 5 - .../aptos/estimateMaxSpendable.test.ts | 22 +-- .../aptos/getFeesForTransaction.test.ts | 116 +++++---------- .../aptos/getTransactionStatus.test.ts | 135 ------------------ .../families/aptos/prepareTransaction.test.ts | 16 --- .../src/families/aptos/transaction.test.ts | 8 -- 9 files changed, 46 insertions(+), 265 deletions(-) diff --git a/libs/ledger-live-common/src/families/aptos/api/index.test.ts b/libs/ledger-live-common/src/families/aptos/api/index.test.ts index eff5afc25973..4f1a2f192c09 100644 --- a/libs/ledger-live-common/src/families/aptos/api/index.test.ts +++ b/libs/ledger-live-common/src/families/aptos/api/index.test.ts @@ -376,8 +376,6 @@ describe("Aptos API", () => { options: { maxGasAmount: Number(options.maxGasAmount), gasUnitPrice: Number(options.gasUnitPrice), - accountSequenceNumber: Number(options.sequenceNumber), - expireTimestamp: Number(options.expirationTimestampSecs), }, sender: Account.APTOS_1.address, }); @@ -415,7 +413,6 @@ describe("Aptos API", () => { options: { maxGasAmount: Number(options.maxGasAmount), gasUnitPrice: Number(options.gasUnitPrice), - accountSequenceNumber: Number(options.sequenceNumber), expireTimestamp: 120, }, sender: Account.APTOS_1.address, diff --git a/libs/ledger-live-common/src/families/aptos/bridge.integration.test.ts b/libs/ledger-live-common/src/families/aptos/bridge.integration.test.ts index 838a13e351c4..96b1ef003fdc 100644 --- a/libs/ledger-live-common/src/families/aptos/bridge.integration.test.ts +++ b/libs/ledger-live-common/src/families/aptos/bridge.integration.test.ts @@ -64,10 +64,8 @@ const aptos: CurrenciesData = { family: "aptos", mode: "send", fees: "1100", - //options: '{ "maxGasAmount": "11", "gasUnitPrice": "100" }', estimate: '{ "maxGasAmount": "11", "gasUnitPrice": "100", "sequenceNumber": "1", "expirationTimestampSecs": "1734535375" }', - firstEmulation: "false", errors: "{}", }), expectedStatus: () => diff --git a/libs/ledger-live-common/src/families/aptos/buildTransaction.test.ts b/libs/ledger-live-common/src/families/aptos/buildTransaction.test.ts index c65ab1af1b41..83945a1d5d37 100644 --- a/libs/ledger-live-common/src/families/aptos/buildTransaction.test.ts +++ b/libs/ledger-live-common/src/families/aptos/buildTransaction.test.ts @@ -4,7 +4,7 @@ import buildTransaction from "./buildTransaction"; import { AptosAPI } from "./api"; import { normalizeTransactionOptions } from "./logic"; import { InputEntryFunctionData } from "@aptos-labs/ts-sdk"; -import { TransactionOptions } from "./types"; +import { TransactionEstimate } from "./types"; const generateTransaction = jest.fn(() => "tx"); @@ -43,7 +43,7 @@ describe("buildTransaction Test", () => { expect(mockedNormalizeTransactionOptions).toHaveBeenCalledTimes(1); expect(generateTransaction).toHaveBeenCalledTimes(1); - const generateTransactionArgs: [string, InputEntryFunctionData, TransactionOptions][] = + const generateTransactionArgs: [string, InputEntryFunctionData, TransactionEstimate][] = generateTransaction.mock.calls[0]; expect(mockedNormalizeTransactionOptions.mock.calls[0][0]).toEqual({ diff --git a/libs/ledger-live-common/src/families/aptos/createTransaction.test.ts b/libs/ledger-live-common/src/families/aptos/createTransaction.test.ts index fbff2ea83902..857e84fca4b2 100644 --- a/libs/ledger-live-common/src/families/aptos/createTransaction.test.ts +++ b/libs/ledger-live-common/src/families/aptos/createTransaction.test.ts @@ -16,11 +16,6 @@ describe("createTransaction Test", () => { amount: BigNumber(0), recipient: "", useAllAmount: false, - firstEmulation: true, - options: { - maxGasAmount: "100", - gasUnitPrice: "200", - }, estimate: { maxGasAmount: "100", gasUnitPrice: "200", diff --git a/libs/ledger-live-common/src/families/aptos/estimateMaxSpendable.test.ts b/libs/ledger-live-common/src/families/aptos/estimateMaxSpendable.test.ts index 4c3840d018aa..00d50cb8c9e9 100644 --- a/libs/ledger-live-common/src/families/aptos/estimateMaxSpendable.test.ts +++ b/libs/ledger-live-common/src/families/aptos/estimateMaxSpendable.test.ts @@ -4,16 +4,18 @@ import estimateMaxSpendable from "./estimateMaxSpendable"; import BigNumber from "bignumber.js"; jest.mock("./getFeesForTransaction", () => ({ - getEstimatedGas: jest.fn(() => ({ - fees: new BigNumber(0), - estimate: { - maxGasAmount: 1, - gasUnitPrice: 2, - sequenceNumber: "", - expirationTimestampSecs: "", - }, - errors: {}, - })), + getEstimatedGas: jest.fn(() => + Promise.resolve({ + fees: new BigNumber(0), + estimate: { + maxGasAmount: 1, + gasUnitPrice: 2, + sequenceNumber: "", + expirationTimestampSecs: "", + }, + errors: {}, + }), + ), })); describe("estimateMaxSpendable Test", () => { diff --git a/libs/ledger-live-common/src/families/aptos/getFeesForTransaction.test.ts b/libs/ledger-live-common/src/families/aptos/getFeesForTransaction.test.ts index 2d8945043fc0..461e8301350c 100644 --- a/libs/ledger-live-common/src/families/aptos/getFeesForTransaction.test.ts +++ b/libs/ledger-live-common/src/families/aptos/getFeesForTransaction.test.ts @@ -36,80 +36,6 @@ jest.mock("./logic", () => { describe("getFeesForTransaction Test", () => { describe("when using getFee", () => { - describe("with vm_status as SEQUENCE_NUMBER", () => { - it("should return a fee estimation object", async () => { - simulateTransaction = jest.fn(() => [ - { - success: false, - vm_status: ["SEQUENCE_NUMBER"], - expiration_timestamp_secs: 5, - }, - ]); - - const account = createFixtureAccount(); - const transaction = createTransaction(); - const aptosClient = new AptosAPI(account.currency.id); - - transaction.amount = new BigNumber(1); - account.xpub = "xpub"; - account.spendableBalance = new BigNumber(100000000); - - const result = await getFeesForTransaction.getFee(account, transaction, aptosClient); - - const expected = { - fees: new BigNumber(20301), - estimate: { - maxGasAmount: "201", - gasUnitPrice: "101", - sequenceNumber: "123", - expirationTimestampSecs: "", - }, - errors: { - sequenceNumber: ["SEQUENCE_NUMBER"], - }, - }; - - expect(result).toEqual(expected); - }); - }); - - describe("with vm_status as TRANSACTION_EXPIRED", () => { - it("should return a fee estimation object", async () => { - simulateTransaction = jest.fn(() => [ - { - success: false, - vm_status: ["TRANSACTION_EXPIRED"], - expiration_timestamp_secs: 5, - }, - ]); - - const account = createFixtureAccount(); - const transaction = createTransaction(); - const aptosClient = new AptosAPI(account.currency.id); - - transaction.amount = new BigNumber(1); - account.xpub = "xpub"; - account.spendableBalance = new BigNumber(100000000); - - const result = await getFeesForTransaction.getFee(account, transaction, aptosClient); - - const expected = { - fees: new BigNumber(20301), - estimate: { - maxGasAmount: "201", - gasUnitPrice: "101", - sequenceNumber: "123", - expirationTimestampSecs: "", - }, - errors: { - expirationTimestampSecs: ["TRANSACTION_EXPIRED"], - }, - }; - - expect(result).toEqual(expected); - }); - }); - describe("with vm_status as INSUFFICIENT_BALANCE", () => { it("should return a fee estimation object", async () => { simulateTransaction = jest.fn(() => [ @@ -117,6 +43,8 @@ describe("getFeesForTransaction Test", () => { success: false, vm_status: ["INSUFFICIENT_BALANCE"], expiration_timestamp_secs: 5, + gas_used: "201", + gas_unit_price: "101", }, ]); @@ -135,8 +63,6 @@ describe("getFeesForTransaction Test", () => { estimate: { maxGasAmount: "201", gasUnitPrice: "101", - sequenceNumber: "123", - expirationTimestampSecs: "", }, errors: {}, }; @@ -152,6 +78,8 @@ describe("getFeesForTransaction Test", () => { success: false, vm_status: ["DUMMY_STATE"], expiration_timestamp_secs: 5, + gas_used: "9", + gas_unit_price: "100", }, ]); @@ -173,10 +101,24 @@ describe("getFeesForTransaction Test", () => { describe("when using getEstimatedGas", () => { describe("when key not in cache", () => { it("should return cached fee", async () => { + simulateTransaction = jest.fn(() => [ + { + success: true, + vm_status: [], + expiration_timestamp_secs: 5, + gas_used: "9", + gas_unit_price: "102", + }, + ]); + const account = createFixtureAccount(); const transaction = createTransaction(); const aptosClient = new AptosAPI(account.currency.id); + transaction.amount = new BigNumber(1); + account.xpub = "xpub"; + account.spendableBalance = new BigNumber(100000000); + const result = await getFeesForTransaction.getEstimatedGas( account, transaction, @@ -186,19 +128,17 @@ describe("getFeesForTransaction Test", () => { const expected = { errors: {}, estimate: { - expirationTimestampSecs: "", - gasUnitPrice: "101", - maxGasAmount: "201", - sequenceNumber: "123", + gasUnitPrice: "102", + maxGasAmount: "9", }, - fees: new BigNumber("20301"), + fees: new BigNumber("918"), }; expect(result).toEqual(expected); }); }); - describe("when key is in cache 22", () => { + describe("when key is in cache", () => { beforeEach(() => { jest.clearAllMocks(); }); @@ -212,6 +152,16 @@ describe("getFeesForTransaction Test", () => { transaction.amount = new BigNumber(10); + simulateTransaction = jest.fn(() => [ + { + success: true, + vm_status: [], + expiration_timestamp_secs: 5, + gas_used: "9", + gas_unit_price: "100", + }, + ]); + const result1 = await getFeesForTransaction.getEstimatedGas( account, transaction, @@ -228,10 +178,8 @@ describe("getFeesForTransaction Test", () => { const expected = { errors: {}, estimate: { - expirationTimestampSecs: "", gasUnitPrice: "101", maxGasAmount: "201", - sequenceNumber: "123", }, fees: new BigNumber("20301"), }; diff --git a/libs/ledger-live-common/src/families/aptos/getTransactionStatus.test.ts b/libs/ledger-live-common/src/families/aptos/getTransactionStatus.test.ts index 029a4e2952fe..14c066053786 100644 --- a/libs/ledger-live-common/src/families/aptos/getTransactionStatus.test.ts +++ b/libs/ledger-live-common/src/families/aptos/getTransactionStatus.test.ts @@ -11,11 +11,6 @@ import { NotEnoughBalance, RecipientRequired, } from "@ledgerhq/errors"; -import { - SequenceNumberTooNewError, - SequenceNumberTooOldError, - TransactionExpiredError, -} from "./errors"; describe("getTransactionStatus Test", () => { it("should return errors for AmountRequired", async () => { @@ -155,134 +150,4 @@ describe("getTransactionStatus Test", () => { expect(result).toEqual(expected); }); - - it("should return errors for GasLessThanEstimate", async () => { - const account = createFixtureAccount(); - const transaction = createTransaction(); - - transaction.amount = new BigNumber(2); - transaction.fees = new BigNumber(2); - transaction.recipient = "0x" + "0".repeat(64); - - //transaction.options.maxGasAmount = "50"; - transaction.estimate.maxGasAmount = "100"; - - const result = await getTransactionStatus(account, transaction); - - const expected = { - errors: { - maxGasAmount: new GasLessThanEstimate(), - }, - warnings: {}, - estimatedFees: new BigNumber(2), - amount: new BigNumber(2), - totalSpent: new BigNumber(4), - }; - - expect(result).toEqual(expected); - }); - - it("should return errors for GasLessThanEstimate", async () => { - const account = createFixtureAccount(); - const transaction = createTransaction(); - - transaction.amount = new BigNumber(2); - transaction.fees = new BigNumber(2); - transaction.recipient = "0x" + "0".repeat(64); - - //transaction.options.gasUnitPrice = "50"; - transaction.estimate.gasUnitPrice = "100"; - - const result = await getTransactionStatus(account, transaction); - - const expected = { - errors: { - gasUnitPrice: new GasLessThanEstimate(), - }, - warnings: {}, - estimatedFees: new BigNumber(2), - amount: new BigNumber(2), - totalSpent: new BigNumber(4), - }; - - expect(result).toEqual(expected); - }); - - it("should return errors for SequenceNumberTooOldError", async () => { - const account = createFixtureAccount(); - const transaction = createTransaction(); - - transaction.amount = new BigNumber(2); - transaction.fees = new BigNumber(2); - transaction.recipient = "0x" + "0".repeat(64); - transaction.errors = { - sequenceNumber: "TOO_OLD", - }; - - const result = await getTransactionStatus(account, transaction); - - const expected = { - errors: { - sequenceNumber: new SequenceNumberTooOldError(), - }, - warnings: {}, - estimatedFees: new BigNumber(2), - amount: new BigNumber(2), - totalSpent: new BigNumber(4), - }; - - expect(result).toEqual(expected); - }); - - it("should return errors for SequenceNumberTooNewError", async () => { - const account = createFixtureAccount(); - const transaction = createTransaction(); - - transaction.amount = new BigNumber(2); - transaction.fees = new BigNumber(2); - transaction.recipient = "0x" + "0".repeat(64); - transaction.errors = { - sequenceNumber: "TOO_NEW", - }; - - const result = await getTransactionStatus(account, transaction); - - const expected = { - errors: { - sequenceNumber: new SequenceNumberTooNewError(), - }, - warnings: {}, - estimatedFees: new BigNumber(2), - amount: new BigNumber(2), - totalSpent: new BigNumber(4), - }; - - expect(result).toEqual(expected); - }); - - it("should return errors for TransactionExpiredError", async () => { - const account = createFixtureAccount(); - const transaction = createTransaction(); - - transaction.amount = new BigNumber(2); - transaction.fees = new BigNumber(2); - transaction.recipient = "0x" + "0".repeat(64); - transaction.errors = { - expirationTimestampSecs: "expirationTimestampSecs", - }; - - const result = await getTransactionStatus(account, transaction); - - const expected = { - errors: { - expirationTimestampSecs: new TransactionExpiredError(), - }, - warnings: {}, - estimatedFees: new BigNumber(2), - amount: new BigNumber(2), - totalSpent: new BigNumber(4), - }; - - expect(result).toEqual(expected); - }); }); diff --git a/libs/ledger-live-common/src/families/aptos/prepareTransaction.test.ts b/libs/ledger-live-common/src/families/aptos/prepareTransaction.test.ts index 49bb3ae3393d..434b708438af 100644 --- a/libs/ledger-live-common/src/families/aptos/prepareTransaction.test.ts +++ b/libs/ledger-live-common/src/families/aptos/prepareTransaction.test.ts @@ -40,8 +40,6 @@ describe("Aptos prepareTransaction", () => { recipient: "", useAllAmount: false, fees: new BigNumber(0), - firstEmulation: true, - //options: {}, } as Transaction; }); @@ -89,18 +87,6 @@ describe("Aptos prepareTransaction", () => { expect(result.errors).toEqual({}); }); - it("should set firstEmulation to false after the first call", async () => { - transaction.recipient = "test-recipient"; - transaction.amount = new BigNumber(100); - (getEstimatedGas as jest.Mock).mockResolvedValue({ - fees: new BigNumber(10), - estimate: { maxGasAmount: new BigNumber(200) }, - errors: {}, - }); - - const result = await prepareTransaction(account, transaction); - expect(result.firstEmulation).toBe(false); - }); it("should return the transaction with updated fees and estimate if recipient is set and amount is not zero", async () => { transaction.recipient = "test-recipient"; @@ -120,7 +106,6 @@ describe("Aptos prepareTransaction", () => { it("should set maxGasAmount in options", async () => { transaction.recipient = "test-recipient"; transaction.amount = new BigNumber(100); - transaction.firstEmulation = true; (getEstimatedGas as jest.Mock).mockResolvedValue({ fees: new BigNumber(2000), estimate: { maxGasAmount: new BigNumber(200), gasUnitPrice: new BigNumber(10) }, @@ -129,7 +114,6 @@ describe("Aptos prepareTransaction", () => { const result = await prepareTransaction(account, transaction); expect(new BigNumber(result.estimate.maxGasAmount).isEqualTo(new BigNumber(200))).toBe(true); - expect(result.firstEmulation).toBe(false); }); }); }); diff --git a/libs/ledger-live-common/src/families/aptos/transaction.test.ts b/libs/ledger-live-common/src/families/aptos/transaction.test.ts index 17e0154d6b1c..f6b3e438edb8 100644 --- a/libs/ledger-live-common/src/families/aptos/transaction.test.ts +++ b/libs/ledger-live-common/src/families/aptos/transaction.test.ts @@ -148,9 +148,7 @@ with fees=0`; family: "aptos", mode: "send", fees: null, - //options: "{}", estimate: "{}", - firstEmulation: "{}", amount: "0.5", recipient: "0xff00", useAllAmount: false, @@ -164,9 +162,7 @@ with fees=0`; family: "aptos", amount: new BigNumber("0.5"), estimate: {}, - firstEmulation: {}, mode: "send", - options: {}, recipient: "0xff00", recipientDomain: {}, subAccountId: "0xff01", @@ -183,9 +179,7 @@ with fees=0`; family: "aptos", amount: new BigNumber("0.5"), estimate: {}, - firstEmulation: {}, mode: "send", - //options: {}, recipient: "0xff00", recipientDomain: {}, subAccountId: "0xff01", @@ -198,9 +192,7 @@ with fees=0`; family: "aptos", mode: "send", fees: null, - //options: "{}", estimate: "{}", - firstEmulation: "{}", amount: "0.5", recipient: "0xff00", useAllAmount: false,