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

add initial ERC721Factory class, add initial TestContractHandler

This commit is contained in:
lacoop6tu 2021-10-18 14:34:19 -05:00
parent 252eca12e4
commit 9cda532da6
2 changed files with 406 additions and 0 deletions

View File

@ -1,9 +1,14 @@
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 { Logger, getFairGasPrice, generateDtName } from '../utils'
interface Template {
templateAddress: string
isActive: boolean
}
/**
* Provides an interface for NFT DataTokens
*/
@ -88,4 +93,278 @@ export class NFTFactory {
}
return tokenAddress
}
/** Get Current NFT Count (NFT created)
* @return {Promise<number>} Number of NFT created from this factory
*/
public async getCurrentNFTCount(): Promise<number> {
const trxReceipt = await this.factory721.methods.getCurrentNFTCount().call()
return trxReceipt
}
/** Get Current Datatoken Count
* @return {Promise<number>} Number of DTs created from this factory
*/
public async getCurrentTokenCount(): Promise<number> {
const trxReceipt = await this.factory721.methods.getCurrentTokenCount().call()
return trxReceipt
}
/** Get Factory Owner
* @return {Promise<string>} Factory Owner address
*/
public async getOwner(): Promise<string> {
const trxReceipt = await this.factory721.methods.owner().call()
return trxReceipt
}
/** Get Current NFT Template Count
* @return {Promise<number>} Number of NFT Template added to this factory
*/
public async getCurrentNFTTemplateCount(): Promise<number> {
const count = await this.factory721.methods.getCurrentNFTTemplateCount().call()
return count
}
/** Get Current Template Datatoken (ERC20) Count
* @return {Promise<number>} Number of ERC20 Template added to this factory
*/
public async getCurrentTokenTemplateCount(): Promise<number> {
const count = await this.factory721.methods.getCurrentTokenTemplateCount().call()
return count
}
/** Get NFT Template
* @param {Number} index Template index
* @return {Promise<Template>} Number of Template added to this factory
*/
public async getNFTTemplate(index: number): Promise<Template> {
const template = await this.factory721.methods.getNFTTemplate(index).call()
return template
}
/** Get Datatoken(erc20) Template
* @param {Number} index Template index
* @return {Promise<Template>} DT Template info
*/
public async getTokenTemplate(index: number): Promise<Template> {
const template = await this.factory721.methods.getTokenTemplate(index).call()
return template
}
/**
* Add a new erc721 token template - only factory Owner
* @param {String} address
* @param {String} templateAddress template address to add
* @return {Promise<TransactionReceipt>}
*/
public async addNFTTemplate(
address: string,
templateAddress: string
): Promise<TransactionReceipt> {
if ((await this.getOwner()) != address) {
throw new Error(`Caller is not Factory Owner`)
}
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await this.factory721.methods
.add721TokenTemplate(templateAddress)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
// Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods
.add721TokenTemplate(templateAddress)
.send({
from: address,
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3)
})
return trxReceipt
}
/**
* Disable token template - only factory Owner
* @param {String} address
* @param {Number} templateIndex index of the template we want to disable
* @return {Promise<TransactionReceipt>} current token template count
*/
public async disableNFTTemplate(
address: string,
templateIndex: number
): Promise<TransactionReceipt> {
if ((await this.getOwner()) != address) {
throw new Error(`Caller is not Factory Owner`)
}
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await this.factory721.methods
.disable721TokenTemplate(templateIndex)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
// Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods
.disable721TokenTemplate(templateIndex)
.send({
from: address,
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3)
})
return trxReceipt
}
/**
* Reactivate a previously disabled token template - only factory Owner
* @param {String} address
* @param {Number} templateIndex index of the template we want to reactivate
* @return {Promise<TransactionReceipt>} current token template count
*/
public async reactivateNFTTemplate(
address: string,
templateIndex: number
): Promise<TransactionReceipt> {
if ((await this.getOwner()) != address) {
throw new Error(`Caller is not Factory Owner`)
}
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await this.factory721.methods
.reactivate721TokenTemplate(templateIndex)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
// Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods
.reactivate721TokenTemplate(templateIndex)
.send({
from: address,
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3)
})
return trxReceipt
}
/**
* Add a new erc721 token template - only factory Owner
* @param {String} address
* @param {String} templateAddress template address to add
* @return {Promise<TransactionReceipt>}
*/
public async addTokenTemplate(
address: string,
templateAddress: string
): Promise<TransactionReceipt> {
if ((await this.getOwner()) != address) {
throw new Error(`Caller is not Factory Owner`)
}
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await this.factory721.methods
.addTokenTemplate(templateAddress)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
// Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods
.addTokenTemplate(templateAddress)
.send({
from: address,
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3)
})
return trxReceipt
}
/**
* Disable token template - only factory Owner
* @param {String} address
* @param {Number} templateIndex index of the template we want to disable
* @return {Promise<TransactionReceipt>} current token template count
*/
public async disableTokenTemplate(
address: string,
templateIndex: number
): Promise<TransactionReceipt> {
if ((await this.getOwner()) != address) {
throw new Error(`Caller is not Factory Owner`)
}
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await this.factory721.methods
.disableTokenTemplate(templateIndex)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
// Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods
.disableTokenTemplate(templateIndex)
.send({
from: address,
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3)
})
return trxReceipt
}
/**
* Reactivate a previously disabled token template - only factory Owner
* @param {String} address
* @param {Number} templateIndex index of the template we want to reactivate
* @return {Promise<TransactionReceipt>} current token template count
*/
public async reactivateTokenTemplate(
address: string,
templateIndex: number
): Promise<TransactionReceipt> {
if ((await this.getOwner()) != address) {
throw new Error(`Caller is not Factory Owner`)
}
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await this.factory721.methods
.reactivateTokenTemplate(templateIndex)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
// Invoke createToken function of the contract
const trxReceipt = await this.factory721.methods
.reactivateTokenTemplate(templateIndex)
.send({
from: address,
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3)
})
return trxReceipt
}
}

