1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00
squid-js/src/ocean/Account.ts

41 lines
1.2 KiB
TypeScript
Raw Normal View History

2018-10-05 12:34:31 +02:00
import BigNumber from "bignumber.js"
2018-10-16 14:56:18 +02:00
import Keeper from "../keeper/Keeper"
import Web3Provider from "../keeper/Web3Provider"
import Balance from "../models/Balance"
2018-10-09 10:55:53 +02:00
import OceanBase from "./OceanBase"
2018-10-05 12:34:31 +02:00
2018-10-09 10:55:53 +02:00
export default class Account extends OceanBase {
2018-10-16 14:56:18 +02:00
private balance: Balance
2018-10-05 12:34:31 +02:00
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
}
2018-10-16 14:56:18 +02:00
public async getEthBalance(): Promise<number> {
2018-10-05 12:34:31 +02:00
// Logger.log("getting balance for", account);
2018-10-16 14:56:18 +02:00
return Web3Provider.getWeb3().eth
.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()
})
}
2018-10-16 14:56:18 +02:00
public async getBalance(): Promise<Balance> {
2018-10-05 12:34:31 +02:00
2018-10-16 14:56:18 +02:00
if (!this.balance) {
this.balance = {
eth: await this.getEthBalance(),
ocn: await this.getOceanBalance(),
} as Balance
}
return this.balance
2018-10-05 12:34:31 +02:00
}
// Transactions with gas cost
2018-10-16 14:56:18 +02:00
public async requestTokens(amount: number): Promise<boolean> {
return (await Keeper.getInstance()).market.requestTokens(amount, this.id)
2018-10-05 12:34:31 +02:00
}
}