From 56b765cfc6269302433d892e67becda254be86ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Guimar=C3=A3es?= Date: Thu, 2 Jan 2025 09:55:47 +0000 Subject: [PATCH 1/3] test(aptos): improve unit tests --- .../aptos/getTransactionStatus.test.ts | 283 ++++++++++++++++++ .../families/aptos/getTransactionStatus.ts | 2 +- 2 files changed, 284 insertions(+), 1 deletion(-) create mode 100644 libs/ledger-live-common/src/families/aptos/getTransactionStatus.test.ts diff --git a/libs/ledger-live-common/src/families/aptos/getTransactionStatus.test.ts b/libs/ledger-live-common/src/families/aptos/getTransactionStatus.test.ts new file mode 100644 index 000000000000..715f7db76a40 --- /dev/null +++ b/libs/ledger-live-common/src/families/aptos/getTransactionStatus.test.ts @@ -0,0 +1,283 @@ +import BigNumber from "bignumber.js"; +import { createFixtureAccount } from "../../mock/fixtures/cryptoCurrencies"; +import createTransaction from "./createTransaction"; +import getTransactionStatus from "./getTransactionStatus"; +import { + AmountRequired, + FeeNotLoaded, + GasLessThanEstimate, + InvalidAddress, + InvalidAddressBecauseDestinationIsAlsoSource, + NotEnoughBalance, + RecipientRequired, +} from "@ledgerhq/errors"; +import { SequenseNumberTooNewError, SequenseNumberTooOldError, TransactionExpiredError } from "./errors"; + +describe("getTransactionStatus Test", () => { + it("should return errors for AmountRequired", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + transaction.fees = new BigNumber(2); + transaction.recipient = "0x" + "0".repeat(64); + + const result = await getTransactionStatus(account, transaction); + + const expected = { + errors: { + amount: new AmountRequired(), + }, + warnings: {}, + estimatedFees: new BigNumber(2), + amount: new BigNumber(0), + totalSpent: new BigNumber(2), + }; + + expect(result).toEqual(expected); + }); + + it("should return errors for FeeNotLoaded", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + transaction.amount = new BigNumber(2); + transaction.recipient = "0x" + "0".repeat(64); + + const result = await getTransactionStatus(account, transaction); + + const expected = { + errors: { + fees: new FeeNotLoaded(), + }, + warnings: {}, + estimatedFees: new BigNumber(0), + amount: new BigNumber(2), + totalSpent: new BigNumber(2), + }; + + expect(result).toEqual(expected); + }); + + it("should return errors for NotEnoughBalance", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + account.balance = new BigNumber(1); + transaction.recipient = "0x" + "0".repeat(64); + transaction.amount = new BigNumber(2); + transaction.fees = new BigNumber(2); + + const result = await getTransactionStatus(account, transaction); + + const expected = { + errors: { + amount: new NotEnoughBalance(), + }, + warnings: {}, + estimatedFees: new BigNumber(2), + amount: new BigNumber(2), + totalSpent: new BigNumber(4), + }; + + expect(result).toEqual(expected); + }); + + it("should return errors for RecipientRequired", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + transaction.amount = new BigNumber(2); + transaction.fees = new BigNumber(2); + + const result = await getTransactionStatus(account, transaction); + + const expected = { + errors: { + recipient: new RecipientRequired(), + }, + warnings: {}, + estimatedFees: new BigNumber(2), + amount: new BigNumber(2), + totalSpent: new BigNumber(4), + }; + + expect(result).toEqual(expected); + }); + + it("should return errors for InvalidAddress", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + transaction.amount = new BigNumber(2); + transaction.fees = new BigNumber(2); + transaction.recipient = "0x"; + + const result = await getTransactionStatus(account, transaction); + + const expected = { + errors: { + recipient: new InvalidAddress(), + }, + warnings: {}, + estimatedFees: new BigNumber(2), + amount: new BigNumber(2), + totalSpent: new BigNumber(4), + }; + + expect(result).toEqual(expected); + }); + + it("should return errors for InvalidAddressBecauseDestinationIsAlsoSource", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + transaction.amount = new BigNumber(2); + transaction.fees = new BigNumber(2); + transaction.recipient = "0x" + "0".repeat(64); + account.freshAddress = "0x" + "0".repeat(64); + + const result = await getTransactionStatus(account, transaction); + + const expected = { + errors: { + recipient: new InvalidAddressBecauseDestinationIsAlsoSource(), + }, + 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.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 SequenseNumberTooOldError", 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 SequenseNumberTooOldError(), + }, + warnings: {}, + estimatedFees: new BigNumber(2), + amount: new BigNumber(2), + totalSpent: new BigNumber(4), + }; + + expect(result).toEqual(expected); + }); + + it("should return errors for SequenseNumberTooNewError", 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 SequenseNumberTooNewError(), + }, + 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/getTransactionStatus.ts b/libs/ledger-live-common/src/families/aptos/getTransactionStatus.ts index f42e2a0b5523..05a5b773a76d 100644 --- a/libs/ledger-live-common/src/families/aptos/getTransactionStatus.ts +++ b/libs/ledger-live-common/src/families/aptos/getTransactionStatus.ts @@ -47,7 +47,7 @@ const getTransactionStatus = async (a: Account, t: Transaction): Promise Date: Thu, 2 Jan 2025 15:26:37 +0000 Subject: [PATCH 2/3] test(aptos): improve unit tests --- apps/ledger-live-desktop/static/i18n/en/app.json | 4 ++-- .../src/locales/en/common.json | 4 ++-- .../src/families/aptos/errors.ts | 4 ++-- .../families/aptos/getTransactionStatus.test.ts | 16 ++++++++++------ .../src/families/aptos/getTransactionStatus.ts | 8 ++++---- 5 files changed, 20 insertions(+), 16 deletions(-) diff --git a/apps/ledger-live-desktop/static/i18n/en/app.json b/apps/ledger-live-desktop/static/i18n/en/app.json index 4136b56593db..81f179d4dbf1 100644 --- a/apps/ledger-live-desktop/static/i18n/en/app.json +++ b/apps/ledger-live-desktop/static/i18n/en/app.json @@ -5820,10 +5820,10 @@ "title": "Account not scanned by full node", "description": "Please configure your full node to scan for the accounts associated with this device. Your full node must first scan the blockchain for this account before you can add it to your portfolio." }, - "SequenseNumberTooNew": { + "SequenceNumberTooNew": { "title": "Sequence number too new" }, - "SequenseNumberTooOld": { + "SequenceNumberTooOld": { "title": "Sequence number too old", "description": "Sequence number too old" }, diff --git a/apps/ledger-live-mobile/src/locales/en/common.json b/apps/ledger-live-mobile/src/locales/en/common.json index 2f1129dbe46d..276d0686af8c 100644 --- a/apps/ledger-live-mobile/src/locales/en/common.json +++ b/apps/ledger-live-mobile/src/locales/en/common.json @@ -974,10 +974,10 @@ "GasLessThanEstimate": { "title": "This may be too low. Please increase" }, - "SequenseNumberTooNew": { + "SequenceNumberTooNew": { "title": "Sequence number too new" }, - "SequenseNumberTooOld": { + "SequenceNumberTooOld": { "title": "Sequence number too old" }, "TransactionExpired": { diff --git a/libs/ledger-live-common/src/families/aptos/errors.ts b/libs/ledger-live-common/src/families/aptos/errors.ts index ae84f3169171..50f0279260ea 100644 --- a/libs/ledger-live-common/src/families/aptos/errors.ts +++ b/libs/ledger-live-common/src/families/aptos/errors.ts @@ -1,7 +1,7 @@ import { createCustomErrorClass } from "@ledgerhq/errors"; -export const SequenseNumberTooOldError = createCustomErrorClass("SequenseNumberTooOld"); +export const SequenceNumberTooOldError = createCustomErrorClass("SequenceNumberTooOld"); -export const SequenseNumberTooNewError = createCustomErrorClass("SequenseNumberTooNew"); +export const SequenceNumberTooNewError = createCustomErrorClass("SequenceNumberTooNew"); export const TransactionExpiredError = createCustomErrorClass("TransactionExpired"); 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 715f7db76a40..8419ebf40413 100644 --- a/libs/ledger-live-common/src/families/aptos/getTransactionStatus.test.ts +++ b/libs/ledger-live-common/src/families/aptos/getTransactionStatus.test.ts @@ -11,7 +11,11 @@ import { NotEnoughBalance, RecipientRequired, } from "@ledgerhq/errors"; -import { SequenseNumberTooNewError, SequenseNumberTooOldError, TransactionExpiredError } from "./errors"; +import { + SequenceNumberTooNewError, + SequenceNumberTooOldError, + TransactionExpiredError, +} from "./errors"; describe("getTransactionStatus Test", () => { it("should return errors for AmountRequired", async () => { @@ -134,7 +138,7 @@ describe("getTransactionStatus Test", () => { transaction.amount = new BigNumber(2); transaction.fees = new BigNumber(2); transaction.recipient = "0x" + "0".repeat(64); - account.freshAddress = "0x" + "0".repeat(64); + account.freshAddress = transaction.recipient; const result = await getTransactionStatus(account, transaction); @@ -203,7 +207,7 @@ describe("getTransactionStatus Test", () => { expect(result).toEqual(expected); }); - it("should return errors for SequenseNumberTooOldError", async () => { + it("should return errors for SequenceNumberTooOldError", async () => { const account = createFixtureAccount(); const transaction = createTransaction(); @@ -218,7 +222,7 @@ describe("getTransactionStatus Test", () => { const expected = { errors: { - sequenceNumber: new SequenseNumberTooOldError(), + sequenceNumber: new SequenceNumberTooOldError(), }, warnings: {}, estimatedFees: new BigNumber(2), @@ -229,7 +233,7 @@ describe("getTransactionStatus Test", () => { expect(result).toEqual(expected); }); - it("should return errors for SequenseNumberTooNewError", async () => { + it("should return errors for SequenceNumberTooNewError", async () => { const account = createFixtureAccount(); const transaction = createTransaction(); @@ -244,7 +248,7 @@ describe("getTransactionStatus Test", () => { const expected = { errors: { - sequenceNumber: new SequenseNumberTooNewError(), + sequenceNumber: new SequenceNumberTooNewError(), }, warnings: {}, estimatedFees: new BigNumber(2), diff --git a/libs/ledger-live-common/src/families/aptos/getTransactionStatus.ts b/libs/ledger-live-common/src/families/aptos/getTransactionStatus.ts index 05a5b773a76d..2dbb59532b3d 100644 --- a/libs/ledger-live-common/src/families/aptos/getTransactionStatus.ts +++ b/libs/ledger-live-common/src/families/aptos/getTransactionStatus.ts @@ -14,8 +14,8 @@ import type { Transaction } from "./types"; import { isValidAddress } from "./logic"; import { - SequenseNumberTooNewError, - SequenseNumberTooOldError, + SequenceNumberTooNewError, + SequenceNumberTooOldError, TransactionExpiredError, } from "./errors"; @@ -68,9 +68,9 @@ const getTransactionStatus = async (a: Account, t: Transaction): Promise Date: Fri, 3 Jan 2025 08:45:11 +0000 Subject: [PATCH 3/3] test(aptos): improve unit tests --- .../src/families/aptos/getTransactionStatus.test.ts | 1 + 1 file changed, 1 insertion(+) 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 8419ebf40413..bc1a72f4c700 100644 --- a/libs/ledger-live-common/src/families/aptos/getTransactionStatus.test.ts +++ b/libs/ledger-live-common/src/families/aptos/getTransactionStatus.test.ts @@ -44,6 +44,7 @@ describe("getTransactionStatus Test", () => { const account = createFixtureAccount(); const transaction = createTransaction(); + transaction.fees = null; transaction.amount = new BigNumber(2); transaction.recipient = "0x" + "0".repeat(64);