Skip to content

Commit

Permalink
feat(aptos): improve aptos unit tests (#8781)
Browse files Browse the repository at this point in the history
* test(aptos): improve unit tests
  • Loading branch information
jccguimaraes authored Jan 6, 2025
1 parent 4e7b8e3 commit b901ba9
Show file tree
Hide file tree
Showing 2 changed files with 246 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
import BigNumber from "bignumber.js";
import { createFixtureAccount } from "../../mock/fixtures/cryptoCurrencies";
import createTransaction from "./createTransaction";
import * as getFeesForTransaction from "./getFeesForTransaction";
import { AptosAPI } from "./api";

let simulateTransaction = jest.fn();

jest.mock("./api", () => {
return {
AptosAPI: function () {
return {
estimateGasPrice: jest.fn(() => ({ gas_estimate: 101 })),
generateTransaction: jest.fn(() => "tx"),
simulateTransaction,
getAccount: jest.fn(() => ({ sequence_number: "123" })),
};
},
};
});

jest.mock("@aptos-labs/ts-sdk", () => {
return {
Ed25519PublicKey: jest.fn(),
};
});

jest.mock("./logic", () => {
return {
DEFAULT_GAS: 201,
DEFAULT_GAS_PRICE: 101,
ESTIMATE_GAS_MUL: 1,
normalizeTransactionOptions: jest.fn(),
};
});

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(() => [
{
success: false,
vm_status: ["INSUFFICIENT_BALANCE"],
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: {},
};

expect(result).toEqual(expected);
});
});

describe("with vm_status as DUMMY_STATE", () => {
it("should return a fee estimation object", () => {
simulateTransaction = jest.fn(() => [
{
success: false,
vm_status: ["DUMMY_STATE"],
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);

expect(async () => {
await getFeesForTransaction.getFee(account, transaction, aptosClient);
}).rejects.toThrow("Simulation failed with following error: DUMMY_STATE");
});
});
});

describe("when using getEstimatedGas", () => {
describe("when key not in cache", () => {
it("should return cached fee", async () => {
const account = createFixtureAccount();
const transaction = createTransaction();
const aptosClient = new AptosAPI(account.currency.id);

const result = await getFeesForTransaction.getEstimatedGas(
account,
transaction,
aptosClient,
);

const expected = {
errors: {},
estimate: {
expirationTimestampSecs: "",
gasUnitPrice: "101",
maxGasAmount: "201",
sequenceNumber: "123",
},
fees: new BigNumber("20301"),
};

expect(result).toEqual(expected);
});
});

describe("when key is in cache 22", () => {
beforeEach(() => {
jest.clearAllMocks();
});

it("should return cached fee", async () => {
const mocked = jest.spyOn(getFeesForTransaction, "getFee");

const account = createFixtureAccount();
const transaction = createTransaction();
const aptosClient = new AptosAPI(account.currency.id);

transaction.amount = new BigNumber(10);

const result1 = await getFeesForTransaction.getEstimatedGas(
account,
transaction,
aptosClient,
);
const result2 = await getFeesForTransaction.getEstimatedGas(
account,
transaction,
aptosClient,
);

expect(mocked).toHaveBeenCalledTimes(1);

const expected = {
errors: {},
estimate: {
expirationTimestampSecs: "",
gasUnitPrice: "101",
maxGasAmount: "201",
sequenceNumber: "123",
},
fees: new BigNumber("20301"),
};

expect(result1).toEqual(expected);
expect(result2).toEqual(expected);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const getFee = async (
res.errors.expirationTimestampSecs = completedTx.vm_status;
break;
}
case completedTx.vm_status.includes("EINSUFFICIENT_BALANCE"): {
case completedTx.vm_status.includes("INSUFFICIENT_BALANCE"): {
// skip, processed in getTransactionStatus
break;
}
Expand Down Expand Up @@ -126,7 +126,7 @@ export const getEstimatedGas = async (
return CACHE.get(key);
}

CACHE.set(key, getFee(account, transaction, aptosClient));
CACHE.set(key, await getFee(account, transaction, aptosClient));

return CACHE.get(key);
};

0 comments on commit b901ba9

Please sign in to comment.