mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
Merge pull request #158 from oceanprotocol/feature/balances
add ocean token & any token balance to Account
This commit is contained in:
commit
1ebdff5321
@ -76,12 +76,52 @@ export default class Account extends Instantiable {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Balance of Any Token.
|
* Balance of Any Token (converted from wei).
|
||||||
* @return {Promise<number>}
|
* @return {Promise<string>}
|
||||||
*/
|
*/
|
||||||
public async getTokenBalance(TokenAdress: string): Promise<number> {
|
public async getTokenBalance(TokenAdress: string): Promise<string> {
|
||||||
// TO DO
|
if (TokenAdress === null) return null
|
||||||
return 0
|
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<string>}
|
||||||
|
*/
|
||||||
|
public async getOceanBalance(): Promise<string> {
|
||||||
|
return this.getTokenBalance(this.config.oceanTokenAddress)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -94,17 +134,11 @@ export default class Account extends Instantiable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Balance of Ether.
|
* Balance of Ether.(converted from wei).
|
||||||
* @return {Promise<number>}
|
* @return {Promise<string>}
|
||||||
*/
|
*/
|
||||||
public async getEtherBalance(): Promise<number> {
|
public async getEtherBalance(): Promise<string> {
|
||||||
// TO DO
|
const result = await this.web3.eth.getBalance(this.id, 'latest')
|
||||||
/* return this.web3.eth
|
return this.web3.utils.fromWei(result)
|
||||||
.getBalance(this.id, 'latest')
|
|
||||||
.then((balance: string): number => {
|
|
||||||
return new BigNumber(balance).toNumber()
|
|
||||||
})
|
|
||||||
*/
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,21 +31,30 @@ export class Accounts extends Instantiable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return account balance.
|
* Return account balance for a given ERC20 token
|
||||||
* @param {String} TokenAddress .
|
* @param {String} TokenAddress .
|
||||||
* @param {Account} account Account instance.
|
* @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.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()
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return account balance in ETH
|
* Return account balance in ETH
|
||||||
* @param {Account} account Account instance.
|
* @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()
|
return account.getEtherBalance()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user