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

added unit tests for Datatoken class

This commit is contained in:
Bogdan Fazakas 2021-10-25 13:18:54 +03:00
parent 5e31a7ebcf
commit 11c5c22b8b
2 changed files with 76 additions and 2 deletions

View File

@ -271,6 +271,55 @@ export class Datatoken {
return trxReceipt
}
/**
* Set a new fee Collector, if feeCollector is address(0), feeCollector is NFT Owner
* only NFT owner can call
* @param dtAddress datatoken address where we want to clean permissions address
* @param address Caller address
* @param feeCollector User to be set as new fee collector
* @return {Promise<TransactionReceipt>} trxReceipt
*/
public async setFeeCollector(
dtAddress: string,
address: string,
feeCollector: string
): Promise<TransactionReceipt> {
const dtContract = new this.web3.eth.Contract(this.datatokensABI, dtAddress)
if ((await this.getDTPermissions(dtAddress, address)).feeManager !== true) {
throw new Error(`Caller is not Fee Manager`)
}
// Estimate gas for setFeeCollector method
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await dtContract.methods
.setFeeCollector(feeCollector)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
// Call setFeeCollector method of the contract
const trxReceipt = await dtContract.methods.setFeeCollector(feeCollector).send({
from: address,
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3)
})
return trxReceipt
}
/** Get Fee Collector
* @param dtAddress datatoken address
* @return {Promise<string>}
*/
public async getFeeCollector(dtAddress: string): Promise<string> {
const dtContract = new this.web3.eth.Contract(this.datatokensABI, dtAddress)
const feeCollector = await dtContract.methods.getFeeCollector().call()
return feeCollector
}
/**
* Transfer as number from address to toAddress
* @param {String} dtAddress Datatoken address
@ -482,8 +531,22 @@ export class Datatoken {
* @return {Promise<string>}
*/
public async getCap(dtAddress: string): Promise<string> {
const datatoken = new this.web3.eth.Contract(this.datatokensABI, dtAddress)
const cap = await datatoken.methods.cap().call()
const dtContract = new this.web3.eth.Contract(this.datatokensABI, dtAddress)
const cap = await dtContract.methods.cap().call()
return this.web3.utils.fromWei(cap)
}
/**
* Get Address Balance for datatoken
* @param {String} dtAddress Datatoken adress
* @param {String} address user adress
* @return {Promise<String>} balance Number of datatokens. Will be converted from wei
*/
public async balance(dataTokenAddress: string, address: string): Promise<string> {
const dtContract = new this.web3.eth.Contract(this.datatokensABI, dataTokenAddress, {
from: address
})
const balance = await dtContract.methods.balanceOf(address).call()
return this.web3.utils.fromWei(balance)
}
}

View File

@ -528,4 +528,15 @@ export class NFTDatatoken {
const roles = await nftContract.methods._getPermissions(address).call()
return roles
}
/** Gets data at a given `key`
* @param {String} nftAddress erc721 contract adress
* @param {String} key the key which value to retrieve
* @return {Promise<string>} The data stored at the key
*/
public async getData(nftAddress: string, key: string): Promise<string> {
const nftContract = new this.web3.eth.Contract(this.nftDatatokenABI, nftAddress)
const data = await nftContract.methods.getData(key).call()
return data
}
}