2018-10-09 10:55:53 +02:00
|
|
|
import Event from "web3"
|
2018-10-05 12:34:31 +02:00
|
|
|
import Contract from "web3-eth-contract"
|
2018-10-02 10:06:26 +02:00
|
|
|
import Config from "../models/Config"
|
|
|
|
import ContractHandler from "./ContractHandler"
|
|
|
|
import Web3Helper from "./Web3Helper"
|
2018-10-01 18:10:26 +02:00
|
|
|
|
|
|
|
export default class ContractWrapperBase {
|
|
|
|
|
2018-10-09 10:55:53 +02:00
|
|
|
public static async getInstance(config: Config, web3Helper: Web3Helper): Promise<any> {
|
|
|
|
// stub
|
|
|
|
}
|
|
|
|
|
2018-10-05 12:34:31 +02:00
|
|
|
protected contract: Contract = null
|
2018-10-02 10:06:26 +02:00
|
|
|
protected config: Config
|
|
|
|
protected web3Helper: Web3Helper
|
2018-10-01 18:10:26 +02:00
|
|
|
|
2018-10-02 10:06:26 +02:00
|
|
|
private contractName: string
|
2018-10-01 18:10:26 +02:00
|
|
|
|
|
|
|
constructor(config: Config, contractName: string, web3Helper: Web3Helper) {
|
2018-10-02 10:06:26 +02:00
|
|
|
this.config = config
|
|
|
|
this.contractName = contractName
|
|
|
|
this.web3Helper = web3Helper
|
2018-10-01 18:10:26 +02:00
|
|
|
}
|
|
|
|
|
2018-10-04 13:14:03 +02:00
|
|
|
public async listenToEventOnce(eventName: string, options: any): Promise<any> {
|
|
|
|
return new Promise<any>((resolve, reject) => {
|
|
|
|
if (!this.contract.events[eventName]) {
|
|
|
|
throw new Error(`Event ${eventName} not found on contract ${this.contractName}`)
|
|
|
|
}
|
2018-10-09 10:55:53 +02:00
|
|
|
this.contract.events[eventName](options, (err) => {
|
|
|
|
reject(err)
|
2018-10-04 13:14:03 +02:00
|
|
|
})
|
2018-10-09 10:55:53 +02:00
|
|
|
.on("data", (eventData: any) => {
|
2018-10-04 13:14:03 +02:00
|
|
|
resolve(eventData)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-09 10:55:53 +02:00
|
|
|
public async getEventData(eventName: any, options: any): Promise<Event[]> {
|
|
|
|
if (!this.contract.events[eventName]) {
|
|
|
|
throw new Error(`Event ${eventName} not found on contract ${this.contractName}`)
|
|
|
|
}
|
|
|
|
return this.contract.getPastEvents(eventName, options)
|
2018-10-01 18:10:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public getAddress() {
|
2018-10-05 12:34:31 +02:00
|
|
|
return this.contract.options.address
|
2018-10-01 18:10:26 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 10:55:53 +02:00
|
|
|
protected async init() {
|
|
|
|
this.contract = await ContractHandler.get(this.contractName, this.web3Helper)
|
|
|
|
}
|
|
|
|
|
2018-10-01 18:10:26 +02:00
|
|
|
}
|