UNPKG

@ledgerhq/coin-celo

Version:
239 lines 10.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const bignumber_js_1 = __importDefault(require("bignumber.js")); const fixtures_1 = require("../../bridge/fixtures"); const prepareTransaction_1 = __importDefault(require("../../bridge/prepareTransaction")); const chainIdMock = jest.fn(); const nonceMock = jest.fn(); jest.mock("../../network/sdk", () => { return { celoKit: jest.fn(() => ({ contracts: { getLockedGold: jest.fn(async () => ({ address: "address", lock: jest.fn(() => ({ txo: { encodeABI: jest.fn(() => "0x6C6F636B5F64617461"), // lock_data estimateGas: jest.fn(async () => 2), }, })), unlock: jest.fn(() => ({ txo: { encodeABI: jest.fn(() => "0x756E6C6F636B5F64617461"), // unlock_data estimateGas: jest.fn(async () => 3), }, })), withdraw: jest.fn(() => ({ txo: { encodeABI: jest.fn(() => "0x77697468647261775F64617461"), // withdraw_data estimateGas: jest.fn(async () => 3), }, })), vote: jest.fn(() => ({ txo: { encodeABI: jest.fn(() => "0x766F74655F64617461"), // vote_data estimateGas: jest.fn(async () => 3), }, })), getAccountNonvotingLockedGold: jest.fn(() => (0, bignumber_js_1.default)(0)), })), getGoldToken: jest.fn(async () => ({ address: "gold_token_address", transfer: jest.fn(() => ({ txo: { encodeABI: jest.fn(() => "0x73656E645F64617461"), // send_data }, })), })), getStableToken: jest.fn(async () => ({ address: "stable_token_address", transfer: jest.fn(() => ({ txo: { encodeABI: jest.fn(() => "0x73656E645F746F6B656E5F64617461"), // stable_token_data }, })), })), getErc20: jest.fn(async () => ({ address: "erc20_token_address", transfer: jest.fn(() => ({ txo: { encodeABI: jest.fn(() => "0x73656E645F65726332305F746F6B656E5F64617461"), // send_erc20_token_data }, })), })), }, connection: { chainId: chainIdMock, nonce: nonceMock, estimateGasWithInflationFactor: jest.fn().mockReturnValue(3), gasPrice: jest.fn(async () => (0, bignumber_js_1.default)(2)), web3: { eth: { getBlock: jest.fn().mockResolvedValue({ baseFeePerGas: 10 }) } }, getMaxPriorityFeePerGas: jest.fn().mockResolvedValue(1), }, })), }; }); describe("prepareTransaction", () => { it("should return the transaction if the address is invalid", async () => { const transaction = await (0, prepareTransaction_1.default)(fixtures_1.accountFixture, fixtures_1.transactionFixture); expect(transaction).toMatchObject({ amount: (0, bignumber_js_1.default)(10), recipient: "recipient", useAllAmount: false, family: "celo", mode: "send", index: 0, fees: null, }); }); it("should return the transaction if it doesn't have a recipient", async () => { const transaction = await (0, prepareTransaction_1.default)(fixtures_1.accountFixture, { ...fixtures_1.transactionFixture, recipient: "", }); expect(transaction).toMatchObject({ amount: (0, bignumber_js_1.default)(10), recipient: "", useAllAmount: false, family: "celo", mode: "send", index: 0, fees: null, }); }); it("should return the vote transaction if it doesn't have a recipient", async () => { const transaction = await (0, prepareTransaction_1.default)(fixtures_1.accountFixture, { ...fixtures_1.transactionFixture, recipient: "", mode: "vote", }); expect(transaction).toMatchObject({ amount: (0, bignumber_js_1.default)(10), recipient: "", useAllAmount: false, family: "celo", mode: "vote", index: 0, fees: null, }); }); it("should return the vote transaction if the amount is less than or equal to zero", async () => { const transaction = await (0, prepareTransaction_1.default)(fixtures_1.accountFixture, { ...fixtures_1.transactionFixture, recipient: "0x79D5A290D7ba4b99322d91b577589e8d0BF87072", mode: "vote", amount: (0, bignumber_js_1.default)(0), }); expect(transaction).toMatchObject({ amount: (0, bignumber_js_1.default)(0), recipient: "0x79D5A290D7ba4b99322d91b577589e8d0BF87072", useAllAmount: false, family: "celo", mode: "vote", index: 0, fees: null, }); }); it("should return the prepared transaction", async () => { const transaction = await (0, prepareTransaction_1.default)({ ...fixtures_1.accountFixture, balance: (0, bignumber_js_1.default)(222222), spendableBalance: (0, bignumber_js_1.default)(222222) }, { ...fixtures_1.transactionFixture, recipient: "0x79D5A290D7ba4b99322d91b577589e8d0BF87072", amount: (0, bignumber_js_1.default)(22), }); expect(transaction).toMatchObject({ amount: (0, bignumber_js_1.default)(22), recipient: "0x79D5A290D7ba4b99322d91b577589e8d0BF87072", useAllAmount: false, family: "celo", mode: "send", index: 0, fees: (0, bignumber_js_1.default)(24), }); }); it("should return the prepared stable token transaction", async () => { const transaction = await (0, prepareTransaction_1.default)({ ...fixtures_1.accountWithTokenAccountFixture, balance: (0, bignumber_js_1.default)(222222), spendableBalance: (0, bignumber_js_1.default)(222222), subAccounts: [ { ...fixtures_1.accountWithTokenAccountFixture.subAccounts[0], token: { ...fixtures_1.accountWithTokenAccountFixture.subAccounts[0].token, id: "cEUR", }, }, ], }, { ...fixtures_1.tokenTransactionFixture, recipient: "0x79D5A290D7ba4b99322d91b577589e8d0BF87072", amount: (0, bignumber_js_1.default)(22), }); expect(transaction).toMatchObject({ amount: (0, bignumber_js_1.default)(22), recipient: "0x79D5A290D7ba4b99322d91b577589e8d0BF87072", useAllAmount: false, family: "celo", mode: "send", index: 0, fees: (0, bignumber_js_1.default)(24), subAccountId: "subAccountId", maxFeePerGas: "1000000010", maxPriorityFeePerGas: 1, data: Buffer.from("0x73656E645F746F6B656E5F64617461"), }); }); it("should return the prepared token transaction", async () => { const transaction = await (0, prepareTransaction_1.default)({ ...fixtures_1.accountWithTokenAccountFixture, balance: (0, bignumber_js_1.default)(222222), spendableBalance: (0, bignumber_js_1.default)(222222), }, { ...fixtures_1.tokenTransactionFixture, recipient: "0x79D5A290D7ba4b99322d91b577589e8d0BF87072", amount: (0, bignumber_js_1.default)(22), }); expect(transaction).toMatchObject({ amount: (0, bignumber_js_1.default)(22), recipient: "0x79D5A290D7ba4b99322d91b577589e8d0BF87072", useAllAmount: false, family: "celo", mode: "send", index: 0, fees: (0, bignumber_js_1.default)(24), subAccountId: "subAccountId", maxFeePerGas: "1000000010", maxPriorityFeePerGas: 1, data: Buffer.from("0x73656E645F65726332305F746F6B656E5F64617461"), }); }); it("should return the prepared token transaction with useAllAmount", async () => { const transaction = await (0, prepareTransaction_1.default)({ ...fixtures_1.accountWithTokenAccountFixture, balance: (0, bignumber_js_1.default)(222222), spendableBalance: (0, bignumber_js_1.default)(222222), }, { ...fixtures_1.tokenTransactionFixture, recipient: "0x79D5A290D7ba4b99322d91b577589e8d0BF87072", amount: (0, bignumber_js_1.default)(22), useAllAmount: true, }); expect(transaction).toMatchObject({ amount: (0, bignumber_js_1.default)(212), recipient: "0x79D5A290D7ba4b99322d91b577589e8d0BF87072", useAllAmount: true, family: "celo", mode: "send", index: 0, fees: (0, bignumber_js_1.default)(24), subAccountId: "subAccountId", maxFeePerGas: "1000000010", maxPriorityFeePerGas: 1, data: Buffer.from("0x73656E645F65726332305F746F6B656E5F64617461"), }); }); }); //# sourceMappingURL=prepareTransaction.test.js.map