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

add contracts deployment for test, add initial NFTFactory.test.ts

This commit is contained in:
lacoop6tu 2021-10-19 13:58:04 -05:00
parent 9cda532da6
commit 3e247d46bf
7 changed files with 4683 additions and 52813 deletions

57135
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -24,7 +24,7 @@
"release": "release-it --non-interactive", "release": "release-it --non-interactive",
"changelog": "auto-changelog -p", "changelog": "auto-changelog -p",
"prepublishOnly": "npm run build", "prepublishOnly": "npm run build",
"test": "npm run lint && npm run test:unit:cover && npm run test:integration:cover", "test:pool": "mocha --config=test/unit/.mocharc.json --node-env=test --exit 'test/unit/NFTFactory.test.ts'",
"test:unit": "mocha --config=test/unit/.mocharc.json --node-env=test --exit 'test/unit/**/*.test.ts'", "test:unit": "mocha --config=test/unit/.mocharc.json --node-env=test --exit 'test/unit/**/*.test.ts'",
"test:unit:cover": "nyc --report-dir coverage/unit npm run test:unit", "test:unit:cover": "nyc --report-dir coverage/unit npm run test:unit",
"test:integration": "mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/**/*.test.ts'", "test:integration": "mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/**/*.test.ts'",
@ -46,7 +46,7 @@
}, },
"dependencies": { "dependencies": {
"@ethereum-navigator/navigator": "^0.5.3", "@ethereum-navigator/navigator": "^0.5.3",
"@oceanprotocol/contracts": "^0.6.9", "@oceanprotocol/contracts": "https://github.com/oceanprotocol/contracts#v4main",
"cross-fetch": "^3.1.4", "cross-fetch": "^3.1.4",
"crypto-js": "^4.0.0", "crypto-js": "^4.0.0",
"decimal.js": "^10.2.1", "decimal.js": "^10.2.1",

View File

@ -3,42 +3,82 @@ import { Contract } from 'web3-eth-contract'
import { AbiItem } from 'web3-utils/types' import { AbiItem } from 'web3-utils/types'
const communityCollector = '0xeE9300b7961e0a01d9f0adb863C7A227A07AaD75' const communityCollector = '0xeE9300b7961e0a01d9f0adb863C7A227A07AaD75'
const oceanAddress = '0x967da4048cd07ab37855c090aaf366e4ce1b9f48'
export class TestContractHandler { export class TestContractHandler {
public accounts: string[] public accounts: string[]
public ERC721Factory: Contract public ERC721Factory: Contract
public ERC20Template: Contract public ERC20Template: Contract
public ERC721Template: Contract public ERC721Template: Contract
public Router: Contract
public SideStaking: Contract
public FixedRate: Contract
public Dispenser: Contract
public OPFCollector: Contract
public PoolTemplate: Contract
public ERC721FactoryBytecode: string public ERC721FactoryBytecode: string
public ERC20TemplateBytecode: string public ERC20TemplateBytecode: string
public ERC721TemplateBytecode: string public ERC721TemplateBytecode: string
public RouterBytecode: string
public SideStakingBytecode: string
public FixedRateBytecode: string
public DispenserBytecode: string
public PoolTemplateBytecode: string
public OPFCollectorBytecode: string
public factory721Address: string public factory721Address: string
public template721Address: string public template721Address: string
public template20Address: string public template20Address: string
public routerAddress:string
public sideStakingAddress: string
public fixedRateAddress:string
public dispenserAddress: string
public poolTemplateAddress: string
public opfCollectorAddress: string
public web3: Web3 public web3: Web3
constructor( constructor(
ERC721FactoryABI: AbiItem | AbiItem[], web3: Web3,
ERC721TemplateABI: AbiItem | AbiItem[], ERC721TemplateABI: AbiItem | AbiItem[],
ERC20TemplateABI: AbiItem | AbiItem[], ERC20TemplateABI: AbiItem | AbiItem[],
factory721Bytecode: string, PoolTemplateABI?: AbiItem | AbiItem[],
template721Bytecode: string, ERC721FactoryABI?: AbiItem | AbiItem[],
template20Bytecode: string, RouterABI?: AbiItem | AbiItem[],
web3: Web3 SideStakingABI?: AbiItem | AbiItem[],
FixedRateABI?: AbiItem | AbiItem[],
DispenserABI?: AbiItem | AbiItem[],
template721Bytecode?: string,
template20Bytecode?: string,
poolTemplateBytecode?: string,
factory721Bytecode?: string,
routerBytecode?: string,
sideStakingBytecode?: string,
fixedRateBytecode?:string,
dispenserBytecode?: string,
) { ) {
this.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.ERC721Template = new this.web3.eth.Contract(ERC721TemplateABI)
this.ERC20Template = new this.web3.eth.Contract(ERC20TemplateABI)
this.PoolTemplate = new this.web3.eth.Contract(PoolTemplateABI)
this.ERC721Factory = new this.web3.eth.Contract(ERC721FactoryABI)
this.Router=new this.web3.eth.Contract(RouterABI)
this.SideStaking = new this.web3.eth.Contract(SideStakingABI)
this.FixedRate= new this.web3.eth.Contract(FixedRateABI)
this.Dispenser = new this.web3.eth.Contract(DispenserABI)
this.ERC721FactoryBytecode = factory721Bytecode this.ERC721FactoryBytecode = factory721Bytecode
this.ERC20TemplateBytecode = template20Bytecode this.ERC20TemplateBytecode = template20Bytecode
this.PoolTemplateBytecode = poolTemplateBytecode
this.ERC721TemplateBytecode = template721Bytecode this.ERC721TemplateBytecode = template721Bytecode
this.RouterBytecode = routerBytecode
this.SideStakingBytecode = sideStakingBytecode
this.FixedRateBytecode = fixedRateBytecode
this.DispenserBytecode = dispenserBytecode
} }
public async getAccounts(): Promise<string[]> { public async getAccounts(): Promise<string[]> {
@ -46,10 +86,35 @@ export class TestContractHandler {
return this.accounts return this.accounts
} }
public async deployContracts(minter: string) { public async deployContracts(owner: string) {
let estGas let estGas
// console.log(this.ERC721TemplateBytecode) // console.log(this.ERC721TemplateBytecode)
// console.log(this.ERC20TemplateBytecode) // console.log(this.ERC20TemplateBytecode)
// DEPLOY POOL TEMPLATE
// get est gascost
estGas = await this.PoolTemplate.deploy({
data: this.PoolTemplateBytecode,
arguments: []
}).estimateGas(function (err, estGas) {
if (err) console.log('DeployContracts: ' + err)
return estGas
})
// deploy the contract and get it's address
this.poolTemplateAddress = await this.PoolTemplate.deploy({
data: this.PoolTemplateBytecode,
arguments: []
})
.send({
from: owner,
gas: estGas + 1,
gasPrice: '3000000000'
})
.then(function (contract) {
return contract.options.address
})
// DEPLOY ERC20 TEMPLATE // DEPLOY ERC20 TEMPLATE
// get est gascost // get est gascost
estGas = await this.ERC20Template.deploy({ estGas = await this.ERC20Template.deploy({
@ -65,7 +130,7 @@ export class TestContractHandler {
arguments: [] arguments: []
}) })
.send({ .send({
from: minter, from: owner,
gas: estGas + 1, gas: estGas + 1,
gasPrice: '3000000000' gasPrice: '3000000000'
}) })
@ -73,6 +138,9 @@ export class TestContractHandler {
return contract.options.address return contract.options.address
}) })
// DEPLOY ERC721 TEMPLATE // DEPLOY ERC721 TEMPLATE
// get est gascost // get est gascost
estGas = await this.ERC721Template.deploy({ estGas = await this.ERC721Template.deploy({
@ -88,7 +156,29 @@ export class TestContractHandler {
arguments: [] arguments: []
}) })
.send({ .send({
from: minter, from: owner,
gas: estGas + 1,
gasPrice: '3000000000'
})
.then(function (contract) {
return contract.options.address
})
// DEPLOY ROUTER
estGas = await this.Router.deploy({
data: this.RouterBytecode,
arguments: [owner,oceanAddress,this.poolTemplateAddress,communityCollector,[]]
}).estimateGas(function (err, estGas) {
if (err) console.log('DeployContracts: ' + err)
return estGas
})
// deploy the contract and get it's address
this.routerAddress = await this.Router.deploy({
data: this.RouterBytecode,
arguments: [owner,oceanAddress,this.poolTemplateAddress,communityCollector,[]]
})
.send({
from: owner,
gas: estGas + 1, gas: estGas + 1,
gasPrice: '3000000000' gasPrice: '3000000000'
}) })
@ -97,11 +187,76 @@ export class TestContractHandler {
}) })
// DEPLOY SIDE STAKING
estGas = await this.SideStaking.deploy({
data: this.SideStakingBytecode,
arguments: [this.routerAddress]
}).estimateGas(function (err, estGas) {
if (err) console.log('DeployContracts: ' + err)
return estGas
})
// deploy the contract and get it's address
this.sideStakingAddress = await this.SideStaking.deploy({
data: this.SideStakingBytecode,
arguments: [this.routerAddress]
})
.send({
from: owner,
gas: estGas + 1,
gasPrice: '3000000000'
})
.then(function (contract) {
return contract.options.address
})
// DEPLOY FIXED RATE
estGas = await this.FixedRate.deploy({
data: this.FixedRateBytecode,
arguments: [this.routerAddress, communityCollector]
}).estimateGas(function (err, estGas) {
if (err) console.log('DeployContracts: ' + err)
return estGas
})
// deploy the contract and get it's address
this.fixedRateAddress = await this.FixedRate.deploy({
data: this.FixedRateBytecode,
arguments: [this.routerAddress, communityCollector]
})
.send({
from: owner,
gas: estGas + 1,
gasPrice: '3000000000'
})
.then(function (contract) {
return contract.options.address
})
// DEPLOY Dispenser
estGas = await this.Dispenser.deploy({
data: this.DispenserBytecode,
arguments: [this.routerAddress]
}).estimateGas(function (err, estGas) {
if (err) console.log('DeployContracts: ' + err)
return estGas
})
// deploy the contract and get it's address
this.dispenserAddress = await this.Dispenser.deploy({
data: this.DispenserBytecode,
arguments: [this.routerAddress]
})
.send({
from: owner,
gas: estGas + 1,
gasPrice: '3000000000'
})
.then(function (contract) {
return contract.options.address
})
// DEPLOY ERC721 FACTORY // DEPLOY ERC721 FACTORY
estGas = await this.ERC721Factory.deploy({ estGas = await this.ERC721Factory.deploy({
data: this.ERC721FactoryBytecode, data: this.ERC721FactoryBytecode,
arguments: [this.template721Address, communityCollector,this.factory20Address,this.metadataAddress] arguments: [this.template721Address, this.template20Address, communityCollector,this.routerAddress]
}).estimateGas(function (err, estGas) { }).estimateGas(function (err, estGas) {
if (err) console.log('DeployContracts: ' + err) if (err) console.log('DeployContracts: ' + err)
return estGas return estGas
@ -109,10 +264,10 @@ export class TestContractHandler {
// deploy the contract and get it's address // deploy the contract and get it's address
this.factory721Address = await this.ERC721Factory.deploy({ this.factory721Address = await this.ERC721Factory.deploy({
data: this.ERC721FactoryBytecode, data: this.ERC721FactoryBytecode,
arguments: [this.template721Address,communityCollector,this.factory20Address,this.metadataAddress] arguments: [this.template721Address, this.template20Address, communityCollector,this.routerAddress]
}) })
.send({ .send({
from: minter, from: owner,
gas: estGas + 1, gas: estGas + 1,
gasPrice: '3000000000' gasPrice: '3000000000'
}) })
@ -120,7 +275,6 @@ export class TestContractHandler {
return contract.options.address return contract.options.address
}) })
// // TODO: SET ERC721 Factory address in ERC20 Factory // // TODO: SET ERC721 Factory address in ERC20 Factory
} }

View File

@ -0,0 +1,9 @@
{
"compilerOptions": {
"resolveJsonModule": true,
"lib": ["es6", "es7"],
"noUnusedLocals": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
}
}

11
test/unit/.mocharc.json Normal file
View File

@ -0,0 +1,11 @@
{
"require": [
"ts-node/register",
"source-map-support/register",
"mock-local-storage"
],
"full-trace": true,
"bail": true,
"exit": true,
"timeout": "20000"
}

View File

@ -0,0 +1,108 @@
import { assert } from 'chai'
import { AbiItem } from 'web3-utils/types'
import { TestContractHandler } from '../TestContractHandler'
import Web3 from 'web3'
import ERC721Factory from '@oceanprotocol/contracts/artifacts/contracts/ERC721Factory.sol/ERC721Factory.json'
import ERC721Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC721Template.sol/ERC721Template.json'
import SideStaking from '@oceanprotocol/contracts/artifacts/contracts/pools/ssContracts/SideStaking.sol/SideStaking.json'
import Router from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json'
import ERC20Template from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json'
import Dispenser from '@oceanprotocol/contracts/artifacts/contracts/pools/dispenser/Dispenser.sol/Dispenser.json'
import FixedRate from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json'
import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json'
// import { NFTDataToken } from '../../../src/datatokens/NFTDatatoken'
// import { NFTFactory } from '../../../src/factories/NFTFactory'
// import { DT20Factory } from '../../../src/factories/DT20Factory'
const web3 = new Web3('http://127.0.0.1:8545')
describe('NFT Factory test', () => {
let factoryOwner: string
let nftOwner: string
let user1: string
let user2: string
let contracts: TestContractHandler
// let nftDatatoken: NFTDataToken
// let nftFactory: NFTFactory
// let erc20Factory: DT20Factory
// let nftAddress: string
// let newNFTAddress: string
// let newNFTDatatoken: NFTDataToken
const nftName = 'NFT'
const nftSymbol = 'NFTSymbol'
const nftTemplateIndex = 1
const data = web3.utils.asciiToHex('SomeData')
const flags = web3.utils.asciiToHex(
'f8929916089218bdb4aa78c3ecd16633afd44b8aef89299160'
)
// TODO: complete unit test
it('should deploy contracts', async () => {
contracts = new TestContractHandler(
web3,
ERC721Template.abi as AbiItem[],
ERC20Template.abi as AbiItem[],
PoolTemplate.abi as AbiItem[],
ERC721Factory.abi as AbiItem[],
Router.abi as AbiItem[],
SideStaking.abi as AbiItem[],
FixedRate.abi as AbiItem[],
Dispenser.abi as AbiItem[],
ERC721Template.bytecode,
ERC20Template.bytecode,
PoolTemplate.bytecode,
ERC721Factory.bytecode,
Router.bytecode,
SideStaking.bytecode,
FixedRate.bytecode,
Dispenser.bytecode,
)
await contracts.getAccounts()
factoryOwner = contracts.accounts[0]
nftOwner = contracts.accounts[1]
user1 = contracts.accounts[2]
console.log(factoryOwner)
await contracts.deployContracts(factoryOwner)
console.log('BOOM')
})
// it('should set ERC721Factory on ERC20Factory', async () => {
// erc20Factory = new DT20Factory(
// contracts.factory20Address,
// //ERC20Factory.abi as AbiItem[],
// web3,
// LoggerInstance
// )
// await erc20Factory.setERC721Factory(factoryOwner, contracts.factory721Address)
// })
// it('should initialize NFTFactory instance, create a new NFT and initializing a NFT dt class', async () => {
// nftFactory = new NFTFactory(
// contracts.factory721Address,
// web3,
// LoggerInstance
// // ERC721Factory.abi as AbiItem[],
// )
// nftAddress = await nftFactory.createNFT(
// nftOwner,
// data,
// flags,
// nftName,
// nftSymbol,
// nftTemplateIndex
// )
// //console.log(newNFTAddress)
// nftDatatoken = new NFTDataToken(
// web3,
// LoggerInstance
// // ERC721Template.abi as AbiItem[],
// )
})

9
test/unit/tsconfig.json Normal file
View File

@ -0,0 +1,9 @@
{
"compilerOptions": {
"resolveJsonModule": true,
"lib": ["es6", "es7"],
"noUnusedLocals": false,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
}
}