import * as web3utils from 'web3-utils' ; import { EncodedTransaction , Account } from 'web3/types' ; import config from '../../config' ; import Contract from '../external/web3.contract' ; import { Web3ClientInterface } from '../external/web3.client' ; import { Logger } from '../../logger' ; import { decimalsToUnitMap } from './helpers' ; const DEFAULT_DECIMALS = 18 ; TODO export class Erc20TokenService { protected erc20Token : Contract ; protected logger = Logger . getInstance ( 'ERC20_TOKEN_SERVICE' ) ; param web3 constructor ( web3 , private contractAddress ) { this . erc20Token = web3 . getContract ( config . contracts . erc20Token . abi , contractAddress ) ; } /**
     *
     */ async getBalanceOf ( address , decimals = 0 ) { return web3utils . fromWei ( await this . erc20Token . queryMethod ( { methodName : 'balanceOf' , gasPrice : '0' , arguments : [ address ] } ) , decimals ? decimalsToUnitMap ( decimals ) : 'wei' ) . toString ( ) ; } private catchInfoError ( name , err ) { this . logger . warn ( '[catchInfoError] Cannot get info for' , name , { meta : { contractAddress : this . contractAddress , error : err } } ) ; return '' ; } /**
     *
     */ getInfo ( ) { return Promise . all ( [ this . erc20Token . queryMethod ( { methodName : 'name' , gasPrice : '0' , arguments : [ ] } ) . catch ( e => this . catchInfoError ( 'name' , e ) ) , this . erc20Token . queryMethod ( { methodName : 'symbol' , gasPrice : '0' , arguments : [ ] } ) . catch ( e => this . catchInfoError ( 'symbol' , e ) ) , this . erc20Token . queryMethod ( { methodName : 'decimals' , gasPrice : '0' , arguments : [ ] } ) . catch ( e => this . catchInfoError ( 'decimals' , e ) ) ] ) . then ( ( [ name , symbol , decimals ] ) => { return { name , symbol , decimals } ; } ) ; } param fromAddress param toAddress param amount async transfer ( account , params , toAddress , amount ) { return this . erc20Token . executeMethod ( { amount : '0' , methodName : 'transfer' , arguments : [ toAddress , amount ] , gas : params . gas , gasPrice : params . gasPrice } , account ) ; } }