From fce3a09285d202726ec7f7394b63576ffa647aca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Guti=C3=A9rrez?= Date: Fri, 15 Feb 2019 14:44:48 +0100 Subject: [PATCH] added Ocean.tokens module --- package.json | 1 + src/keeper/contracts/Token.ts | 4 +++ src/ocean/OceanTokens.ts | 54 +++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 src/ocean/OceanTokens.ts diff --git a/package.json b/package.json index 93bc2ae..b959f00 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/keeper/contracts/Token.ts b/src/keeper/contracts/Token.ts index 1b04255..f746c74 100644 --- a/src/keeper/contracts/Token.ts +++ b/src/keeper/contracts/Token.ts @@ -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 { + return this.send("transfer", from, [to, amount]) + } } diff --git a/src/ocean/OceanTokens.ts b/src/ocean/OceanTokens.ts new file mode 100644 index 0000000..8c23435 --- /dev/null +++ b/src/ocean/OceanTokens.ts @@ -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} + */ + public static async getInstance(): Promise { + 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} Success, + */ + public async transfer(to: string, amount: number, from: Account): Promise { + 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} Success. + */ + public async request(account: Account, amount: number): Promise { + try { + await account.requestTokens(amount) + return true + } catch(e) { + return false + } + } +}