mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import BigNumber from "bignumber.js"
|
|
import Keeper from "../keeper/Keeper"
|
|
import Web3Provider from "../keeper/Web3Provider"
|
|
import Balance from "../models/Balance"
|
|
import OceanBase from "./OceanBase"
|
|
|
|
export default class Account extends OceanBase {
|
|
private balance: Balance
|
|
|
|
public async getOceanBalance(): Promise<number> {
|
|
return (await Keeper.getInstance()).token.balanceOf(this.id)
|
|
}
|
|
|
|
public async getEtherBalance(): Promise<number> {
|
|
// Logger.log("getting balance for", account);
|
|
return Web3Provider.getWeb3().eth
|
|
.getBalance(this.id, "latest")
|
|
.then((balance: string): number => {
|
|
// Logger.log("balance", balance);
|
|
return new BigNumber(balance).toNumber()
|
|
})
|
|
}
|
|
|
|
public async getBalance(): Promise<Balance> {
|
|
|
|
if (!this.balance) {
|
|
this.balance = {
|
|
eth: await this.getEtherBalance(),
|
|
ocn: await this.getOceanBalance(),
|
|
} as Balance
|
|
}
|
|
|
|
return this.balance
|
|
}
|
|
|
|
// Transactions with gas cost
|
|
public async requestTokens(amount: number): Promise<number> {
|
|
await (await Keeper.getInstance()).market.requestTokens(amount, this.id)
|
|
return amount
|
|
}
|
|
}
|