From cdd609c62f7b635c47413f4fc99028851e6976de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Guimar=C3=A3es?= Date: Tue, 31 Dec 2024 16:39:54 +0000 Subject: [PATCH 1/4] test(aptos): improve unit tests --- .../families/aptos/buildTransaction.test.ts | 33 +++ .../families/aptos/createTransaction.test.ts | 32 +++ .../aptos/estimateMaxSpendable.test.ts | 97 ++++++++ .../families/aptos/getFeesForTransaction.ts | 6 +- .../src/families/aptos/transaction.test.ts | 220 ++++++++++++++++++ 5 files changed, 384 insertions(+), 4 deletions(-) create mode 100644 libs/ledger-live-common/src/families/aptos/buildTransaction.test.ts create mode 100644 libs/ledger-live-common/src/families/aptos/createTransaction.test.ts create mode 100644 libs/ledger-live-common/src/families/aptos/estimateMaxSpendable.test.ts create mode 100644 libs/ledger-live-common/src/families/aptos/transaction.test.ts diff --git a/libs/ledger-live-common/src/families/aptos/buildTransaction.test.ts b/libs/ledger-live-common/src/families/aptos/buildTransaction.test.ts new file mode 100644 index 000000000000..2b48c1687b0a --- /dev/null +++ b/libs/ledger-live-common/src/families/aptos/buildTransaction.test.ts @@ -0,0 +1,33 @@ +import { createFixtureAccount } from "../../mock/fixtures/cryptoCurrencies"; +import createTransaction from "./createTransaction"; +import buildTransaction from "./buildTransaction"; +import { AptosAPI } from "./api"; + +jest.mock("./logic", () => ({ + normalizeTransactionOptions: jest.fn(), + DEFAULT_GAS: 100, + DEFAULT_GAS_PRICE: 200, +})); + +jest.mock("./api", () => { + return { + AptosAPI: function () { + return { + generateTransaction: jest.fn(() => "tx"), + }; + }, + }; +}); + +describe("buildTransaction Test", () => { + it("should return tx", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + const aptosClient = new AptosAPI(account.currency.id); + const result = await buildTransaction(account, transaction, aptosClient); + + const expected = "tx"; + + expect(result).toBe(expected); + }); +}); diff --git a/libs/ledger-live-common/src/families/aptos/createTransaction.test.ts b/libs/ledger-live-common/src/families/aptos/createTransaction.test.ts new file mode 100644 index 000000000000..fbff2ea83902 --- /dev/null +++ b/libs/ledger-live-common/src/families/aptos/createTransaction.test.ts @@ -0,0 +1,32 @@ +import BigNumber from "bignumber.js"; +import createTransaction from "./createTransaction"; + +jest.mock("./logic", () => ({ + DEFAULT_GAS: 100, + DEFAULT_GAS_PRICE: 200, +})); + +describe("createTransaction Test", () => { + it("should return a transaction object", async () => { + const result = createTransaction(); + + const expected = { + family: "aptos", + mode: "send", + amount: BigNumber(0), + recipient: "", + useAllAmount: false, + firstEmulation: true, + options: { + maxGasAmount: "100", + gasUnitPrice: "200", + }, + estimate: { + maxGasAmount: "100", + gasUnitPrice: "200", + }, + }; + + expect(result).toEqual(expected); + }); +}); diff --git a/libs/ledger-live-common/src/families/aptos/estimateMaxSpendable.test.ts b/libs/ledger-live-common/src/families/aptos/estimateMaxSpendable.test.ts new file mode 100644 index 000000000000..4c3840d018aa --- /dev/null +++ b/libs/ledger-live-common/src/families/aptos/estimateMaxSpendable.test.ts @@ -0,0 +1,97 @@ +import { createFixtureAccount } from "../../mock/fixtures/cryptoCurrencies"; +import createTransaction from "./createTransaction"; +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: {}, + })), +})); + +describe("estimateMaxSpendable Test", () => { + describe("spendable balance is lower than the total gas", () => { + it("should return 0", async () => { + const account = createFixtureAccount(); + + const spendableBalance = new BigNumber(0); + + account.spendableBalance = spendableBalance; + + const result = await estimateMaxSpendable({ + account, + }); + + const expected = spendableBalance; + + expect(result.isEqualTo(expected)).toBe(true); + }); + }); + + describe("spendable balance is higher than the total gas", () => { + it("should return spendable amount minus total gas", async () => { + const account = createFixtureAccount(); + + const spendableBalance = new BigNumber(100000); + + account.spendableBalance = spendableBalance; + + const result = await estimateMaxSpendable({ + account, + }); + + const expected = new BigNumber(80000); + + expect(result.isEqualTo(expected)).toBe(true); + }); + }); + + describe("transaction spendable balance is higher than the total gas", () => { + it("should return transaction spendable amount minus total gas", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + const spendableBalance = new BigNumber(1); + + account.spendableBalance = spendableBalance; + + const result = await estimateMaxSpendable({ + account, + parentAccount: account, + transaction, + }); + + const expected = new BigNumber(0); + + expect(result.isEqualTo(expected)).toBe(true); + }); + }); + + describe("transaction spendable balance is higher than the total gas", () => { + it("should return transaction spendable amount minus total gas", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + const spendableBalance = new BigNumber(100000); + + account.spendableBalance = spendableBalance; + + const result = await estimateMaxSpendable({ + account, + parentAccount: account, + transaction, + }); + + const expected = new BigNumber(99998); + + expect(result.isEqualTo(expected)).toBe(true); + }); + }); +}); diff --git a/libs/ledger-live-common/src/families/aptos/getFeesForTransaction.ts b/libs/ledger-live-common/src/families/aptos/getFeesForTransaction.ts index dd2cfe335316..3069be8d2600 100644 --- a/libs/ledger-live-common/src/families/aptos/getFeesForTransaction.ts +++ b/libs/ledger-live-common/src/families/aptos/getFeesForTransaction.ts @@ -123,11 +123,9 @@ export const getEstimatedGas = async ( ): Promise => { const key = getCacheKey(transaction); - if (CACHE.has(key)) { - return CACHE.get(key); + if (!CACHE.has(key)) { + CACHE.set(key, getFee(account, transaction, aptosClient)); } - CACHE.set(key, getFee(account, transaction, aptosClient)); - return CACHE.get(key); }; diff --git a/libs/ledger-live-common/src/families/aptos/transaction.test.ts b/libs/ledger-live-common/src/families/aptos/transaction.test.ts new file mode 100644 index 000000000000..39f0f77eb5d4 --- /dev/null +++ b/libs/ledger-live-common/src/families/aptos/transaction.test.ts @@ -0,0 +1,220 @@ +import BigNumber from "bignumber.js"; +import { createFixtureAccount } from "../../mock/fixtures/cryptoCurrencies"; +import createTransaction from "./createTransaction"; +import { formatTransaction, fromTransactionRaw, toTransactionRaw } from "./transaction"; +import { Transaction, TransactionRaw } from "./types"; + +jest.mock("./logic", () => ({ + DEFAULT_GAS: 100, + DEFAULT_GAS_PRICE: 200, +})); + +describe("transaction Test", () => { + describe("when formatTransaction", () => { + describe("when amount is 0 and fee is 0", () => { + it("should return a transaction SEND to 0xff00 with fees=?", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + transaction.recipient = "0xff00"; + const result = formatTransaction(transaction, account); + + const expected = ` +SEND +TO 0xff00 +with fees=?`; + + expect(result).toBe(expected); + }); + }); + + describe("when amount is 0 and fee is 0.0001", () => { + it("should return a transaction SEND to 0xff00 with fees=0", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + transaction.recipient = "0xff00"; + transaction.fees = new BigNumber("0.0001"); + const result = formatTransaction(transaction, account); + + const expected = ` +SEND +TO 0xff00 +with fees=0`; + + expect(result).toBe(expected); + }); + }); + + describe("when amount is 0 and fee is 0.1", () => { + it("should return a transaction SEND to 0xff00 with fees=0", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + transaction.recipient = "0xff00"; + transaction.fees = new BigNumber("0.1"); + const result = formatTransaction(transaction, account); + + const expected = ` +SEND +TO 0xff00 +with fees=0`; + + expect(result).toBe(expected); + }); + }); + + describe("when amount is 1 and fee is 0.1", () => { + it("should return a transaction SEND to 0xff00 with fees=0", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + transaction.amount = new BigNumber("1"); + transaction.recipient = "0xff00"; + transaction.fees = new BigNumber("0.1"); + const result = formatTransaction(transaction, account); + + const expected = ` +SEND 0 +TO 0xff00 +with fees=0`; + + expect(result).toBe(expected); + }); + }); + + describe("when amount is 10 and fee is 1", () => { + it("should return a transaction SEND to 0xff00 with fees=0", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + transaction.amount = new BigNumber("10"); + transaction.recipient = "0xff00"; + transaction.fees = new BigNumber("1"); + const result = formatTransaction(transaction, account); + + const expected = ` +SEND 0 +TO 0xff00 +with fees=0`; + + expect(result).toBe(expected); + }); + }); + + describe("when amount is 1000 and fee is 1", () => { + it("should return a transaction SEND to 0xff00 with fees=0", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + transaction.amount = new BigNumber("1000"); + transaction.recipient = "0xff00"; + transaction.fees = new BigNumber("1"); + const result = formatTransaction(transaction, account); + + const expected = ` +SEND 0 +TO 0xff00 +with fees=0`; + + expect(result).toBe(expected); + }); + }); + + describe("when using MAX with amount is 1000 and fee is 1", () => { + it("should return a transaction SEND to 0xff00 with fees=0", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + transaction.amount = new BigNumber("1000"); + transaction.useAllAmount = true; + transaction.recipient = "0xff00"; + transaction.fees = new BigNumber("1"); + const result = formatTransaction(transaction, account); + + const expected = ` +SEND MAX +TO 0xff00 +with fees=0`; + + expect(result).toBe(expected); + }); + }); + }); + + describe("when fromTransactionRaw", () => { + it("should return the transaction object", () => { + const txRaw = { + family: "aptos", + mode: "send", + fees: null, + options: "{}", + estimate: "{}", + firstEmulation: "{}", + amount: "0.5", + recipient: "0xff00", + useAllAmount: false, + subAccountId: "0xff01", + recipientDomain: {}, + } as TransactionRaw; + + const result = fromTransactionRaw(txRaw); + + const expected = { + family: "aptos", + amount: new BigNumber("0.5"), + estimate: {}, + firstEmulation: {}, + mode: "send", + options: {}, + recipient: "0xff00", + recipientDomain: {}, + subAccountId: "0xff01", + useAllAmount: false, + }; + + expect(result).toEqual(expected); + }); + }); + + describe("when toTransactionRaw", () => { + it("should return the raw transaction object", () => { + const tx = { + family: "aptos", + amount: new BigNumber("0.5"), + estimate: {}, + firstEmulation: {}, + mode: "send", + options: {}, + recipient: "0xff00", + recipientDomain: {}, + subAccountId: "0xff01", + useAllAmount: false, + } as Transaction; + + const result = toTransactionRaw(tx); + + const expected = { + family: "aptos", + mode: "send", + fees: null, + options: "{}", + estimate: "{}", + firstEmulation: "{}", + amount: "0.5", + recipient: "0xff00", + useAllAmount: false, + subAccountId: "0xff01", + recipientDomain: {}, + } as TransactionRaw; + + expect(result).toEqual(expected); + }); + }); + + describe("when fromTransactionStatusRaw", () => {}); + + describe("when toTransactionStatusRaw", () => {}); + + describe("when formatTransactionStatus", () => {}); +}); From e1f1b5077cb746079b3d35a2493f4beb60216cdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Guimar=C3=A3es?= Date: Fri, 3 Jan 2025 15:41:04 +0000 Subject: [PATCH 2/4] test(aptos): improve unit tests --- .../families/aptos/buildTransaction.test.ts | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) 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 2b48c1687b0a..c65ab1af1b41 100644 --- a/libs/ledger-live-common/src/families/aptos/buildTransaction.test.ts +++ b/libs/ledger-live-common/src/families/aptos/buildTransaction.test.ts @@ -2,9 +2,17 @@ import { createFixtureAccount } from "../../mock/fixtures/cryptoCurrencies"; import createTransaction from "./createTransaction"; import buildTransaction from "./buildTransaction"; import { AptosAPI } from "./api"; +import { normalizeTransactionOptions } from "./logic"; +import { InputEntryFunctionData } from "@aptos-labs/ts-sdk"; +import { TransactionOptions } from "./types"; + +const generateTransaction = jest.fn(() => "tx"); jest.mock("./logic", () => ({ - normalizeTransactionOptions: jest.fn(), + normalizeTransactionOptions: jest.fn(() => ({ + maxGasAmount: "100", + gasUnitPrice: "200", + })), DEFAULT_GAS: 100, DEFAULT_GAS_PRICE: 200, })); @@ -13,7 +21,7 @@ jest.mock("./api", () => { return { AptosAPI: function () { return { - generateTransaction: jest.fn(() => "tx"), + generateTransaction, }; }, }; @@ -29,5 +37,26 @@ describe("buildTransaction Test", () => { const expected = "tx"; expect(result).toBe(expected); + + const mockedNormalizeTransactionOptions = jest.mocked(normalizeTransactionOptions); + + expect(mockedNormalizeTransactionOptions).toHaveBeenCalledTimes(1); + expect(generateTransaction).toHaveBeenCalledTimes(1); + + const generateTransactionArgs: [string, InputEntryFunctionData, TransactionOptions][] = + generateTransaction.mock.calls[0]; + + expect(mockedNormalizeTransactionOptions.mock.calls[0][0]).toEqual({ + maxGasAmount: "100", + gasUnitPrice: "200", + }); + + expect(generateTransactionArgs[0]).toBe("0x01"); + expect(generateTransactionArgs[1]).toEqual({ + function: "0x1::aptos_account::transfer_coins", + typeArguments: ["0x1::aptos_coin::AptosCoin"], + functionArguments: ["", "0"], + }); + expect(generateTransactionArgs[2]).toEqual({ maxGasAmount: "100", gasUnitPrice: "200" }); }); }); From 1d0e7ca6ef8b4f34ff7d938bcadd270d34774892 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Guimar=C3=A3es?= Date: Mon, 6 Jan 2025 10:56:25 +0000 Subject: [PATCH 3/4] test(aptos): improve unit tests --- .../src/families/aptos/transaction.test.ts | 142 +----------------- .../src/families/aptos/transaction.ts | 17 --- 2 files changed, 1 insertion(+), 158 deletions(-) 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 39f0f77eb5d4..b81f95f979ef 100644 --- a/libs/ledger-live-common/src/families/aptos/transaction.test.ts +++ b/libs/ledger-live-common/src/families/aptos/transaction.test.ts @@ -1,7 +1,5 @@ import BigNumber from "bignumber.js"; -import { createFixtureAccount } from "../../mock/fixtures/cryptoCurrencies"; -import createTransaction from "./createTransaction"; -import { formatTransaction, fromTransactionRaw, toTransactionRaw } from "./transaction"; +import { fromTransactionRaw, toTransactionRaw } from "./transaction"; import { Transaction, TransactionRaw } from "./types"; jest.mock("./logic", () => ({ @@ -10,138 +8,6 @@ jest.mock("./logic", () => ({ })); describe("transaction Test", () => { - describe("when formatTransaction", () => { - describe("when amount is 0 and fee is 0", () => { - it("should return a transaction SEND to 0xff00 with fees=?", async () => { - const account = createFixtureAccount(); - const transaction = createTransaction(); - - transaction.recipient = "0xff00"; - const result = formatTransaction(transaction, account); - - const expected = ` -SEND -TO 0xff00 -with fees=?`; - - expect(result).toBe(expected); - }); - }); - - describe("when amount is 0 and fee is 0.0001", () => { - it("should return a transaction SEND to 0xff00 with fees=0", async () => { - const account = createFixtureAccount(); - const transaction = createTransaction(); - - transaction.recipient = "0xff00"; - transaction.fees = new BigNumber("0.0001"); - const result = formatTransaction(transaction, account); - - const expected = ` -SEND -TO 0xff00 -with fees=0`; - - expect(result).toBe(expected); - }); - }); - - describe("when amount is 0 and fee is 0.1", () => { - it("should return a transaction SEND to 0xff00 with fees=0", async () => { - const account = createFixtureAccount(); - const transaction = createTransaction(); - - transaction.recipient = "0xff00"; - transaction.fees = new BigNumber("0.1"); - const result = formatTransaction(transaction, account); - - const expected = ` -SEND -TO 0xff00 -with fees=0`; - - expect(result).toBe(expected); - }); - }); - - describe("when amount is 1 and fee is 0.1", () => { - it("should return a transaction SEND to 0xff00 with fees=0", async () => { - const account = createFixtureAccount(); - const transaction = createTransaction(); - - transaction.amount = new BigNumber("1"); - transaction.recipient = "0xff00"; - transaction.fees = new BigNumber("0.1"); - const result = formatTransaction(transaction, account); - - const expected = ` -SEND 0 -TO 0xff00 -with fees=0`; - - expect(result).toBe(expected); - }); - }); - - describe("when amount is 10 and fee is 1", () => { - it("should return a transaction SEND to 0xff00 with fees=0", async () => { - const account = createFixtureAccount(); - const transaction = createTransaction(); - - transaction.amount = new BigNumber("10"); - transaction.recipient = "0xff00"; - transaction.fees = new BigNumber("1"); - const result = formatTransaction(transaction, account); - - const expected = ` -SEND 0 -TO 0xff00 -with fees=0`; - - expect(result).toBe(expected); - }); - }); - - describe("when amount is 1000 and fee is 1", () => { - it("should return a transaction SEND to 0xff00 with fees=0", async () => { - const account = createFixtureAccount(); - const transaction = createTransaction(); - - transaction.amount = new BigNumber("1000"); - transaction.recipient = "0xff00"; - transaction.fees = new BigNumber("1"); - const result = formatTransaction(transaction, account); - - const expected = ` -SEND 0 -TO 0xff00 -with fees=0`; - - expect(result).toBe(expected); - }); - }); - - describe("when using MAX with amount is 1000 and fee is 1", () => { - it("should return a transaction SEND to 0xff00 with fees=0", async () => { - const account = createFixtureAccount(); - const transaction = createTransaction(); - - transaction.amount = new BigNumber("1000"); - transaction.useAllAmount = true; - transaction.recipient = "0xff00"; - transaction.fees = new BigNumber("1"); - const result = formatTransaction(transaction, account); - - const expected = ` -SEND MAX -TO 0xff00 -with fees=0`; - - expect(result).toBe(expected); - }); - }); - }); - describe("when fromTransactionRaw", () => { it("should return the transaction object", () => { const txRaw = { @@ -211,10 +77,4 @@ with fees=0`; expect(result).toEqual(expected); }); }); - - describe("when fromTransactionStatusRaw", () => {}); - - describe("when toTransactionStatusRaw", () => {}); - - describe("when formatTransactionStatus", () => {}); }); diff --git a/libs/ledger-live-common/src/families/aptos/transaction.ts b/libs/ledger-live-common/src/families/aptos/transaction.ts index 86b51f85e939..28618055d91d 100644 --- a/libs/ledger-live-common/src/families/aptos/transaction.ts +++ b/libs/ledger-live-common/src/families/aptos/transaction.ts @@ -8,22 +8,6 @@ import { toTransactionCommonRaw, toTransactionStatusRawCommon as toTransactionStatusRaw, } from "@ledgerhq/coin-framework/serialization"; -import { Account } from "@ledgerhq/types-live"; -import { formatCurrencyUnit } from "../../currencies"; - -export const formatTransaction = ( - { mode, amount, fees, recipient, useAllAmount }: Transaction, - account: Account, -): string => ` -${mode.toUpperCase()} ${ - useAllAmount - ? "MAX" - : amount.isZero() - ? "" - : " " + formatCurrencyUnit(account.currency.units[0], amount) -} -TO ${recipient} -with fees=${fees ? formatCurrencyUnit(account.currency.units[0], fees) : "?"}`; export const fromTransactionRaw = (t: TransactionRaw): Transaction => { const common = fromTransactionCommonRaw(t); @@ -54,7 +38,6 @@ export const toTransactionRaw = (t: Transaction): TransactionRaw => { }; export default { - formatTransaction, fromTransactionRaw, toTransactionRaw, fromTransactionStatusRaw, From e8881a47d977b9cb6531ba6f35cbe1db05bbca20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Guimar=C3=A3es?= Date: Mon, 6 Jan 2025 11:09:25 +0000 Subject: [PATCH 4/4] test(aptos): improve unit tests --- .../src/families/aptos/transaction.test.ts | 136 +++++++++++++++++- .../src/families/aptos/transaction.ts | 19 +++ 2 files changed, 154 insertions(+), 1 deletion(-) 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 b81f95f979ef..6408d66fc4d2 100644 --- a/libs/ledger-live-common/src/families/aptos/transaction.test.ts +++ b/libs/ledger-live-common/src/families/aptos/transaction.test.ts @@ -1,5 +1,7 @@ import BigNumber from "bignumber.js"; -import { fromTransactionRaw, toTransactionRaw } from "./transaction"; +import { createFixtureAccount } from "../../mock/fixtures/cryptoCurrencies"; +import createTransaction from "./createTransaction"; +import { formatTransaction, fromTransactionRaw, toTransactionRaw } from "./transaction"; import { Transaction, TransactionRaw } from "./types"; jest.mock("./logic", () => ({ @@ -8,6 +10,138 @@ jest.mock("./logic", () => ({ })); describe("transaction Test", () => { + describe("when formatTransaction", () => { + describe("when amount is 0 and fee is 0", () => { + it("should return a transaction SEND to 0xff00 with fees=?", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + transaction.recipient = "0xff00"; + const result = formatTransaction(transaction, account); + + const expected = ` +SEND +TO 0xff00 +with fees=?`; + + expect(result).toBe(expected); + }); + }); + + describe("when amount is 0 and fee is 0.0001", () => { + it("should return a transaction SEND to 0xff00 with fees=0", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + transaction.recipient = "0xff00"; + transaction.fees = new BigNumber("0.0001"); + const result = formatTransaction(transaction, account); + + const expected = ` +SEND +TO 0xff00 +with fees=0`; + + expect(result).toBe(expected); + }); + }); + + describe("when amount is 0 and fee is 0.1", () => { + it("should return a transaction SEND to 0xff00 with fees=0", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + transaction.recipient = "0xff00"; + transaction.fees = new BigNumber("0.1"); + const result = formatTransaction(transaction, account); + + const expected = ` +SEND +TO 0xff00 +with fees=0`; + + expect(result).toBe(expected); + }); + }); + + describe("when amount is 1 and fee is 0.1", () => { + it("should return a transaction SEND to 0xff00 with fees=0", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + transaction.amount = new BigNumber("1"); + transaction.recipient = "0xff00"; + transaction.fees = new BigNumber("0.1"); + const result = formatTransaction(transaction, account); + + const expected = ` +SEND 0 +TO 0xff00 +with fees=0`; + + expect(result).toBe(expected); + }); + }); + + describe("when amount is 10 and fee is 1", () => { + it("should return a transaction SEND to 0xff00 with fees=0", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + transaction.amount = new BigNumber("10"); + transaction.recipient = "0xff00"; + transaction.fees = new BigNumber("1"); + const result = formatTransaction(transaction, account); + + const expected = ` +SEND 0 +TO 0xff00 +with fees=0`; + + expect(result).toBe(expected); + }); + }); + + describe("when amount is 1000 and fee is 1", () => { + it("should return a transaction SEND to 0xff00 with fees=0", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + transaction.amount = new BigNumber("1000"); + transaction.recipient = "0xff00"; + transaction.fees = new BigNumber("1"); + const result = formatTransaction(transaction, account); + + const expected = ` +SEND 0 +TO 0xff00 +with fees=0`; + + expect(result).toBe(expected); + }); + }); + + describe("when using MAX with amount is 1000 and fee is 1", () => { + it("should return a transaction SEND to 0xff00 with fees=0", async () => { + const account = createFixtureAccount(); + const transaction = createTransaction(); + + transaction.amount = new BigNumber("1000"); + transaction.useAllAmount = true; + transaction.recipient = "0xff00"; + transaction.fees = new BigNumber("1"); + const result = formatTransaction(transaction, account); + + const expected = ` +SEND MAX +TO 0xff00 +with fees=0`; + + expect(result).toBe(expected); + }); + }); + }); + describe("when fromTransactionRaw", () => { it("should return the transaction object", () => { const txRaw = { diff --git a/libs/ledger-live-common/src/families/aptos/transaction.ts b/libs/ledger-live-common/src/families/aptos/transaction.ts index 28618055d91d..b9ddbadf948a 100644 --- a/libs/ledger-live-common/src/families/aptos/transaction.ts +++ b/libs/ledger-live-common/src/families/aptos/transaction.ts @@ -8,6 +8,24 @@ import { toTransactionCommonRaw, toTransactionStatusRawCommon as toTransactionStatusRaw, } from "@ledgerhq/coin-framework/serialization"; +import { Account } from "@ledgerhq/types-live"; +import { formatCurrencyUnit } from "../../currencies"; + +export const formatTransaction = ( + { mode, amount, fees, recipient, useAllAmount }: Transaction, + account: Account, +): string => { + return ` +${mode.toUpperCase()} ${ + useAllAmount + ? "MAX" + : amount.isZero() + ? "" + : " " + formatCurrencyUnit(account.currency.units[0], amount) + } +TO ${recipient} +with fees=${fees ? formatCurrencyUnit(account.currency.units[0], fees) : "?"}`; +}; export const fromTransactionRaw = (t: TransactionRaw): Transaction => { const common = fromTransactionCommonRaw(t); @@ -38,6 +56,7 @@ export const toTransactionRaw = (t: Transaction): TransactionRaw => { }; export default { + formatTransaction, fromTransactionRaw, toTransactionRaw, fromTransactionStatusRaw,