UNPKG

@ledgerhq/coin-tezos

Version:
178 lines • 8.93 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 bridge_fixture_1 = require("../types/bridge.fixture"); const prepareTransaction_1 = __importDefault(require("./prepareTransaction")); const faker_1 = require("@faker-js/faker"); const config_1 = __importDefault(require("../config")); const config_2 = require("../test/config"); const taquito_1 = require("@taquito/taquito"); const mockTezosEstimate = jest.fn(); jest.mock("../logic/tezosToolkit", () => ({ getTezosToolkit: () => ({ setProvider: jest.fn(), estimate: { transfer: () => mockTezosEstimate(), }, }), })); describe("prepareTransaction", () => { beforeAll(() => { config_1.default.setCoinConfig(() => config_2.mockConfig); }); beforeEach(() => { mockTezosEstimate.mockReset(); }); it("returns the same transaction when account balance is 0", async () => { // Given const tx = (0, bridge_fixture_1.createFixtureTransaction)({ amount: (0, bignumber_js_1.default)(0) }); // When const newTx = await (0, prepareTransaction_1.default)((0, bridge_fixture_1.createFixtureAccount)(), tx); // Then expect(newTx).toBe(tx); }); it("returns error when amount is 0", async () => { // Given const tx = (0, bridge_fixture_1.createFixtureTransaction)({ amount: (0, bignumber_js_1.default)(0), recipient: "tz1VSUr8wwNhLAzempoch5d6hLRiTh8Cjcjb", }); mockTezosEstimate.mockRejectedValue({ id: "proto.020-PsParisC.contract.empty_transaction", }); // When const newTx = await (0, prepareTransaction_1.default)((0, bridge_fixture_1.createFixtureAccount)({ balance: (0, bignumber_js_1.default)(10) }), tx); // Then expect(newTx.taquitoError).toEqual("proto.020-PsParisC.contract.empty_transaction"); }); it("returns new transaction with estimated fees by TezosToolkit", async () => { // Given const tx = (0, bridge_fixture_1.createFixtureTransaction)({ amount: (0, bignumber_js_1.default)(200), recipient: "tz1VSUr8wwNhLAzempoch5d6hLRiTh8Cjcjb", }); const tezosEstimate = { suggestedFeeMutez: faker_1.faker.number.int(20), gasLimit: faker_1.faker.number.int(20), storageLimit: faker_1.faker.number.int(20), }; mockTezosEstimate.mockResolvedValue(tezosEstimate); // When const newTx = await (0, prepareTransaction_1.default)((0, bridge_fixture_1.createFixtureAccount)({ balance: (0, bignumber_js_1.default)(1_000) }), tx); // Then expect(newTx).toEqual({ ...tx, fees: new bignumber_js_1.default(tezosEstimate.suggestedFeeMutez), estimatedFees: new bignumber_js_1.default(tezosEstimate.suggestedFeeMutez), gasLimit: new bignumber_js_1.default(tezosEstimate.gasLimit), storageLimit: new bignumber_js_1.default(tezosEstimate.storageLimit), }); }); it("returns new transaction with estimated fees by TezosToolkit when useAllAmount from revealed account to revealed account", async () => { // Given const tx = (0, bridge_fixture_1.createFixtureTransaction)({ amount: (0, bignumber_js_1.default)(200), recipient: "tz1VSUr8wwNhLAzempoch5d6hLRiTh8Cjcjb", useAllAmount: true, }); const tezosEstimate = { suggestedFeeMutez: faker_1.faker.number.int(20), gasLimit: faker_1.faker.number.int(20), storageLimit: faker_1.faker.number.int(20), opSize: faker_1.faker.number.int(20), burnFeeMutez: faker_1.faker.number.int(0), }; mockTezosEstimate.mockResolvedValue(tezosEstimate); const account = (0, bridge_fixture_1.createFixtureAccount)({ balance: (0, bignumber_js_1.default)(1_000) }); // When const newTx = await (0, prepareTransaction_1.default)(account, tx); // Then const gasLimit = 500; // hardcoded in the function const maxAmount = account.balance.minus(tezosEstimate.suggestedFeeMutez); expect(newTx).toEqual({ ...tx, fees: new bignumber_js_1.default(tezosEstimate.suggestedFeeMutez), estimatedFees: new bignumber_js_1.default(tezosEstimate.suggestedFeeMutez), gasLimit: new bignumber_js_1.default(tezosEstimate.gasLimit), storageLimit: new bignumber_js_1.default(tezosEstimate.storageLimit), amount: maxAmount.minus(gasLimit - (tezosEstimate.opSize + gasLimit * 0.1)), }); }); it("returns new transaction with estimated fees by TezosToolkit when useAllAmount from revealed account to account with zero balance", async () => { // Given const tx = (0, bridge_fixture_1.createFixtureTransaction)({ amount: (0, bignumber_js_1.default)(0), recipient: "tz1VSUr8wwNhLAzempoch5d6hLRiTh8Cjcjb", useAllAmount: true, }); const tezosEstimate = { suggestedFeeMutez: faker_1.faker.number.int(20), gasLimit: faker_1.faker.number.int(20), storageLimit: faker_1.faker.number.int(20), opSize: faker_1.faker.number.int(20), burnFeeMutez: faker_1.faker.number.int(20), }; mockTezosEstimate.mockResolvedValue(tezosEstimate); const account = (0, bridge_fixture_1.createFixtureAccount)({ balance: (0, bignumber_js_1.default)(1_000) }); // When const newTx = await (0, prepareTransaction_1.default)(account, tx); // Then const gasLimit = 500; // hardcoded in the function const computedFees = new bignumber_js_1.default(tezosEstimate.suggestedFeeMutez + tezosEstimate.burnFeeMutez - 20 * taquito_1.COST_PER_BYTE); const maxAmount = account.balance.minus(computedFees); expect(newTx).toEqual({ ...tx, fees: new bignumber_js_1.default(tezosEstimate.suggestedFeeMutez), estimatedFees: new bignumber_js_1.default(tezosEstimate.suggestedFeeMutez), gasLimit: new bignumber_js_1.default(tezosEstimate.gasLimit), storageLimit: new bignumber_js_1.default(tezosEstimate.storageLimit), amount: maxAmount.minus(gasLimit - (tezosEstimate.opSize + gasLimit * 0.1)), }); }); it("returns new transaction with estimated fees by TezosToolkit when useAllAmount from unrevealed account to revealed account", async () => { // Given const tx = (0, bridge_fixture_1.createFixtureTransaction)({ amount: (0, bignumber_js_1.default)(200), recipient: "tz1VSUr8wwNhLAzempoch5d6hLRiTh8Cjcjb", useAllAmount: true, }); const tezosEstimate = { suggestedFeeMutez: faker_1.faker.number.int(20), gasLimit: faker_1.faker.number.int(20), storageLimit: faker_1.faker.number.int(20), opSize: faker_1.faker.number.int(20), burnFeeMutez: faker_1.faker.number.int(0), }; mockTezosEstimate.mockResolvedValue(tezosEstimate); const account = (0, bridge_fixture_1.createFixtureAccount)({ balance: (0, bignumber_js_1.default)(1_000) }); account.tezosResources.revealed = false; // When const newTx = await (0, prepareTransaction_1.default)(account, tx); // Then const gasLimit = 500; // hardcoded in the function const computedFees = new bignumber_js_1.default(tezosEstimate.suggestedFeeMutez + taquito_1.DEFAULT_FEE.REVEAL); const maxAmount = account.balance.minus(computedFees); expect(newTx).toEqual({ ...tx, fees: new bignumber_js_1.default(tezosEstimate.suggestedFeeMutez), estimatedFees: new bignumber_js_1.default(tezosEstimate.suggestedFeeMutez + taquito_1.DEFAULT_FEE.REVEAL), gasLimit: new bignumber_js_1.default(tezosEstimate.gasLimit), storageLimit: new bignumber_js_1.default(tezosEstimate.storageLimit), amount: maxAmount.minus(gasLimit - (tezosEstimate.opSize + gasLimit * 0.1)), }); }); // it("returns the passed transaction if fees are the same", async () => { // // Given // const fees = new BigNumber(faker.number.int(50)); // mockEstimateFees.mockResolvedValue(fees); // const tx = createFixtureTransaction({ fees }); // // When // const newTx = await prepareTransaction(createFixtureAccount(), tx); // // Then // expect(newTx).toBe(tx); // }); }); //# sourceMappingURL=prepareTransaction.test.js.map