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

inherit NFTFactory from SmartContract

This commit is contained in:
Miquel A. Cabot 2022-06-06 17:33:01 +02:00
parent 4515f938e2
commit c09a48fa72
2 changed files with 55 additions and 88 deletions

View File

@ -1,8 +1,7 @@
import { Contract } from 'web3-eth-contract'
import Web3 from 'web3'
import { TransactionReceipt } from 'web3-core'
import { AbiItem } from 'web3-utils'
import defaultFactory721Abi from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json'
import ERC721Factory from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json'
import {
LoggerInstance,
getFairGasPrice,
@ -10,11 +9,9 @@ import {
getFreCreationParams,
getErcCreationParams,
getPoolCreationParams,
setContractDefaults,
estimateGas,
ZERO_ADDRESS
} from '../../utils'
import { Config, ConfigHelper } from '../../config'
import {
FreCreationParams,
Erc20CreateParams,
@ -24,38 +21,14 @@ import {
Template,
TokenOrder
} from '../../@types'
import { SmartContractWithAddress } from '..'
/**
* Provides an interface for NFT Factory contract
*/
export class NftFactory {
public factory721Address: string
public factory721Abi: AbiItem | AbiItem[]
public web3: Web3
public config: Config
public factory721: Contract
/**
* Instantiate Datatokens.
* @param {String} factory721Address
* @param {AbiItem | AbiItem[]} factory721ABI
* @param {Web3} web3
*/
constructor(
factory721Address: string,
web3: Web3,
network?: string | number,
factory721Abi?: AbiItem | AbiItem[],
config?: Config
) {
this.factory721Address = factory721Address
this.factory721Abi = factory721Abi || (defaultFactory721Abi.abi as AbiItem[])
this.web3 = web3
this.config = config || new ConfigHelper().getConfig(network || 'unknown')
this.factory721 = setContractDefaults(
new this.web3.eth.Contract(this.factory721Abi, this.factory721Address),
this.config
)
export class NftFactory extends SmartContractWithAddress {
getDefaultAbi(): AbiItem | AbiItem[] {
return ERC721Factory.abi as AbiItem[]
}
/**
@ -67,7 +40,7 @@ export class NftFactory {
public async estGasCreateNFT(address: string, nftData: NftCreateData): Promise<string> {
return estimateGas(
address,
this.factory721.methods.deployERC721Contract,
this.contract.methods.deployERC721Contract,
nftData.name,
nftData.symbol,
nftData.templateIndex,
@ -105,7 +78,7 @@ export class NftFactory {
}
const estGas = await estimateGas(
address,
this.factory721.methods.deployERC721Contract,
this.contract.methods.deployERC721Contract,
nftData.name,
nftData.symbol,
nftData.templateIndex,
@ -117,7 +90,7 @@ export class NftFactory {
)
// Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods
const trxReceipt = await this.contract.methods
.deployERC721Contract(
nftData.name,
nftData.symbol,
@ -147,7 +120,7 @@ export class NftFactory {
* @return {Promise<number>} Number of NFT created from this factory
*/
public async getCurrentNFTCount(): Promise<number> {
const trxReceipt = await this.factory721.methods.getCurrentNFTCount().call()
const trxReceipt = await this.contract.methods.getCurrentNFTCount().call()
return trxReceipt
}
@ -155,7 +128,7 @@ export class NftFactory {
* @return {Promise<number>} Number of DTs created from this factory
*/
public async getCurrentTokenCount(): Promise<number> {
const trxReceipt = await this.factory721.methods.getCurrentTokenCount().call()
const trxReceipt = await this.contract.methods.getCurrentTokenCount().call()
return trxReceipt
}
@ -163,7 +136,7 @@ export class NftFactory {
* @return {Promise<string>} Factory Owner address
*/
public async getOwner(): Promise<string> {
const trxReceipt = await this.factory721.methods.owner().call()
const trxReceipt = await this.contract.methods.owner().call()
return trxReceipt
}
@ -171,7 +144,7 @@ export class NftFactory {
* @return {Promise<number>} Number of NFT Template added to this factory
*/
public async getCurrentNFTTemplateCount(): Promise<number> {
const count = await this.factory721.methods.getCurrentNFTTemplateCount().call()
const count = await this.contract.methods.getCurrentNFTTemplateCount().call()
return count
}
@ -179,7 +152,7 @@ export class NftFactory {
* @return {Promise<number>} Number of ERC20 Template added to this factory
*/
public async getCurrentTokenTemplateCount(): Promise<number> {
const count = await this.factory721.methods.getCurrentTemplateCount().call()
const count = await this.contract.methods.getCurrentTemplateCount().call()
return count
}
@ -195,7 +168,7 @@ export class NftFactory {
if (index === 0) {
throw new Error(`Template index cannot be ZERO`)
}
const template = await this.factory721.methods.getNFTTemplate(index).call()
const template = await this.contract.methods.getNFTTemplate(index).call()
return template
}
@ -204,7 +177,7 @@ export class NftFactory {
* @return {Promise<Template>} DT Template info
*/
public async getTokenTemplate(index: number): Promise<Template> {
const template = await this.factory721.methods.getTokenTemplate(index).call()
const template = await this.contract.methods.getTokenTemplate(index).call()
return template
}
@ -213,7 +186,7 @@ export class NftFactory {
* @return {Promise<Boolean>} return true if deployed from this factory
*/
public async checkDatatoken(datatoken: string): Promise<Boolean> {
const isDeployed = await this.factory721.methods.erc20List(datatoken).call()
const isDeployed = await this.contract.methods.erc20List(datatoken).call()
return isDeployed
}
@ -222,7 +195,7 @@ export class NftFactory {
* @return {Promise<String>} return address(0) if it's not, or the nftAddress if true
*/
public async checkNFT(nftAddress: string): Promise<String> {
const confirmAddress = await this.factory721.methods.erc721List(nftAddress).call()
const confirmAddress = await this.contract.methods.erc721List(nftAddress).call()
return confirmAddress
}
@ -238,7 +211,7 @@ export class NftFactory {
): Promise<any> {
return estimateGas(
address,
this.factory721.methods.add721TokenTemplate,
this.contract.methods.add721TokenTemplate,
templateAddress
)
}
@ -262,12 +235,12 @@ export class NftFactory {
const estGas = await estimateGas(
address,
this.factory721.methods.add721TokenTemplate,
this.contract.methods.add721TokenTemplate,
templateAddress
)
// Invoke add721TokenTemplate function of the contract
const trxReceipt = await this.factory721.methods
const trxReceipt = await this.contract.methods
.add721TokenTemplate(templateAddress)
.send({
from: address,
@ -290,7 +263,7 @@ export class NftFactory {
): Promise<any> {
return estimateGas(
address,
this.factory721.methods.disable721TokenTemplate,
this.contract.methods.disable721TokenTemplate,
templateIndex
)
}
@ -317,12 +290,12 @@ export class NftFactory {
}
const estGas = await estimateGas(
address,
this.factory721.methods.disable721TokenTemplate,
this.contract.methods.disable721TokenTemplate,
templateIndex
)
// Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods
const trxReceipt = await this.contract.methods
.disable721TokenTemplate(templateIndex)
.send({
from: address,
@ -345,7 +318,7 @@ export class NftFactory {
): Promise<any> {
return estimateGas(
address,
this.factory721.methods.reactivate721TokenTemplate,
this.contract.methods.reactivate721TokenTemplate,
templateIndex
)
}
@ -373,12 +346,12 @@ export class NftFactory {
const estGas = await estimateGas(
address,
this.factory721.methods.reactivate721TokenTemplate,
this.contract.methods.reactivate721TokenTemplate,
templateIndex
)
// Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods
const trxReceipt = await this.contract.methods
.reactivate721TokenTemplate(templateIndex)
.send({
from: address,
@ -399,7 +372,7 @@ export class NftFactory {
address: string,
templateAddress: string
): Promise<any> {
return estimateGas(address, this.factory721.methods.addTokenTemplate, templateAddress)
return estimateGas(address, this.contract.methods.addTokenTemplate, templateAddress)
}
/**
@ -421,12 +394,12 @@ export class NftFactory {
const estGas = await estimateGas(
address,
this.factory721.methods.addTokenTemplate,
this.contract.methods.addTokenTemplate,
templateAddress
)
// Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods
const trxReceipt = await this.contract.methods
.addTokenTemplate(templateAddress)
.send({
from: address,
@ -447,11 +420,7 @@ export class NftFactory {
address: string,
templateIndex: number
): Promise<any> {
return estimateGas(
address,
this.factory721.methods.disableTokenTemplate,
templateIndex
)
return estimateGas(address, this.contract.methods.disableTokenTemplate, templateIndex)
}
/**
@ -479,12 +448,12 @@ export class NftFactory {
}
const estGas = await estimateGas(
address,
this.factory721.methods.disableTokenTemplate,
this.contract.methods.disableTokenTemplate,
templateIndex
)
// Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods
const trxReceipt = await this.contract.methods
.disableTokenTemplate(templateIndex)
.send({
from: address,
@ -507,7 +476,7 @@ export class NftFactory {
): Promise<any> {
return estimateGas(
address,
this.factory721.methods.reactivateTokenTemplate,
this.contract.methods.reactivateTokenTemplate,
templateIndex
)
}
@ -538,12 +507,12 @@ export class NftFactory {
const estGas = await estimateGas(
address,
this.factory721.methods.reactivateTokenTemplate,
this.contract.methods.reactivateTokenTemplate,
templateIndex
)
// Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods
const trxReceipt = await this.contract.methods
.reactivateTokenTemplate(templateIndex)
.send({
from: address,
@ -563,7 +532,7 @@ export class NftFactory {
address: string,
orders: TokenOrder[]
): Promise<any> {
return estimateGas(address, this.factory721.methods.startMultipleTokenOrder, orders)
return estimateGas(address, this.contract.methods.startMultipleTokenOrder, orders)
}
/**
@ -588,18 +557,16 @@ export class NftFactory {
const estGas = await estimateGas(
address,
this.factory721.methods.startMultipleTokenOrder,
this.contract.methods.startMultipleTokenOrder,
orders
)
// Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods
.startMultipleTokenOrder(orders)
.send({
from: address,
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3, this.config)
})
const trxReceipt = await this.contract.methods.startMultipleTokenOrder(orders).send({
from: address,
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3, this.config)
})
return trxReceipt
}
@ -620,7 +587,7 @@ export class NftFactory {
const ercCreateData = getErcCreationParams(ercParams)
return estimateGas(
address,
this.factory721.methods.createNftWithErc20,
this.contract.methods.createNftWithErc20,
nftCreateData,
ercCreateData
)
@ -644,13 +611,13 @@ export class NftFactory {
const estGas = await estimateGas(
address,
this.factory721.methods.createNftWithErc20,
this.contract.methods.createNftWithErc20,
nftCreateData,
ercCreateData
)
// Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods
const trxReceipt = await this.contract.methods
.createNftWithErc20(nftCreateData, ercCreateData)
.send({
from: address,
@ -679,7 +646,7 @@ export class NftFactory {
const poolData = await getPoolCreationParams(this.web3, poolParams)
return estimateGas(
address,
this.factory721.methods.createNftWithErc20WithPool,
this.contract.methods.createNftWithErc20WithPool,
nftCreateData,
ercCreateData,
poolData
@ -707,14 +674,14 @@ export class NftFactory {
const estGas = await estimateGas(
address,
this.factory721.methods.createNftWithErc20WithPool,
this.contract.methods.createNftWithErc20WithPool,
nftCreateData,
ercCreateData,
poolData
)
// Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods
const trxReceipt = await this.contract.methods
.createNftWithErc20WithPool(nftCreateData, ercCreateData, poolData)
.send({
from: address,
@ -742,7 +709,7 @@ export class NftFactory {
const fixedData = await getFreCreationParams(freParams)
return estimateGas(
address,
this.factory721.methods.createNftWithErc20WithFixedRate,
this.contract.methods.createNftWithErc20WithFixedRate,
nftCreateData,
ercCreateData,
fixedData
@ -770,14 +737,14 @@ export class NftFactory {
const estGas = await estimateGas(
address,
this.factory721.methods.createNftWithErc20WithFixedRate,
this.contract.methods.createNftWithErc20WithFixedRate,
nftCreateData,
ercCreateData,
fixedData
)
// Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods
const trxReceipt = await this.contract.methods
.createNftWithErc20WithFixedRate(nftCreateData, ercCreateData, fixedData)
.send({
from: address,
@ -804,7 +771,7 @@ export class NftFactory {
const ercCreateData = getErcCreationParams(ercParams)
return estimateGas(
address,
this.factory721.methods.createNftWithErc20WithDispenser,
this.contract.methods.createNftWithErc20WithDispenser,
nftCreateData,
ercCreateData,
dispenserParams
@ -834,14 +801,14 @@ export class NftFactory {
const estGas = await estimateGas(
address,
this.factory721.methods.createNftWithErc20WithDispenser,
this.contract.methods.createNftWithErc20WithDispenser,
nftCreateData,
ercCreateData,
dispenserParams
)
// Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods
const trxReceipt = await this.contract.methods
.createNftWithErc20WithDispenser(nftCreateData, ercCreateData, dispenserParams)
.send({
from: address,

View File

@ -10,7 +10,7 @@ import {
} from '../../utils'
import { Contract } from 'web3-eth-contract'
import { MetadataProof, MetadataAndTokenURI, NftRoles } from '../../@types'
import { SmartContract } from '../SmartContract'
import { SmartContract } from '..'
export class Nft extends SmartContract {
getDefaultAbi(): AbiItem | AbiItem[] {