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

use estimateGas() function in Datatoken

This commit is contained in:
Miquel A. Cabot 2022-04-06 13:44:52 +02:00
parent 8d9964448a
commit f9aa47c4d2

View File

@ -10,7 +10,8 @@ import {
getFairGasPrice, getFairGasPrice,
setContractDefaults, setContractDefaults,
configHelperNetworks, configHelperNetworks,
getFreOrderParams getFreOrderParams,
estimateGas
} from '../utils' } from '../utils'
import { import {
ConsumeMarketFee, ConsumeMarketFee,
@ -95,17 +96,12 @@ export class Datatoken {
this.config this.config
) )
// Estimate gas cost for mint method return estimateGas(
const gasLimitDefault = this.GASLIMIT_DEFAULT address,
let estGas dtContract.methods.approve,
try { spender,
estGas = await dtContract.methods this.web3.utils.toWei(amount)
.approve(spender, this.web3.utils.toWei(amount)) )
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -127,12 +123,11 @@ export class Datatoken {
this.config this.config
) )
const estGas = await this.estGasApprove( const estGas = await estimateGas(
dtAddress,
spender,
amount,
address, address,
dtContract dtContract.methods.approve,
spender,
this.web3.utils.toWei(amount)
) )
// Call mint contract method // Call mint contract method
@ -169,17 +164,12 @@ export class Datatoken {
this.config this.config
) )
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(
let estGas address,
try { dtContract.methods.mint,
estGas = await dtContract.methods toAddress || address,
.mint(toAddress || address, this.web3.utils.toWei(amount)) this.web3.utils.toWei(amount)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) )
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -204,16 +194,13 @@ export class Datatoken {
this.config this.config
) )
const gasLimitDefault = this.GASLIMIT_DEFAULT
if (!fixedRateParams.allowedConsumer) if (!fixedRateParams.allowedConsumer)
fixedRateParams.allowedConsumer = '0x0000000000000000000000000000000000000000' fixedRateParams.allowedConsumer = '0x0000000000000000000000000000000000000000'
const withMint = fixedRateParams.withMint ? 1 : 0 const withMint = fixedRateParams.withMint ? 1 : 0
let estGas return estimateGas(
try { address,
estGas = await dtContract.methods dtContract.methods.createFixedRate,
.createFixedRate(
fixedRateParams.fixedRateAddress, fixedRateParams.fixedRateAddress,
[ [
fixedRateParams.baseTokenAddress, fixedRateParams.baseTokenAddress,
@ -229,12 +216,6 @@ export class Datatoken {
withMint withMint
] ]
) )
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -264,11 +245,23 @@ export class Datatoken {
// should check ERC20Deployer role using erc721 level .. // should check ERC20Deployer role using erc721 level ..
const estGas = await this.estGasCreateFixedRate( const estGas = await estimateGas(
dtAddress,
address, address,
fixedRateParams, dtContract.methods.createFixedRate,
dtContract fixedRateParams.fixedRateAddress,
[
fixedRateParams.baseTokenAddress,
fixedRateParams.owner,
fixedRateParams.marketFeeCollector,
fixedRateParams.allowedConsumer
],
[
fixedRateParams.baseTokenDecimals,
fixedRateParams.datatokenDecimals,
fixedRateParams.fixedRate,
fixedRateParams.marketFee,
withMint
]
) )
// Call createFixedRate contract method // Call createFixedRate contract method
@ -325,23 +318,15 @@ export class Datatoken {
if (!dispenserParams.withMint) dispenserParams.withMint = false if (!dispenserParams.withMint) dispenserParams.withMint = false
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(
let estGas address,
try { dtContract.methods.createDispenser,
estGas = await dtContract.methods
.createDispenser(
dispenserAddress, dispenserAddress,
dispenserParams.maxTokens, dispenserParams.maxTokens,
dispenserParams.maxBalance, dispenserParams.maxBalance,
dispenserParams.withMint, dispenserParams.withMint,
dispenserParams.allowedSwapper dispenserParams.allowedSwapper
) )
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -374,12 +359,14 @@ export class Datatoken {
// should check ERC20Deployer role using erc721 level .. // should check ERC20Deployer role using erc721 level ..
const estGas = await this.estGasCreateDispenser( const estGas = await estimateGas(
dtAddress,
address, address,
dtContract.methods.createDispenser,
dispenserAddress, dispenserAddress,
dispenserParams, dispenserParams.maxTokens,
dtContract dispenserParams.maxBalance,
dispenserParams.withMint,
dispenserParams.allowedSwapper
) )
// Call createFixedRate contract method // Call createFixedRate contract method
@ -424,12 +411,11 @@ export class Datatoken {
const capAvailble = await this.getCap(dtAddress) const capAvailble = await this.getCap(dtAddress)
if (new Decimal(capAvailble).gte(amount)) { if (new Decimal(capAvailble).gte(amount)) {
const estGas = await this.estGasMint( const estGas = await estimateGas(
dtAddress,
address, address,
amount, dtContract.methods.mint,
toAddress, toAddress || address,
dtContract this.web3.utils.toWei(amount)
) )
// Call mint contract method // Call mint contract method
@ -467,17 +453,7 @@ export class Datatoken {
this.config this.config
) )
// Estimate gas cost for addMinter method return estimateGas(address, dtContract.methods.addMinter, minter)
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await dtContract.methods
.addMinter(minter)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -502,7 +478,7 @@ export class Datatoken {
throw new Error(`Caller is not ERC20Deployer`) throw new Error(`Caller is not ERC20Deployer`)
} }
// Estimate gas cost for addMinter method // Estimate gas cost for addMinter method
const estGas = await this.estGasAddMinter(dtAddress, address, minter, dtContract) const estGas = await estimateGas(address, dtContract.methods.addMinter, minter)
// Call addMinter function of the contract // Call addMinter function of the contract
const trxReceipt = await dtContract.methods.addMinter(minter).send({ const trxReceipt = await dtContract.methods.addMinter(minter).send({
@ -537,18 +513,7 @@ export class Datatoken {
// should check ERC20Deployer role using erc721 level .. // should check ERC20Deployer role using erc721 level ..
// Estimate gas for removeMinter method return estimateGas(address, dtContract.methods.removeMinter, minter)
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await dtContract.methods
.removeMinter(minter)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -574,7 +539,7 @@ export class Datatoken {
throw new Error(`Caller is not ERC20Deployer`) throw new Error(`Caller is not ERC20Deployer`)
} }
const estGas = await this.estGasRemoveMinter(dtAddress, address, minter, dtContract) const estGas = await estimateGas(address, dtContract.methods.removeMinter, minter)
// Call dtContract function of the contract // Call dtContract function of the contract
const trxReceipt = await dtContract.methods.removeMinter(minter).send({ const trxReceipt = await dtContract.methods.removeMinter(minter).send({
@ -607,18 +572,7 @@ export class Datatoken {
this.config this.config
) )
// Estimate gas for addFeeManager method return estimateGas(address, dtContract.methods.addPaymentManager, paymentManager)
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await dtContract.methods
.addPaymentManager(paymentManager)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -643,11 +597,10 @@ export class Datatoken {
throw new Error(`Caller is not ERC20Deployer`) throw new Error(`Caller is not ERC20Deployer`)
} }
const estGas = await this.estGasAddPaymentManager( const estGas = await estimateGas(
dtAddress,
address, address,
paymentManager, dtContract.methods.addPaymentManager,
dtContract paymentManager
) )
// Call addPaymentManager function of the contract // Call addPaymentManager function of the contract
@ -681,16 +634,7 @@ export class Datatoken {
this.config this.config
) )
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(address, dtContract.methods.removePaymentManager, paymentManager)
let estGas
try {
estGas = await dtContract.methods
.removePaymentManager(paymentManager)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -715,11 +659,10 @@ export class Datatoken {
throw new Error(`Caller is not ERC20Deployer`) throw new Error(`Caller is not ERC20Deployer`)
} }
const estGas = await this.estGasRemovePaymentManager( const estGas = await estimateGas(
dtAddress,
address, address,
paymentManager, dtContract.methods.removePaymentManager,
dtContract paymentManager
) )
// Call removeFeeManager function of the contract // Call removeFeeManager function of the contract
@ -755,16 +698,7 @@ export class Datatoken {
this.config this.config
) )
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(address, dtContract.methods.setPaymentCollector, paymentCollector)
let estGas
try {
estGas = await dtContract.methods
.setPaymentCollector(paymentCollector)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -789,11 +723,10 @@ export class Datatoken {
throw new Error(`Caller is not Fee Manager`) throw new Error(`Caller is not Fee Manager`)
} }
const estGas = await this.estGasSetPaymentCollector( const estGas = await estimateGas(
dtAddress,
address, address,
paymentCollector, dtContract.methods.setPaymentCollector,
dtContract paymentCollector
) )
// Call setFeeCollector method of the contract // Call setFeeCollector method of the contract
@ -862,16 +795,7 @@ export class Datatoken {
this.config this.config
) )
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(address, dtContract.methods.transfer, toAddress, amount)
let estGas
try {
estGas = await dtContract.methods
.transfer(toAddress, amount)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -893,12 +817,11 @@ export class Datatoken {
this.config this.config
) )
try { try {
const estGas = await this.estGasTransfer( const estGas = await estimateGas(
dtAddress,
toAddress,
amount,
address, address,
dtContract dtContract.methods.transfer,
toAddress,
amount
) )
// Call transfer function of the contract // Call transfer function of the contract
const trxReceipt = await dtContract.methods.transfer(toAddress, amount).send({ const trxReceipt = await dtContract.methods.transfer(toAddress, amount).send({
@ -939,17 +862,14 @@ export class Datatoken {
this.config this.config
) )
// Estimate gas for startOrder method return estimateGas(
const gasLimitDefault = this.GASLIMIT_DEFAULT address,
let estGas dtContract.methods.startOrder,
try { consumer,
estGas = await dtContract.methods serviceIndex,
.startOrder(consumer, serviceIndex, providerFees, consumeMarketFee) providerFees,
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) consumeMarketFee
} catch (e) { )
estGas = gasLimitDefault
}
return estGas
} }
/** Start Order: called by payer or consumer prior ordering a service consume on a marketplace. /** Start Order: called by payer or consumer prior ordering a service consume on a marketplace.
@ -981,14 +901,13 @@ export class Datatoken {
} }
} }
try { try {
const estGas = await this.estGasStartOrder( const estGas = await estimateGas(
dtAddress,
address, address,
dtContract.methods.startOrder,
consumer, consumer,
serviceIndex, serviceIndex,
providerFees, providerFees,
consumeMarketFee, consumeMarketFee
dtContract
) )
const trxReceipt = await dtContract.methods const trxReceipt = await dtContract.methods
@ -1024,17 +943,12 @@ export class Datatoken {
contractInstance || contractInstance ||
new this.web3.eth.Contract(this.datatokensEnterpriseAbi, dtAddress) new this.web3.eth.Contract(this.datatokensEnterpriseAbi, dtAddress)
// Estimate gas for startOrder method return estimateGas(
const gasLimitDefault = this.GASLIMIT_DEFAULT address,
let estGas dtContract.methods.buyFromFreAndOrder,
try { orderParams,
estGas = await dtContract.methods freParams
.buyFromFreAndOrder(orderParams, freParams) )
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** Buys 1 DT from the FRE and then startsOrder, while burning that DT /** Buys 1 DT from the FRE and then startsOrder, while burning that DT
@ -1054,12 +968,11 @@ export class Datatoken {
try { try {
const freContractParams = getFreOrderParams(freParams) const freContractParams = getFreOrderParams(freParams)
const estGas = await this.estGasBuyFromFreAndOrder( const estGas = await estimateGas(
dtAddress,
address, address,
dtContract.methods.buyFromFreAndOrder,
orderParams, orderParams,
freContractParams, freContractParams
dtContract
) )
const trxReceipt = await dtContract.methods const trxReceipt = await dtContract.methods
@ -1095,17 +1008,12 @@ export class Datatoken {
contractInstance || contractInstance ||
new this.web3.eth.Contract(this.datatokensEnterpriseAbi, dtAddress) new this.web3.eth.Contract(this.datatokensEnterpriseAbi, dtAddress)
// Estimate gas for startOrder method return estimateGas(
const gasLimitDefault = this.GASLIMIT_DEFAULT address,
let estGas dtContract.methods.buyFromDispenserAndOrder,
try { orderParams,
estGas = await dtContract.methods dispenserContract
.buyFromDispenserAndOrder(orderParams, dispenserContract) )
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** Gets DT from dispenser and then startsOrder, while burning that DT /** Gets DT from dispenser and then startsOrder, while burning that DT
@ -1123,12 +1031,11 @@ export class Datatoken {
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
const dtContract = new this.web3.eth.Contract(this.datatokensEnterpriseAbi, dtAddress) const dtContract = new this.web3.eth.Contract(this.datatokensEnterpriseAbi, dtAddress)
try { try {
const estGas = await this.estGasBuyFromDispenserAndOrder( const estGas = await estimateGas(
dtAddress,
address, address,
dtContract.methods.buyFromDispenserAndOrder,
orderParams, orderParams,
dispenserContract, dispenserContract
dtContract
) )
const trxReceipt = await dtContract.methods const trxReceipt = await dtContract.methods
@ -1165,16 +1072,7 @@ export class Datatoken {
this.config this.config
) )
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(address, dtContract.methods.setData, value)
let estGas
try {
estGas = await dtContract.methods
.setData(value)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** setData /** setData
@ -1199,7 +1097,7 @@ export class Datatoken {
this.config this.config
) )
const estGas = await this.estGasSetData(dtAddress, address, value, dtContract) const estGas = await estimateGas(address, dtContract.methods.setData, value)
// Call setData function of the contract // Call setData function of the contract
const trxReceipt = await dtContract.methods.setData(value).send({ const trxReceipt = await dtContract.methods.setData(value).send({
@ -1229,17 +1127,7 @@ export class Datatoken {
this.config this.config
) )
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(address, dtContract.methods.cleanPermissions)
let estGas
try {
estGas = await dtContract.methods
.cleanPermissions()
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -1261,7 +1149,7 @@ export class Datatoken {
this.config this.config
) )
const estGas = await this.estGasCleanPermissions(dtAddress, address, dtContract) const estGas = await estimateGas(address, dtContract.methods.cleanPermissions)
// Call cleanPermissions function of the contract // Call cleanPermissions function of the contract
const trxReceipt = await dtContract.methods.cleanPermissions().send({ const trxReceipt = await dtContract.methods.cleanPermissions().send({