@ledgerhq/coin-aptos
Version:
Ledger Aptos Coin integration
114 lines • 5.95 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 bridge_fixture_1 = require("../../bridge/bridge.fixture");
const buildTransaction_1 = __importDefault(require("../../logic/buildTransaction"));
const network_1 = require("../../network");
const normalizeTransactionOptions_1 = require("../../logic/normalizeTransactionOptions");
const generateTransaction = jest.fn(() => "tx");
jest.mock("../../logic/normalizeTransactionOptions", () => ({
normalizeTransactionOptions: jest.fn(() => ({
maxGasAmount: "100",
gasUnitPrice: "200",
})),
}));
jest.mock("../../constants", () => {
const originalModules = jest.requireActual("../../constants");
return {
...originalModules,
DEFAULT_GAS: 100,
DEFAULT_GAS_PRICE: 200,
};
});
jest.mock("../../network", () => {
return {
AptosAPI: function () {
return {
generateTransaction,
};
},
};
});
afterEach(() => {
jest.clearAllMocks();
});
describe("buildTransaction Test", () => {
it("should return transaction for main account", async () => {
const account = (0, bridge_fixture_1.createFixtureAccount)();
const transaction = (0, bridge_fixture_1.createFixtureTransaction)();
const aptosClient = new network_1.AptosAPI(account.currency.id);
const result = await (0, buildTransaction_1.default)(account, transaction, aptosClient);
const expected = "tx";
expect(result).toBe(expected);
const mockedNormalizeTransactionOptions = jest.mocked(normalizeTransactionOptions_1.normalizeTransactionOptions);
expect(mockedNormalizeTransactionOptions).toHaveBeenCalledTimes(1);
expect(generateTransaction).toHaveBeenCalledTimes(1);
const generateTransactionArgs = generateTransaction.mock.calls[0];
expect(mockedNormalizeTransactionOptions.mock.calls[0][0]).toEqual({
maxGasAmount: "0",
gasUnitPrice: "0",
});
expect(generateTransactionArgs[0]).toBe("address");
expect(generateTransactionArgs[1]).toEqual({
function: "0x1::aptos_account::transfer_coins",
typeArguments: ["0x1::aptos_coin::AptosCoin"],
functionArguments: ["recipient", "0"],
});
expect(generateTransactionArgs[2]).toEqual({ maxGasAmount: "100", gasUnitPrice: "200" });
});
it("should return transaction for token account of type fungible asset", async () => {
const account = (0, bridge_fixture_1.createFixtureAccountWithSubAccount)("fungible_asset");
const transaction = (0, bridge_fixture_1.createFixtureTransactionWithSubAccount)();
const aptosClient = new network_1.AptosAPI(account.currency.id);
const result = await (0, buildTransaction_1.default)(account, transaction, aptosClient);
const expected = "tx";
expect(result).toBe(expected);
const mockedNormalizeTransactionOptions = jest.mocked(normalizeTransactionOptions_1.normalizeTransactionOptions);
expect(mockedNormalizeTransactionOptions).toHaveBeenCalledTimes(1);
expect(generateTransaction).toHaveBeenCalledTimes(1);
const generateTransactionArgs = generateTransaction.mock.calls[0];
expect(mockedNormalizeTransactionOptions.mock.calls[0][0]).toEqual({
maxGasAmount: "0",
gasUnitPrice: "0",
});
expect(generateTransactionArgs[0]).toBe("address");
expect(generateTransactionArgs[1]).toEqual({
function: "0x1::primary_fungible_store::transfer",
typeArguments: ["0x1::fungible_asset::Metadata"],
functionArguments: ["contract_address", "recipient", "0"],
});
expect(generateTransactionArgs[2]).toEqual({ maxGasAmount: "100", gasUnitPrice: "200" });
});
it("should return transaction for token account of type coin", async () => {
const account = (0, bridge_fixture_1.createFixtureAccountWithSubAccount)("coin");
const transaction = (0, bridge_fixture_1.createFixtureTransactionWithSubAccount)();
const aptosClient = new network_1.AptosAPI(account.currency.id);
const result = await (0, buildTransaction_1.default)(account, transaction, aptosClient);
const expected = "tx";
expect(result).toBe(expected);
const mockedNormalizeTransactionOptions = jest.mocked(normalizeTransactionOptions_1.normalizeTransactionOptions);
expect(mockedNormalizeTransactionOptions).toHaveBeenCalledTimes(1);
expect(generateTransaction).toHaveBeenCalledTimes(1);
const generateTransactionArgs = generateTransaction.mock.calls[0];
expect(mockedNormalizeTransactionOptions.mock.calls[0][0]).toEqual({
maxGasAmount: "0",
gasUnitPrice: "0",
});
expect(generateTransactionArgs[0]).toBe("address");
expect(generateTransactionArgs[1]).toEqual({
function: "0x1::aptos_account::transfer_coins",
typeArguments: ["contract_address"],
functionArguments: ["recipient", "0"],
});
expect(generateTransactionArgs[2]).toEqual({ maxGasAmount: "100", gasUnitPrice: "200" });
});
it("should throw error if token is not supported", async () => {
const account = (0, bridge_fixture_1.createFixtureAccountWithSubAccount)("not_supported_token_type");
const transaction = (0, bridge_fixture_1.createFixtureTransactionWithSubAccount)();
const aptosClient = new network_1.AptosAPI(account.currency.id);
expect(async () => await (0, buildTransaction_1.default)(account, transaction, aptosClient)).rejects.toThrow("Token type not_supported_token_type not supported");
});
});
//# sourceMappingURL=buildTransaction.test.js.map