1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00

Merge remote-tracking branch 'origin/develop' into feature/v0.1.23

# Conflicts:
#	package-lock.json
#	test/ocean/ServiceAgreement.test.ts
This commit is contained in:
Sebastian Gerske 2018-11-22 13:37:10 +01:00
commit 63c844b87f
6 changed files with 859 additions and 433 deletions

1168
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -60,16 +60,18 @@ export default abstract class ContractBase {
if (!this.contract.methods[name]) {
throw new Error(`Method "${name}" is not part of contract "${this.contractName}"`)
}
// Logger.log(name, args)
const method = this.contract.methods[name]
try {
const tx = method(...args)
const estimatedGas = await tx.estimateGas(args, {
const methodInstance = method(...args)
const estimatedGas = await methodInstance.estimateGas(args, {
from,
})
return tx.send({
const tx = methodInstance.send({
from,
gas: estimatedGas,
})
return tx
} catch (err) {
const mappedArgs = this.searchMethod(name).inputs.map((input, i) => {
return {
@ -87,6 +89,7 @@ export default abstract class ContractBase {
if (!this.contract.methods[name]) {
throw new Error(`Method ${name} is not part of contract ${this.contractName}`)
}
// Logger.log(name)
try {
const method = this.contract.methods[name](...args)
return method.call(from ? {from} : null)

View File

@ -16,12 +16,12 @@ export default class ServiceAgreement extends ContractBase {
name: any,
fulfillmentIndices: number[],
fulfillmentOperator: number,
ownerAddress: string)
: Promise<Receipt> {
ownerAddress: string): Promise<Receipt> {
return this.send("setupAgreementTemplate", ownerAddress, [
templateId, methodReflections.map((r) => r.address),
methodReflections.map((r) => r.signature), dependencyMatrix, name, fulfillmentIndices, fulfillmentOperator,
methodReflections.map((r) => r.signature), dependencyMatrix, name, fulfillmentIndices,
fulfillmentOperator,
])
}

View File

@ -205,8 +205,10 @@ export default class ServiceAgreement extends OceanBase {
super(serviceAgreementId)
}
public async lockPayment(assetId: string, price: number, consumer: Account): Promise<boolean> {
const {paymentConditions} = await Keeper.getInstance()
public async buyAsset(assetId: string, price: number, consumer: Account): Promise<boolean> {
const {paymentConditions, token} = await Keeper.getInstance()
await token.approve(paymentConditions.getAddress(), price, consumer.getId())
const lockPaymentReceipt =
await paymentConditions.lockPayment(this.getId(), assetId, price,
@ -222,7 +224,7 @@ export default class ServiceAgreement extends OceanBase {
await accessConditions.grantAccess(this.getId(), assetId, documentId,
this.publisher.getId())
return grantAccessReceipt.status
return !!grantAccessReceipt.events.AccessGranted
}
public async getStatus() {

View File

@ -35,13 +35,11 @@ export default class ServiceAgreementTemplate extends OceanBase {
}))
const fulfillmentIndices: number[] = this.template.Methods
.map((method: Method, i: number) => method.isTerminalCondition ? i : undefined)
.filter((index: number) => index !== undefined)
.map((method: Method, i: number) => method.isTerminalCondition ? i : null)
.filter((index: number) => index !== null)
const {serviceAgreement} = await Keeper.getInstance()
const methodReflections = await this.getMethodReflections()
const owner = await this.getOwner()
if (owner.getId() === templateOwnerAddress) {
@ -58,7 +56,7 @@ export default class ServiceAgreementTemplate extends OceanBase {
const receipt = await serviceAgreement
.setupAgreementTemplate(
this.template.id,
methodReflections,
await this.getMethodReflections(),
dependencyMatrix,
Web3Provider.getWeb3().utils.fromAscii(this.template.templateName),
fulfillmentIndices,
@ -124,23 +122,32 @@ export default class ServiceAgreementTemplate extends OceanBase {
throw new Error("Deps and timeouts need the same length")
}
// map name to index
const mappedDependencies: number[] = dependencies.map((dep: string) => {
return this.template.Methods.findIndex((m) => m.name === dep)
const mappedDependencies: number[] = []
const mappedDependencyTimeoutFlags: number[] = []
this.template.Methods.forEach((m, i) => {
const di = dependencies.findIndex((d) => d === m.name)
mappedDependencies.push(di > -1 ? 1 : 0)
mappedDependencyTimeoutFlags.push((di > -1 && dependencyTimeoutFlags[di]) ? 1 : 0)
})
let compressedDependencyValue = 0
const numBits = 2 // 1st for dependency, 2nd for timeout flag
for (let i = 0; i < mappedDependencies.length; i++) {
const dependencyIndex = mappedDependencies[i]
const timeout = dependencyTimeoutFlags[i]
const offset = i * numBits
// tslint:disable-next-line
compressedDependencyValue |= dependencyIndex * 2 ** (offset + 0) // the dependency bit
// tslint:disable-next-line
compressedDependencyValue |= timeout * 2 ** (offset + 1) // the timeout bit
if (mappedDependencies.length !== mappedDependencyTimeoutFlags.length) {
throw new Error("Deps and timeouts need the same length")
}
// Logger.log(dependencies, mappedDependencies, dependencyTimeoutFlags, mappedDependencyTimeoutFlags)
let compressedDependencyValue: number = 0
const numBits: number = 2 // 1st for dependency, 2nd for timeout flag
mappedDependencies.forEach((d: number, i: number) => {
const t: number = mappedDependencyTimeoutFlags[i]
const offset: number = i * numBits
// tslint:disable-next-line
compressedDependencyValue |= d * 2 ** (offset + 0) // the dependency bit
// tslint:disable-next-line
compressedDependencyValue |= t * 2 ** (offset + 1) // the timeout bit
})
return compressedDependencyValue
}

View File

@ -14,7 +14,6 @@ import Condition from "../../src/ocean/ServiceAgreements/Condition"
import ServiceAgreement from "../../src/ocean/ServiceAgreements/ServiceAgreement"
import ServiceAgreementTemplate from "../../src/ocean/ServiceAgreements/ServiceAgreementTemplate"
import Access from "../../src/ocean/ServiceAgreements/Templates/Access"
import Logger from "../../src/utils/Logger"
import WebServiceConnectorProvider from "../../src/utils/WebServiceConnectorProvider"
import config from "../config"
import TestContractHandler from "../keeper/TestContractHandler"
@ -74,7 +73,8 @@ describe("ServiceAgreement", () => {
case "assetId":
return "0x" + assetId
case "documentKeyId":
return "0x1234"
return "0x" + assetId
}
return null
@ -119,8 +119,7 @@ describe("ServiceAgreement", () => {
describe("#signServiceAgreement()", () => {
it("should sign an service agreement", async () => {
const id: string = IdGenerator.generateId()
const did: string = `did:op:${id}`
const did: string = `did:op:${assetId}`
const ddo = new DDO({id: did, service: [accessService]})
const serviceAgreementId: string = IdGenerator.generateId()
@ -139,8 +138,7 @@ describe("ServiceAgreement", () => {
describe("#executeServiceAgreement()", () => {
it("should execute an service agreement", async () => {
const id: string = IdGenerator.generateId()
const did: string = `did:op:${id}`
const did: string = `did:op:${assetId}`
const ddo = new DDO({id: did, service: [accessService]})
const serviceAgreementId: string = IdGenerator.generateId()
@ -164,8 +162,7 @@ describe("ServiceAgreement", () => {
describe("#getStatus()", () => {
it("should get the status of a newly created service agreement", async () => {
const id: string = IdGenerator.generateId()
const did: string = `did:op:${id}`
const did: string = `did:op:${assetId}`
const ddo = new DDO({id: did, service: [accessService]})
const serviceAgreementId: string = IdGenerator.generateId()
@ -186,16 +183,16 @@ describe("ServiceAgreement", () => {
})
})
describe("#lockPayment()", () => {
xit("should lock the payment in that service agreement", async () => {
describe("#buyAsset()", () => {
it("should lock the payment in that service agreement", async () => {
const id: string = IdGenerator.generateId()
const did: string = `did:op:${id}`
const did: string = `did:op:${assetId}`
const ddo = new DDO({id: did, service: [accessService, metaDataService]})
const serviceAgreementId: string = IdGenerator.generateId()
// @ts-ignore
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
const serviceAgreementSignature: string =
await ServiceAgreement.signServiceAgreement(assetId, ddo, accessService.serviceDefinitionId,
serviceAgreementId, consumerAccount)
@ -209,19 +206,16 @@ describe("ServiceAgreement", () => {
// get funds
await consumerAccount.requestTokens(metaDataService.metadata.base.price)
Logger.log(await consumerAccount.getBalance())
const paid: boolean = await serviceAgreement.lockPayment(assetId, metaDataService.metadata.base.price,
const paid: boolean = await serviceAgreement.buyAsset(assetId, metaDataService.metadata.base.price,
consumerAccount)
assert(paid)
})
})
describe("#grantAccess()", () => {
xit("should grant access in that service agreement", async () => {
it("should grant access in that service agreement", async () => {
const id: string = IdGenerator.generateId()
const did: string = `did:op:${id}`
const did: string = `did:op:${assetId}`
const ddo = new DDO({id: did, service: [accessService]})
const serviceAgreementId: string = IdGenerator.generateId()
@ -240,12 +234,36 @@ describe("ServiceAgreement", () => {
// get funds
await consumerAccount.requestTokens(metaDataService.metadata.base.price)
const paid: boolean = await serviceAgreement.lockPayment(assetId, metaDataService.metadata.base.price,
const paid: boolean = await serviceAgreement.buyAsset(assetId, metaDataService.metadata.base.price,
consumerAccount)
assert(paid)
const accessGranted: boolean = await serviceAgreement.grantAccess(assetId, IdGenerator.generateId())
// todo: use document id
const accessGranted: boolean = await serviceAgreement.grantAccess(assetId, assetId)
assert(accessGranted)
})
it("should fail to grant grant access if there is no payment", async () => {
const did: string = `did:op:${assetId}`
const ddo = new DDO({id: did, service: [accessService]})
const serviceAgreementId: string = IdGenerator.generateId()
// @ts-ignore
WebServiceConnectorProvider.setConnector(new WebServiceConnectorMock(ddo))
const serviceAgreementSignature: string =
await ServiceAgreement.signServiceAgreement(assetId, ddo, accessService.serviceDefinitionId,
serviceAgreementId, consumerAccount)
assert(serviceAgreementSignature)
const serviceAgreement: ServiceAgreement =
await ServiceAgreement.executeServiceAgreement(assetId, ddo, accessService.serviceDefinitionId,
serviceAgreementId, serviceAgreementSignature, consumerAccount, publisherAccount)
assert(serviceAgreement)
// todo: use document id
const accessGranted: boolean = await serviceAgreement.grantAccess(assetId, assetId)
assert(!accessGranted)
})
})
})