diff --git a/src/ocean/Account.ts b/src/ocean/Account.ts index 4d316d0fe..fb932ece1 100644 --- a/src/ocean/Account.ts +++ b/src/ocean/Account.ts @@ -76,12 +76,52 @@ export default class Account extends Instantiable { */ /** - * Balance of Any Token. - * @return {Promise} + * Balance of Any Token (converted from wei). + * @return {Promise} */ - public async getTokenBalance(TokenAdress: string): Promise { - // TO DO - return 0 + public async getTokenBalance(TokenAdress: string): Promise { + if (TokenAdress === null) return null + const minABI = [ + { + constant: true, + inputs: [ + { + name: '_owner', + type: 'address' + } + ], + name: 'balanceOf', + outputs: [ + { + name: 'balance', + type: 'uint256' + } + ], + payable: false, + stateMutability: 'view', + type: 'function' + } + ] + let result = null + try { + const token = new this.web3.eth.Contract(minABI as any, TokenAdress, { + from: this.id + }) + result = this.web3.utils.fromWei( + await token.methods.balanceOf(this.id).call() + ) + } catch (e) { + console.error(e) + } + return result + } + + /** + * Balance of Ocean Token. (converted from wei). + * @return {Promise} + */ + public async getOceanBalance(): Promise { + return this.getTokenBalance(this.config.oceanTokenAddress) } /** @@ -94,17 +134,11 @@ export default class Account extends Instantiable { } /** - * Balance of Ether. - * @return {Promise} + * Balance of Ether.(converted from wei). + * @return {Promise} */ - public async getEtherBalance(): Promise { - // TO DO - /* return this.web3.eth - .getBalance(this.id, 'latest') - .then((balance: string): number => { - return new BigNumber(balance).toNumber() - }) - */ - return 0 + public async getEtherBalance(): Promise { + const result = await this.web3.eth.getBalance(this.id, 'latest') + return this.web3.utils.fromWei(result) } } diff --git a/src/ocean/Accounts.ts b/src/ocean/Accounts.ts index 041a69bad..ed3e20cef 100644 --- a/src/ocean/Accounts.ts +++ b/src/ocean/Accounts.ts @@ -31,21 +31,30 @@ export class Accounts extends Instantiable { } /** - * Return account balance. + * Return account balance for a given ERC20 token * @param {String} TokenAddress . * @param {Account} account Account instance. - * @return {Promise} Ether and Ocean Token balance. + * @return {Promise} Token balance. */ - public balance(TokenAddress: string, account: Account): Promise { + public getTokenBalance(TokenAddress: string, account: Account): Promise { return account.getTokenBalance(TokenAddress) } + /** + * Return account balance for a Ocean Tokens + * @param {Account} account Account instance. + * @return {Promise} Ocean Token balance. + */ + public getOceanBalance(account: Account): Promise { + return account.getOceanBalance() + } + /** * Return account balance in ETH * @param {Account} account Account instance. - * @return {Promise} Ether and Ocean Token balance. + * @return {Promise} Ether balance. */ - public ETHbalance(account: Account): Promise { + public getEtherBalance(account: Account): Promise { return account.getEtherBalance() } }