import ERC20ABI from '../contracts/abi/erc-20' ; import { AbiItem } from 'web3-utils' ; import Web3 from 'web3' ; import { networkName , safeContractCall } from './utils/general-utils' ; export interface IAssets { getNativeTokenBalance ( userAddress ? ) : Promise < string > ; getBalanceForToken ( tokenAddress , tokenHolderAddress ? ) : Promise < string > ; getTokenInfo ( tokenAddress ) : Promise < { decimals : number ; name : string ; symbol : string ; } > ; } export default class Assets implements IAssets { constructor ( private web3 ) { } async getNativeTokenBalance ( userAddress ? ) { let address = userAddress ?? ( await this . web3 . eth . getAccounts ( ) ) [ 0 ] ; if ( [ 'kovan' , 'mainnet' ] . includes ( await networkName ( this . web3 ) ) ) { let previousBlockNumber = ( await this . web3 . eth . getBlockNumber ( ) ) - 1 ; return this . web3 . eth . getBalance ( address , previousBlockNumber ) ; } return this . web3 . eth . getBalance ( address ) ; } async getBalanceForToken ( tokenAddress , tokenHolderAddress ? ) { let address = tokenHolderAddress ?? ( await this . web3 . eth . getAccounts ( ) ) [ 0 ] ; const tokenContract = new this . web3 . eth . Contract ( ERC20ABI as AbiItem [ ] , tokenAddress ) ; return ( await safeContractCall ( this . web3 , tokenContract , 'balanceOf' , address ) ) as string ; } async getTokenInfo ( tokenAddress ) { const tokenContract = new this . web3 . eth . Contract ( ERC20ABI as AbiItem [ ] , tokenAddress ) ; return { decimals : Number ( await safeContractCall ( this . web3 , tokenContract , 'decimals' ) ) , name : ( await safeContractCall ( this . web3 , tokenContract , 'name' ) ) as string , symbol : ( await safeContractCall ( this . web3 , tokenContract , 'symbol' ) ) as string , } ; } }