Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ocean token & any token balance to Account #158

Merged
merged 3 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 50 additions & 16 deletions src/ocean/Account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,52 @@ export default class Account extends Instantiable {
*/

/**
* Balance of Any Token.
* @return {Promise<number>}
* Balance of Any Token (converted from wei).
* @return {Promise<string>}
*/
public async getTokenBalance(TokenAdress: string): Promise<number> {
// TO DO
return 0
public async getTokenBalance(TokenAdress: string): Promise<string> {
if (TokenAdress === null) return null
const minABI = [
0x3bfc marked this conversation as resolved.
Show resolved Hide resolved
{
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<string>}
*/
public async getOceanBalance(): Promise<string> {
return this.getTokenBalance(this.config.oceanTokenAddress)
}

/**
Expand All @@ -94,17 +134,11 @@ export default class Account extends Instantiable {
}

/**
* Balance of Ether.
* @return {Promise<number>}
* Balance of Ether.(converted from wei).
* @return {Promise<string>}
*/
public async getEtherBalance(): Promise<number> {
// 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<string> {
const result = await this.web3.eth.getBalance(this.id, 'latest')
return this.web3.utils.fromWei(result)
}
}
19 changes: 14 additions & 5 deletions src/ocean/Accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Balance>} Ether and Ocean Token balance.
* @return {Promise<String>} Token balance.
*/
public balance(TokenAddress: string, account: Account): Promise<number> {
public getTokenBalance(TokenAddress: string, account: Account): Promise<string> {
return account.getTokenBalance(TokenAddress)
}

/**
* Return account balance for a Ocean Tokens
* @param {Account} account Account instance.
* @return {Promise<String>} Ocean Token balance.
*/
public getOceanBalance(account: Account): Promise<string> {
return account.getOceanBalance()
0x3bfc marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Return account balance in ETH
* @param {Account} account Account instance.
* @return {Promise<Balance>} Ether and Ocean Token balance.
* @return {Promise<String>} Ether balance.
*/
public ETHbalance(account: Account): Promise<number> {
public getEtherBalance(account: Account): Promise<string> {
return account.getEtherBalance()
}
}