127
test/TestContractHandler.ts Normal file
View File

@ -0,0 +1,127 @@
import Web3 from 'web3'
import { Contract } from 'web3-eth-contract'
import { AbiItem } from 'web3-utils/types'
const communityCollector = '0xeE9300b7961e0a01d9f0adb863C7A227A07AaD75'
export class TestContractHandler {
public accounts: string[]
public ERC721Factory: Contract
public ERC20Template: Contract
public ERC721Template: Contract
public ERC721FactoryBytecode: string
public ERC20TemplateBytecode: string
public ERC721TemplateBytecode: string
public factory721Address: string
public template721Address: string
public template20Address: string
public web3: Web3
constructor(
ERC721FactoryABI: AbiItem | AbiItem[],
ERC721TemplateABI: AbiItem | AbiItem[],
ERC20TemplateABI: AbiItem | AbiItem[],
factory721Bytecode: string,
template721Bytecode: string,
template20Bytecode: string,
web3: Web3
) {
this.web3 = web3
this.ERC721Factory = new this.web3.eth.Contract(ERC721FactoryABI)
this.ERC20Template = new this.web3.eth.Contract(ERC20TemplateABI)
this.ERC721Template = new this.web3.eth.Contract(ERC721TemplateABI)
this.ERC721FactoryBytecode = factory721Bytecode
this.ERC20TemplateBytecode = template20Bytecode
this.ERC721TemplateBytecode = template721Bytecode
}
public async getAccounts(): Promise<string[]> {
this.accounts = await this.web3.eth.getAccounts()
return this.accounts
}
public async deployContracts(minter: string) {
let estGas
// console.log(this.ERC721TemplateBytecode)
// console.log(this.ERC20TemplateBytecode)
// DEPLOY ERC20 TEMPLATE
// get est gascost
estGas = await this.ERC20Template.deploy({
data: this.ERC20TemplateBytecode,
arguments: []
}).estimateGas(function (err, estGas) {
if (err) console.log('DeployContracts: ' + err)
return estGas
})
// deploy the contract and get it's address
this.template20Address = await this.ERC20Template.deploy({
data: this.ERC20TemplateBytecode,
arguments: []
})
.send({
from: minter,
gas: estGas + 1,
gasPrice: '3000000000'
})
.then(function (contract) {
return contract.options.address
})
// DEPLOY ERC721 TEMPLATE
// get est gascost
estGas = await this.ERC721Template.deploy({
data: this.ERC721TemplateBytecode,
arguments: []
}).estimateGas(function (err, estGas) {
if (err) console.log('DeployContracts: ' + err)
return estGas
})
// deploy the contract and get it's address
this.template721Address = await this.ERC721Template.deploy({
data: this.ERC721TemplateBytecode,
arguments: []
})
.send({
from: minter,
gas: estGas + 1,
gasPrice: '3000000000'
})
.then(function (contract) {
return contract.options.address
})
// DEPLOY ERC721 FACTORY
estGas = await this.ERC721Factory.deploy({
data: this.ERC721FactoryBytecode,
arguments: [this.template721Address, communityCollector,this.factory20Address,this.metadataAddress]
}).estimateGas(function (err, estGas) {
if (err) console.log('DeployContracts: ' + err)
return estGas
})
// deploy the contract and get it's address
this.factory721Address = await this.ERC721Factory.deploy({
data: this.ERC721FactoryBytecode,
arguments: [this.template721Address,communityCollector,this.factory20Address,this.metadataAddress]
})
.send({
from: minter,
gas: estGas + 1,
gasPrice: '3000000000'
})
.then(function (contract) {
return contract.options.address
})
// // TODO: SET ERC721 Factory address in ERC20 Factory
}
}