From 1b2522e679ae5c220368d69caae6548c0a573323 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Tue, 14 Jul 2020 02:33:54 -0700 Subject: [PATCH] moved everything to wei --- src/datatokens/Datatokens.ts | 60 +++++++++++++++++++++++- src/ddo/interfaces/MetadataMain.ts | 7 --- src/ddo/interfaces/Service.ts | 2 +- src/ocean/Assets.ts | 10 +++- src/ocean/Compute.ts | 10 ++++ test/integration/ComputeFlow.test.ts | 8 ++-- test/integration/Marketplaceflow.test.ts | 6 +-- 7 files changed, 86 insertions(+), 17 deletions(-) diff --git a/src/datatokens/Datatokens.ts b/src/datatokens/Datatokens.ts index 3f9551ed..7ac01600 100644 --- a/src/datatokens/Datatokens.ts +++ b/src/datatokens/Datatokens.ts @@ -129,7 +129,7 @@ export class DataTokens { } /** - * Transfer from Account to Address + * Transfer as number from Account to Address * @param {String} dataTokenAddress * @param {String} toAddress * @param {Number} amount Number of datatokens, as number. Will be converted to wei @@ -141,6 +141,23 @@ export class DataTokens { toAddress: string, amount: number, account: Account + ): Promise { + return this.transferToken(dataTokenAddress, toAddress, amount, account) + } + + /** + * Transfer as number from Account to Address + * @param {String} dataTokenAddress + * @param {String} toAddress + * @param {Number} amount Number of datatokens, as number. Will be converted to wei + * @param {Account} account + * @return {Promise} transactionId + */ + public async transferToken( + dataTokenAddress: string, + toAddress: string, + amount: number, + account: Account ): Promise { const datatoken = new this.web3.eth.Contract( this.datatokensABI, @@ -153,6 +170,31 @@ export class DataTokens { return trxReceipt } + /** + * Transfer in wei from Account to Address + * @param {String} dataTokenAddress + * @param {String} toAddress + * @param {Number} amount Number of datatokens, as number. Expressed as wei + * @param {Account} account + * @return {Promise} transactionId + */ + public async transferWei( + dataTokenAddress: string, + toAddress: string, + amount: string, + account: Account + ): Promise { + const datatoken = new this.web3.eth.Contract( + this.datatokensABI, + dataTokenAddress, + { from: account } + ) + const trxReceipt = await datatoken.methods + .transfer(toAddress, String(amount)) + .send() + return trxReceipt + } + /** * Transfer from Address to Account (needs an Approve operation before) * @param {String} dataTokenAddress @@ -272,4 +314,20 @@ export class DataTokens { const trxReceipt = await datatoken.methods.cap().call() return this.web3.utils.fromWei(trxReceipt) } + + /** Convert from number to wei + * @param {Number} amount + * @return {Promise} string + */ + public toWei(amount) { + return this.web3.utils.toWei(String(amount)) + } + + /** Convert from wei to number + * @param {String} amount + * @return {Promise} string + */ + public fromWei(amount) { + return this.web3.utils.fromWei(amount) + } } diff --git a/src/ddo/interfaces/MetadataMain.ts b/src/ddo/interfaces/MetadataMain.ts index c567dacc..6e420846 100644 --- a/src/ddo/interfaces/MetadataMain.ts +++ b/src/ddo/interfaces/MetadataMain.ts @@ -52,13 +52,6 @@ export interface MetadataMain { */ license: string - /** - * Price of the asset in vodka (attoOCEAN). It must be an integer encoded as a string. - * @type {string} - * @example "1000000000000000000" - */ - price: string - /** * Array of File objects including the encrypted file urls and some additional information. * @type {File[]} diff --git a/src/ddo/interfaces/Service.ts b/src/ddo/interfaces/Service.ts index 6e69c86b..a7ff2089 100644 --- a/src/ddo/interfaces/Service.ts +++ b/src/ddo/interfaces/Service.ts @@ -19,7 +19,7 @@ export interface ServiceAccessAttributes extends ServiceCommonAttributes { creator: string name: string datePublished: string - cost: number + cost: string timeout: number } } diff --git a/src/ocean/Assets.ts b/src/ocean/Assets.ts index daaa29fe..9a105338 100644 --- a/src/ocean/Assets.ts +++ b/src/ocean/Assets.ts @@ -361,9 +361,17 @@ export class Assets extends Instantiable { return service } + /** + * Creates an access service + * @param {Account} creator + * @param {String} cost number of datatokens needed for this service, expressed in wei + * @param {String} datePublished + * @param {Number} timeout + * @return {Promise} service + */ public async createAccessServiceAttributes( creator: Account, - dtCost: number, + dtCost: string, datePublished: string, timeout: number = 0 ): Promise { diff --git a/src/ocean/Compute.ts b/src/ocean/Compute.ts index d3836477..3302321e 100644 --- a/src/ocean/Compute.ts +++ b/src/ocean/Compute.ts @@ -233,6 +233,16 @@ export class Compute extends Instantiable { } } + /** + * Creates a compute service + * @param {Account} consumerAccount + * @param {String} cost number of datatokens needed for this service, expressed in wei + * @param {String} datePublished + * @param {Object} providerAttributes + * @param {Object} computePrivacy + * @param {Number} timeout + * @return {Promise} service + */ public createComputeService( consumerAccount: Account, cost: string, diff --git a/test/integration/ComputeFlow.test.ts b/test/integration/ComputeFlow.test.ts index f48b5707..3b969055 100644 --- a/test/integration/ComputeFlow.test.ts +++ b/test/integration/ComputeFlow.test.ts @@ -114,7 +114,7 @@ describe('Marketplace flow', () => { }) it('Alice publishes dataset with a compute service that allows Raw Algo', async () => { - price = 2 // in datatoken + price = datatoken.toWei(2) // in datatoken cluster = ocean.compute.createClusterAttributes( 'Kubernetes', 'http://10.0.0.17/xxx' @@ -281,7 +281,7 @@ describe('Marketplace flow', () => { ) assert(order != null) const computeOrder = JSON.parse(order) - const tx = await datatoken.transfer( + const tx = await datatoken.transferWei( computeOrder['dataToken'], computeOrder['to'], computeOrder['numTokens'], @@ -349,7 +349,7 @@ describe('Marketplace flow', () => { bob.getId() ) const algoOrder = JSON.parse(orderalgo) - const algoTx = await datatoken.transfer( + const algoTx = await datatoken.transferWei( algoOrder['dataToken'], algoOrder['to'], algoOrder['numTokens'], @@ -364,7 +364,7 @@ describe('Marketplace flow', () => { ) assert(order != null) const computeOrder = JSON.parse(order) - const tx = await datatoken.transfer( + const tx = await datatoken.transferWei( computeOrder['dataToken'], computeOrder['to'], computeOrder['numTokens'], diff --git a/test/integration/Marketplaceflow.test.ts b/test/integration/Marketplaceflow.test.ts index 3ff1e2d6..db16ddc9 100644 --- a/test/integration/Marketplaceflow.test.ts +++ b/test/integration/Marketplaceflow.test.ts @@ -84,7 +84,7 @@ describe('Marketplace flow', () => { }) it('Alice publishes a dataset', async () => { - price = 10 // in datatoken + price = datatoken.toWei(10) // in datatoken const publishedDate = new Date(Date.now()).toISOString().split('.')[0] + 'Z' const timeout = 0 service1 = await ocean.assets.createAccessServiceAttributes( @@ -146,7 +146,7 @@ describe('Marketplace flow', () => { it('Marketplace posts asset for sale', async () => { accessService = await ocean.assets.getServiceByType(ddo.id, 'access') price = 20 - assert(accessService.attributes.main.cost * price === 200) + assert(accessService.attributes.main.cost * price === datatoken.toWei(200)) }) it('Bob gets datatokens', async () => { @@ -164,7 +164,7 @@ describe('Marketplace flow', () => { .order(ddo.id, accessService.type, bob.getId()) .then(async (res: string) => { res = JSON.parse(res) - return await datatoken.transfer( + return await datatoken.transferWei( res['dataToken'], res['to'], res['numTokens'],