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

use estimateGas() function in NFTFactory

This commit is contained in:
Miquel A. Cabot 2022-04-05 15:29:00 +02:00
parent 2af104497a
commit 00fc5d21e9

View File

@ -11,7 +11,8 @@ import {
getErcCreationParams, getErcCreationParams,
getPoolCreationParams, getPoolCreationParams,
configHelperNetworks, configHelperNetworks,
setContractDefaults setContractDefaults,
estimateGas
} from '../utils' } from '../utils'
import { Config } from '../models/index.js' import { Config } from '../models/index.js'
import { import {
@ -86,25 +87,18 @@ export class NftFactory {
* @return {Promise<string>} NFT datatoken address * @return {Promise<string>} NFT datatoken address
*/ */
public async estGasCreateNFT(address: string, nftData: NftCreateData): Promise<string> { public async estGasCreateNFT(address: string, nftData: NftCreateData): Promise<string> {
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(
let estGas address,
try { this.factory721.methods.deployERC721Contract,
estGas = await this.factory721.methods nftData.name,
.deployERC721Contract( nftData.symbol,
nftData.name, nftData.templateIndex,
nftData.symbol, addressZERO,
nftData.templateIndex, addressZERO,
addressZERO, nftData.tokenURI,
addressZERO, nftData.transferable,
nftData.tokenURI, nftData.owner
nftData.transferable, )
nftData.owner
)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -131,7 +125,18 @@ export class NftFactory {
if ((await this.getNFTTemplate(nftData.templateIndex)).isActive === false) { if ((await this.getNFTTemplate(nftData.templateIndex)).isActive === false) {
throw new Error(`Template is not active`) throw new Error(`Template is not active`)
} }
const estGas = await this.estGasCreateNFT(address, nftData) const estGas = await estimateGas(
address,
this.factory721.methods.deployERC721Contract,
nftData.name,
nftData.symbol,
nftData.templateIndex,
addressZERO,
addressZERO,
nftData.tokenURI,
nftData.transferable,
nftData.owner
)
// Invoke createToken function of the contract // Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods const trxReceipt = await this.factory721.methods
@ -253,16 +258,11 @@ export class NftFactory {
address: string, address: string,
templateAddress: string templateAddress: string
): Promise<any> { ): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(
let estGas address,
try { this.factory721.methods.add721TokenTemplate,
estGas = await this.factory721.methods templateAddress
.add721TokenTemplate(templateAddress) )
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -282,7 +282,11 @@ export class NftFactory {
throw new Error(`Template cannot be ZERO address`) throw new Error(`Template cannot be ZERO address`)
} }
const estGas = await this.estGasAddNFTTemplate(address, templateAddress) const estGas = await estimateGas(
address,
this.factory721.methods.add721TokenTemplate,
templateAddress
)
// Invoke add721TokenTemplate function of the contract // Invoke add721TokenTemplate function of the contract
const trxReceipt = await this.factory721.methods const trxReceipt = await this.factory721.methods
@ -306,16 +310,11 @@ export class NftFactory {
address: string, address: string,
templateIndex: number templateIndex: number
): Promise<any> { ): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(
let estGas address,
try { this.factory721.methods.disable721TokenTemplate,
estGas = await this.factory721.methods templateIndex
.disable721TokenTemplate(templateIndex) )
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -338,7 +337,11 @@ export class NftFactory {
if (templateIndex === 0) { if (templateIndex === 0) {
throw new Error(`Template index cannot be ZERO`) throw new Error(`Template index cannot be ZERO`)
} }
const estGas = await this.estGasDisableNFTTemplate(address, templateIndex) const estGas = await estimateGas(
address,
this.factory721.methods.disable721TokenTemplate,
templateIndex
)
// Invoke createToken function of the contract // Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods const trxReceipt = await this.factory721.methods
@ -362,16 +365,11 @@ export class NftFactory {
address: string, address: string,
templateIndex: number templateIndex: number
): Promise<any> { ): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(
let estGas address,
try { this.factory721.methods.reactivate721TokenTemplate,
estGas = await this.factory721.methods templateIndex
.reactivate721TokenTemplate(templateIndex) )
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -395,7 +393,11 @@ export class NftFactory {
throw new Error(`Template index cannot be ZERO`) throw new Error(`Template index cannot be ZERO`)
} }
const estGas = await this.estGasReactivateNFTTemplate(address, templateIndex) const estGas = await estimateGas(
address,
this.factory721.methods.reactivate721TokenTemplate,
templateIndex
)
// Invoke createToken function of the contract // Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods const trxReceipt = await this.factory721.methods
@ -419,17 +421,7 @@ export class NftFactory {
address: string, address: string,
templateAddress: string templateAddress: string
): Promise<any> { ): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(address, this.factory721.methods.addTokenTemplate, templateAddress)
let estGas
try {
estGas = await this.factory721.methods
.addTokenTemplate(templateAddress)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -449,7 +441,11 @@ export class NftFactory {
throw new Error(`Template cannot be address ZERO`) throw new Error(`Template cannot be address ZERO`)
} }
const estGas = await this.estGasAddTokenTemplate(address, templateAddress) const estGas = await estimateGas(
address,
this.factory721.methods.addTokenTemplate,
templateAddress
)
// Invoke createToken function of the contract // Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods const trxReceipt = await this.factory721.methods
@ -473,16 +469,11 @@ export class NftFactory {
address: string, address: string,
templateIndex: number templateIndex: number
): Promise<any> { ): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(
let estGas address,
try { this.factory721.methods.disableTokenTemplate,
estGas = await this.factory721.methods templateIndex
.disableTokenTemplate(templateIndex) )
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -508,7 +499,11 @@ export class NftFactory {
if ((await this.getTokenTemplate(templateIndex)).isActive === false) { if ((await this.getTokenTemplate(templateIndex)).isActive === false) {
throw new Error(`Template is already disabled`) throw new Error(`Template is already disabled`)
} }
const estGas = await this.estGasDisableTokenTemplate(address, templateIndex) const estGas = await estimateGas(
address,
this.factory721.methods.disableTokenTemplate,
templateIndex
)
// Invoke createToken function of the contract // Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods const trxReceipt = await this.factory721.methods
@ -532,16 +527,11 @@ export class NftFactory {
address: string, address: string,
templateIndex: number templateIndex: number
): Promise<any> { ): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(
let estGas address,
try { this.factory721.methods.reactivateTokenTemplate,
estGas = await this.factory721.methods templateIndex
.reactivateTokenTemplate(templateIndex) )
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -568,7 +558,11 @@ export class NftFactory {
throw new Error(`Template is already active`) throw new Error(`Template is already active`)
} }
const estGas = await this.estGasReactivateTokenTemplate(address, templateIndex) const estGas = await estimateGas(
address,
this.factory721.methods.reactivateTokenTemplate,
templateIndex
)
// Invoke createToken function of the contract // Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods const trxReceipt = await this.factory721.methods
@ -591,16 +585,7 @@ export class NftFactory {
address: string, address: string,
orders: TokenOrder[] orders: TokenOrder[]
): Promise<any> { ): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(address, this.factory721.methods.startMultipleTokenOrder, orders)
let estGas
try {
estGas = await this.factory721.methods
.startMultipleTokenOrder(orders)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -623,7 +608,11 @@ export class NftFactory {
throw new Error(`Too many orders`) throw new Error(`Too many orders`)
} }
const estGas = await this.estGasStartMultipleTokenOrder(address, orders) const estGas = await estimateGas(
address,
this.factory721.methods.startMultipleTokenOrder,
orders
)
// Invoke createToken function of the contract // Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods const trxReceipt = await this.factory721.methods
@ -650,18 +639,13 @@ export class NftFactory {
nftCreateData: NftCreateData, nftCreateData: NftCreateData,
ercParams: Erc20CreateParams ercParams: Erc20CreateParams
): Promise<any> { ): Promise<any> {
// Get estimated gas value const ercCreateData = getErcCreationParams(ercParams)
const gasLimitDefault = this.GASLIMIT_DEFAULT return estimateGas(
let estGas address,
try { this.factory721.methods.createNftWithErc20,
const ercCreateData = getErcCreationParams(ercParams) nftCreateData,
estGas = await this.factory721.methods ercCreateData
.createNftWithErc20(nftCreateData, ercCreateData) )
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -680,7 +664,13 @@ export class NftFactory {
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
const ercCreateData = getErcCreationParams(ercParams) const ercCreateData = getErcCreationParams(ercParams)
const estGas = await this.estGasCreateNftWithErc20(address, nftCreateData, ercParams) const estGas = await estimateGas(
address,
this.factory721.methods.createNftWithErc20,
nftCreateData,
ercCreateData
)
// Invoke createToken function of the contract // Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods const trxReceipt = await this.factory721.methods
.createNftWithErc20(nftCreateData, ercCreateData) .createNftWithErc20(nftCreateData, ercCreateData)
@ -707,18 +697,15 @@ export class NftFactory {
ercParams: Erc20CreateParams, ercParams: Erc20CreateParams,
poolParams: PoolCreationParams poolParams: PoolCreationParams
): Promise<any> { ): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT const ercCreateData = getErcCreationParams(ercParams)
let estGas const poolData = await getPoolCreationParams(this.web3, poolParams)
try { return estimateGas(
const ercCreateData = getErcCreationParams(ercParams) address,
const poolData = await getPoolCreationParams(this.web3, poolParams) this.factory721.methods.createNftWithErc20WithPool,
estGas = await this.factory721.methods nftCreateData,
.createNftWithErc20WithPool(nftCreateData, ercCreateData, poolData) ercCreateData,
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) poolData
} catch (e) { )
estGas = gasLimitDefault
}
return estGas
} }
/** /**
@ -737,15 +724,17 @@ export class NftFactory {
ercParams: Erc20CreateParams, ercParams: Erc20CreateParams,
poolParams: PoolCreationParams poolParams: PoolCreationParams
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
const estGas = await this.estGasCreateNftErc20WithPool(
address,
nftCreateData,
ercParams,
poolParams
)
const ercCreateData = getErcCreationParams(ercParams) const ercCreateData = getErcCreationParams(ercParams)
const poolData = await getPoolCreationParams(this.web3, poolParams) const poolData = await getPoolCreationParams(this.web3, poolParams)
const estGas = await estimateGas(
address,
this.factory721.methods.createNftWithErc20WithPool,
nftCreateData,
ercCreateData,
poolData
)
// Invoke createToken function of the contract // Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods const trxReceipt = await this.factory721.methods
.createNftWithErc20WithPool(nftCreateData, ercCreateData, poolData) .createNftWithErc20WithPool(nftCreateData, ercCreateData, poolData)
@ -771,20 +760,15 @@ export class NftFactory {
ercParams: Erc20CreateParams, ercParams: Erc20CreateParams,
freParams: FreCreationParams freParams: FreCreationParams
): Promise<any> { ): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
const ercCreateData = getErcCreationParams(ercParams) const ercCreateData = getErcCreationParams(ercParams)
const fixedData = await getFreCreationParams(freParams) const fixedData = await getFreCreationParams(freParams)
return estimateGas(
try { address,
estGas = await this.factory721.methods this.factory721.methods.createNftWithErc20WithFixedRate,
.createNftWithErc20WithFixedRate(nftCreateData, ercCreateData, fixedData) nftCreateData,
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) ercCreateData,
} catch (e) { fixedData
estGas = gasLimitDefault )
}
return estGas
} }
/** /**
@ -806,11 +790,12 @@ export class NftFactory {
const ercCreateData = getErcCreationParams(ercParams) const ercCreateData = getErcCreationParams(ercParams)
const fixedData = getFreCreationParams(freParams) const fixedData = getFreCreationParams(freParams)
const estGas = await this.estGasCreateNftErc20WithFixedRate( const estGas = await estimateGas(
address, address,
this.factory721.methods.createNftWithErc20WithFixedRate,
nftCreateData, nftCreateData,
ercParams, ercCreateData,
freParams fixedData
) )
// Invoke createToken function of the contract // Invoke createToken function of the contract
@ -838,20 +823,14 @@ export class NftFactory {
ercParams: Erc20CreateParams, ercParams: Erc20CreateParams,
dispenserParams: DispenserCreationParams dispenserParams: DispenserCreationParams
): Promise<any> { ): Promise<any> {
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
const ercCreateData = getErcCreationParams(ercParams) const ercCreateData = getErcCreationParams(ercParams)
return estimateGas(
try { address,
estGas = await this.factory721.methods this.factory721.methods.createNftWithErc20WithDispenser,
.createNftWithErc20WithDispenser(nftCreateData, ercCreateData, dispenserParams) nftCreateData,
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) ercCreateData,
} catch (e) { dispenserParams
estGas = gasLimitDefault )
LoggerInstance.error('Failed to estimate gas for createNftErc20WithDispenser', e)
}
return estGas
} }
/** /**
@ -875,10 +854,11 @@ export class NftFactory {
dispenserParams.maxBalance = Web3.utils.toWei(dispenserParams.maxBalance) dispenserParams.maxBalance = Web3.utils.toWei(dispenserParams.maxBalance)
dispenserParams.maxTokens = Web3.utils.toWei(dispenserParams.maxTokens) dispenserParams.maxTokens = Web3.utils.toWei(dispenserParams.maxTokens)
const estGas = await this.estGasCreateNftErc20WithDispenser( const estGas = await estimateGas(
address, address,
this.factory721.methods.createNftWithErc20WithDispenser,
nftCreateData, nftCreateData,
ercParams, ercCreateData,
dispenserParams dispenserParams
) )