Skip to content

Commit

Permalink
Merge pull request #147 from oceanprotocol/bug/fix_dt_decimals
Browse files Browse the repository at this point in the history
DataTokens - use wei in the library
  • Loading branch information
Ahmed Ali authored Jul 14, 2020
2 parents 088f267 + dc5b252 commit ed18f81
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 76 deletions.
172 changes: 114 additions & 58 deletions src/datatokens/Datatokens.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import Account from '../ocean/Account'

const defaultFactoryABI = require('@oceanprotocol/contracts/artifacts/development/Factory.json')
const defaultDatatokensABI = require('@oceanprotocol/contracts/artifacts/development/DataTokenTemplate.json')

Expand Down Expand Up @@ -35,13 +33,13 @@ export class DataTokens {
/**
* Create new datatoken
* @param {String} metaDataStoreURI
* @param {Account} account
* @param {String} address
* @return {Promise<string>} datatoken address
*/
public async create(metaDataStoreURI: string, account: Account): Promise<string> {
public async create(metaDataStoreURI: string, address: string): Promise<string> {
// Create factory contract object
const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, {
from: account
from: address
})
const estGas = await factory.methods
.createToken(metaDataStoreURI)
Expand All @@ -51,7 +49,7 @@ export class DataTokens {
})
// Invoke createToken function of the contract
const trxReceipt = await factory.methods.createToken(metaDataStoreURI).send({
from: account,
from: address,
gas: estGas + 1,
gasPrice: '3000000000'
})
Expand All @@ -69,129 +67,171 @@ export class DataTokens {
* Approve
* @param {String} dataTokenAddress
* @param {String} toAddress
* @param {Number} amount
* @param {Account} account
* @param {Number} amount Number of datatokens, as number. Will be converted to wei
* @param {String} address
* @return {Promise<string>} transactionId
*/
public async approve(
dataTokenAddress: string,
spender: string,
amount: number,
account: Account
address: string
): Promise<string> {
const datatoken = new this.web3.eth.Contract(
this.datatokensABI,
dataTokenAddress,
{ from: account }
{ from: address }
)
const trxReceipt = await datatoken.methods.approve(spender, amount).send()
const trxReceipt = await datatoken.methods
.approve(spender, this.web3.utils.toWei(String(amount)))
.send()
return trxReceipt
}

/**
* Mint
* @param {String} dataTokenAddress
* @param {Account} account
* @param {Number} amount
* @param {String} address
* @param {Number} amount Number of datatokens, as number. Will be converted to wei
* @param {String} toAddress - only if toAddress is different from the minter
* @return {Promise<string>} transactionId
*/
public async mint(
dataTokenAddress: string,
account: Account,
address: string,
amount: number,
toAddress?: string
): Promise<string> {
const address = toAddress || account
const destAddress = toAddress || address
const datatoken = new this.web3.eth.Contract(
this.datatokensABI,
dataTokenAddress,
{ from: account }
{ from: address }
)
const estGas = await datatoken.methods
.mint(address, amount)
.mint(destAddress, this.web3.utils.toWei(String(amount)))
.estimateGas(function (err, estGas) {
if (err) console.log('Datatokens: ' + err)
return estGas
})

const trxReceipt = await datatoken.methods.mint(address, amount).send({
from: account,
gas: estGas + 1,
gasPrice: '3000000000'
})
const trxReceipt = await datatoken.methods
.mint(destAddress, this.web3.utils.toWei(String(amount)))
.send({
from: address,
gas: estGas + 1,
gasPrice: '3000000000'
})

return trxReceipt
}

/**
* Transfer from Account to Address
* Transfer as number from address to toAddress
* @param {String} dataTokenAddress
* @param {String} toAddress
* @param {Number} amount
* @param {Account} account
* @param {Number} amount Number of datatokens, as number. Will be converted to wei
* @param {String} address
* @return {Promise<string>} transactionId
*/
public async transfer(
dataTokenAddress: string,
toAddress: string,
amount: number,
account: Account
address: string
): Promise<string> {
return this.transferToken(dataTokenAddress, toAddress, amount, address)
}

/**
* Transfer as number from address to toAddress
* @param {String} dataTokenAddress
* @param {String} toAddress
* @param {Number} amount Number of datatokens, as number. Will be converted to wei
* @param {String} address
* @return {Promise<string>} transactionId
*/
public async transferToken(
dataTokenAddress: string,
toAddress: string,
amount: number,
address: string
): Promise<string> {
const weiAmount = this.web3.utils.toWei(String(amount))
return this.transferWei(dataTokenAddress, toAddress, weiAmount, address)
}

/**
* Transfer in wei from address to toAddress
* @param {String} dataTokenAddress
* @param {String} toAddress
* @param {Number} amount Number of datatokens, as number. Expressed as wei
* @param {String} address
* @return {Promise<string>} transactionId
*/
public async transferWei(
dataTokenAddress: string,
toAddress: string,
amount: string,
address: string
): Promise<string> {
const datatoken = new this.web3.eth.Contract(
this.datatokensABI,
dataTokenAddress,
{ from: account }
{ from: address }
)
const trxReceipt = await datatoken.methods.transfer(toAddress, amount).send()
const trxReceipt = await datatoken.methods
.transfer(toAddress, String(amount))
.send()
return trxReceipt
}

/**
* Transfer from Address to Account (needs an Approve operation before)
* Transfer from fromAddress to address (needs an Approve operation before)
* @param {String} dataTokenAddress
* @param {String} fromAddress
* @param {Number} amount
* @param {Account} account
* @param {Number} amount Number of datatokens, as number. Will be converted to wei
* @param {String} address
* @return {Promise<string>} transactionId
*/
public async transferFrom(
dataTokenAddress: string,
fromAddress: string,
amount: number,
account: string
address: string
): Promise<string> {
const datatoken = new this.web3.eth.Contract(
this.datatokensABI,
dataTokenAddress,
{ from: account }
{ from: address }
)
const trxReceipt = await datatoken.methods
.transferFrom(fromAddress, account, amount)
.transferFrom(fromAddress, address, this.web3.utils.toWei(String(amount)))
.send()
return trxReceipt
}

/**
* Get Account Balance for datatoken
* Get Address Balance for datatoken
* @param {String} dataTokenAddress
* @param {Account} account
* @return {Promise<number>} balance
* @param {String} address
* @return {Promise<number>} balance Number of datatokens, as number. Will be converted from wei
*/
public async balance(dataTokenAddress: string, account: Account): Promise<number> {
public async balance(dataTokenAddress: string, address: string): Promise<number> {
const datatoken = new this.web3.eth.Contract(
this.datatokensABI,
dataTokenAddress,
{ from: account }
{ from: address }
)
const trxReceipt = await datatoken.methods.balanceOf(account).call()
return trxReceipt
const balance = await datatoken.methods.balanceOf(address).call()
return this.web3.utils.fromWei(balance)
}

/**
*
* @param dataTokenAddress
* @param account
* Get Alloance
* @param {String } dataTokenAddress
* @param {String} owner
* @param {String} spender
*/
public async allowance(
dataTokenAddress: string,
Expand All @@ -204,66 +244,82 @@ export class DataTokens {
{ from: spender }
)
const trxReceipt = await datatoken.methods.allowance(owner, spender).call()
return trxReceipt
return this.web3.utils.fromWei(trxReceipt)
}

/** Get Blob
* @param {String} dataTokenAddress
* @param {Account} account
* @param {String} address
* @return {Promise<string>} string
*/
public async getBlob(dataTokenAddress: string, account: Account): Promise<string> {
public async getBlob(dataTokenAddress: string, address: string): Promise<string> {
const datatoken = new this.web3.eth.Contract(
this.datatokensABI,
dataTokenAddress,
{ from: account.getId() }
{ from: address }
)
const trxReceipt = await datatoken.methods.blob().call()
return trxReceipt
}

/** Get Name
* @param {String} dataTokenAddress
* @param {Account} account
* @param {String} address
* @return {Promise<string>} string
*/
public async getName(dataTokenAddress: string, account: Account): Promise<string> {
public async getName(dataTokenAddress: string, address: string): Promise<string> {
const datatoken = new this.web3.eth.Contract(
this.datatokensABI,
dataTokenAddress,
{ from: account.getId() }
{ from: address }
)
const trxReceipt = await datatoken.methods.name().call()
return trxReceipt
}

/** Get Symbol
* @param {String} dataTokenAddress
* @param {Account} account
* @param {String} address
* @return {Promise<string>} string
*/
public async getSymbol(dataTokenAddress: string, account: Account): Promise<string> {
public async getSymbol(dataTokenAddress: string, address: string): Promise<string> {
const datatoken = new this.web3.eth.Contract(
this.datatokensABI,
dataTokenAddress,
{ from: account.getId() }
{ from: address }
)
const trxReceipt = await datatoken.methods.symbol().call()
return trxReceipt
}

/** Get Cap
* @param {String} dataTokenAddress
* @param {Account} account
* @param {String} address
* @return {Promise<string>} string
*/
public async getCap(dataTokenAddress: string, account: Account): Promise<string> {
public async getCap(dataTokenAddress: string, address: string): Promise<string> {
const datatoken = new this.web3.eth.Contract(
this.datatokensABI,
dataTokenAddress,
{ from: account.getId() }
{ from: address }
)
const trxReceipt = await datatoken.methods.cap().call()
return trxReceipt
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)
}
}
7 changes: 0 additions & 7 deletions src/ddo/interfaces/MetadataMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]}
Expand Down
2 changes: 1 addition & 1 deletion src/ddo/interfaces/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface ServiceAccessAttributes extends ServiceCommonAttributes {
creator: string
name: string
datePublished: string
cost: number
cost: string
timeout: number
}
}
Expand Down
Loading

0 comments on commit ed18f81

Please sign in to comment.