2019-02-15 14:44:48 +01:00
|
|
|
import Account from "./Account"
|
2019-03-21 02:56:58 +01:00
|
|
|
import { Instantiable, InstantiableConfig } from "../Instantiable.abstract"
|
2019-02-15 14:44:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tokens submodule of Ocean Protocol.
|
|
|
|
*/
|
2019-03-21 02:56:58 +01:00
|
|
|
export class OceanTokens extends Instantiable {
|
2019-02-15 14:44:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the instance of OceanTokens.
|
|
|
|
* @return {Promise<OceanTokens>}
|
|
|
|
*/
|
2019-03-21 02:56:58 +01:00
|
|
|
public static async getInstance(config: InstantiableConfig): Promise<OceanTokens> {
|
|
|
|
const instance = new OceanTokens()
|
|
|
|
instance.setInstanceConfig(config)
|
2019-02-15 14:44:48 +01:00
|
|
|
|
2019-03-21 02:56:58 +01:00
|
|
|
return instance
|
2019-02-15 14:44:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transfer a number of tokens to the mentioned account.
|
|
|
|
* @param {string} to Address that receives the account.
|
|
|
|
* @param {number} amount Tokens to transfer.
|
|
|
|
* @param {Account} from Sender account address.
|
|
|
|
* @return {Promise<boolean>} Success,
|
|
|
|
*/
|
|
|
|
public async transfer(to: string, amount: number, from: Account): Promise<boolean> {
|
2019-03-21 02:56:58 +01:00
|
|
|
this.ocean.keeper
|
2019-02-15 14:44:48 +01:00
|
|
|
.token
|
|
|
|
.transfer(to, amount, from.getId())
|
2019-03-01 13:24:04 +01:00
|
|
|
return true
|
2019-02-15 14:44:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request tokens for a account.
|
|
|
|
* @param {Account} account Account instance.
|
|
|
|
* @param {number} amount Token amount.
|
|
|
|
* @return {Promise<boolean>} Success.
|
|
|
|
*/
|
|
|
|
public async request(account: Account, amount: number): Promise<boolean> {
|
|
|
|
try {
|
|
|
|
await account.requestTokens(amount)
|
|
|
|
return true
|
2019-02-21 18:14:07 +01:00
|
|
|
} catch (e) {
|
2019-02-15 14:44:48 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|