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

89 lines
2.4 KiB
TypeScript

import Web3 from 'web3'
import { Contract } from 'web3-eth-contract'
import { AbiItem } from 'web3-utils/types'
export class TestContractHandler {
public factory: Contract
public template: Contract
public accounts: string[]
public templateBytecode: string
public factoryBytecode: string
public factoryAddress: string
public templateAddress: string
public web3: Web3
constructor(
factoryABI: AbiItem | AbiItem[],
datatokensABI: AbiItem | AbiItem[],
templateBytecode: string,
factoryBytecode: string,
web3: Web3
) {
this.web3 = web3
this.factory = new this.web3.eth.Contract(factoryABI)
this.template = new this.web3.eth.Contract(datatokensABI)
this.templateBytecode = templateBytecode
this.factoryBytecode = factoryBytecode
}
public async getAccounts() {
this.accounts = await this.web3.eth.getAccounts()
}
public async deployContracts(minter: string) {
let estGas
const blob = 'https://example.com/dataset-1'
const cap = 1400000000
// get est gascost
estGas = await this.template
.deploy({
data: this.templateBytecode,
arguments: ['Template Contract', 'TEMPLATE', minter, cap, blob]
})
.estimateGas(function (err, estGas) {
if (err) console.log('DeployContracts: ' + err)
return estGas
})
// deploy the contract and get it's address
this.templateAddress = await this.template
.deploy({
data: this.templateBytecode,
arguments: ['Template Contract', 'TEMPLATE', minter, cap, blob]
})
.send({
from: minter,
gas: estGas + 1,
gasPrice: '3000000000'
})
.then(function (contract) {
return contract.options.address
})
estGas = await this.factory
.deploy({
data: this.factoryBytecode,
arguments: [this.templateAddress]
})
.estimateGas(function (err, estGas) {
if (err) console.log('DeployContracts: ' + err)
return estGas
})
// deploy the contract and get it's address
this.factoryAddress = await this.factory
.deploy({
data: this.factoryBytecode,
arguments: [this.templateAddress]
})
.send({
from: minter,
gas: estGas + 1,
gasPrice: '3000000000'
})
.then(function (contract) {
return contract.options.address
})
}
}