Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(aptos): improve unit tests #8784

Merged
merged 3 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/ledger-live-desktop/static/i18n/en/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
4 changes: 2 additions & 2 deletions apps/ledger-live-mobile/src/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions libs/ledger-live-common/src/families/aptos/errors.ts
Original file line number Diff line number Diff line change
@@ -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");
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
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 {
SequenceNumberTooNewError,
SequenceNumberTooOldError,
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();
hedi-edelbloute marked this conversation as resolved.
Show resolved Hide resolved

transaction.fees = null;
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 = transaction.recipient;

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 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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import type { Transaction } from "./types";

import { isValidAddress } from "./logic";
import {
SequenseNumberTooNewError,
SequenseNumberTooOldError,
SequenceNumberTooNewError,
SequenceNumberTooOldError,
TransactionExpiredError,
} from "./errors";

Expand Down Expand Up @@ -47,7 +47,7 @@ const getTransactionStatus = async (a: Account, t: Transaction): Promise<Transac
} else if (!isValidAddress(t.recipient)) {
errors.recipient = new InvalidAddress("", { currencyName: a.currency.name });
} else if (t.recipient === a.freshAddress) {
errors.recepient = new InvalidAddressBecauseDestinationIsAlsoSource();
errors.recipient = new InvalidAddressBecauseDestinationIsAlsoSource();
}

if (
Expand All @@ -68,9 +68,9 @@ const getTransactionStatus = async (a: Account, t: Transaction): Promise<Transac

if (t.errors?.sequenceNumber) {
if (t.errors.sequenceNumber.includes("TOO_OLD")) {
errors.sequenceNumber = new SequenseNumberTooOldError();
errors.sequenceNumber = new SequenceNumberTooOldError();
} else if (t.errors.sequenceNumber.includes("TOO_NEW")) {
errors.sequenceNumber = new SequenseNumberTooNewError();
errors.sequenceNumber = new SequenceNumberTooNewError();
}
}

Expand Down
Loading