UNPKG

@ledgerhq/live-common

Version:
212 lines • 10.6 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const calculateProviderTotal_1 = require("../calculateProviderTotal"); const cryptoCurrencies_1 = require("../../../mock/fixtures/cryptoCurrencies"); const account_1 = require("../../../mock/account"); const bignumber_js_1 = __importDefault(require("bignumber.js")); describe("calculateProviderTotals", () => { const mockBTCCurrency = (0, cryptoCurrencies_1.createFixtureCryptoCurrency)("bitcoin"); const mockETHCurrency = (0, cryptoCurrencies_1.createFixtureCryptoCurrency)("ethereum"); const mockBTCAccount = (0, account_1.genAccount)("btc-account", { currency: mockBTCCurrency }); const mockUSDCAccount = (0, account_1.genAccount)("usdc-account", { currency: mockETHCurrency }); const mockBscUSDCAccount = (0, account_1.genAccount)("bsc-usdc-account", { currency: mockETHCurrency }); const mockUSDCToken = { type: "TokenCurrency", id: "ethereum/erc20/usdc", contractAddress: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", parentCurrency: mockETHCurrency, tokenType: "erc20", name: "USD Coin", ticker: "USDC", units: [{ name: "USD Coin", code: "USDC", magnitude: 6 }], }; const mockBscUSDCToken = { type: "TokenCurrency", id: "bsc/bep20/usdc", contractAddress: "0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d", parentCurrency: mockETHCurrency, tokenType: "bep20", name: "USD Coin (BSC)", ticker: "USDC", units: [{ name: "USD Coin", code: "USDC", magnitude: 18 }], }; it("should return totals with hasAccounts false when no accounts exist", () => { const currencies = [mockBTCCurrency, mockUSDCToken]; const groupedAccounts = {}; const result = (0, calculateProviderTotal_1.calculateProviderTotals)(currencies, groupedAccounts); expect(result).toEqual({ totalBalance: new bignumber_js_1.default(0), totalFiatValue: new bignumber_js_1.default(0), hasAccounts: false, referenceCurrency: null, }); }); it("should sum totals across multiple currencies", () => { const currencies = [mockBTCCurrency, mockUSDCToken]; const groupedAccounts = { [mockBTCCurrency.id]: { totalBalance: new bignumber_js_1.default(100000000), totalFiatValue: new bignumber_js_1.default(50000), accounts: [mockBTCAccount], referenceCurrency: mockBTCCurrency, }, [mockUSDCToken.id]: { totalBalance: new bignumber_js_1.default(1000000), totalFiatValue: new bignumber_js_1.default(1), accounts: [mockUSDCAccount], referenceCurrency: mockUSDCToken, }, }; const result = (0, calculateProviderTotal_1.calculateProviderTotals)(currencies, groupedAccounts); // BTC (magnitude 8) sets reference, USDC (magnitude 6) gets normalized // USDC: 1000000 * 10^(8-6) = 1000000 * 100 = 100000000 // Total: 100000000 (BTC) + 100000000 (normalized USDC) = 200000000 expect(result).toEqual({ totalBalance: new bignumber_js_1.default(200000000), totalFiatValue: new bignumber_js_1.default(50001), hasAccounts: true, referenceCurrency: mockBTCCurrency, // First currency with accounts }); }); it("should return referenceCurrency from first asset group with accounts", () => { const currencies = [mockUSDCToken, mockBscUSDCToken]; const groupedAccounts = { [mockUSDCToken.id]: { totalBalance: new bignumber_js_1.default(1000000), totalFiatValue: new bignumber_js_1.default(1), accounts: [mockUSDCAccount], referenceCurrency: mockUSDCToken, // ETH USDC with 6 decimals }, [mockBscUSDCToken.id]: { totalBalance: new bignumber_js_1.default("1000000000000000000"), totalFiatValue: new bignumber_js_1.default(1), accounts: [mockBscUSDCAccount], referenceCurrency: mockBscUSDCToken, // BSC USDC with 18 decimals }, }; const result = (0, calculateProviderTotal_1.calculateProviderTotals)(currencies, groupedAccounts); expect(result.referenceCurrency).toEqual(mockUSDCToken); // First currency expect(result.hasAccounts).toBe(true); }); it("should skip currencies with no accounts", () => { const currencies = [mockBTCCurrency, mockUSDCToken]; const groupedAccounts = { [mockUSDCToken.id]: { totalBalance: new bignumber_js_1.default(1000000), totalFiatValue: new bignumber_js_1.default(1), accounts: [mockUSDCAccount], referenceCurrency: mockUSDCToken, }, // BTC has no entry in groupedAccounts }; const result = (0, calculateProviderTotal_1.calculateProviderTotals)(currencies, groupedAccounts); expect(result).toEqual({ totalBalance: new bignumber_js_1.default(1000000), totalFiatValue: new bignumber_js_1.default(1), hasAccounts: true, referenceCurrency: mockUSDCToken, }); }); it("should skip currencies with empty accounts array", () => { const currencies = [mockBTCCurrency, mockUSDCToken]; const groupedAccounts = { [mockBTCCurrency.id]: { totalBalance: new bignumber_js_1.default(100000000), totalFiatValue: new bignumber_js_1.default(50000), accounts: [], referenceCurrency: mockBTCCurrency, }, [mockUSDCToken.id]: { totalBalance: new bignumber_js_1.default(1000000), totalFiatValue: new bignumber_js_1.default(1), accounts: [mockUSDCAccount], referenceCurrency: mockUSDCToken, }, }; const result = (0, calculateProviderTotal_1.calculateProviderTotals)(currencies, groupedAccounts); expect(result).toEqual({ totalBalance: new bignumber_js_1.default(1000000), totalFiatValue: new bignumber_js_1.default(1), hasAccounts: true, referenceCurrency: mockUSDCToken, }); }); it("should normalize balances based on magnitude differences", () => { // Test the specific code: magnitude normalization between currencies const currencies = [mockUSDCToken, mockBscUSDCToken]; const groupedAccounts = { [mockUSDCToken.id]: { totalBalance: new bignumber_js_1.default(1000000), totalFiatValue: new bignumber_js_1.default(1), accounts: [mockUSDCAccount], referenceCurrency: mockUSDCToken, // 6 decimals }, [mockBscUSDCToken.id]: { totalBalance: new bignumber_js_1.default("1000000000000000000"), totalFiatValue: new bignumber_js_1.default(1), accounts: [mockBscUSDCAccount], referenceCurrency: mockBscUSDCToken, // 18 decimals }, }; const result = (0, calculateProviderTotal_1.calculateProviderTotals)(currencies, groupedAccounts); // First currency (USDC) sets reference magnitude of 6 // Second currency (BSC USDC) has magnitude 18, so diff = 6 - 18 = -12 // BSC balance of 1000000000000000000 shifted by -12 = 1000000 // Total should be 1000000 + 1000000 = 2000000 expect(result.totalBalance).toEqual(new bignumber_js_1.default(2000000)); expect(result.totalFiatValue).toEqual(new bignumber_js_1.default(2)); expect(result.hasAccounts).toBe(true); expect(result.referenceCurrency).toEqual(mockUSDCToken); }); it("should handle magnitude normalization when referenceCurrency is set", () => { const currencies = [mockBscUSDCToken]; const groupedAccounts = { [mockBscUSDCToken.id]: { totalBalance: new bignumber_js_1.default("2000000000000000000"), totalFiatValue: new bignumber_js_1.default(2), accounts: [mockBscUSDCAccount], referenceCurrency: mockBscUSDCToken, }, }; const result = (0, calculateProviderTotal_1.calculateProviderTotals)(currencies, groupedAccounts); expect(result.totalBalance).toEqual(new bignumber_js_1.default("2000000000000000000")); expect(result.referenceCurrency).toEqual(mockBscUSDCToken); }); it("should handle zero magnitude difference correctly", () => { const sameMagnitudeCurrency = { type: "TokenCurrency", id: "ethereum/erc20/usdc2", contractAddress: "0x1234567890123456789012345678901234567890", parentCurrency: mockETHCurrency, tokenType: "erc20", name: "USD Coin 2", ticker: "USDC2", units: [{ name: "USD Coin 2", code: "USDC2", magnitude: 6 }], }; const mockSameMagnitudeAccount = (0, account_1.genAccount)("usdc2-account", { currency: mockETHCurrency }); const currencies = [mockUSDCToken, sameMagnitudeCurrency]; const groupedAccounts = { [mockUSDCToken.id]: { totalBalance: new bignumber_js_1.default(1000000), totalFiatValue: new bignumber_js_1.default(1), accounts: [mockUSDCAccount], referenceCurrency: mockUSDCToken, // 6 decimals }, [sameMagnitudeCurrency.id]: { totalBalance: new bignumber_js_1.default(2000000), totalFiatValue: new bignumber_js_1.default(2), accounts: [mockSameMagnitudeAccount], referenceCurrency: sameMagnitudeCurrency, // 6 decimals }, }; const result = (0, calculateProviderTotal_1.calculateProviderTotals)(currencies, groupedAccounts); // Both have magnitude 6, so diff = 6 - 6 = 0 // No shifting needed, simple addition: 1000000 + 2000000 = 3000000 expect(result.totalBalance).toEqual(new bignumber_js_1.default(3000000)); expect(result.totalFiatValue).toEqual(new bignumber_js_1.default(3)); }); }); //# sourceMappingURL=calculateProviderTotal.test.js.map