web3-utils#fromWei TypeScript Examples

The following examples show how to use web3-utils#fromWei. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CeloService.ts    From tatum-blockchain-connector with MIT License 6 votes vote down vote up
public async getBalance(address: string, testnet?: boolean): Promise<{ celo: string, cUsd: string, cEur: string }> {
        const t = testnet === undefined ? await this.isTestnet() : testnet;
        const provider = new CeloProvider((await this.getNodesUrl(t))[0]);
        // @ts-ignore
        const cUsd = new ((await this.getClient(t))).eth.Contract(token_abi, t ? CUSD_ADDRESS_TESTNET : CUSD_ADDRESS_MAINNET);
        // @ts-ignore
        const cEur = new ((await this.getClient(t))).eth.Contract(token_abi, t ? CEUR_ADDRESS_TESTNET : CEUR_ADDRESS_MAINNET);
        return {
            celo: fromWei((await provider.getBalance(address)).toString(), 'ether'),
            cUsd: fromWei(await cUsd.methods.balanceOf(address).call(), 'ether'),
            cEur: fromWei(await cEur.methods.balanceOf(address).call(), 'ether'),
        };
    }
Example #2
Source File: web3-pure.ts    From rubic-sdk with GNU General Public License v3.0 5 votes vote down vote up
/**
     * @description convert amount from Wei to Ether units
     * @param amountInWei amount to convert
     * @param [decimals=18] token decimals
     */
    static fromWei(amountInWei: BigNumber | string | number, decimals = 18): BigNumber {
        return new BigNumber(amountInWei).div(new BigNumber(10).pow(decimals));
    }
Example #3
Source File: web3-pure.ts    From rubic-sdk with GNU General Public License v3.0 5 votes vote down vote up
/**
     * @description converts Wei amount into Eth
     * @param value to convert in Wei
     */
    static weiToEth(value: string | BigNumber): string {
        return fromWei(value.toString(), 'ether');
    }
Example #4
Source File: ethWallet.ts    From webapp with MIT License 5 votes vote down vote up
@action async nativeBalanceChange(nativeBalance: string) {
    if (nativeBalance)
      vxm.ethBancor.updateUserBalances([
        { balance: fromWei(nativeBalance), id: ethReserveAddress }
      ]);
  }
Example #5
Source File: ethWallet.ts    From webapp with MIT License 5 votes vote down vote up
@action async getBalance({
    accountHolder,
    tokenContractAddress,
    keepWei = false
  }: {
    accountHolder: EthAddress;
    tokenContractAddress: EthAddress;
    keepWei?: boolean;
  }): Promise<string> {
    if (!accountHolder || !tokenContractAddress)
      throw new Error(
        "Cannot get balance without both the account holder and token contract address"
      );
    const web3View = getWeb3(this.currentNetwork, Provider.Alchemy);

    if (
      compareString(
        tokenContractAddress,
        "0xc0829421C1d260BD3cB3E0F06cfE2D52db2cE315"
      ) ||
      compareString(tokenContractAddress, ethReserveAddress)
    ) {
      const weiBalance = await web3View.eth.getBalance(accountHolder);
      return fromWei(weiBalance);
    } else {
      if (!tokenContractAddress)
        throw new Error("tokenContractAddress is falsy");

      const tokenContract = new web3View.eth.Contract(
        ABISmartToken,
        tokenContractAddress
      );

      const [decimals, weiBalance] = await Promise.all([
        tokenContract.methods.decimals().call() as string,
        tokenContract.methods.balanceOf(accountHolder).call() as string
      ]);
      if (keepWei) return weiBalance;
      return shrinkToken(weiBalance, Number(decimals));
    }
  }
Example #6
Source File: currencyFunctions.ts    From core with MIT License 5 votes vote down vote up
weiToETH = (amount: string) => {
  return fromWei(amount, 'ether');
}
Example #7
Source File: BscService.ts    From tatum-blockchain-connector with MIT License 5 votes vote down vote up
public async getBalance(address: string): Promise<{ balance: string }> {
        const client = await this.getClient(await this.isTestnet());
        return {balance: fromWei(await client.eth.getBalance(address), 'ether')};
    }
Example #8
Source File: EthereumService.ts    From tatum-blockchain-connector with MIT License 5 votes vote down vote up
public async getBalance(address: string): Promise<{ balance: string }> {
    const client = await this.getClient()
    return { balance: fromWei(await client.eth.getBalance(address), 'ether') };
  }
Example #9
Source File: KcsService.ts    From tatum-blockchain-connector with MIT License 5 votes vote down vote up
public async getBalance(address: string): Promise<{ balance: string }> {
        const client = await this.getClient(await this.isTestnet());
        return {balance: fromWei(await client.eth.getBalance(address), 'ether')};
    }
Example #10
Source File: OneService.ts    From tatum-blockchain-connector with MIT License 5 votes vote down vote up
public async getBalance(address: string, shardID?: number): Promise<{ balance: string }> {
        const client = await this.getClient(await this.isTestnet(), shardID);
        return {balance: fromWei((await client.blockchain.getBalance({address, shardID})).result, 'ether')};
    }
Example #11
Source File: PolygonService.ts    From tatum-blockchain-connector with MIT License 5 votes vote down vote up
public async getBalance(address: string): Promise<{ balance: string }> {
        const client = await this.getClient(await this.isTestnet());
        return {balance: fromWei(await client.eth.getBalance(address), 'ether')};
    }
Example #12
Source File: XdcService.ts    From tatum-blockchain-connector with MIT License 5 votes vote down vote up
public async getBalance(address: string): Promise<{ balance: string }> {
        const client = await this.getClient(await this.isTestnet());
        return {balance: fromWei(await client.eth.getBalance(fromXdcAddress(address)), 'ether')};
    }