mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
added order unit tests, refactoring
This commit is contained in:
parent
bac7c9e4f0
commit
ac18db1365
@ -33,11 +33,16 @@ export default class OceanAuth extends ContractBaseWrapper {
|
||||
}
|
||||
|
||||
public async initiateAccessRequest(asset: Asset, publicKey: string,
|
||||
timeout, buyerAddress: string): Promise<Receipt> {
|
||||
return this.contract.methods.initiateAccessRequest(asset.assetId, asset.publisherId, publicKey, timeout)
|
||||
.send({
|
||||
from: buyerAddress,
|
||||
gas: this.config.defaultGas,
|
||||
})
|
||||
timeout: number, buyerAddress: string): Promise<Receipt> {
|
||||
|
||||
const args = [asset.assetId, asset.publisherId, publicKey, timeout]
|
||||
const tx = this.contract.methods.initiateAccessRequest(...args)
|
||||
const gas = await tx.estimateGas(args, {
|
||||
from: buyerAddress,
|
||||
})
|
||||
return tx.send({
|
||||
from: buyerAddress,
|
||||
gas,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ export default class ContractHandler {
|
||||
}
|
||||
|
||||
public static async deployContracts(web3Helper: Web3Helper) {
|
||||
Logger.log("Deploying contracts")
|
||||
Logger.log("Trying to deploy contracts")
|
||||
|
||||
const web3 = web3Helper.getWeb3()
|
||||
|
||||
@ -72,7 +72,7 @@ export default class ContractHandler {
|
||||
new RegExp(`_+${token.name}_+`, "g"),
|
||||
token.address.replace("0x", ""))
|
||||
}
|
||||
// Logger.log(bytecode);
|
||||
// Logger.log(bytecode)
|
||||
|
||||
return bytecode.toString()
|
||||
}
|
||||
|
@ -4,7 +4,9 @@ export default class Order {
|
||||
public id: string
|
||||
public asset: Asset
|
||||
public assetId: string
|
||||
public timeout: any
|
||||
public timeout: number
|
||||
public pubkey: string
|
||||
public key: any
|
||||
public paid: boolean
|
||||
public status: number
|
||||
}
|
||||
|
@ -13,18 +13,21 @@ export default class Order extends OceanBase {
|
||||
|
||||
private static create(asset: Asset, args, key): OrderModel {
|
||||
const accessId = args._id
|
||||
Logger.log("got new access request id: ", accessId)
|
||||
return {
|
||||
Logger.log(`got new access request id: ${accessId}`)
|
||||
const order: OrderModel = {
|
||||
id: accessId,
|
||||
assetId: asset.assetId,
|
||||
asset,
|
||||
timeout: args._timeout,
|
||||
timeout: parseInt(args._timeout, 10),
|
||||
pubkey: args._pubKey,
|
||||
key,
|
||||
} as OrderModel
|
||||
// Logger.log("Created order", order)
|
||||
|
||||
return order
|
||||
}
|
||||
|
||||
public async getOrdersByConsumer(consumerAddress: string) {
|
||||
public async getOrdersByConsumer(consumerAddress: string): Promise<OrderModel[]> {
|
||||
const {auth, market} = this.keeper
|
||||
|
||||
Logger.log("Getting orders")
|
||||
@ -43,13 +46,17 @@ export default class Order extends OceanBase {
|
||||
.filter((event: any) => {
|
||||
return event.returnValues._consumer === consumerAddress
|
||||
})
|
||||
// todo: this is not orders model maybe? lacking proper typing here
|
||||
.map(async (event: any) => ({
|
||||
...event.returnValues,
|
||||
id: event.returnValues._id,
|
||||
asset: null,
|
||||
assetId: event.returnValues._resourceId,
|
||||
timeout: parseInt(event.returnValues._timeout, 10),
|
||||
pubkey: null,
|
||||
key: null,
|
||||
status: await auth.getOrderStatus(event.returnValues._id),
|
||||
paid: await market.verifyOrderPayment(event.returnValues._id),
|
||||
key: null,
|
||||
} as Order
|
||||
} as OrderModel
|
||||
),
|
||||
),
|
||||
)
|
||||
@ -71,11 +78,13 @@ export default class Order extends OceanBase {
|
||||
Logger.log("The asset:", asset.assetId, "is it valid?", isValid, "it's price is:", price)
|
||||
|
||||
if (!isValid) {
|
||||
throw new Error("asset not valid")
|
||||
throw new Error("The Asset is not valid!")
|
||||
}
|
||||
try {
|
||||
const marketAddr = market.getAddress()
|
||||
// Allow market contract to transfer funds on the consumer"s behalf
|
||||
await token.approve(market.getAddress(), price, buyerAddress)
|
||||
await token.approve(marketAddr, price, buyerAddress)
|
||||
Logger.log(`${price} tokens approved on market with id: ${marketAddr}`)
|
||||
} catch (err) {
|
||||
Logger.error("token.approve failed", err)
|
||||
}
|
||||
@ -86,10 +95,8 @@ export default class Order extends OceanBase {
|
||||
publicKey, timeout, buyerAddress)
|
||||
|
||||
const args = initiateAccessRequestReceipt.events.AccessConsentRequested.returnValues
|
||||
Logger.log("keeper AccessConsentRequested event received on asset: ", asset.assetId, "\nevent:", args)
|
||||
Logger.log(`keeper AccessConsentRequested event received on asset: ${asset.assetId}`)
|
||||
order = Order.create(asset, args, key)
|
||||
Logger.log("Created order", order)
|
||||
|
||||
} catch (err) {
|
||||
Logger.error("auth.initiateAccessRequest failed", err)
|
||||
}
|
||||
|
87
test/ocean/Order.test.ts
Normal file
87
test/ocean/Order.test.ts
Normal file
@ -0,0 +1,87 @@
|
||||
import * as assert from "assert"
|
||||
import ContractHandler from "../../src/keeper/ContractHandler"
|
||||
import Keeper from "../../src/keeper/Keeper"
|
||||
import Web3Helper from "../../src/keeper/Web3Helper"
|
||||
import AssetModel from "../../src/models/Asset"
|
||||
import Config from "../../src/models/Config"
|
||||
import OrderModel from "../../src/models/Order"
|
||||
import Account from "../../src/ocean/Account"
|
||||
import Asset from "../../src/ocean/Asset"
|
||||
import Order from "../../src/ocean/Order"
|
||||
import Logger from "../../src/utils/Logger";
|
||||
|
||||
let keeper: Keeper
|
||||
let testAsset: AssetModel
|
||||
let accounts
|
||||
let buyerAddr
|
||||
|
||||
const config: Config = {
|
||||
nodeUri: "http://localhost:8545",
|
||||
} as Config
|
||||
const web3Helper = new Web3Helper(config)
|
||||
|
||||
before(async () => {
|
||||
await ContractHandler.deployContracts(web3Helper)
|
||||
keeper = await Keeper.getInstance(config, web3Helper)
|
||||
|
||||
const account = new Account(keeper)
|
||||
accounts = await account.list()
|
||||
|
||||
const sellerAddr = accounts[0].name
|
||||
buyerAddr = accounts[2].name
|
||||
|
||||
const name = "Order Test Asset"
|
||||
const description = "This asset is pure owange"
|
||||
const price = 100
|
||||
|
||||
const asset = new Asset(keeper)
|
||||
testAsset = await asset.registerAsset(name, description, price, sellerAddr)
|
||||
|
||||
// get tokens
|
||||
await account.requestTokens(1000000000000000, buyerAddr)
|
||||
})
|
||||
|
||||
const timeout = 100000000000
|
||||
|
||||
describe("Order", () => {
|
||||
|
||||
describe("#purchaseAsset()", () => {
|
||||
|
||||
it("should purchase an asset", async () => {
|
||||
|
||||
const order = new Order(keeper)
|
||||
const finalOrder: OrderModel = await order.purchaseAsset(testAsset, timeout, buyerAddr)
|
||||
|
||||
assert(finalOrder.assetId === testAsset.assetId)
|
||||
assert(finalOrder.asset.assetId === testAsset.assetId)
|
||||
assert(finalOrder.timeout === timeout)
|
||||
})
|
||||
})
|
||||
|
||||
describe("#getOrdersByConsumer()", () => {
|
||||
|
||||
it("should get orders by consumer if there is one", async () => {
|
||||
|
||||
const order = new Order(keeper)
|
||||
const finalOrder: OrderModel = await order.purchaseAsset(testAsset, timeout, buyerAddr)
|
||||
|
||||
const orders: OrderModel[] = await order.getOrdersByConsumer(buyerAddr)
|
||||
const datOrder = (await orders.filter((o) => o.id === finalOrder.id))[0]
|
||||
|
||||
assert(datOrder !== null)
|
||||
assert(datOrder.assetId === testAsset.assetId)
|
||||
assert(datOrder.timeout === timeout)
|
||||
assert(datOrder.paid === true)
|
||||
assert(datOrder.status === 0)
|
||||
})
|
||||
|
||||
it("should return empty array if no orders found", async () => {
|
||||
|
||||
const order = new Order(keeper)
|
||||
|
||||
const orders: OrderModel[] = await order.getOrdersByConsumer(accounts[4].name)
|
||||
|
||||
assert(orders.length === 0)
|
||||
})
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue
Block a user