From 13095ce710b0ac0171b9349c3e9c199bbd22aa45 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 15 Jul 2020 03:19:00 -0700 Subject: [PATCH 1/3] add ocean token & any token balance to Account --- src/ocean/Account.ts | 48 +++++++++++++++++++++++++++++++++++++++---- src/ocean/Accounts.ts | 15 +++++++++++--- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/src/ocean/Account.ts b/src/ocean/Account.ts index 4d316d0fe..bf4479303 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 + 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, 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. + * @return {Promise} + */ + public async getOceanBalance(): Promise { + return this.getTokenBalance(this.config.oceanTokenAddress) } /** diff --git a/src/ocean/Accounts.ts b/src/ocean/Accounts.ts index 041a69bad..cd7729889 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. */ - 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} Ether and 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. */ - public ETHbalance(account: Account): Promise { + public getEtherBalance(account: Account): Promise { return account.getEtherBalance() } } From a03c9b888f6ea2376c253e841de0657faf2b9c06 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 15 Jul 2020 03:25:01 -0700 Subject: [PATCH 2/3] add ocean token & any token balance to Account --- src/ocean/Account.ts | 22 ++++++++-------------- src/ocean/Accounts.ts | 12 ++++++------ 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/src/ocean/Account.ts b/src/ocean/Account.ts index bf4479303..cd146c51d 100644 --- a/src/ocean/Account.ts +++ b/src/ocean/Account.ts @@ -79,7 +79,7 @@ export default class Account extends Instantiable { * Balance of Any Token (converted from wei). * @return {Promise} */ - public async getTokenBalance(TokenAdress: string): Promise { + public async getTokenBalance(TokenAdress: string): Promise { if (TokenAdress === null) return null const minABI = [ { @@ -117,10 +117,10 @@ export default class Account extends Instantiable { } /** - * Balance of Ocean Token. + * Balance of Ocean Token. (converted from wei). * @return {Promise} */ - public async getOceanBalance(): Promise { + public async getOceanBalance(): Promise { return this.getTokenBalance(this.config.oceanTokenAddress) } @@ -134,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 cd7729889..ed3e20cef 100644 --- a/src/ocean/Accounts.ts +++ b/src/ocean/Accounts.ts @@ -34,27 +34,27 @@ export class Accounts extends Instantiable { * 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 getTokenBalance(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} Ether and Ocean Token balance. + * @return {Promise} Ocean Token balance. */ - public getOceanBalance(account: Account): Promise { + 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 getEtherBalance(account: Account): Promise { + public getEtherBalance(account: Account): Promise { return account.getEtherBalance() } } From fec14ec13987cff45589810d1288f3553bbfc05e Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 15 Jul 2020 03:42:18 -0700 Subject: [PATCH 3/3] fix tsc --- src/ocean/Account.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ocean/Account.ts b/src/ocean/Account.ts index cd146c51d..fb932ece1 100644 --- a/src/ocean/Account.ts +++ b/src/ocean/Account.ts @@ -104,7 +104,7 @@ export default class Account extends Instantiable { ] let result = null try { - const token = new this.web3.eth.Contract(minABI, TokenAdress, { + const token = new this.web3.eth.Contract(minABI as any, TokenAdress, { from: this.id }) result = this.web3.utils.fromWei(