UNPKG

@ledgerhq/live-common

Version:
130 lines 5.65 kB
import { genericPrepareTransaction } from "../prepareTransaction"; import { getAlpacaApi } from "../alpaca"; import { transactionToIntent } from "../utils"; import BigNumber from "bignumber.js"; jest.mock("../alpaca", () => ({ getAlpacaApi: jest.fn(), })); jest.mock("../utils", () => ({ transactionToIntent: jest.fn(), })); describe("genericPrepareTransaction", () => { const network = "testnet"; const kind = "local"; const account = { id: "test-account", address: "0xabc", currency: { id: "ethereum" }, }; const baseTransaction = { amount: new BigNumber(100_000), fees: new BigNumber(500), recipient: "0xrecipient", family: "family", }; const txIntent = { mock: "intent" }; beforeEach(() => { jest.clearAllMocks(); transactionToIntent.mockReturnValue(txIntent); }); it("updates fees if they differ", async () => { const newFee = new BigNumber(700); getAlpacaApi.mockReturnValue({ estimateFees: jest.fn().mockResolvedValue({ value: newFee }), }); const prepareTransaction = genericPrepareTransaction(network, kind); const result = await prepareTransaction(account, { ...baseTransaction }); expect(result.fees.toString()).toBe(newFee.toString()); expect(transactionToIntent).toHaveBeenCalledWith(account, expect.objectContaining(baseTransaction), undefined); }); it("returns original transaction if fees are the same", async () => { const sameFee = baseTransaction.fees; getAlpacaApi.mockReturnValue({ estimateFees: jest.fn().mockResolvedValue({ value: sameFee }), }); const prepareTransaction = genericPrepareTransaction(network, kind); const result = await prepareTransaction(account, baseTransaction); expect(result).toBe(baseTransaction); }); it("sets fee if original fees are undefined", async () => { const newFee = new BigNumber(1234); getAlpacaApi.mockReturnValue({ estimateFees: jest.fn().mockResolvedValue({ value: newFee }), }); const txWithoutFees = { ...baseTransaction, fees: undefined }; const prepareTransaction = genericPrepareTransaction(network, kind); const result = await prepareTransaction(account, txWithoutFees); expect(result.fees.toString()).toBe(newFee.toString()); expect(result).not.toBe(txWithoutFees); }); it("returns original if fees are BigNumber-equal but different instance", async () => { const sameValue = new BigNumber(baseTransaction.fees.toString()); // different instance getAlpacaApi.mockReturnValue({ estimateFees: jest.fn().mockResolvedValue({ value: sameValue }), }); const prepareTransaction = genericPrepareTransaction(network, kind); const result = await prepareTransaction(account, baseTransaction); expect(result).toBe(baseTransaction); // still same reference }); it("propagates storageLimit from second estimation", async () => { const estimatedFee = new BigNumber(491); const estimateFeesFirstCall = jest.fn().mockResolvedValue({ value: estimatedFee, parameters: { storageLimit: 300 }, }); const estimateFeesSecondCall = jest.fn().mockResolvedValue({ value: estimatedFee, parameters: { storageLimit: 0 }, }); getAlpacaApi.mockReturnValue({ estimateFees: jest .fn() .mockImplementationOnce(() => estimateFeesFirstCall()) .mockImplementationOnce(() => estimateFeesSecondCall()), }); const txWithoutCustomFees = { ...baseTransaction, customFees: undefined }; const prepareTransaction = genericPrepareTransaction(network, kind); const result = await prepareTransaction(account, txWithoutCustomFees); expect(result).toEqual(expect.objectContaining({ fees: estimatedFee, storageLimit: new BigNumber(0), customFees: { parameters: { fees: undefined, }, }, })); expect(getAlpacaApi().estimateFees).toHaveBeenCalledTimes(2); }); it("propagates storageLimit showing new account scenario", async () => { const estimatedFee = new BigNumber(491); const estimateFeesFirstCall = jest.fn().mockResolvedValue({ value: estimatedFee, parameters: { storageLimit: 300 }, }); const estimateFeesSecondCall = jest.fn().mockResolvedValue({ value: estimatedFee, parameters: { storageLimit: 277 }, }); getAlpacaApi.mockReturnValue({ estimateFees: jest .fn() .mockImplementationOnce(() => estimateFeesFirstCall()) .mockImplementationOnce(() => estimateFeesSecondCall()), }); const txWithoutCustomFees = { ...baseTransaction, customFees: undefined }; const prepareTransaction = genericPrepareTransaction(network, kind); const result = await prepareTransaction(account, txWithoutCustomFees); expect(result).toEqual(expect.objectContaining({ fees: estimatedFee, storageLimit: new BigNumber(277), customFees: { parameters: { fees: undefined, }, }, })); expect(getAlpacaApi().estimateFees).toHaveBeenCalledTimes(2); }); }); //# sourceMappingURL=prepareTransaction.test.js.map