UNPKG

@ledgerhq/coin-tron

Version:
134 lines 4.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 rxjs_1 = require("rxjs"); const index_1 = require("@ledgerhq/cryptoassets/index"); const index_2 = require("./index"); const synchronization_account_fixture_json_1 = __importDefault(require("./fixtures/synchronization.account.fixture.json")); const index_mock_1 = require("../network/index.mock"); const tron = (0, index_1.getCryptoCurrencyById)("tron"); const defaultSyncConfig = { paginationConfig: {}, blacklistedTokenIds: [], }; function syncAccount(bridge, account, syncConfig = defaultSyncConfig) { return (0, rxjs_1.firstValueFrom)(bridge.sync(account, syncConfig).pipe((0, rxjs_1.reduce)((a, f) => f(a), account))); } const dummyAccount = { type: "Account", id: "", derivationMode: "", seedIdentifier: "", used: false, currency: tron, index: 0, freshAddress: "", freshAddressPath: "", swapHistory: [], blockHeight: 0, balance: new bignumber_js_1.default(0), spendableBalance: new bignumber_js_1.default(0), operationsCount: 0, operations: [], pendingOperations: [], lastSyncDate: new Date(0), creationDate: new Date(), balanceHistoryCache: { HOUR: { balances: [], latestDate: undefined, }, DAY: { balances: [], latestDate: undefined, }, WEEK: { balances: [], latestDate: undefined, }, }, tronResources: {}, }; const reviver = (key, value) => { // Format date if ([ "date", "creationDate", "expiredAt", "lastVotedDate", "lastWithdrawnRewardDate", // "latestDate", ].includes(key) === true) { return value !== null ? typeof value === "string" ? new Date(value) : new Date(value) : new Date("1970-01-01T00:00:00.000Z"); } // BigNumber conversion if ([ "unwithdrawnReward", "amount", "balance", "spendableBalance", "freeLimit", "freeUsed", "gainedLimit", "gainedUsed", "value", "frozenAmount", "unfreezeAmount", "energy", "fee", ].includes(key) === true) { return typeof value === "string" ? new bignumber_js_1.default(value) : value; } // Remove undesired properties as they always change //FIXME: balanceHistoryCache if (["lastSyncDate", "latestDate", "balanceHistoryCache"].includes(key) === true) { return undefined; } return value; }; describe("Sync Accounts", () => { let bridge; beforeAll(() => { const signer = jest.fn(); const coinConfig = () => ({ status: { type: "active", }, explorer: { url: index_mock_1.TRONGRID_BASE_URL_MOCKED, }, }); bridge = (0, index_2.createBridges)(signer, coinConfig); index_mock_1.mockServer.listen({ onUnhandledRequest: "error" }); }); afterAll(() => { index_mock_1.mockServer.close(); }); it.each([ { id: "TL24LCps5FKwp3PoU1MvrYrwhi5LU1tHre", expectedAccount: JSON.parse(JSON.stringify(synchronization_account_fixture_json_1.default["TL24LCps5FKwp3PoU1MvrYrwhi5LU1tHre"]), reviver), }, // "TAVrrARNdnjHgCGMQYeQV7hv4PSu7mVsMj", // "THAe4BNVxp293qgyQEqXEkHMpPcqtG73bi", // "TRqkRnAj6ceJFYAn2p1eE7aWrgBBwtdhS9", // "TUxd6v64YTWkfpFpNDdtgc5Ps4SfGxwizT", // "TY2ksFgpvb82TgGPwUSa7iseqPW5weYQyh", ])("should always be sync without error for address %s", async ({ id, expectedAccount }) => { const account = await syncAccount(bridge.accountBridge, { ...dummyAccount, id: `js:2:tron:${id}:`, freshAddress: id, }); expect(account.id).toEqual(`js:2:tron:${id}:`); expect(account).toMatchObject(expectedAccount); }); }); //# sourceMappingURL=synchronization.test.js.map