2019-06-20 00:20:09 +02:00
|
|
|
import BigNumber from 'bignumber.js'
|
|
|
|
import Balance from '../models/Balance'
|
2019-03-21 02:56:58 +01:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
import { Instantiable, InstantiableConfig } from '../Instantiable.abstract'
|
2018-10-05 12:34:31 +02:00
|
|
|
|
2019-01-09 16:17:23 +01:00
|
|
|
/**
|
|
|
|
* Account information.
|
|
|
|
*/
|
2019-03-21 02:56:58 +01:00
|
|
|
export default class Account extends Instantiable {
|
2019-02-04 17:45:49 +01:00
|
|
|
private password?: string
|
2019-08-19 13:21:19 +02:00
|
|
|
|
2019-05-08 03:10:21 +02:00
|
|
|
private token?: string
|
2019-02-04 17:45:49 +01:00
|
|
|
|
2019-06-20 00:20:09 +02:00
|
|
|
constructor(private id: string = '0x0', config?: InstantiableConfig) {
|
2019-03-21 02:56:58 +01:00
|
|
|
super()
|
|
|
|
if (config) {
|
|
|
|
this.setInstanceConfig(config)
|
|
|
|
}
|
|
|
|
}
|
2019-03-19 12:27:33 +01:00
|
|
|
|
|
|
|
public getId() {
|
|
|
|
return this.id
|
|
|
|
}
|
|
|
|
|
|
|
|
public setId(id) {
|
|
|
|
this.id = id
|
|
|
|
}
|
|
|
|
|
2019-02-04 17:45:49 +01:00
|
|
|
/**
|
|
|
|
* Set account password.
|
|
|
|
* @param {string} password Password for account.
|
|
|
|
*/
|
2019-02-04 18:13:54 +01:00
|
|
|
public setPassword(password: string): void {
|
2019-02-04 17:45:49 +01:00
|
|
|
this.password = password
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns account password.
|
|
|
|
* @return {string} Account password.
|
|
|
|
*/
|
2019-02-04 18:13:54 +01:00
|
|
|
public getPassword(): string {
|
2019-02-04 17:45:49 +01:00
|
|
|
return this.password
|
|
|
|
}
|
|
|
|
|
2019-05-08 03:10:21 +02:00
|
|
|
/**
|
|
|
|
* Set account token.
|
|
|
|
* @param {string} token Token for account.
|
|
|
|
*/
|
|
|
|
public setToken(token: string): void {
|
|
|
|
this.token = token
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns account token.
|
2019-05-09 16:34:44 +02:00
|
|
|
* @return {Promise<string>} Account token.
|
2019-05-08 03:10:21 +02:00
|
|
|
*/
|
2019-05-09 16:34:44 +02:00
|
|
|
public async getToken(): Promise<string> {
|
2019-06-24 13:06:38 +02:00
|
|
|
return this.token || this.ocean.auth.restore(this)
|
2019-05-09 16:34:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns if account token is stored.
|
|
|
|
* @return {Promise<boolean>} Is stored.
|
|
|
|
*/
|
|
|
|
public isTokenStored(): Promise<boolean> {
|
|
|
|
return this.ocean.auth.isStored(this)
|
2019-05-08 03:10:21 +02:00
|
|
|
}
|
|
|
|
|
2019-05-20 11:33:15 +02:00
|
|
|
/**
|
|
|
|
* Authenticate the account.
|
|
|
|
*/
|
|
|
|
public authenticate() {
|
|
|
|
return this.ocean.auth.store(this)
|
|
|
|
}
|
|
|
|
|
2019-01-09 16:17:23 +01:00
|
|
|
/**
|
|
|
|
* Balance of Ocean Token.
|
|
|
|
* @return {Promise<number>}
|
|
|
|
*/
|
2018-10-16 14:56:18 +02:00
|
|
|
public async getOceanBalance(): Promise<number> {
|
2019-06-20 00:20:09 +02:00
|
|
|
const { token } = this.ocean.keeper
|
|
|
|
return (await token.balanceOf(this.id)) / 10 ** (await token.decimals())
|
2018-10-05 12:34:31 +02:00
|
|
|
}
|
|
|
|
|
2019-01-09 16:17:23 +01:00
|
|
|
/**
|
|
|
|
* Balance of Ether.
|
|
|
|
* @return {Promise<number>}
|
|
|
|
*/
|
2018-10-18 13:06:52 +02:00
|
|
|
public async getEtherBalance(): Promise<number> {
|
2019-06-20 00:20:09 +02:00
|
|
|
return this.web3.eth
|
|
|
|
.getBalance(this.id, 'latest')
|
2018-10-16 14:56:18 +02:00
|
|
|
.then((balance: string): number => {
|
2018-10-05 12:34:31 +02:00
|
|
|
return new BigNumber(balance).toNumber()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-09 16:17:23 +01:00
|
|
|
/**
|
|
|
|
* Balances of Ether and Ocean Token.
|
|
|
|
* @return {Promise<Balance>}
|
|
|
|
*/
|
2018-10-16 14:56:18 +02:00
|
|
|
public async getBalance(): Promise<Balance> {
|
2019-02-11 14:56:48 +01:00
|
|
|
return {
|
|
|
|
eth: await this.getEtherBalance(),
|
2019-06-20 00:20:09 +02:00
|
|
|
ocn: await this.getOceanBalance()
|
2018-10-16 14:56:18 +02:00
|
|
|
}
|
2018-10-05 12:34:31 +02:00
|
|
|
}
|
|
|
|
|
2019-01-09 16:17:23 +01:00
|
|
|
/**
|
|
|
|
* Request Ocean Tokens.
|
|
|
|
* @param {number} amount Tokens to be requested.
|
|
|
|
* @return {Promise<number>}
|
|
|
|
*/
|
2019-05-23 11:52:36 +02:00
|
|
|
public async requestTokens(amount: number | string): Promise<string> {
|
|
|
|
amount = String(amount)
|
2019-06-25 11:12:32 +02:00
|
|
|
if (!this.ocean.keeper.dispenser) {
|
|
|
|
throw new Error('Dispenser not available on this network.')
|
|
|
|
}
|
2019-01-24 12:57:03 +01:00
|
|
|
try {
|
2019-06-20 00:20:09 +02:00
|
|
|
await this.ocean.keeper.dispenser.requestTokens(amount, this.id)
|
2019-01-24 12:57:03 +01:00
|
|
|
} catch (e) {
|
2019-03-21 02:56:58 +01:00
|
|
|
this.logger.error(e)
|
2019-06-20 00:20:09 +02:00
|
|
|
throw new Error('Error requesting tokens')
|
2019-01-24 12:57:03 +01:00
|
|
|
}
|
2018-10-18 13:06:52 +02:00
|
|
|
return amount
|
2018-10-05 12:34:31 +02:00
|
|
|
}
|
|
|
|
}
|