1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

moved everything to wei

This commit is contained in:
alexcos20 2020-07-14 02:33:54 -07:00
parent 82178ee3dc
commit 1b2522e679
7 changed files with 86 additions and 17 deletions

View File

@ -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<string> {
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<string>} transactionId
*/
public async transferToken(
dataTokenAddress: string,
toAddress: string,
amount: number,
account: Account
): Promise<string> {
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<string>} transactionId
*/
public async transferWei(
dataTokenAddress: string,
toAddress: string,
amount: string,
account: Account
): Promise<string> {
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>} string
*/
public toWei(amount) {
return this.web3.utils.toWei(String(amount))
}
/** Convert from wei to number
* @param {String} amount
* @return {Promise<string>} string
*/
public fromWei(amount) {
return this.web3.utils.fromWei(amount)
}
}

View File

@ -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[]}

View File

@ -19,7 +19,7 @@ export interface ServiceAccessAttributes extends ServiceCommonAttributes {
creator: string
name: string
datePublished: string
cost: number
cost: string
timeout: number
}
}

View File

@ -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<string>} service
*/
public async createAccessServiceAttributes(
creator: Account,
dtCost: number,
dtCost: string,
datePublished: string,
timeout: number = 0
): Promise<ServiceAccess> {

View File

@ -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<string>} service
*/
public createComputeService(
consumerAccount: Account,
cost: string,

View File

@ -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'],

View File

@ -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'],