1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00
ocean.js/test/DispenserContractHandler.ts
Alex Coseru 92b4be0dce
Feature/dispenser (#790)
* add dispenser support

* bump contracts to 0.6.2
2021-05-10 18:42:19 +03:00

51 lines
1.3 KiB
TypeScript

import Web3 from 'web3'
import { Contract } from 'web3-eth-contract'
import { AbiItem } from 'web3-utils/types'
export class DispenserContractHandler {
public contract: Contract
public accounts: string[]
public contractBytecode: string
public contractAddress: string
public web3: Web3
constructor(contractABI: AbiItem[], contractBytecode: string, web3: Web3) {
this.web3 = web3
this.contract = new this.web3.eth.Contract(contractABI)
this.contractBytecode = contractBytecode
}
public async getAccounts(): Promise<string[]> {
this.accounts = await this.web3.eth.getAccounts()
return this.accounts
}
public async deployContracts() {
await this.getAccounts()
// get est gascost
const 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
})
}
}