Skip to content

Commit 3a70049

Browse files
committed
fixed bug decimal number trx and display locked balance
1 parent 716130f commit 3a70049

File tree

5 files changed

+191
-48
lines changed

5 files changed

+191
-48
lines changed

dist/index.d.mts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,20 @@ interface TokenBalance {
7979
balance: string;
8080
formatted: string;
8181
}
82+
interface DetailedBalance {
83+
total: string;
84+
locked: string;
85+
transferable: string;
86+
formatted: {
87+
total: string;
88+
locked: string;
89+
transferable: string;
90+
};
91+
}
8292
interface WalletBalances {
8393
native: string;
8494
tokens: TokenBalance[];
95+
detailed?: DetailedBalance;
8596
}
8697
interface WalletState {
8798
address: string;
@@ -116,6 +127,9 @@ interface SubstrateAccountInfo {
116127
free: {
117128
toString: () => string;
118129
};
130+
frozen: {
131+
toString: () => string;
132+
};
119133
};
120134
}
121135
interface SubstrateExtrinsic {
@@ -187,6 +201,11 @@ declare class BitrielWalletSDK {
187201
connect(chainId: string): Promise<void>;
188202
disconnect(): Promise<void>;
189203
getWalletState(): Promise<WalletState>;
204+
/**
205+
* Get detailed balance information including locked and transferable balances
206+
* @returns Detailed balance information
207+
*/
208+
getDetailedBalance(): Promise<DetailedBalance>;
190209
sendTransaction(tx: TransactionRequest): Promise<string>;
191210
signMessage(message: string): Promise<string>;
192211
getSupportedNetworks(): NetworkConfig[];
@@ -230,6 +249,11 @@ declare class SubstrateWalletProvider implements WalletProvider {
230249
getAddress(): Promise<string>;
231250
signMessage(message: string): Promise<string>;
232251
getBalance(): Promise<string>;
252+
/**
253+
* Get detailed account information including locked balance
254+
* @returns Account information
255+
*/
256+
getAccountInfo(): Promise<SubstrateAccountInfo>;
233257
sendTransaction(tx: PolkadotTransactionRequest): Promise<string>;
234258
getTokenBalance(tokenAddress: string): Promise<string>;
235259
listTokens(): Promise<TokenInfo[]>;
@@ -266,13 +290,5 @@ declare class EVMWalletProvider implements WalletProvider {
266290
* @returns The amount in base units as string
267291
*/
268292
declare function parseTransactionAmount(amount: string | number | boolean | Uint8Array<ArrayBufferLike> | null, chainType: "substrate" | "evm", decimals?: number): string;
269-
/**
270-
* Formats amount for both Substrate and EVM chains
271-
* @param amount The amount in base units (as string)
272-
* @param chainType The type of chain ('substrate' or 'evm')
273-
* @param decimals Number of decimal places (default: 18)
274-
* @returns The human-readable amount as string
275-
*/
276-
declare function formatTransactionAmount(amount: string, chainType: "substrate" | "evm", decimals?: number): string;
277293

278-
export { BitrielWalletSDK, type EVMNetworkConfig, type EVMTransactionRequest, EVMWalletProvider, EVM_NETWORKS, type FeeEstimate, GENERIC_ABI, type NetworkConfig, type PolkadotTransactionRequest, SUBSTRATE_NETWORKS, SUPPORTED_NETWORKS, type SubstrateAccountInfo, type SubstrateApi, type SubstrateExtrinsic, type SubstrateNetworkConfig, type SubstrateTransactionResult, type SubstrateTxModule, SubstrateWalletProvider, type TokenBalance, type TokenBalanceFormatOptions, type TokenConfig, type TokenInfo, type TransactionRequest, type WalletBalances, type WalletProvider, type WalletState, formatTokenAmount, formatTokenBalance, formatTransactionAmount, parseTokenBalance, parseTransactionAmount };
294+
export { BitrielWalletSDK, type DetailedBalance, type EVMNetworkConfig, type EVMTransactionRequest, EVMWalletProvider, EVM_NETWORKS, type FeeEstimate, GENERIC_ABI, type NetworkConfig, type PolkadotTransactionRequest, SUBSTRATE_NETWORKS, SUPPORTED_NETWORKS, type SubstrateAccountInfo, type SubstrateApi, type SubstrateExtrinsic, type SubstrateNetworkConfig, type SubstrateTransactionResult, type SubstrateTxModule, SubstrateWalletProvider, type TokenBalance, type TokenBalanceFormatOptions, type TokenConfig, type TokenInfo, type TransactionRequest, type WalletBalances, type WalletProvider, type WalletState, formatTokenAmount, formatTokenBalance, parseTokenBalance, parseTransactionAmount };

dist/index.d.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,20 @@ interface TokenBalance {
7979
balance: string;
8080
formatted: string;
8181
}
82+
interface DetailedBalance {
83+
total: string;
84+
locked: string;
85+
transferable: string;
86+
formatted: {
87+
total: string;
88+
locked: string;
89+
transferable: string;
90+
};
91+
}
8292
interface WalletBalances {
8393
native: string;
8494
tokens: TokenBalance[];
95+
detailed?: DetailedBalance;
8596
}
8697
interface WalletState {
8798
address: string;
@@ -116,6 +127,9 @@ interface SubstrateAccountInfo {
116127
free: {
117128
toString: () => string;
118129
};
130+
frozen: {
131+
toString: () => string;
132+
};
119133
};
120134
}
121135
interface SubstrateExtrinsic {
@@ -187,6 +201,11 @@ declare class BitrielWalletSDK {
187201
connect(chainId: string): Promise<void>;
188202
disconnect(): Promise<void>;
189203
getWalletState(): Promise<WalletState>;
204+
/**
205+
* Get detailed balance information including locked and transferable balances
206+
* @returns Detailed balance information
207+
*/
208+
getDetailedBalance(): Promise<DetailedBalance>;
190209
sendTransaction(tx: TransactionRequest): Promise<string>;
191210
signMessage(message: string): Promise<string>;
192211
getSupportedNetworks(): NetworkConfig[];
@@ -230,6 +249,11 @@ declare class SubstrateWalletProvider implements WalletProvider {
230249
getAddress(): Promise<string>;
231250
signMessage(message: string): Promise<string>;
232251
getBalance(): Promise<string>;
252+
/**
253+
* Get detailed account information including locked balance
254+
* @returns Account information
255+
*/
256+
getAccountInfo(): Promise<SubstrateAccountInfo>;
233257
sendTransaction(tx: PolkadotTransactionRequest): Promise<string>;
234258
getTokenBalance(tokenAddress: string): Promise<string>;
235259
listTokens(): Promise<TokenInfo[]>;
@@ -266,13 +290,5 @@ declare class EVMWalletProvider implements WalletProvider {
266290
* @returns The amount in base units as string
267291
*/
268292
declare function parseTransactionAmount(amount: string | number | boolean | Uint8Array<ArrayBufferLike> | null, chainType: "substrate" | "evm", decimals?: number): string;
269-
/**
270-
* Formats amount for both Substrate and EVM chains
271-
* @param amount The amount in base units (as string)
272-
* @param chainType The type of chain ('substrate' or 'evm')
273-
* @param decimals Number of decimal places (default: 18)
274-
* @returns The human-readable amount as string
275-
*/
276-
declare function formatTransactionAmount(amount: string, chainType: "substrate" | "evm", decimals?: number): string;
277293

278-
export { BitrielWalletSDK, type EVMNetworkConfig, type EVMTransactionRequest, EVMWalletProvider, EVM_NETWORKS, type FeeEstimate, GENERIC_ABI, type NetworkConfig, type PolkadotTransactionRequest, SUBSTRATE_NETWORKS, SUPPORTED_NETWORKS, type SubstrateAccountInfo, type SubstrateApi, type SubstrateExtrinsic, type SubstrateNetworkConfig, type SubstrateTransactionResult, type SubstrateTxModule, SubstrateWalletProvider, type TokenBalance, type TokenBalanceFormatOptions, type TokenConfig, type TokenInfo, type TransactionRequest, type WalletBalances, type WalletProvider, type WalletState, formatTokenAmount, formatTokenBalance, formatTransactionAmount, parseTokenBalance, parseTransactionAmount };
294+
export { BitrielWalletSDK, type DetailedBalance, type EVMNetworkConfig, type EVMTransactionRequest, EVMWalletProvider, EVM_NETWORKS, type FeeEstimate, GENERIC_ABI, type NetworkConfig, type PolkadotTransactionRequest, SUBSTRATE_NETWORKS, SUPPORTED_NETWORKS, type SubstrateAccountInfo, type SubstrateApi, type SubstrateExtrinsic, type SubstrateNetworkConfig, type SubstrateTransactionResult, type SubstrateTxModule, SubstrateWalletProvider, type TokenBalance, type TokenBalanceFormatOptions, type TokenConfig, type TokenInfo, type TransactionRequest, type WalletBalances, type WalletProvider, type WalletState, formatTokenAmount, formatTokenBalance, parseTokenBalance, parseTransactionAmount };

dist/index.js

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ __export(index_exports, {
2929
SubstrateWalletProvider: () => SubstrateWalletProvider,
3030
formatTokenAmount: () => formatTokenAmount,
3131
formatTokenBalance: () => formatTokenBalance,
32-
formatTransactionAmount: () => formatTransactionAmount,
3332
parseTokenBalance: () => parseTokenBalance,
3433
parseTransactionAmount: () => parseTransactionAmount
3534
});
@@ -1070,6 +1069,18 @@ var SubstrateWalletProvider = class {
10701069
);
10711070
return accountInfo.data.free.toString();
10721071
}
1072+
/**
1073+
* Get detailed account information including locked balance
1074+
* @returns Account information
1075+
*/
1076+
async getAccountInfo() {
1077+
if (!this.api || !this.pair) {
1078+
throw new Error("Wallet not connected");
1079+
}
1080+
return await this.api.query.system.account(
1081+
this.pair.address
1082+
);
1083+
}
10731084
async sendTransaction(tx) {
10741085
if (!this.api || !this.pair) {
10751086
throw new Error("Wallet not connected");
@@ -1560,15 +1571,67 @@ var BitrielWalletSDK = class {
15601571
});
15611572
}
15621573
}
1574+
const detailedBalance = await this.getDetailedBalance();
15631575
return {
15641576
address,
15651577
balances: {
15661578
native: nativeBalance,
1567-
tokens: tokenBalances
1579+
tokens: tokenBalances,
1580+
detailed: detailedBalance
15681581
},
15691582
network: this.currentNetwork
15701583
};
15711584
}
1585+
/**
1586+
* Get detailed balance information including locked and transferable balances
1587+
* @returns Detailed balance information
1588+
*/
1589+
async getDetailedBalance() {
1590+
if (!this.currentNetwork) {
1591+
throw new Error("No network connected");
1592+
}
1593+
const provider = this.providers.get(
1594+
this.currentNetwork.chainId.toString()
1595+
);
1596+
if (!provider) {
1597+
throw new Error("Provider not found");
1598+
}
1599+
const totalBalance = await provider.getBalance();
1600+
const decimals = this.currentNetwork.nativeCurrency.decimals;
1601+
if (this.currentNetwork.type === "substrate") {
1602+
const substrateProvider = provider;
1603+
const accountInfo = await substrateProvider.getAccountInfo();
1604+
const locked = accountInfo.data.frozen.toString();
1605+
const transferable = (BigInt(totalBalance) - BigInt(locked)).toString();
1606+
return {
1607+
total: totalBalance,
1608+
locked,
1609+
transferable,
1610+
formatted: {
1611+
total: this.formatTokenBalance(totalBalance, decimals),
1612+
locked: this.formatTokenBalance(locked, decimals),
1613+
transferable: this.formatTokenBalance(
1614+
transferable,
1615+
decimals
1616+
)
1617+
}
1618+
};
1619+
} else {
1620+
return {
1621+
total: totalBalance,
1622+
locked: "0",
1623+
transferable: totalBalance,
1624+
formatted: {
1625+
total: this.formatTokenBalance(totalBalance, decimals),
1626+
locked: "0.0",
1627+
transferable: this.formatTokenBalance(
1628+
totalBalance,
1629+
decimals
1630+
)
1631+
}
1632+
};
1633+
}
1634+
}
15721635
async sendTransaction(tx) {
15731636
if (!this.currentNetwork) {
15741637
throw new Error("No network connected");
@@ -1662,18 +1725,11 @@ function parseTransactionAmount(amount, chainType, decimals = 18) {
16621725
if (chainType === "evm") {
16631726
return import_ethers2.ethers.parseEther(amount.toString()).toString();
16641727
} else {
1665-
return new import_util.BN(amount.toString()).mul(new import_util.BN(10).pow(new import_util.BN(decimals))).toString();
1666-
}
1667-
}
1668-
function formatTransactionAmount(amount, chainType, decimals = 18) {
1669-
if (chainType === "evm") {
1670-
return import_ethers2.ethers.formatEther(amount);
1671-
} else {
1672-
const bn = new import_util.BN(amount);
1673-
const divisor = new import_util.BN(10).pow(new import_util.BN(decimals));
1674-
const whole = bn.div(divisor).toString();
1675-
const fraction = bn.mod(divisor).toString().padStart(decimals, "0");
1676-
return `${whole}.${fraction}`;
1728+
const amountStr = amount.toString();
1729+
const [whole, fraction = ""] = amountStr.split(".");
1730+
const paddedFraction = fraction.padEnd(decimals, "0").slice(0, decimals);
1731+
const fullNumber = whole + paddedFraction;
1732+
return new import_util.BN(fullNumber).toString();
16771733
}
16781734
}
16791735
// Annotate the CommonJS export names for ESM import in node:
@@ -1687,7 +1743,6 @@ function formatTransactionAmount(amount, chainType, decimals = 18) {
16871743
SubstrateWalletProvider,
16881744
formatTokenAmount,
16891745
formatTokenBalance,
1690-
formatTransactionAmount,
16911746
parseTokenBalance,
16921747
parseTransactionAmount
16931748
});

dist/index.mjs

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,18 @@ var SubstrateWalletProvider = class {
10331033
);
10341034
return accountInfo.data.free.toString();
10351035
}
1036+
/**
1037+
* Get detailed account information including locked balance
1038+
* @returns Account information
1039+
*/
1040+
async getAccountInfo() {
1041+
if (!this.api || !this.pair) {
1042+
throw new Error("Wallet not connected");
1043+
}
1044+
return await this.api.query.system.account(
1045+
this.pair.address
1046+
);
1047+
}
10361048
async sendTransaction(tx) {
10371049
if (!this.api || !this.pair) {
10381050
throw new Error("Wallet not connected");
@@ -1523,15 +1535,67 @@ var BitrielWalletSDK = class {
15231535
});
15241536
}
15251537
}
1538+
const detailedBalance = await this.getDetailedBalance();
15261539
return {
15271540
address,
15281541
balances: {
15291542
native: nativeBalance,
1530-
tokens: tokenBalances
1543+
tokens: tokenBalances,
1544+
detailed: detailedBalance
15311545
},
15321546
network: this.currentNetwork
15331547
};
15341548
}
1549+
/**
1550+
* Get detailed balance information including locked and transferable balances
1551+
* @returns Detailed balance information
1552+
*/
1553+
async getDetailedBalance() {
1554+
if (!this.currentNetwork) {
1555+
throw new Error("No network connected");
1556+
}
1557+
const provider = this.providers.get(
1558+
this.currentNetwork.chainId.toString()
1559+
);
1560+
if (!provider) {
1561+
throw new Error("Provider not found");
1562+
}
1563+
const totalBalance = await provider.getBalance();
1564+
const decimals = this.currentNetwork.nativeCurrency.decimals;
1565+
if (this.currentNetwork.type === "substrate") {
1566+
const substrateProvider = provider;
1567+
const accountInfo = await substrateProvider.getAccountInfo();
1568+
const locked = accountInfo.data.frozen.toString();
1569+
const transferable = (BigInt(totalBalance) - BigInt(locked)).toString();
1570+
return {
1571+
total: totalBalance,
1572+
locked,
1573+
transferable,
1574+
formatted: {
1575+
total: this.formatTokenBalance(totalBalance, decimals),
1576+
locked: this.formatTokenBalance(locked, decimals),
1577+
transferable: this.formatTokenBalance(
1578+
transferable,
1579+
decimals
1580+
)
1581+
}
1582+
};
1583+
} else {
1584+
return {
1585+
total: totalBalance,
1586+
locked: "0",
1587+
transferable: totalBalance,
1588+
formatted: {
1589+
total: this.formatTokenBalance(totalBalance, decimals),
1590+
locked: "0.0",
1591+
transferable: this.formatTokenBalance(
1592+
totalBalance,
1593+
decimals
1594+
)
1595+
}
1596+
};
1597+
}
1598+
}
15351599
async sendTransaction(tx) {
15361600
if (!this.currentNetwork) {
15371601
throw new Error("No network connected");
@@ -1625,18 +1689,11 @@ function parseTransactionAmount(amount, chainType, decimals = 18) {
16251689
if (chainType === "evm") {
16261690
return ethers2.parseEther(amount.toString()).toString();
16271691
} else {
1628-
return new BN(amount.toString()).mul(new BN(10).pow(new BN(decimals))).toString();
1629-
}
1630-
}
1631-
function formatTransactionAmount(amount, chainType, decimals = 18) {
1632-
if (chainType === "evm") {
1633-
return ethers2.formatEther(amount);
1634-
} else {
1635-
const bn = new BN(amount);
1636-
const divisor = new BN(10).pow(new BN(decimals));
1637-
const whole = bn.div(divisor).toString();
1638-
const fraction = bn.mod(divisor).toString().padStart(decimals, "0");
1639-
return `${whole}.${fraction}`;
1692+
const amountStr = amount.toString();
1693+
const [whole, fraction = ""] = amountStr.split(".");
1694+
const paddedFraction = fraction.padEnd(decimals, "0").slice(0, decimals);
1695+
const fullNumber = whole + paddedFraction;
1696+
return new BN(fullNumber).toString();
16401697
}
16411698
}
16421699
export {
@@ -1649,7 +1706,6 @@ export {
16491706
SubstrateWalletProvider,
16501707
formatTokenAmount,
16511708
formatTokenBalance,
1652-
formatTransactionAmount,
16531709
parseTokenBalance,
16541710
parseTransactionAmount
16551711
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bitriel-wallet-sdk",
3-
"version": "1.1.15",
3+
"version": "1.1.16",
44
"description": "This is bitriel Wallet SDK",
55
"main": "./dist/index.js",
66
"module": "./dist/index.mjs",

0 commit comments

Comments
 (0)