diff --git a/src/keeper/ContractHandler.ts b/src/keeper/ContractHandler.ts index c3a770e..d9278a3 100644 --- a/src/keeper/ContractHandler.ts +++ b/src/keeper/ContractHandler.ts @@ -1,4 +1,4 @@ -import Contract from "web3" +import {Contract} from "web3-utils" import Logger from "../utils/Logger" import Web3Helper from "./Web3Helper" diff --git a/src/keeper/ContractWrapperBase.ts b/src/keeper/ContractWrapperBase.ts index cc1110f..a25772a 100644 --- a/src/keeper/ContractWrapperBase.ts +++ b/src/keeper/ContractWrapperBase.ts @@ -1,4 +1,5 @@ import Config from "../models/Config" +import Logger from "../utils/Logger" import ContractHandler from "./ContractHandler" import Web3Helper from "./Web3Helper" @@ -16,6 +17,37 @@ export default class ContractWrapperBase { this.web3Helper = web3Helper } + public async listenToEventOnce(eventName: string, options: any): Promise { + return new Promise((resolve, reject) => { + if (!this.contract.events[eventName]) { + throw new Error(`Event ${eventName} not found on contract ${this.contractName}`) + } + this.contract.once(eventName, options, (error: any, eventData: any) => { + if (error) { + Logger.log(`Error in keeper ${eventName} event: `, error) + return reject(error) + } + resolve(eventData) + }) + }) + } + + public async getEventData(eventName: any, options: any): Promise { + return new Promise((resolve, reject) => { + if (!this.contract.events[eventName]) { + throw new Error(`Event ${eventName} not found on contract ${this.contractName}`) + } + this.contract.events[eventName](options) + .on("data", (eventData: any[]) => { + Logger.log(eventData) + resolve(eventData) + }) + .on("error", (error) => { + return reject(error) + }) + }) + } + public async init() { this.contract = await ContractHandler.get(this.contractName, this.web3Helper) } @@ -24,10 +56,4 @@ export default class ContractWrapperBase { return this.contract.address } - public getEvent(name: string) { - if (!this.contract.events[name]) { - throw new Error(`Event ${name} not found on contract ${this.contractName}`) - } - return this.contract.events[name] - } } diff --git a/src/keeper/EventListener.ts b/src/keeper/EventListener.ts deleted file mode 100644 index 5080147..0000000 --- a/src/keeper/EventListener.ts +++ /dev/null @@ -1,30 +0,0 @@ -import Logger from "../utils/Logger" - -export default class EventListener { - - public static async listenOnce(event: any, eventName: string): Promise { - return new Promise((resolve, reject) => { - event.watch((error: any, result: any) => { - event.stopWatching() - if (error) { - Logger.log(`Error in keeper ${eventName} event: `, error) - return reject(error) - } - resolve(result) - }) - }) - } - - public static getEvents(event: any): Promise { - return new Promise((resolve, reject) => { - event.get((error: any, logs: any[]) => { - if (error) { - reject(error) - throw new Error(error) - } else { - resolve(logs) - } - }) - }) - } -} diff --git a/src/keeper/Web3Helper.ts b/src/keeper/Web3Helper.ts index 508765d..b33f44c 100644 --- a/src/keeper/Web3Helper.ts +++ b/src/keeper/Web3Helper.ts @@ -1,5 +1,9 @@ -import Web3 = require("web3") +import * as Web3 from "web3" +import * as web3pkg from "../../node_modules/web3/package.json" import Config from "../models/Config" +import Logger from "../utils/Logger" + +Logger.log(web3pkg.version) export default class Web3Helper { @@ -7,7 +11,7 @@ export default class Web3Helper { public constructor(config: Config) { const web3Provider = config.web3Provider || new Web3.providers.HttpProvider(config.nodeUri) - this.web3 = new Web3(web3Provider) + this.web3 = new Web3(Web3.givenProvider || web3Provider) } public getWeb3() { diff --git a/src/ocean/Order.ts b/src/ocean/Order.ts index 3127b22..147beb4 100644 --- a/src/ocean/Order.ts +++ b/src/ocean/Order.ts @@ -2,7 +2,6 @@ import * as EthCrypto from "eth-crypto" import EthEcies from "eth-ecies" import * as EthjsUtil from "ethereumjs-util" import JWT from "jsonwebtoken" -import EventListener from "../keeper/EventListener" import Keeper from "../keeper/Keeper" import Asset from "../models/Asset" import OrderModel from "../models/Order" @@ -34,24 +33,30 @@ export default class Order { public async getOrdersByConsumer(consumerAddress: string) { const {auth, market} = this.keeper - const accessConsentEvent = auth.getEvent("AccessConsentRequested")({ - _consumer: consumerAddress, - }, { + + const accessConsentRequestedData = await auth.getEventData("AccessConsentRequested", { + filter: { + _consumer: consumerAddress, + }, fromBlock: 0, - toBlock: "latest", }) - const events = await EventListener.getEvents(accessConsentEvent) + Logger.log("wai", accessConsentRequestedData) + + const orders = await Promise.all( + accessConsentRequestedData + .filter((event: any) => (event.args._consumer === consumerAddress)) + .map(async (event: any) => ({ + ...event.args, + timeout: event.args._timeout.toNumber(), + status: await auth.getOrderStatus(event.args._id), + paid: await market.verifyOrderPayment(event.args._id), + key: null, + } as Order + ), + ), + ) - const orders = await Promise.all(events - .filter((event: any) => (event.args._consumer === consumerAddress)) - .map(async (event: any) => ({ - ...event.args, - timeout: event.args._timeout.toNumber(), - status: await auth.getOrderStatus(event.args._id), - paid: await market.verifyOrderPayment(event.args._id), - key: null, - } as Order))) Logger.log("Got orders: ", orders) return orders } @@ -81,36 +86,33 @@ export default class Order { } catch (err) { Logger.log("initiateAccessRequest", err) } - const resourceFilter = { - _resourceId: asset.assetId, - _consumer: buyerAddress, - } - // todo: Event - implement proper eventing - const accessConsentRequestedEvent = auth.getEvent("AccessConsentRequested")(resourceFilter) + let order: OrderModel - const finalOrder: OrderModel = await EventListener.listenOnce( - accessConsentRequestedEvent, - "AccessConsentRequested") + const finalOrder: OrderModel = await auth.listenToEventOnce( + "AccessConsentRequested", { + filter: { + _resourceId: asset.assetId, + _consumer: buyerAddress, + }, + }) .then((accessConsentRequestedResult) => { order = Order.create(asset, accessConsentRequestedResult.args, key) - const requestIdFilter = { - _id: order.id, - } - // todo: Event - implement proper eventing - const accessCommittedEvent = auth.getEvent("AccessRequestCommitted")(requestIdFilter) - return EventListener.listenOnce(accessCommittedEvent, "AccessRequestCommitted") + return auth.listenToEventOnce("AccessRequestCommitted", { + filter: { + _id: order.id, + }, + }) }) .then((accessRequestCommittedResult) => { return this.payAsset(asset, accessRequestCommittedResult.args, order, buyerAddress) }) .then(() => { - const requestIdFilter = { - _id: order.id, - } - // todo: Event - implement proper eventing - const tokenPublishedEvent = auth.getEvent("EncryptedTokenPublished")(requestIdFilter) - return EventListener.listenOnce(tokenPublishedEvent, "EncryptedTokenPublished") + return auth.listenToEventOnce("EncryptedTokenPublished", { + filter: { + _id: order.id, + }, + }) }) .then((result) => { return this.finalizePurchaseAsset(