@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
135 lines • 6.23 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 = require("../prepareTransaction");
const alpaca_1 = require("../alpaca");
const utils_1 = require("../utils");
const bignumber_js_1 = __importDefault(require("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_js_1.default(100_000),
fees: new bignumber_js_1.default(500),
recipient: "0xrecipient",
family: "family",
};
const txIntent = { mock: "intent" };
beforeEach(() => {
jest.clearAllMocks();
utils_1.transactionToIntent.mockReturnValue(txIntent);
});
it("updates fees if they differ", async () => {
const newFee = new bignumber_js_1.default(700);
alpaca_1.getAlpacaApi.mockReturnValue({
estimateFees: jest.fn().mockResolvedValue({ value: newFee }),
});
const prepareTransaction = (0, prepareTransaction_1.genericPrepareTransaction)(network, kind);
const result = await prepareTransaction(account, { ...baseTransaction });
expect(result.fees.toString()).toBe(newFee.toString());
expect(utils_1.transactionToIntent).toHaveBeenCalledWith(account, expect.objectContaining(baseTransaction), undefined);
});
it("returns original transaction if fees are the same", async () => {
const sameFee = baseTransaction.fees;
alpaca_1.getAlpacaApi.mockReturnValue({
estimateFees: jest.fn().mockResolvedValue({ value: sameFee }),
});
const prepareTransaction = (0, prepareTransaction_1.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_js_1.default(1234);
alpaca_1.getAlpacaApi.mockReturnValue({
estimateFees: jest.fn().mockResolvedValue({ value: newFee }),
});
const txWithoutFees = { ...baseTransaction, fees: undefined };
const prepareTransaction = (0, prepareTransaction_1.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_js_1.default(baseTransaction.fees.toString()); // different instance
alpaca_1.getAlpacaApi.mockReturnValue({
estimateFees: jest.fn().mockResolvedValue({ value: sameValue }),
});
const prepareTransaction = (0, prepareTransaction_1.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_js_1.default(491);
const estimateFeesFirstCall = jest.fn().mockResolvedValue({
value: estimatedFee,
parameters: { storageLimit: 300 },
});
const estimateFeesSecondCall = jest.fn().mockResolvedValue({
value: estimatedFee,
parameters: { storageLimit: 0 },
});
alpaca_1.getAlpacaApi.mockReturnValue({
estimateFees: jest
.fn()
.mockImplementationOnce(() => estimateFeesFirstCall())
.mockImplementationOnce(() => estimateFeesSecondCall()),
});
const txWithoutCustomFees = { ...baseTransaction, customFees: undefined };
const prepareTransaction = (0, prepareTransaction_1.genericPrepareTransaction)(network, kind);
const result = await prepareTransaction(account, txWithoutCustomFees);
expect(result).toEqual(expect.objectContaining({
fees: estimatedFee,
storageLimit: new bignumber_js_1.default(0),
customFees: {
parameters: {
fees: undefined,
},
},
}));
expect(alpaca_1.getAlpacaApi().estimateFees).toHaveBeenCalledTimes(2);
});
it("propagates storageLimit showing new account scenario", async () => {
const estimatedFee = new bignumber_js_1.default(491);
const estimateFeesFirstCall = jest.fn().mockResolvedValue({
value: estimatedFee,
parameters: { storageLimit: 300 },
});
const estimateFeesSecondCall = jest.fn().mockResolvedValue({
value: estimatedFee,
parameters: { storageLimit: 277 },
});
alpaca_1.getAlpacaApi.mockReturnValue({
estimateFees: jest
.fn()
.mockImplementationOnce(() => estimateFeesFirstCall())
.mockImplementationOnce(() => estimateFeesSecondCall()),
});
const txWithoutCustomFees = { ...baseTransaction, customFees: undefined };
const prepareTransaction = (0, prepareTransaction_1.genericPrepareTransaction)(network, kind);
const result = await prepareTransaction(account, txWithoutCustomFees);
expect(result).toEqual(expect.objectContaining({
fees: estimatedFee,
storageLimit: new bignumber_js_1.default(277),
customFees: {
parameters: {
fees: undefined,
},
},
}));
expect(alpaca_1.getAlpacaApi().estimateFees).toHaveBeenCalledTimes(2);
});
});
//# sourceMappingURL=prepareTransaction.test.js.map