@ledgerhq/coin-aptos
Version:
Ledger Aptos Coin integration
109 lines • 6.05 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const prepareTransaction_1 = __importDefault(require("../../bridge/prepareTransaction"));
const network_1 = require("../../network");
const getFeesForTransaction_1 = require("../../bridge/getFeesForTransaction");
const logic_1 = require("../../bridge/logic");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
jest.mock("../../network");
jest.mock("../../bridge/getFeesForTransaction");
jest.mock("../../bridge/logic");
describe("Aptos prepareTransaction", () => {
describe("prepareTransaction", () => {
let account;
let transaction;
beforeEach(() => {
account = {
id: "test-account-id",
name: "Test Account",
currency: {
id: "aptos",
name: "Aptos",
ticker: "APT",
units: [{ name: "Aptos", code: "APT", magnitude: 6 }],
},
spendableBalance: new bignumber_js_1.default(1000),
balance: new bignumber_js_1.default(1000),
blockHeight: 0,
operations: [],
pendingOperations: [],
unit: { code: "APT", name: "Aptos", magnitude: 6 },
lastSyncDate: new Date(),
subAccounts: [],
};
transaction = {
amount: new bignumber_js_1.default(0),
recipient: "",
useAllAmount: false,
fees: new bignumber_js_1.default(0),
mode: "send",
};
});
it("should return the transaction if recipient is not set", async () => {
const result = await (0, prepareTransaction_1.default)(account, transaction);
expect(result).toEqual(transaction);
});
it("should return the transaction with zero fees if amount is zero and useAllAmount is false", async () => {
transaction.recipient = "test-recipient";
const result = await (0, prepareTransaction_1.default)(account, transaction);
expect(result.fees?.isZero()).toBe(true);
});
it("should set the amount to max sendable balance if useAllAmount is true", async () => {
transaction.recipient = "test-recipient";
transaction.useAllAmount = true;
logic_1.getMaxSendBalance.mockReturnValue(new bignumber_js_1.default(900));
getFeesForTransaction_1.getEstimatedGas.mockResolvedValue({
fees: new bignumber_js_1.default(2000),
estimate: { maxGasAmount: new bignumber_js_1.default(200), gasUnitPrice: new bignumber_js_1.default(10) },
errors: {},
});
const result = await (0, prepareTransaction_1.default)(account, transaction);
expect(result.amount.isEqualTo(new bignumber_js_1.default(900))).toBe(true);
expect(result.fees?.isEqualTo(new bignumber_js_1.default(2000))).toBe(true);
expect(new bignumber_js_1.default(result.options.maxGasAmount).isEqualTo(new bignumber_js_1.default(200))).toBe(true);
expect(result.errors).toEqual({});
});
it("should call getEstimatedGas and set the transaction fees, estimate, and errors", async () => {
transaction.recipient = "test-recipient";
transaction.amount = new bignumber_js_1.default(100);
getFeesForTransaction_1.getEstimatedGas.mockResolvedValue({
fees: new bignumber_js_1.default(10),
estimate: { maxGasAmount: new bignumber_js_1.default(200) },
errors: {},
});
const result = await (0, prepareTransaction_1.default)(account, transaction);
expect(getFeesForTransaction_1.getEstimatedGas).toHaveBeenCalledWith(account, transaction, expect.any(network_1.AptosAPI));
expect(result.fees?.isEqualTo(new bignumber_js_1.default(10))).toBe(true);
expect(new bignumber_js_1.default(result.options.maxGasAmount).isEqualTo(new bignumber_js_1.default(200))).toBe(true);
expect(result.errors).toEqual({});
});
it("should return the transaction with updated fees and estimate if recipient is set and amount is not zero", async () => {
transaction.recipient = "test-recipient";
transaction.amount = new bignumber_js_1.default(100);
getFeesForTransaction_1.getEstimatedGas.mockResolvedValue({
fees: new bignumber_js_1.default(2000),
estimate: { maxGasAmount: new bignumber_js_1.default(200), gasUnitPrice: new bignumber_js_1.default(10) },
errors: {},
});
const result = await (0, prepareTransaction_1.default)(account, transaction);
expect(result.fees?.isEqualTo(new bignumber_js_1.default(2000))).toBe(true);
expect(new bignumber_js_1.default(result.options.maxGasAmount).isEqualTo(new bignumber_js_1.default(200))).toBe(true);
expect(result.errors).toEqual({});
});
it("should set maxGasAmount in options", async () => {
transaction.recipient = "test-recipient";
transaction.amount = new bignumber_js_1.default(100);
getFeesForTransaction_1.getEstimatedGas.mockResolvedValue({
fees: new bignumber_js_1.default(2000),
estimate: { maxGasAmount: new bignumber_js_1.default(200), gasUnitPrice: new bignumber_js_1.default(10) },
errors: {},
});
const result = await (0, prepareTransaction_1.default)(account, transaction);
expect(new bignumber_js_1.default(result.options.maxGasAmount).isEqualTo(new bignumber_js_1.default(200))).toBe(true);
});
});
});
//# sourceMappingURL=prepareTransaction.test.js.map