@ledgerhq/coin-mina
Version:
164 lines • 7.26 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 synchronisation_1 = require("./synchronisation");
jest.mock("@ledgerhq/coin-framework/account/accountId");
jest.mock("@ledgerhq/coin-framework/bridge/jsHelpers");
jest.mock("@ledgerhq/coin-framework/operation");
jest.mock("../api");
const accountId_1 = require("@ledgerhq/coin-framework/account/accountId");
const jsHelpers_1 = require("@ledgerhq/coin-framework/bridge/jsHelpers");
const operation_1 = require("@ledgerhq/coin-framework/operation");
const api_1 = require("../api");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const fixtures_1 = require("../test/fixtures");
describe("synchronisation", () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe("mapRosettaTxnToOperation", () => {
const mockAccountId = "account_id";
const mockAddress = "sender_address";
beforeEach(() => {
jest.spyOn({ getBlockInfo: api_1.getBlockInfo }, "getBlockInfo").mockResolvedValue(fixtures_1.mockBlockInfo);
jest
.spyOn({ encodeOperationId: operation_1.encodeOperationId }, "encodeOperationId")
.mockReturnValue("encoded_operation_id");
});
it("should map payment transaction (OUT)", async () => {
const mockTxn = (0, fixtures_1.createMockTxn)({
type: "OUT",
senderAddress: mockAddress,
receiverAddress: "receiver_address",
status: "Success",
});
const result = await (0, synchronisation_1.mapRosettaTxnToOperation)(mockAccountId, mockAddress, mockTxn);
expect(result).toHaveLength(1);
expect(result[0]).toEqual({
id: "encoded_operation_id",
type: "OUT",
hash: "tx_hash",
value: new bignumber_js_1.default(900),
fee: new bignumber_js_1.default(-100),
blockHeight: 123,
hasFailed: false,
blockHash: "block_hash",
accountId: mockAccountId,
senders: [mockAddress],
recipients: ["receiver_address"],
date: new Date(1672531200000),
extra: {
memo: "test memo",
accountCreationFee: "0",
},
});
});
it("should map payment transaction (IN)", async () => {
const mockTxn = (0, fixtures_1.createMockTxn)({
type: "IN",
senderAddress: "sender_other",
receiverAddress: mockAddress,
status: "Success",
});
const result = await (0, synchronisation_1.mapRosettaTxnToOperation)(mockAccountId, mockAddress, mockTxn);
expect(result).toHaveLength(1);
expect(result[0]).toEqual({
id: "encoded_operation_id",
type: "IN",
hash: "tx_hash",
value: new bignumber_js_1.default(1000),
fee: new bignumber_js_1.default(-100),
blockHeight: 123,
hasFailed: false,
blockHash: "block_hash",
accountId: mockAccountId,
senders: ["sender_other"],
recipients: [mockAddress],
date: new Date(1672531200000),
extra: {
memo: "test memo",
accountCreationFee: "0",
},
});
});
it("should map redelegate transaction", async () => {
const mockTxn = (0, fixtures_1.createMockTxn)({
type: "REDELEGATE",
senderAddress: mockAddress,
receiverAddress: "not_used",
memo: "redelegate",
status: "Success",
});
const result = await (0, synchronisation_1.mapRosettaTxnToOperation)(mockAccountId, mockAddress, mockTxn);
expect(result).toHaveLength(1);
expect(result[0]).toEqual({
id: "encoded_operation_id",
type: "REDELEGATE",
hash: "tx_hash",
value: new bignumber_js_1.default(0),
fee: new bignumber_js_1.default(0),
blockHeight: 123,
hasFailed: false,
blockHash: "block_hash",
accountId: mockAccountId,
senders: [mockAddress],
recipients: ["unknown"],
date: new Date(1672531200000),
extra: {
memo: "redelegate",
accountCreationFee: "0",
},
});
});
it("should handle failed transactions", async () => {
const mockTxn = (0, fixtures_1.createMockTxn)({
type: "OUT",
senderAddress: mockAddress,
receiverAddress: "receiver_address",
status: "Failed",
memo: "failed",
});
const result = await (0, synchronisation_1.mapRosettaTxnToOperation)(mockAccountId, mockAddress, mockTxn);
expect(result).toHaveLength(1);
expect(result[0].hasFailed).toBe(true);
});
});
describe("getAccountShape", () => {
beforeEach(() => {
jest.spyOn({ encodeAccountId: accountId_1.encodeAccountId }, "encodeAccountId").mockReturnValue("account_id");
jest.spyOn({ getAccount: api_1.getAccount }, "getAccount").mockResolvedValue(fixtures_1.mockAccountData);
jest.spyOn({ getTransactions: api_1.getTransactions }, "getTransactions").mockResolvedValue([]);
jest.spyOn({ mergeOps: jsHelpers_1.mergeOps }, "mergeOps").mockReturnValue([]);
});
it("should get account shape with correct data", async () => {
const mockInfo = (0, fixtures_1.createMockAccountInfo)();
const result = await (0, synchronisation_1.getAccountShape)(mockInfo, {
paginationConfig: {
operationsPerAccountId: {
account_id: 10,
},
},
});
expect(accountId_1.encodeAccountId).toHaveBeenCalledWith({
type: "js",
version: "2",
currencyId: "mina",
xpubOrAddress: "test_address",
derivationMode: "minabip44",
});
expect(api_1.getAccount).toHaveBeenCalledWith("test_address");
expect(api_1.getTransactions).toHaveBeenCalledWith("test_address", 0);
expect(result).toEqual({
id: "account_id",
balance: fixtures_1.mockAccountData.balance,
spendableBalance: fixtures_1.mockAccountData.spendableBalance,
blockHeight: fixtures_1.mockAccountData.blockHeight,
operationsCount: 0,
operations: [],
});
});
});
});
//# sourceMappingURL=synchronisation.test.js.map