1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00
This commit is contained in:
Sebastian Gerske 2018-10-17 10:12:40 +02:00
parent 61bd56ba54
commit 0285c90d3c
7 changed files with 53 additions and 16 deletions

View File

@ -65,7 +65,7 @@ export default class Asset extends OceanBase {
publicKey, timeout, account.getId())
const {returnValues} = initiateAccessRequestReceipt.events.AccessConsentRequested
Logger.log(`Keeper AccessConsentRequested event received on asset: ${this.getId()}`, returnValues)
Logger.log(`Keeper AccessConsentRequested event received on asset: ${this.getId()}`)
order = new Order(this, returnValues._timeout, returnValues._pubKey, key)
order.setId(returnValues._id)
} catch (err) {

View File

@ -12,27 +12,29 @@ export default class Ocean {
if (!Ocean.instance) {
ConfigProvider.configure(config)
Ocean.instance = new Ocean()
Ocean.instance = new Ocean(await Keeper.getInstance())
}
return Ocean.instance
}
private static instance = null
private keeper: Keeper
private constructor(keeper: Keeper) {
this.keeper = keeper
}
public async getAccounts(): Promise<Account[]> {
// retrieve eth accounts
const ethAccounts = await Web3Provider.getWeb3().eth.getAccounts()
return ethAccounts
.map((address: string) => {
return new Account(address)
})
return ethAccounts.map((address: string) => new Account(address))
}
public async register(asset: Asset): Promise<Asset> {
const {market} = await Keeper.getInstance()
const {market} = this.keeper
// generate an id
const assetId = await market.generateId(asset.name + asset.description)
@ -46,7 +48,7 @@ export default class Ocean {
}
public async getOrdersByConsumer(consumer: Account): Promise<Order[]> {
const {auth, market} = await Keeper.getInstance()
const {auth, market} = this.keeper
Logger.log("Getting orders")

View File

@ -29,7 +29,6 @@ export default class Order extends OceanBase {
}
public setAccessId(accessId: string) {
Logger.log("accessId", accessId)
this.accessId = accessId
}
@ -61,11 +60,12 @@ export default class Order extends OceanBase {
return this.key
}
public async pay(account: Account) {
public async pay(account: Account): Promise<string> {
const {market} = await Keeper.getInstance()
// send payment
Logger.log("Sending payment: ", this.getId(), this.accessId,
this.asset.publisher.getId(), this.asset.price, this.timeout)
return market.payOrder(this, account.getId())
Logger.log(`Sending payment: ${this.getId()} ${this.accessId}
${this.asset.publisher.getId()} ${this.asset.price} ${this.timeout}`)
const payReceipt = await market.payOrder(this, account.getId())
return payReceipt.events.PaymentReceived.returnValues._paymentId
}
}

View File

@ -1,7 +1,13 @@
import Ocean from "./ocean/Ocean"
import Order from "./ocean/Order"
import Account from "./ocean/Order"
import Asset from "./ocean/Order"
import Logger from "./utils/Logger"
export {
Ocean,
Order,
Asset,
Account,
Logger,
}

25
test/Squid.test.ts Normal file
View File

@ -0,0 +1,25 @@
import * as assert from "assert"
import * as squid from "../src/squid"
describe("Squid", () => {
describe("interface", () => {
it("should expose Ocean", async () => {
assert(squid.Ocean)
})
it("should expose Logger", async () => {
assert(squid.Logger)
})
it("should expose Asset", async () => {
assert(squid.Asset)
})
it("should expose Order", async () => {
assert(squid.Order)
})
})
})

View File

@ -4,6 +4,7 @@ import ContractHandler from "../../src/keeper/ContractHandler"
import Account from "../../src/ocean/Account"
import Asset from "../../src/ocean/Asset"
import Ocean from "../../src/ocean/Ocean"
import Order from "../../src/ocean/Order"
import config from "../config"
const testName = "Test Asset 2"
@ -49,7 +50,8 @@ describe("Asset", () => {
it("should purchase an asset", async () => {
// todo
await testAsset.purchase(accounts[5], 10000)
const order: Order = await testAsset.purchase(accounts[5], 10000)
assert(order)
})
})

View File

@ -5,6 +5,7 @@ import Account from "../../src/ocean/Account"
import Asset from "../../src/ocean/Asset"
import Ocean from "../../src/ocean/Ocean"
import Order from "../../src/ocean/Order"
import Logger from "../../src/utils/Logger"
import config from "../config"
const testName = "Test Asset 333"
@ -36,7 +37,8 @@ describe("Order", () => {
const order: Order = await testAsset.purchase(accounts[0], 10000)
assert(order)
await order.pay(accounts[0])
const paymentId: string = await order.pay(accounts[0])
Logger.log("paymentId", paymentId)
})
})