2018-10-05 12:34:31 +02:00
|
|
|
import BigNumber from "bignumber.js"
|
2018-11-12 10:06:24 +01:00
|
|
|
import * as EthJsUtils from "ethereumjs-util"
|
2018-10-16 14:56:18 +02:00
|
|
|
import Keeper from "../keeper/Keeper"
|
|
|
|
import Web3Provider from "../keeper/Web3Provider"
|
|
|
|
import Balance from "../models/Balance"
|
2019-01-24 12:57:03 +01:00
|
|
|
import Logger from "../utils/Logger"
|
2018-10-09 10:55:53 +02:00
|
|
|
import OceanBase from "./OceanBase"
|
2018-10-05 12:34:31 +02:00
|
|
|
|
2019-01-09 16:17:23 +01:00
|
|
|
/**
|
|
|
|
* Account information.
|
|
|
|
*/
|
2018-10-09 10:55:53 +02:00
|
|
|
export default class Account extends OceanBase {
|
2019-02-04 17:45:49 +01:00
|
|
|
private password?: string
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-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> {
|
|
|
|
return (await Keeper.getInstance()).token.balanceOf(this.id)
|
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> {
|
2018-10-05 12:34:31 +02:00
|
|
|
// Logger.log("getting balance for", account);
|
2018-11-27 12:27:15 +01:00
|
|
|
return Web3Provider
|
|
|
|
.getWeb3()
|
|
|
|
.eth
|
2018-10-16 14:56:18 +02:00
|
|
|
.getBalance(this.id, "latest")
|
|
|
|
.then((balance: string): number => {
|
2018-10-05 12:34:31 +02:00
|
|
|
// Logger.log("balance", balance);
|
|
|
|
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(),
|
|
|
|
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>}
|
|
|
|
*/
|
2018-10-18 13:06:52 +02:00
|
|
|
public async requestTokens(amount: number): Promise<number> {
|
2019-01-24 12:57:03 +01:00
|
|
|
try {
|
|
|
|
await (await Keeper.getInstance())
|
2019-01-28 15:41:19 +01:00
|
|
|
.dispenser
|
2019-01-24 12:57:03 +01:00
|
|
|
.requestTokens(amount, this.id)
|
|
|
|
} catch (e) {
|
|
|
|
Logger.error(e)
|
|
|
|
throw new Error("Error requesting tokens")
|
|
|
|
|
|
|
|
}
|
2018-10-18 13:06:52 +02:00
|
|
|
return amount
|
2018-10-05 12:34:31 +02:00
|
|
|
}
|
2018-11-12 10:06:24 +01:00
|
|
|
|
2019-01-09 16:17:23 +01:00
|
|
|
/**
|
|
|
|
* Returns the account public key.
|
|
|
|
* @return {Promise<string>}
|
|
|
|
*/
|
2018-11-12 10:06:24 +01:00
|
|
|
public async getPublicKey(): Promise<string> {
|
|
|
|
|
|
|
|
const web3 = Web3Provider.getWeb3()
|
|
|
|
|
|
|
|
const msg = web3.utils.sha3(this.getId())
|
|
|
|
const sig = await web3.eth.sign(msg, this.getId())
|
|
|
|
const {v, r, s} = EthJsUtils.fromRpcSig(sig)
|
|
|
|
|
|
|
|
return EthJsUtils.ecrecover(EthJsUtils.toBuffer(msg), v, r, s).toString("hex")
|
|
|
|
}
|
2018-10-05 12:34:31 +02:00
|
|
|
}
|