mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
added serviceAgreementTemplates, wip
This commit is contained in:
parent
bb6de972eb
commit
df56556e87
8
package-lock.json
generated
8
package-lock.json
generated
@ -120,9 +120,9 @@
|
||||
}
|
||||
},
|
||||
"@oceanprotocol/keeper-contracts": {
|
||||
"version": "0.3.3",
|
||||
"resolved": "https://registry.npmjs.org/@oceanprotocol/keeper-contracts/-/keeper-contracts-0.3.3.tgz",
|
||||
"integrity": "sha512-UVt9m9WmFm4mTDWmMekT8lhBh2hWr2baEVnF0dEN3OL5BDAWoynFQeeKtUqTepVuOVgNy5bHYKLiHkCWzbOEqA=="
|
||||
"version": "0.4.1",
|
||||
"resolved": "https://registry.npmjs.org/@oceanprotocol/keeper-contracts/-/keeper-contracts-0.4.1.tgz",
|
||||
"integrity": "sha512-UmgPsJul9ZJb5BPIa1qfMpokJWzdROni/wisd4Dnm+wwHUA7/ymsr34zVn+4v+uAcMLHAYPKbsROBT5xlbiKAQ=="
|
||||
},
|
||||
"@oceanprotocol/secret-store-client": {
|
||||
"version": "0.0.6",
|
||||
@ -1802,7 +1802,7 @@
|
||||
},
|
||||
"es6-promisify": {
|
||||
"version": "5.0.0",
|
||||
"resolved": "http://registry.npmjs.org/es6-promisify/-/es6-promisify-5.0.0.tgz",
|
||||
"resolved": "https://registry.npmjs.org/es6-promisify/-/es6-promisify-5.0.0.tgz",
|
||||
"integrity": "sha1-UQnWLz5W6pZ8S2NQWu8IKRyKUgM=",
|
||||
"requires": {
|
||||
"es6-promise": "^4.0.3"
|
||||
|
@ -49,7 +49,7 @@
|
||||
"node": ">=8 <10"
|
||||
},
|
||||
"dependencies": {
|
||||
"@oceanprotocol/keeper-contracts": "^0.3.3",
|
||||
"@oceanprotocol/keeper-contracts": "^0.4.1",
|
||||
"@oceanprotocol/secret-store-client": "^0.0.6",
|
||||
"bignumber.js": "^7.2.1",
|
||||
"eth-crypto": "^1.2.4",
|
||||
|
@ -50,7 +50,19 @@ export default class ContractHandler {
|
||||
const market = await ContractHandler.deployContract("OceanMarket", deployerAddress, {
|
||||
args: [token.options.address],
|
||||
})
|
||||
await ContractHandler.deployContract("ServiceAgreement", deployerAddress, {})
|
||||
|
||||
const sa = await ContractHandler.deployContract("ServiceAgreement", deployerAddress, {
|
||||
args: [],
|
||||
|
||||
})
|
||||
await ContractHandler.deployContract("AccessConditions", deployerAddress, {
|
||||
args: [sa.options.address],
|
||||
})
|
||||
|
||||
await ContractHandler.deployContract("PaymentConditions", deployerAddress, {
|
||||
args: [sa.options.address, token.options.address],
|
||||
})
|
||||
|
||||
await ContractHandler.deployContract("DIDRegistry", deployerAddress, {})
|
||||
/* not part of trilobite
|
||||
const dispute = await ContractHandler.deployContract("OceanDispute", deployerAddress, {
|
||||
|
@ -33,7 +33,7 @@ export default abstract class ContractBase {
|
||||
|
||||
public async getEventData(eventName: any, options: any): Promise<Event[]> {
|
||||
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}"`)
|
||||
}
|
||||
return this.contract.getPastEvents(eventName, options)
|
||||
}
|
||||
@ -42,13 +42,28 @@ export default abstract class ContractBase {
|
||||
return this.contract.options.address
|
||||
}
|
||||
|
||||
public getSignatureOfMethod(methodName: string): string {
|
||||
|
||||
const foundMethod = this.contract.options.jsonInterface.find((method) => {
|
||||
if (method.name === methodName) {
|
||||
return method
|
||||
}
|
||||
})
|
||||
|
||||
if (!foundMethod) {
|
||||
throw new Error(`Method "${methodName}" is not part of contract "${this.contractName}"`)
|
||||
}
|
||||
|
||||
return foundMethod.signature
|
||||
}
|
||||
|
||||
protected async init() {
|
||||
this.contract = await ContractHandler.get(this.contractName)
|
||||
}
|
||||
|
||||
protected async send(name: string, from: string, args: any[]): Promise<Receipt> {
|
||||
if (!this.contract.methods[name]) {
|
||||
throw new Error(`Method ${name} is not part of contract ${this.contractName}`)
|
||||
throw new Error(`Method "${name}" is not part of contract "${this.contractName}"`)
|
||||
}
|
||||
try {
|
||||
const tx = this.contract.methods[name](...args)
|
||||
@ -61,7 +76,7 @@ export default abstract class ContractBase {
|
||||
})
|
||||
} catch (err) {
|
||||
const argString = JSON.stringify(args, null, 2)
|
||||
Logger.error(`Sending transaction ${name} on contract ${this.contractName} failed.`)
|
||||
Logger.error(`Sending transaction "${name}" on contract "${this.contractName}" failed.`)
|
||||
Logger.error(`Args: ${argString} From: ${from}`)
|
||||
throw err
|
||||
}
|
||||
@ -75,7 +90,7 @@ export default abstract class ContractBase {
|
||||
const method = this.contract.methods[name](...args)
|
||||
return method.call(from ? {from} : null)
|
||||
} catch (err) {
|
||||
Logger.error(`Calling method ${name} on contract ${this.contractName} failed. Args: ${args}`, err)
|
||||
Logger.error(`Calling method "${name}" on contract "${this.contractName}" failed. Args: ${args}`, err)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
import {Receipt} from "web3-utils"
|
||||
import ContractBase from "./ContractBase"
|
||||
|
||||
export default class ServiceAgreement extends ContractBase {
|
||||
@ -7,4 +8,26 @@ export default class ServiceAgreement extends ContractBase {
|
||||
await serviceAgreement.init()
|
||||
return serviceAgreement
|
||||
}
|
||||
|
||||
public async setupAgreementTemplate(contractAddresses: any[], contractFunctionSignatures: string[],
|
||||
depencyMatrix: number[], name: any, ownerAddress: string): Promise<Receipt> {
|
||||
|
||||
return this.send("setupAgreementTemplate", ownerAddress, [
|
||||
contractAddresses, contractFunctionSignatures, depencyMatrix, name,
|
||||
])
|
||||
}
|
||||
|
||||
public async getAgreementStatus(agreementId: string) {
|
||||
|
||||
return this.call("getAgreementStatus", [agreementId])
|
||||
}
|
||||
|
||||
public async executeAgreement(templateId: string, signature: string, consumerAddress: string, valueHashes: string[],
|
||||
timeoutValues: number[], serviceDefinitionId: string, did: string,
|
||||
publisherAddress: string): Promise<Receipt> {
|
||||
|
||||
return this.send("executeAgreement", publisherAddress, [
|
||||
templateId, signature, consumerAddress, valueHashes, timeoutValues, serviceDefinitionId, "0x" + did,
|
||||
])
|
||||
}
|
||||
}
|
||||
|
10
src/keeper/contracts/conditions/AccessConditions.ts
Normal file
10
src/keeper/contracts/conditions/AccessConditions.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import ContractBase from "../ContractBase"
|
||||
|
||||
export default class AccessConditions extends ContractBase {
|
||||
|
||||
public static async getInstance(): Promise<AccessConditions> {
|
||||
const accessConditions: AccessConditions = new AccessConditions("AccessConditions")
|
||||
await accessConditions.init()
|
||||
return accessConditions
|
||||
}
|
||||
}
|
10
src/keeper/contracts/conditions/PaymentConditions.ts
Normal file
10
src/keeper/contracts/conditions/PaymentConditions.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import ContractBase from "../ContractBase"
|
||||
|
||||
export default class PaymentConditions extends ContractBase {
|
||||
|
||||
public static async getInstance(): Promise<PaymentConditions> {
|
||||
const paymentConditions: PaymentConditions = new PaymentConditions("PaymentConditions")
|
||||
await paymentConditions.init()
|
||||
return paymentConditions
|
||||
}
|
||||
}
|
73
src/ocean/ServiceAgreement.ts
Normal file
73
src/ocean/ServiceAgreement.ts
Normal file
@ -0,0 +1,73 @@
|
||||
import ServiceAgreementContract from "../keeper/contracts/ServiceAgreement"
|
||||
import Web3Provider from "../keeper/Web3Provider"
|
||||
import Account from "./Account"
|
||||
import OceanBase from "./OceanBase"
|
||||
import ServiceAgreementTemplate from "./ServiceAgreementTemplate"
|
||||
|
||||
export default class ServiceAgreement extends OceanBase {
|
||||
|
||||
public static async executeServiceAgreement(serviceAgreementTemplate: ServiceAgreementTemplate,
|
||||
did: string, consumer: Account): Promise<ServiceAgreement> {
|
||||
|
||||
const valHashList = [
|
||||
ServiceAgreement.valueHash("bool", true),
|
||||
ServiceAgreement.valueHash("bool", false),
|
||||
ServiceAgreement.valueHash("uint", 120),
|
||||
// asset Id: 797FD5B9045B841FDFF72
|
||||
ServiceAgreement.valueHash("string", "797FD5B9045B841FDFF72"),
|
||||
]
|
||||
|
||||
const serviceDefinitionId = "0x515f158c3a5d81d15b0160cf8929916089218bdb4aa78c3ecd16633afd44b894"
|
||||
|
||||
const timeoutValues = [0, 0, 0, 3] // timeout 5 blocks @ condition 4
|
||||
|
||||
const saMerkleRoot = ServiceAgreement.merkelizeServiceAgreement(serviceAgreementTemplate, valHashList,
|
||||
timeoutValues, serviceDefinitionId, did)
|
||||
const saMerkleRootSignature = await Web3Provider.getWeb3().eth.sign(saMerkleRoot, consumer.getId())
|
||||
|
||||
const serviceAgreement: ServiceAgreementContract = await ServiceAgreementContract.getInstance()
|
||||
|
||||
const receipt = await serviceAgreement.executeAgreement(
|
||||
serviceAgreementTemplate.getId(), saMerkleRootSignature, consumer.getId(), valHashList, timeoutValues,
|
||||
serviceDefinitionId, did, serviceAgreementTemplate.getPublisher().getId())
|
||||
|
||||
const id = receipt.events.ExecuteAgreement.returnValues.serviceId
|
||||
return new ServiceAgreement(
|
||||
id,
|
||||
receipt.events.ExecuteAgreement.returnValues.templateId,
|
||||
receipt.events.ExecuteAgreement.returnValues.templateOwner,
|
||||
receipt.events.ExecuteAgreement.returnValues.consumer,
|
||||
receipt.events.ExecuteAgreement.returnValues.state,
|
||||
receipt.events.ExecuteAgreement.returnValues.status,
|
||||
)
|
||||
}
|
||||
|
||||
protected static valueHash(type: string, value: any) {
|
||||
const args = {type, value}
|
||||
return Web3Provider.getWeb3().utils.soliditySha3(args).toString("hex")
|
||||
}
|
||||
|
||||
private static merkelizeServiceAgreement(serviceAgreementTemplate: ServiceAgreementTemplate, valueHashes: string[],
|
||||
timeouts: number[], serviceDefinitionId: string, did: string) {
|
||||
const args = [
|
||||
{type: "bytes32", value: serviceAgreementTemplate.getId()},
|
||||
{type: "bytes32[]", value: serviceAgreementTemplate.getConditionKeys()},
|
||||
{type: "bytes32[]", value: valueHashes},
|
||||
{type: "uint256[]", value: timeouts},
|
||||
{type: "bytes32", value: serviceDefinitionId},
|
||||
{type: "bytes32", value: did},
|
||||
]
|
||||
return Web3Provider.getWeb3().utils.soliditySha3(...args).toString("hex")
|
||||
}
|
||||
|
||||
private constructor(id: string, templateId: string, templateOwnerId: string,
|
||||
consumerId: string, state: boolean, status: boolean) {
|
||||
super(id)
|
||||
}
|
||||
|
||||
public async getStatus() {
|
||||
const sa = await ServiceAgreementContract.getInstance()
|
||||
|
||||
return sa.getAgreementStatus(this.getId())
|
||||
}
|
||||
}
|
67
src/ocean/ServiceAgreementTemplate.ts
Normal file
67
src/ocean/ServiceAgreementTemplate.ts
Normal file
@ -0,0 +1,67 @@
|
||||
import AccessConditions from "../keeper/contracts/conditions/AccessConditions"
|
||||
import PaymentConditions from "../keeper/contracts/conditions/PaymentConditions"
|
||||
import ServiceAgreement from "../keeper/contracts/ServiceAgreement"
|
||||
import Web3Provider from "../keeper/Web3Provider"
|
||||
import Account from "./Account"
|
||||
import OceanBase from "./OceanBase"
|
||||
|
||||
export default class ServiceAgreementTemplate extends OceanBase {
|
||||
|
||||
public static async registerServiceAgreementsTemplate(resourceName: string, publisher: Account):
|
||||
Promise<ServiceAgreementTemplate> {
|
||||
|
||||
const paymentConditions: PaymentConditions = await PaymentConditions.getInstance()
|
||||
const accessConditions: AccessConditions = await AccessConditions.getInstance()
|
||||
|
||||
const contractAddresses = [
|
||||
await paymentConditions.getAddress(),
|
||||
await accessConditions.getAddress(),
|
||||
await paymentConditions.getAddress(),
|
||||
await paymentConditions.getAddress(),
|
||||
]
|
||||
const functionSignatures = [
|
||||
await paymentConditions.getSignatureOfMethod("lockPayment"),
|
||||
await accessConditions.getSignatureOfMethod("grantAccess"),
|
||||
await paymentConditions.getSignatureOfMethod("releasePayment"),
|
||||
await paymentConditions.getSignatureOfMethod("refundPayment"),
|
||||
]
|
||||
|
||||
const dependencies = [0, 1, 4, 1 | 2 ** 4 | 2 ** 5] // dependency bit | timeout bit
|
||||
|
||||
const serviceAgreement: ServiceAgreement = await ServiceAgreement.getInstance()
|
||||
|
||||
const receipt = await serviceAgreement.setupAgreementTemplate(
|
||||
contractAddresses, functionSignatures, dependencies,
|
||||
Web3Provider.getWeb3().utils.fromAscii(resourceName), publisher.getId())
|
||||
|
||||
const id = receipt.events.SetupAgreementTemplate.returnValues.serviceTemplateId
|
||||
|
||||
return new ServiceAgreementTemplate(
|
||||
id,
|
||||
ServiceAgreementTemplate.generateConditionsKeys(id, contractAddresses, functionSignatures),
|
||||
publisher)
|
||||
}
|
||||
|
||||
private static generateConditionsKeys(serviceAgreementTemplateId: string, contractAddresses: string[],
|
||||
functionSignatures: string[]): string[] {
|
||||
const conditions = []
|
||||
for (let i = 0; i < contractAddresses.length; i++) {
|
||||
const types = ["bytes32", "address", "bytes4"]
|
||||
const values = [serviceAgreementTemplateId, contractAddresses[i], functionSignatures[i]]
|
||||
conditions.push(Web3Provider.getWeb3().utils.soliditySha3(...types, ...values).toString("hex"))
|
||||
}
|
||||
return conditions
|
||||
}
|
||||
|
||||
private constructor(id, private conditionKeys: string[], private publisher: Account) {
|
||||
super(id)
|
||||
}
|
||||
|
||||
public getPublisher(): Account {
|
||||
return this.publisher
|
||||
}
|
||||
|
||||
public getConditionKeys(): string[] {
|
||||
return this.conditionKeys
|
||||
}
|
||||
}
|
@ -1,16 +1,22 @@
|
||||
import {assert} from "chai"
|
||||
import ConfigProvider from "../../src/ConfigProvider"
|
||||
import ContractHandler from "../../src/keeper/ContractHandler"
|
||||
import Account from "../../src/ocean/Account"
|
||||
import Ocean from "../../src/ocean/Ocean"
|
||||
import config from "../config"
|
||||
import ContractBaseMock from "../mocks/ContractBase.Mock"
|
||||
|
||||
const wrappedContract = new ContractBaseMock("OceanToken")
|
||||
let accounts: Account[]
|
||||
|
||||
describe("ContractWrapperBase", () => {
|
||||
|
||||
before(async () => {
|
||||
ConfigProvider.setConfig(config)
|
||||
await ContractHandler.deployContracts()
|
||||
wrappedContract.initMock()
|
||||
await wrappedContract.initMock()
|
||||
const ocean: Ocean = await Ocean.getInstance(config)
|
||||
accounts = await ocean.getAccounts()
|
||||
})
|
||||
|
||||
describe("#call()", () => {
|
||||
@ -52,6 +58,29 @@ describe("ContractWrapperBase", () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe("#send()", () => {
|
||||
|
||||
it("should fail to call on an unknown contract function", (done) => {
|
||||
|
||||
wrappedContract.sendMock("transferxxx", accounts[0].getId(), [])
|
||||
.catch(() => {
|
||||
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("#getSignatureOfMethod()", () => {
|
||||
|
||||
it("should a signature of the function", async () => {
|
||||
|
||||
const sig = wrappedContract.getSignatureOfMethod("name")
|
||||
assert(sig)
|
||||
assert(typeof sig === "string")
|
||||
assert(sig.startsWith("0x"))
|
||||
})
|
||||
})
|
||||
|
||||
describe("#getEventData()", () => {
|
||||
|
||||
it("should fail on unknown event", (done) => {
|
||||
|
@ -2,7 +2,7 @@ import ContractBase from "../../src/keeper/contracts/ContractBase"
|
||||
|
||||
export default class ContractBaseMock extends ContractBase {
|
||||
public async initMock() {
|
||||
this.init()
|
||||
await this.init()
|
||||
}
|
||||
|
||||
public async callMock(name: string, args: any[], from?: string) {
|
||||
|
56
test/ocean/ServiceAgreement.test.ts
Normal file
56
test/ocean/ServiceAgreement.test.ts
Normal file
@ -0,0 +1,56 @@
|
||||
import {assert} from "chai"
|
||||
import ConfigProvider from "../../src/ConfigProvider"
|
||||
import ContractHandler from "../../src/keeper/ContractHandler"
|
||||
import Account from "../../src/ocean/Account"
|
||||
import IdGenerator from "../../src/ocean/IdGenerator"
|
||||
import Ocean from "../../src/ocean/Ocean"
|
||||
import ServiceAgreement from "../../src/ocean/ServiceAgreement"
|
||||
import ServiceAgreementTemplate from "../../src/ocean/ServiceAgreementTemplate"
|
||||
import config from "../config"
|
||||
import Logger from "../../src/utils/Logger"
|
||||
|
||||
let ocean: Ocean
|
||||
let accounts: Account[]
|
||||
let testServiceAgreementTemplate: ServiceAgreementTemplate
|
||||
|
||||
describe("ServiceAgreement", () => {
|
||||
|
||||
before(async () => {
|
||||
ConfigProvider.setConfig(config)
|
||||
await ContractHandler.deployContracts()
|
||||
ocean = await Ocean.getInstance(config)
|
||||
accounts = await ocean.getAccounts()
|
||||
|
||||
const publisherAccount = accounts[0]
|
||||
const resourceName = "superb car data"
|
||||
testServiceAgreementTemplate =
|
||||
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, publisherAccount)
|
||||
|
||||
})
|
||||
|
||||
describe("#executeServiceAgreement()", () => {
|
||||
it("should execture an service agreement", async () => {
|
||||
|
||||
const consumerAccount = accounts[0]
|
||||
const did: string = IdGenerator.generateId()
|
||||
const serviceAgreement: ServiceAgreement =
|
||||
await ServiceAgreement.executeServiceAgreement(testServiceAgreementTemplate, did, consumerAccount)
|
||||
|
||||
assert(serviceAgreement)
|
||||
assert(serviceAgreement.getId().startsWith("0x"))
|
||||
})
|
||||
})
|
||||
|
||||
describe("#getStatus()", () => {
|
||||
it("should execture an service agreement", async () => {
|
||||
|
||||
const consumerAccount = accounts[0]
|
||||
const did: string = IdGenerator.generateId()
|
||||
const serviceAgreement: ServiceAgreement =
|
||||
await ServiceAgreement.executeServiceAgreement(testServiceAgreementTemplate, did, consumerAccount)
|
||||
|
||||
const status = await serviceAgreement.getStatus()
|
||||
Logger.log(status)
|
||||
})
|
||||
})
|
||||
})
|
34
test/ocean/ServiceAgreementTemplate.test.ts
Normal file
34
test/ocean/ServiceAgreementTemplate.test.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import {assert} from "chai"
|
||||
import ConfigProvider from "../../src/ConfigProvider"
|
||||
import ContractHandler from "../../src/keeper/ContractHandler"
|
||||
import Account from "../../src/ocean/Account"
|
||||
import Ocean from "../../src/ocean/Ocean"
|
||||
import ServiceAgreementTemplate from "../../src/ocean/ServiceAgreementTemplate"
|
||||
import config from "../config"
|
||||
|
||||
let ocean: Ocean
|
||||
let accounts: Account[]
|
||||
|
||||
describe("ServiceAgreementTemplate", () => {
|
||||
|
||||
before(async () => {
|
||||
ConfigProvider.setConfig(config)
|
||||
await ContractHandler.deployContracts()
|
||||
ocean = await Ocean.getInstance(config)
|
||||
accounts = await ocean.getAccounts()
|
||||
})
|
||||
|
||||
describe("#registerServiceAgreementsTemplate()", () => {
|
||||
it("should setup an agreement template", async () => {
|
||||
|
||||
const publisherAccount = accounts[0]
|
||||
const resourceName = "test data"
|
||||
const serviceAgreementTemplate: ServiceAgreementTemplate =
|
||||
await ServiceAgreementTemplate.registerServiceAgreementsTemplate(resourceName, publisherAccount)
|
||||
assert(serviceAgreementTemplate)
|
||||
assert(serviceAgreementTemplate.getId())
|
||||
assert(serviceAgreementTemplate.getPublisher().getId() === publisherAccount.getId())
|
||||
})
|
||||
})
|
||||
|
||||
})
|
Loading…
Reference in New Issue
Block a user