1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00
ocean.js/test/FixedPriceContractHandler.ts
2020-08-27 03:29:00 -07:00

59 lines
1.3 KiB
TypeScript

import Web3 from 'web3'
import { Contract } from 'web3-eth-contract'
import { AbiItem } from 'web3-utils/types'
export class FixedPricedContractHandler {
public contract: Contract
public accounts: string[]
public contractBytecode: string
public contractAddress: string
public web3: Web3
constructor(
contractABI: AbiItem | AbiItem[],
contractBytecode: string,
web3: Web3
) {
this.web3 = web3
this.contract = new this.web3.eth.Contract(contractABI)
this.contractBytecode = contractBytecode
}
public async getAccounts() {
this.accounts = await this.web3.eth.getAccounts()
}
public async deployContracts() {
let estGas
await this.getAccounts()
// get est gascost
estGas = await this.contract
.deploy({
data: this.contractBytecode,
arguments: []
})
.estimateGas(function (err, estGas) {
if (err) console.log('DeployContracts: ' + err)
return estGas
})
// deploy the contract and get it's address
this.contractAddress = await this.contract
.deploy({
data: this.contractBytecode,
arguments: []
})
.send({
from: this.accounts[0],
gas: estGas + 1,
gasPrice: '3000000000'
})
.then(function (contract) {
return contract.options.address
})
}
}