diff --git a/packages/cli/src/commands/acc/get-balance.ts b/packages/cli/src/commands/acc/get-balance.ts index c56b2f4..9e622b2 100644 --- a/packages/cli/src/commands/acc/get-balance.ts +++ b/packages/cli/src/commands/acc/get-balance.ts @@ -3,6 +3,7 @@ import { WalletApi } from '@thepowereco/tssdk'; import { colorize } from 'json-colorizer'; import color from '@oclif/color'; +import { formatUnits } from 'viem/utils'; import { initializeNetworkApi, loadWallet } from '../../helpers/network.helper'; import { BaseCommand } from '../../baseCommand'; @@ -55,7 +56,7 @@ export default class AccGetBalance extends BaseCommand { const wallet = new WalletApi(networkApi); const result = await wallet.loadBalance(walletAddress); - + result.amount = Object.keys(result.amount).reduce((acc, key) => Object.assign(acc, { [key]: formatUnits(result.amount[key], networkApi.decimals[key]) }), {}); ux.action.stop(); if (result !== undefined) { diff --git a/packages/tssdk/src/libs/network/network.ts b/packages/tssdk/src/libs/network/network.ts index 962313c..b87238e 100755 --- a/packages/tssdk/src/libs/network/network.ts +++ b/packages/tssdk/src/libs/network/network.ts @@ -58,8 +58,7 @@ export class NetworkApi { public upload() { const { currentChain, currentNodes, nodeIndex, feeSettings, gasSettings, decimals, - } = - this; + } = this; return { currentChain, currentNodes, diff --git a/packages/tssdk/src/libs/wallet.ts b/packages/tssdk/src/libs/wallet.ts index eac9132..f73cf2e 100644 --- a/packages/tssdk/src/libs/wallet.ts +++ b/packages/tssdk/src/libs/wallet.ts @@ -3,7 +3,6 @@ import { AddressApi } from './address/address'; import { NetworkApi } from './network/network'; import { TransactionsApi } from './transactions'; import { COIN, CryptoApi, DERIVATION_PATH_BASE } from './crypto/crypto'; -import { correctAmount, correctAmountsObject } from '../utils/numbers'; import { Maybe, RegisteredAccount } from '../typings'; import { NetworkEnum } from '../config/network.enum'; @@ -157,7 +156,7 @@ export class WalletApi { ); if (payment) { tx.cur = payment.cur; - tx.amount = correctAmount(payment.amount, tx.cur); + tx.amount = payment.amount; } } if (!tx.cur || !tx.amount) { @@ -171,11 +170,8 @@ export class WalletApi { {}, ) : []; - } else if (tx.amount) { - tx.amount = correctAmount(tx.amount, tx.cur); } - // Common conversions if (tx.address) { tx.address = AddressApi.hexToTextAddress(tx.address); } @@ -204,10 +200,7 @@ export class WalletApi { // Correct the sums and addresses: we bring the addresses to text form, and the sums to the required number of characters after the decimal point block.bals = Object.keys(block.bals).reduce( (acc, key) => Object.assign(acc, { - [AddressApi.hexToTextAddress(key)]: { - ...block.bals[key], - amount: correctAmountsObject(block.bals[key].amount), - }, + [AddressApi.hexToTextAddress(key)]: block.bals[key], }), {}, ); @@ -223,10 +216,7 @@ export class WalletApi { public async loadBalance(address: string) { const walletData = await this.networkApi.getWallet(address); - return { - ...walletData, - amount: correctAmountsObject(walletData.amount), - }; + return walletData; } public async getWalletSequence(address: string) { diff --git a/packages/tssdk/src/utils/numbers.ts b/packages/tssdk/src/utils/numbers.ts deleted file mode 100644 index 6cf4c3e..0000000 --- a/packages/tssdk/src/utils/numbers.ts +++ /dev/null @@ -1,72 +0,0 @@ -export const correctAmount = ( - value: number, - inputToken: Uint8Array | string | number[], - incoming = true, -) => { - let multiplier = 1000000000; - - const token = Array.isArray(inputToken) || (inputToken instanceof Uint8Array) - // @ts-ignore - ? inputToken.reduce((acc: string, val: number) => acc + String.fromCharCode(val), '') - : inputToken; - - if (token === 'SK') { - multiplier = 1000000000; - } - - if (incoming) { - return value / multiplier; - } - - return Math.round(value * multiplier); -}; - -export const correctAmountsObject = (amountObj: any) => Object.keys(amountObj).reduce( - (acc, key) => Object.assign(acc, { [key]: correctAmount(amountObj[key], key) }), - {}, -); - -export const correctAmountToStr = ( - value: bigint, - digits: number, - roundTo: number, -) => { - const multiplier = 10 ** digits; - let integerPart = '0'; - let fractionalPart = ''; - - integerPart = (value / BigInt(multiplier)).toString(); - const fractionalB = value % BigInt(multiplier); - if (fractionalB > 0n) { - fractionalPart = fractionalB.toString().slice(0, roundTo); - } - - if (fractionalPart === '') { - return integerPart; - } - return `${integerPart}.${fractionalPart}`; -}; - -export const correctAmountFromStr = ( - value: string, - digits: number, -) => { - const correctValue = value.replace(',', '.'); - const valueArray = correctValue.split('.'); - if (valueArray.length <= 2) { - const multiplier = 10 ** digits; - let result = BigInt(multiplier) * BigInt(valueArray[0]); - if (valueArray.length === 2) { - const fractionalDigits = digits - valueArray[1].length; - if (fractionalDigits >= 0) { - const fractionalMultiplier = 10 ** fractionalDigits; - result += BigInt(fractionalMultiplier) * BigInt(valueArray[1]); - } else { - const fractionalMultiplier = 10 ** (-fractionalDigits); - result += BigInt(valueArray[1]) / BigInt(fractionalMultiplier); - } - } - return result; - } - return 0n; -};