mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
reworked event handling
This commit is contained in:
parent
d8b9248bad
commit
fbeae84eda
@ -1,4 +1,3 @@
|
|||||||
import BigNumber from "bignumber.js"
|
|
||||||
import {Receipt} from "web3-utils"
|
import {Receipt} from "web3-utils"
|
||||||
import Asset from "../models/Asset"
|
import Asset from "../models/Asset"
|
||||||
import Config from "../models/Config"
|
import Config from "../models/Config"
|
||||||
@ -7,8 +6,8 @@ import Web3Helper from "./Web3Helper"
|
|||||||
|
|
||||||
export default class OceanAuth extends ContractBaseWrapper {
|
export default class OceanAuth extends ContractBaseWrapper {
|
||||||
|
|
||||||
public static async getInstance(config: Config, web3Helper: Web3Helper) {
|
public static async getInstance(config: Config, web3Helper: Web3Helper): Promise<OceanAuth> {
|
||||||
const auth = new OceanAuth(config, "OceanAuth", web3Helper)
|
const auth: OceanAuth = new OceanAuth(config, "OceanAuth", web3Helper)
|
||||||
await auth.init()
|
await auth.init()
|
||||||
return auth
|
return auth
|
||||||
}
|
}
|
||||||
@ -16,7 +15,7 @@ export default class OceanAuth extends ContractBaseWrapper {
|
|||||||
public async getOrderStatus(orderId: string): Promise<number> {
|
public async getOrderStatus(orderId: string): Promise<number> {
|
||||||
return this.contract.methods.statusOfAccessRequest(orderId)
|
return this.contract.methods.statusOfAccessRequest(orderId)
|
||||||
.call()
|
.call()
|
||||||
.then((status: BigNumber) => status.toNumber())
|
.then((status: string) => parseInt(status, 10))
|
||||||
}
|
}
|
||||||
|
|
||||||
public async cancelAccessRequest(orderId: string, senderAddress: string): Promise<Receipt> {
|
public async cancelAccessRequest(orderId: string, senderAddress: string): Promise<Receipt> {
|
||||||
|
@ -53,7 +53,7 @@ export default class ContractHandler {
|
|||||||
try {
|
try {
|
||||||
const artifact = require(`@oceanprotocol/keeper-contracts/artifacts/${what}.${where}`)
|
const artifact = require(`@oceanprotocol/keeper-contracts/artifacts/${what}.${where}`)
|
||||||
// Logger.log('Loaded artifact', artifact)
|
// Logger.log('Loaded artifact', artifact)
|
||||||
Logger.log("Getting instance of", what, "from", where, "at", artifact.address)
|
// Logger.log("Getting instance of", what, "from", where, "at", artifact.address)
|
||||||
const web3 = web3Helper.getWeb3()
|
const web3 = web3Helper.getWeb3()
|
||||||
const contract = new web3.eth.Contract(artifact.abi, artifact.address)
|
const contract = new web3.eth.Contract(artifact.abi, artifact.address)
|
||||||
Logger.log("Loaded", what, "from", where)
|
Logger.log("Loaded", what, "from", where)
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import Event from "web3"
|
||||||
import Contract from "web3-eth-contract"
|
import Contract from "web3-eth-contract"
|
||||||
import Config from "../models/Config"
|
import Config from "../models/Config"
|
||||||
import Logger from "../utils/Logger"
|
import Logger from "../utils/Logger"
|
||||||
@ -6,6 +7,10 @@ import Web3Helper from "./Web3Helper"
|
|||||||
|
|
||||||
export default class ContractWrapperBase {
|
export default class ContractWrapperBase {
|
||||||
|
|
||||||
|
public static async getInstance(config: Config, web3Helper: Web3Helper): Promise<any> {
|
||||||
|
// stub
|
||||||
|
}
|
||||||
|
|
||||||
protected contract: Contract = null
|
protected contract: Contract = null
|
||||||
protected config: Config
|
protected config: Config
|
||||||
protected web3Helper: Web3Helper
|
protected web3Helper: Web3Helper
|
||||||
@ -23,38 +28,28 @@ export default class ContractWrapperBase {
|
|||||||
if (!this.contract.events[eventName]) {
|
if (!this.contract.events[eventName]) {
|
||||||
throw new Error(`Event ${eventName} not found on contract ${this.contractName}`)
|
throw new Error(`Event ${eventName} not found on contract ${this.contractName}`)
|
||||||
}
|
}
|
||||||
this.contract.once(eventName, options, (error: any, eventData: any) => {
|
this.contract.events[eventName](options, (err) => {
|
||||||
if (error) {
|
reject(err)
|
||||||
Logger.log(`Error in keeper ${eventName} event: `, error)
|
|
||||||
return reject(error)
|
|
||||||
}
|
|
||||||
resolve(eventData)
|
|
||||||
})
|
})
|
||||||
})
|
.on("data", (eventData: any) => {
|
||||||
}
|
|
||||||
|
|
||||||
public async getEventData(eventName: any, 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}`)
|
|
||||||
}
|
|
||||||
this.contract.events[eventName](options)
|
|
||||||
.on("data", (eventData: any[]) => {
|
|
||||||
Logger.log(eventData)
|
|
||||||
resolve(eventData)
|
resolve(eventData)
|
||||||
})
|
})
|
||||||
.on("error", (error) => {
|
|
||||||
return reject(error)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
public async init() {
|
public async getEventData(eventName: any, options: any): Promise<Event[]> {
|
||||||
this.contract = await ContractHandler.get(this.contractName, this.web3Helper)
|
if (!this.contract.events[eventName]) {
|
||||||
|
throw new Error(`Event ${eventName} not found on contract ${this.contractName}`)
|
||||||
|
}
|
||||||
|
return this.contract.getPastEvents(eventName, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
public getAddress() {
|
public getAddress() {
|
||||||
return this.contract.options.address
|
return this.contract.options.address
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected async init() {
|
||||||
|
this.contract = await ContractHandler.get(this.contractName, this.web3Helper)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,9 +9,8 @@ import Web3Helper from "./Web3Helper"
|
|||||||
|
|
||||||
export default class OceanMarket extends ContractWrapperBase {
|
export default class OceanMarket extends ContractWrapperBase {
|
||||||
|
|
||||||
public static async getInstance(config: Config, web3Helper: Web3Helper) {
|
public static async getInstance(config: Config, web3Helper: Web3Helper): Promise<OceanMarket> {
|
||||||
|
const market: OceanMarket = new OceanMarket(config, "OceanMarket", web3Helper)
|
||||||
const market = new OceanMarket(config, "OceanMarket", web3Helper)
|
|
||||||
await market.init()
|
await market.init()
|
||||||
return market
|
return market
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,8 @@ import Web3Helper from "./Web3Helper"
|
|||||||
|
|
||||||
export default class OceanToken extends ContractBaseWrapper {
|
export default class OceanToken extends ContractBaseWrapper {
|
||||||
|
|
||||||
public static async getInstance(config: Config, web3Helper: Web3Helper) {
|
public static async getInstance(config: Config, web3Helper: Web3Helper): Promise<OceanToken> {
|
||||||
const token = new OceanToken(config, "OceanToken", web3Helper)
|
const token: OceanToken = new OceanToken(config, "OceanToken", web3Helper)
|
||||||
await token.init()
|
await token.init()
|
||||||
return token
|
return token
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,8 @@
|
|||||||
import BigNumber from "bignumber.js"
|
import BigNumber from "bignumber.js"
|
||||||
import Keeper from "../keeper/Keeper"
|
import Keeper from "../keeper/Keeper"
|
||||||
import Web3Helper from "../keeper/Web3Helper"
|
import OceanBase from "./OceanBase"
|
||||||
|
|
||||||
export default class Account {
|
export default class Account extends OceanBase {
|
||||||
|
|
||||||
private keeper: Keeper
|
|
||||||
|
|
||||||
constructor(keeper: Keeper) {
|
|
||||||
this.keeper = keeper
|
|
||||||
}
|
|
||||||
|
|
||||||
public async getTokenBalance(accountAddress: string): Promise<number> {
|
public async getTokenBalance(accountAddress: string): Promise<number> {
|
||||||
return this.keeper.token.balanceOf(accountAddress)
|
return this.keeper.token.balanceOf(accountAddress)
|
||||||
|
@ -1,13 +1,8 @@
|
|||||||
import Keeper from "../keeper/Keeper"
|
import Keeper from "../keeper/Keeper"
|
||||||
import Logger from "../utils/Logger"
|
import Logger from "../utils/Logger"
|
||||||
|
import OceanBase from "./OceanBase"
|
||||||
|
|
||||||
export default class Asset {
|
export default class Asset extends OceanBase {
|
||||||
|
|
||||||
private keeper: Keeper
|
|
||||||
|
|
||||||
constructor(keeper: Keeper) {
|
|
||||||
this.keeper = keeper
|
|
||||||
}
|
|
||||||
|
|
||||||
public async isAssetActive(assetId: string): Promise<boolean> {
|
public async isAssetActive(assetId: string): Promise<boolean> {
|
||||||
const {market} = this.keeper
|
const {market} = this.keeper
|
||||||
|
10
src/ocean/OceanBase.ts
Normal file
10
src/ocean/OceanBase.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import Keeper from "../keeper/Keeper"
|
||||||
|
|
||||||
|
export default class OceanBase {
|
||||||
|
|
||||||
|
protected keeper: Keeper
|
||||||
|
|
||||||
|
constructor(keeper: Keeper) {
|
||||||
|
this.keeper = keeper
|
||||||
|
}
|
||||||
|
}
|
@ -2,17 +2,16 @@ import * as EthCrypto from "eth-crypto"
|
|||||||
import EthEcies from "eth-ecies"
|
import EthEcies from "eth-ecies"
|
||||||
import * as EthjsUtil from "ethereumjs-util"
|
import * as EthjsUtil from "ethereumjs-util"
|
||||||
import JWT from "jsonwebtoken"
|
import JWT from "jsonwebtoken"
|
||||||
import Keeper from "../keeper/Keeper"
|
|
||||||
import Asset from "../models/Asset"
|
import Asset from "../models/Asset"
|
||||||
import OrderModel from "../models/Order"
|
import OrderModel from "../models/Order"
|
||||||
import Logger from "../utils/Logger"
|
import Logger from "../utils/Logger"
|
||||||
|
import OceanBase from "./OceanBase"
|
||||||
|
|
||||||
declare var fetch
|
declare var fetch
|
||||||
|
|
||||||
export default class Order {
|
export default class Order extends OceanBase {
|
||||||
|
|
||||||
private static create(asset: Asset, args, key): OrderModel {
|
private static create(asset: Asset, args, key): OrderModel {
|
||||||
Logger.log("keeper AccessConsentRequested event received on asset: ", asset.assetId, "\nevent:", args)
|
|
||||||
const accessId = args._id
|
const accessId = args._id
|
||||||
Logger.log("got new access request id: ", accessId)
|
Logger.log("got new access request id: ", accessId)
|
||||||
return {
|
return {
|
||||||
@ -25,39 +24,39 @@ export default class Order {
|
|||||||
} as OrderModel
|
} as OrderModel
|
||||||
}
|
}
|
||||||
|
|
||||||
private keeper: Keeper
|
|
||||||
|
|
||||||
constructor(keeper: Keeper) {
|
|
||||||
this.keeper = keeper
|
|
||||||
}
|
|
||||||
|
|
||||||
public async getOrdersByConsumer(consumerAddress: string) {
|
public async getOrdersByConsumer(consumerAddress: string) {
|
||||||
const {auth, market} = this.keeper
|
const {auth, market} = this.keeper
|
||||||
|
|
||||||
const accessConsentRequestedData = await auth.getEventData("AccessConsentRequested", {
|
Logger.log("Getting orders")
|
||||||
filter: {
|
|
||||||
_consumer: consumerAddress,
|
|
||||||
},
|
|
||||||
fromBlock: 0,
|
|
||||||
})
|
|
||||||
|
|
||||||
Logger.log("wai", accessConsentRequestedData)
|
const accessConsentRequestedData = await auth.getEventData(
|
||||||
|
"AccessConsentRequested", {
|
||||||
|
filter: {
|
||||||
|
_consumer: consumerAddress,
|
||||||
|
},
|
||||||
|
fromBlock: 0,
|
||||||
|
toBlock: "latest",
|
||||||
|
})
|
||||||
|
|
||||||
const orders = await Promise.all(
|
const orders = await Promise.all(
|
||||||
accessConsentRequestedData
|
accessConsentRequestedData
|
||||||
.filter((event: any) => (event.args._consumer === consumerAddress))
|
.filter((event: any) => {
|
||||||
|
return event.returnValues._consumer === consumerAddress
|
||||||
|
})
|
||||||
.map(async (event: any) => ({
|
.map(async (event: any) => ({
|
||||||
...event.args,
|
...event.returnValues,
|
||||||
timeout: event.args._timeout.toNumber(),
|
timeout: parseInt(event.returnValues._timeout, 10),
|
||||||
status: await auth.getOrderStatus(event.args._id),
|
status: await auth.getOrderStatus(event.returnValues._id),
|
||||||
paid: await market.verifyOrderPayment(event.args._id),
|
paid: await market.verifyOrderPayment(event.returnValues._id),
|
||||||
key: null,
|
key: null,
|
||||||
} as Order
|
} as Order
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
Logger.log("Got orders: ", orders)
|
// Logger.log("Got orders:", JSON.stringify(orders, null, 2))
|
||||||
|
Logger.log(`Got ${Object.keys(orders).length} orders`)
|
||||||
|
|
||||||
return orders
|
return orders
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,49 +77,53 @@ export default class Order {
|
|||||||
// Allow market contract to transfer funds on the consumer"s behalf
|
// Allow market contract to transfer funds on the consumer"s behalf
|
||||||
await token.approve(market.getAddress(), price, buyerAddress)
|
await token.approve(market.getAddress(), price, buyerAddress)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
Logger.log("token approve", err)
|
Logger.error("token.approve failed", err)
|
||||||
}
|
}
|
||||||
|
let order: OrderModel
|
||||||
try {
|
try {
|
||||||
// Submit the access request
|
// Submit the access request
|
||||||
await auth.initiateAccessRequest(asset, publicKey, timeout, buyerAddress)
|
const initiateAccessRequestReceipt = await auth.initiateAccessRequest(asset,
|
||||||
|
publicKey, timeout, buyerAddress)
|
||||||
|
|
||||||
|
const args = initiateAccessRequestReceipt.events.AccessConsentRequested.returnValues
|
||||||
|
Logger.log("keeper AccessConsentRequested event received on asset: ", asset.assetId, "\nevent:", args)
|
||||||
|
order = Order.create(asset, args, key)
|
||||||
|
Logger.log("Created order", order)
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
Logger.log("initiateAccessRequest", err)
|
Logger.error("auth.initiateAccessRequest failed", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
let order: OrderModel
|
return order
|
||||||
const finalOrder: OrderModel = await auth.listenToEventOnce(
|
if (false) {
|
||||||
"AccessConsentRequested", {
|
// todo: AccessRequestCommitted event is not emitted in this flow
|
||||||
filter: {
|
const finalOrder: OrderModel = await auth.listenToEventOnce(
|
||||||
_resourceId: asset.assetId,
|
"AccessRequestCommitted", {
|
||||||
_consumer: buyerAddress,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.then((accessConsentRequestedResult) => {
|
|
||||||
order = Order.create(asset, accessConsentRequestedResult.args, key)
|
|
||||||
|
|
||||||
return auth.listenToEventOnce("AccessRequestCommitted", {
|
|
||||||
filter: {
|
filter: {
|
||||||
_id: order.id,
|
_id: order.id,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
})
|
.then((accessRequestCommittedResult) => {
|
||||||
.then((accessRequestCommittedResult) => {
|
Logger.log("Got AccessRequestCommitted Event")
|
||||||
return this.payAsset(asset, accessRequestCommittedResult.args, order, buyerAddress)
|
|
||||||
})
|
|
||||||
.then(() => {
|
|
||||||
return auth.listenToEventOnce("EncryptedTokenPublished", {
|
|
||||||
filter: {
|
|
||||||
_id: order.id,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.then((result) => {
|
|
||||||
return this.finalizePurchaseAsset(
|
|
||||||
result, order, key, buyerAddress,
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
return finalOrder
|
return this.payAsset(asset, accessRequestCommittedResult.returnValues, order, buyerAddress)
|
||||||
|
})
|
||||||
|
.then((payAssetReceipt) => {
|
||||||
|
return auth.listenToEventOnce(
|
||||||
|
"EncryptedTokenPublished", {
|
||||||
|
filter: {
|
||||||
|
_id: order.id,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.then((result) => {
|
||||||
|
Logger.log("Got EncryptedTokenPublished Event")
|
||||||
|
|
||||||
|
return this.finalizePurchaseAsset(
|
||||||
|
result, order, key, buyerAddress,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async payAsset(asset: Asset, args, order, buyerAddress) {
|
private async payAsset(asset: Asset, args, order, buyerAddress) {
|
||||||
@ -134,8 +137,6 @@ export default class Order {
|
|||||||
private async finalizePurchaseAsset(args, order, key, buyerAddress): Promise<OrderModel> {
|
private async finalizePurchaseAsset(args, order, key, buyerAddress): Promise<OrderModel> {
|
||||||
const {auth, web3Helper} = this.keeper
|
const {auth, web3Helper} = this.keeper
|
||||||
|
|
||||||
// Logger.log('keeper EncryptedTokenPublished event received: ', order.id, eventResult.args)
|
|
||||||
|
|
||||||
const encryptedAccessToken = await auth.getEncryptedAccessToken(args._id, buyerAddress)
|
const encryptedAccessToken = await auth.getEncryptedAccessToken(args._id, buyerAddress)
|
||||||
|
|
||||||
// grab the access token from acl contract
|
// grab the access token from acl contract
|
||||||
|
Loading…
Reference in New Issue
Block a user