|
| 1 | +import { BitrielWalletSDK } from "../src/sdk"; |
| 2 | +import { SUBSTRATE_NETWORKS, EVM_NETWORKS } from "../src/config/networks"; |
| 3 | +import { DetailedBalance } from "../src/wallet/types"; |
| 4 | + |
| 5 | +async function testBalanceDisplay() { |
| 6 | + // Test mnemonic (replace with your test mnemonic) |
| 7 | + const mnemonic = "REPLACE_WITH_YOUR_TEST_MNEMONIC"; // DO NOT commit real mnemonics to version control |
| 8 | + |
| 9 | + // Initialize the SDK with mnemonic |
| 10 | + const sdk = new BitrielWalletSDK(mnemonic); |
| 11 | + |
| 12 | + try { |
| 13 | + // Test Substrate Chain (Selendra) |
| 14 | + console.log("\n=== Testing Balance Display on Selendra Network ==="); |
| 15 | + const selendraNetwork = SUBSTRATE_NETWORKS.find( |
| 16 | + (n) => n.name === "Selendra" |
| 17 | + ); |
| 18 | + if (selendraNetwork) { |
| 19 | + console.log("Connecting to Selendra network..."); |
| 20 | + await sdk.connect(selendraNetwork.chainId.toString()); |
| 21 | + |
| 22 | + // Get detailed balance information |
| 23 | + console.log("\nGetting detailed balance information..."); |
| 24 | + const detailedBalance = await sdk.getDetailedBalance(); |
| 25 | + |
| 26 | + // Display formatted balance information |
| 27 | + console.log("\nDetailed Balance Information:"); |
| 28 | + console.log( |
| 29 | + "Total Balance:", |
| 30 | + detailedBalance.formatted.total, |
| 31 | + selendraNetwork.nativeCurrency.symbol |
| 32 | + ); |
| 33 | + console.log( |
| 34 | + "Locked Balance:", |
| 35 | + detailedBalance.formatted.locked, |
| 36 | + selendraNetwork.nativeCurrency.symbol |
| 37 | + ); |
| 38 | + console.log( |
| 39 | + "Transferable Balance:", |
| 40 | + detailedBalance.formatted.transferable, |
| 41 | + selendraNetwork.nativeCurrency.symbol |
| 42 | + ); |
| 43 | + |
| 44 | + // Verify balance calculations |
| 45 | + console.log("\nVerifying balance calculations..."); |
| 46 | + const totalBigInt = BigInt(detailedBalance.total); |
| 47 | + const lockedBigInt = BigInt(detailedBalance.locked); |
| 48 | + const transferableBigInt = BigInt(detailedBalance.transferable); |
| 49 | + |
| 50 | + console.log("Total (raw):", totalBigInt.toString()); |
| 51 | + console.log("Locked (raw):", lockedBigInt.toString()); |
| 52 | + console.log("Transferable (raw):", transferableBigInt.toString()); |
| 53 | + |
| 54 | + // Verify that total = locked + transferable |
| 55 | + const calculatedTotal = lockedBigInt + transferableBigInt; |
| 56 | + console.log("\nVerifying total = locked + transferable:"); |
| 57 | + console.log("Calculated Total:", calculatedTotal.toString()); |
| 58 | + console.log("Actual Total:", totalBigInt.toString()); |
| 59 | + console.log("Match:", calculatedTotal === totalBigInt); |
| 60 | + |
| 61 | + // Test balance formatting |
| 62 | + console.log("\nTesting balance formatting..."); |
| 63 | + const formattedTotal = sdk.formatTokenBalance( |
| 64 | + detailedBalance.total, |
| 65 | + selendraNetwork.nativeCurrency.decimals, |
| 66 | + { precision: 2 } |
| 67 | + ); |
| 68 | + console.log("Formatted with 2 decimal places:", formattedTotal); |
| 69 | + |
| 70 | + // Disconnect from Selendra |
| 71 | + await sdk.disconnect(); |
| 72 | + } |
| 73 | + |
| 74 | + // Test EVM Chain |
| 75 | + console.log("\n=== Testing Balance Display on EVM Network ==="); |
| 76 | + const evmNetwork = EVM_NETWORKS.find( |
| 77 | + (n) => n.name === "Selendra Mainnet" |
| 78 | + ); |
| 79 | + if (evmNetwork) { |
| 80 | + console.log("Connecting to EVM network..."); |
| 81 | + await sdk.connect(evmNetwork.chainId.toString()); |
| 82 | + |
| 83 | + // Get detailed balance information |
| 84 | + console.log("\nGetting detailed balance information..."); |
| 85 | + const detailedBalance = await sdk.getDetailedBalance(); |
| 86 | + |
| 87 | + // Display formatted balance information |
| 88 | + console.log("\nDetailed Balance Information:"); |
| 89 | + console.log( |
| 90 | + "Total Balance:", |
| 91 | + detailedBalance.formatted.total, |
| 92 | + evmNetwork.nativeCurrency.symbol |
| 93 | + ); |
| 94 | + console.log( |
| 95 | + "Locked Balance:", |
| 96 | + detailedBalance.formatted.locked, |
| 97 | + evmNetwork.nativeCurrency.symbol |
| 98 | + ); |
| 99 | + console.log( |
| 100 | + "Transferable Balance:", |
| 101 | + detailedBalance.formatted.transferable, |
| 102 | + evmNetwork.nativeCurrency.symbol |
| 103 | + ); |
| 104 | + |
| 105 | + // Verify EVM balance properties |
| 106 | + console.log("\nVerifying EVM balance properties..."); |
| 107 | + console.log( |
| 108 | + "Locked balance should be 0:", |
| 109 | + detailedBalance.locked === "0" |
| 110 | + ); |
| 111 | + console.log( |
| 112 | + "Transferable should equal total:", |
| 113 | + detailedBalance.transferable === detailedBalance.total |
| 114 | + ); |
| 115 | + |
| 116 | + // Disconnect from EVM |
| 117 | + await sdk.disconnect(); |
| 118 | + } |
| 119 | + } catch (error) { |
| 120 | + console.error("\nError during balance display test:", error); |
| 121 | + |
| 122 | + // Detailed error logging |
| 123 | + if (error instanceof Error) { |
| 124 | + console.error("Error Name:", error.name); |
| 125 | + console.error("Error Message:", error.message); |
| 126 | + console.error("Error Stack:", error.stack); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// Run the test |
| 132 | +testBalanceDisplay().catch(console.error); |
0 commit comments