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): improvement to unit tests #8779

Merged
merged 7 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -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", () => {
hedi-edelbloute marked this conversation as resolved.
Show resolved Hide resolved
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);
});
});
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
hedi-edelbloute marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,9 @@ export const getEstimatedGas = async (
): Promise<IGetEstimatedGasReturnType> => {
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);
};
220 changes: 220 additions & 0 deletions libs/ledger-live-common/src/families/aptos/transaction.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {});
});
Loading