mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
added Ocean.tokens module
This commit is contained in:
parent
057714988a
commit
fce3a09285
@ -3,6 +3,7 @@
|
||||
"version": "0.3.0",
|
||||
"description": "JavaScript client library for Ocean Protocol",
|
||||
"main": "./dist/node/squid.js",
|
||||
"typings": "./dist/node/squid.d.ts",
|
||||
"browser": "./dist/browser/squid.cjs2.min.js",
|
||||
"scripts": {
|
||||
"test": "mocha",
|
||||
|
@ -18,4 +18,8 @@ export default class OceanToken extends ContractBase {
|
||||
return this.call("balanceOf", [address])
|
||||
.then((balance: string) => new BigNumber(balance).toNumber())
|
||||
}
|
||||
|
||||
public async transfer(to: string, amount: number, from: string): Promise<boolean> {
|
||||
return this.send("transfer", from, [to, amount])
|
||||
}
|
||||
}
|
||||
|
54
src/ocean/OceanTokens.ts
Normal file
54
src/ocean/OceanTokens.ts
Normal file
@ -0,0 +1,54 @@
|
||||
import Keeper from "../keeper/Keeper"
|
||||
import Account from "./Account"
|
||||
|
||||
/**
|
||||
* Tokens submodule of Ocean Protocol.
|
||||
*/
|
||||
export default class OceanTokens {
|
||||
|
||||
/**
|
||||
* Returns the instance of OceanTokens.
|
||||
* @return {Promise<OceanTokens>}
|
||||
*/
|
||||
public static async getInstance(): Promise<OceanTokens> {
|
||||
if (!OceanTokens.instance) {
|
||||
OceanTokens.instance = new OceanTokens()
|
||||
}
|
||||
|
||||
return OceanTokens.instance
|
||||
}
|
||||
|
||||
/**
|
||||
* OceanTokens instance.
|
||||
* @type {OceanTokens}
|
||||
*/
|
||||
private static instance: OceanTokens = null
|
||||
|
||||
/**
|
||||
* 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> {
|
||||
return (await Keeper.getInstance())
|
||||
.token
|
||||
.transfer(to, amount, from.getId())
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
} catch(e) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user