mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
Adding estimate gas method and fixing tests
This commit is contained in:
parent
4eefc083e2
commit
8c9e46a8dd
@ -1364,6 +1364,42 @@ export class Datatoken {
|
||||
return this.web3.utils.fromWei(balance)
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev estGasSetPublishingMarketFee
|
||||
* Estimating gas for publishMarketFeeAddress method
|
||||
* @param {string} datatokenAddress Datatoken adress
|
||||
* @param {string} publishMarketFeeAddress new publish Market Fee Address
|
||||
* @param {string} publishMarketFeeToken new publish Market Fee Token
|
||||
* @param {string} publishMarketFeeAmount new fee amount
|
||||
* @param {String} address user adress
|
||||
*/
|
||||
public async estGasSetPublishingMarketFee(
|
||||
datatokenAddress: string,
|
||||
publishMarketFeeAddress: string,
|
||||
publishMarketFeeToken: string,
|
||||
publishMarketFeeAmount: string,
|
||||
address: string
|
||||
): Promise<number> {
|
||||
// Estimate gas cost for publishMarketFeeAddress method
|
||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
||||
const dtContract = new this.web3.eth.Contract(this.datatokensAbi, datatokenAddress, {
|
||||
from: address
|
||||
})
|
||||
let estGas
|
||||
try {
|
||||
estGas = await dtContract.methods
|
||||
.setPublishingMarketFee(
|
||||
publishMarketFeeAddress,
|
||||
publishMarketFeeToken,
|
||||
publishMarketFeeAmount
|
||||
)
|
||||
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
||||
} catch (error) {
|
||||
estGas = gasLimitDefault
|
||||
}
|
||||
return estGas
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev setPublishingMarketFee
|
||||
* Only publishMarketFeeAddress can call it
|
||||
@ -1384,13 +1420,24 @@ export class Datatoken {
|
||||
const dtContract = new this.web3.eth.Contract(this.datatokensAbi, datatokenAddress, {
|
||||
from: address
|
||||
})
|
||||
const estGas = await this.estGasSetPublishingMarketFee(
|
||||
datatokenAddress,
|
||||
publishMarketFeeAddress,
|
||||
publishMarketFeeToken,
|
||||
publishMarketFeeAmount,
|
||||
address
|
||||
)
|
||||
await dtContract.methods
|
||||
.setPublishingMarketFee(
|
||||
publishMarketFeeAddress,
|
||||
publishMarketFeeToken,
|
||||
publishMarketFeeAmount
|
||||
)
|
||||
.call()
|
||||
.send({
|
||||
from: address,
|
||||
gas: estGas + 1,
|
||||
gasPrice: await getFairGasPrice(this.web3, this.config)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1410,13 +1457,11 @@ export class Datatoken {
|
||||
})
|
||||
|
||||
const publishingMarketFee = await dtContract.methods.getPublishingMarketFee().call()
|
||||
console.log('publishingMarketFee', publishingMarketFee)
|
||||
const returnValues = {
|
||||
publishMarketFeeAddress: publishingMarketFee[0],
|
||||
publishMarketFeeToken: publishingMarketFee[1],
|
||||
publishMarketFeeAmount: publishingMarketFee[2]
|
||||
}
|
||||
console.log('returnValues', returnValues)
|
||||
return returnValues
|
||||
}
|
||||
}
|
||||
|
@ -556,23 +556,28 @@ describe('Datatoken', () => {
|
||||
it('#setPublishingMarketFee - User should not be able to set the Publishing Market Fee', async () => {
|
||||
const originalPublishingMarketFee = await datatoken.getPublishingMarketFee(
|
||||
datatokenAddress,
|
||||
user3
|
||||
user1
|
||||
)
|
||||
try {
|
||||
await datatoken.setPublishingMarketFee(
|
||||
datatokenAddress,
|
||||
user3,
|
||||
user1,
|
||||
contracts.daiAddress,
|
||||
web3.utils.toWei('10'),
|
||||
user3
|
||||
user1
|
||||
)
|
||||
} catch (e) {
|
||||
assert(e.message === 'ERC20Template: not publishMarketFeeAddress')
|
||||
console.log('Message:', e.message)
|
||||
assert(
|
||||
e.message ===
|
||||
'Returned error: VM Exception while processing transaction: revert ERC20Template: not publishMarketFeeAddress'
|
||||
)
|
||||
}
|
||||
const newPublishingMarketFee = await datatoken.getPublishingMarketFee(
|
||||
datatokenAddress,
|
||||
user3
|
||||
)
|
||||
|
||||
assert(
|
||||
newPublishingMarketFee.publishMarketFeeAddress ===
|
||||
originalPublishingMarketFee.publishMarketFeeAddress
|
||||
@ -587,12 +592,10 @@ describe('Datatoken', () => {
|
||||
)
|
||||
})
|
||||
it('#setPublishingMarketFee - Marketplace fee address should be able to set the Publishing Market Fee', async () => {
|
||||
console.log('users: ', user1, user2, user3, nftOwner, erc20DeployerUser)
|
||||
const originalPublishingMarketFee = await datatoken.getPublishingMarketFee(
|
||||
datatokenAddress,
|
||||
user2
|
||||
)
|
||||
console.log('originalPublishingMarketFee', originalPublishingMarketFee)
|
||||
try {
|
||||
await datatoken.setPublishingMarketFee(
|
||||
datatokenAddress,
|
||||
@ -602,18 +605,13 @@ describe('Datatoken', () => {
|
||||
user2
|
||||
)
|
||||
} catch (e) {
|
||||
console.log('Error', e)
|
||||
console.log('Error:', e)
|
||||
}
|
||||
const newPublishingMarketFee = await datatoken.getPublishingMarketFee(
|
||||
datatokenAddress,
|
||||
user2
|
||||
)
|
||||
|
||||
console.log('newPublishingMarketFee', newPublishingMarketFee)
|
||||
console.log(newPublishingMarketFee.publishMarketFeeAddress, nftOwner)
|
||||
console.log(newPublishingMarketFee.publishMarketFeeToken, contracts.daiAddress)
|
||||
console.log(newPublishingMarketFee.publishMarketFeeAmount, web3.utils.toWei('10'))
|
||||
|
||||
assert(newPublishingMarketFee !== originalPublishingMarketFee)
|
||||
assert(newPublishingMarketFee.publishMarketFeeAddress === user2)
|
||||
assert(newPublishingMarketFee.publishMarketFeeAmount === web3.utils.toWei('10'))
|
||||
|
Loading…
x
Reference in New Issue
Block a user