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

feat(aptos): improve aptos unit tests #8781

Merged
merged 7 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
hedi-edelbloute marked this conversation as resolved.
Show resolved Hide resolved
hedi-edelbloute marked this conversation as resolved.
Show resolved Hide resolved
hedi-edelbloute marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
import BigNumber from "bignumber.js";
import { createFixtureAccount } from "../../mock/fixtures/cryptoCurrencies";
import createTransaction from "./createTransaction";
import { getFee, getEstimatedGas } from "./getFeesForTransaction";
import { AptosAPI } from "./api";

let simulateTransaction = jest.fn();

jest.mock("./api", () => {
return {
AptosAPI: function () {
return {
generateTransaction: jest.fn(() => "tx"),
simulateTransaction,
};
},
};
});

jest.mock("@aptos-labs/ts-sdk", () => {
return {
Ed25519PublicKey: 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 getFee(account, transaction, aptosClient);

const expected = {
fees: new BigNumber(20000),
estimate: {
maxGasAmount: "200",
gasUnitPrice: "100",
sequenceNumber: "",
expirationTimestampSecs: 5,
},
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 getFee(account, transaction, aptosClient);

const expected = {
fees: new BigNumber(20000),
estimate: {
maxGasAmount: "200",
gasUnitPrice: "100",
sequenceNumber: "",
expirationTimestampSecs: 5,
},
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 getFee(account, transaction, aptosClient);

const expected = {
fees: new BigNumber(20000),
estimate: {
maxGasAmount: "200",
gasUnitPrice: "100",
sequenceNumber: "",
expirationTimestampSecs: 5,
},
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 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 getEstimatedGas(account, transaction, aptosClient);

const expected = {
errors: {},
estimate: {
expirationTimestampSecs: "",
gasUnitPrice: "100",
maxGasAmount: "200",
sequenceNumber: "",
},
fees: new BigNumber("20000"),
};

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

describe("when key is in cache", () => {
it("should return cached fee", async () => {
hedi-edelbloute marked this conversation as resolved.
Show resolved Hide resolved
const account = createFixtureAccount();
const transaction = createTransaction();
const aptosClient = new AptosAPI(account.currency.id);

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

const expected = {
errors: {},
estimate: {
expirationTimestampSecs: "",
gasUnitPrice: "100",
maxGasAmount: "200",
sequenceNumber: "",
},
fees: new BigNumber("20000"),
};

expect(result).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
Loading