2018-10-16 14:56:18 +02:00
|
|
|
import ConfigProvider from "../ConfigProvider"
|
2018-10-05 12:34:31 +02:00
|
|
|
import Keeper from "../keeper/Keeper"
|
2018-10-16 14:56:18 +02:00
|
|
|
import Web3Provider from "../keeper/Web3Provider"
|
|
|
|
import Logger from "../utils/Logger"
|
2018-10-05 12:34:31 +02:00
|
|
|
import Account from "./Account"
|
|
|
|
import Asset from "./Asset"
|
|
|
|
import Order from "./Order"
|
|
|
|
|
|
|
|
export default class Ocean {
|
|
|
|
|
|
|
|
public static async getInstance(config) {
|
2018-10-16 14:56:18 +02:00
|
|
|
|
|
|
|
if (!Ocean.instance) {
|
|
|
|
ConfigProvider.configure(config)
|
|
|
|
Ocean.instance = new Ocean()
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ocean.instance
|
2018-10-05 12:34:31 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 14:56:18 +02:00
|
|
|
private static instance = null
|
|
|
|
|
|
|
|
public async getAccounts(): Promise<Account[]> {
|
|
|
|
|
|
|
|
// retrieve eth accounts
|
|
|
|
const ethAccounts = await Web3Provider.getWeb3().eth.getAccounts()
|
|
|
|
|
|
|
|
return ethAccounts
|
|
|
|
.map((address: string) => {
|
|
|
|
return new Account(address)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
public async register(asset: Asset): Promise<Asset> {
|
|
|
|
const {market} = await Keeper.getInstance()
|
|
|
|
|
|
|
|
// generate an id
|
|
|
|
const assetId = await market.generateId(asset.name + asset.description)
|
|
|
|
Logger.log(`Registering: ${assetId} with price ${asset.price}`)
|
|
|
|
asset.setId(assetId)
|
|
|
|
// register asset in the market
|
|
|
|
const result = await market.register(asset.getId(), asset.price, asset.publisher.getId())
|
|
|
|
Logger.log("Registered:", assetId, "in block", result.blockNumber)
|
|
|
|
|
|
|
|
return asset
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getOrdersByConsumer(consumer: Account): Promise<Order[]> {
|
|
|
|
const {auth, market} = await Keeper.getInstance()
|
|
|
|
|
|
|
|
Logger.log("Getting orders")
|
|
|
|
|
|
|
|
const accessConsentRequestedData = await auth.getEventData(
|
|
|
|
"AccessConsentRequested", {
|
|
|
|
filter: {
|
|
|
|
_consumer: consumer.getId(),
|
|
|
|
},
|
|
|
|
fromBlock: 0,
|
|
|
|
toBlock: "latest",
|
|
|
|
})
|
|
|
|
|
|
|
|
const orders = await Promise.all(
|
|
|
|
accessConsentRequestedData
|
|
|
|
.map(async (event: any) => {
|
|
|
|
|
|
|
|
const {returnValues} = event
|
|
|
|
|
|
|
|
const order: Order = new Order(
|
|
|
|
await Asset.load(returnValues._resourceId),
|
|
|
|
parseInt(returnValues._timeout, 10),
|
|
|
|
null, null)
|
|
|
|
|
|
|
|
order.setId(returnValues._id)
|
|
|
|
order.setStatus(await auth.getOrderStatus(returnValues._id))
|
|
|
|
order.setPaid(await market.verifyOrderPayment(returnValues._id))
|
2018-10-05 12:34:31 +02:00
|
|
|
|
2018-10-16 14:56:18 +02:00
|
|
|
return order
|
|
|
|
}),
|
|
|
|
)
|
2018-10-05 12:34:31 +02:00
|
|
|
|
2018-10-16 14:56:18 +02:00
|
|
|
// Logger.log("Got orders:", JSON.stringify(orders, null, 2))
|
|
|
|
Logger.log(`Got ${Object.keys(orders).length} orders`)
|
2018-10-05 12:34:31 +02:00
|
|
|
|
2018-10-16 14:56:18 +02:00
|
|
|
return orders
|
2018-10-05 12:34:31 +02:00
|
|
|
}
|
|
|
|
}
|