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

104 lines
3.0 KiB
TypeScript

import ContractBase from '../ContractBase'
import { zeroX } from '../../../utils'
import { InstantiableConfig } from '../../../Instantiable.abstract'
export interface AgreementData {
did: string
didOwner: string
templateId: string
conditionIds: string[]
lastUpdatedBy: string
blockNumberUpdated: number
}
export class AgreementStoreManager extends ContractBase {
public static async getInstance(
config: InstantiableConfig
): Promise<AgreementStoreManager> {
const templateStoreManager: AgreementStoreManager = new AgreementStoreManager(
'AgreementStoreManager'
)
await templateStoreManager.init(config)
return templateStoreManager
}
public getOwner(): Promise<string> {
return this.call('owner', [])
}
public async getAgreement(agreementId: string) {
const {
did,
didOwner,
templateId,
conditionIds,
lastUpdatedBy,
blockNumberUpdated
} = await this.call('getAgreement', [zeroX(agreementId)])
return {
did,
didOwner,
templateId,
conditionIds,
lastUpdatedBy,
blockNumberUpdated: +blockNumberUpdated
} as AgreementData
}
/**
* Create a agreement using EscrowComputeExecutionTemplate.
* @param {string} agreementId Generated agreement ID.
* @param {string} did Asset DID.
* @param {string} templateId Template ID.
* @param {string[]} conditionIds List of conditions IDs.
* @param {number[]} timeLocks Timelocks.
* @param {number[]} timeOuts Timeouts.
* @param {string[]} actors ETH account addresses of provider, consumer, etc.
* @param {string} from Action sender.
*
* @return {any} Transaction receipt.
*/
public async createAgreement(
agreementId: string,
did: string,
templateId: string,
conditionIds: string[],
timeLocks: number[],
timeOuts: number[],
actors: string[],
from?: string
): Promise<any> {
this.sendFrom(
'createAgreement',
[
zeroX(agreementId),
zeroX(did),
zeroX(templateId),
conditionIds.map(zeroX),
timeLocks,
timeOuts,
actors
],
from
)
}
/**
* Generates and returns the agreement creation event.
* @param {string} agreementId Agreement ID.
* @return {Event} Agreement created event.
*/
public getAgreementCreatedEvent(agreementId: string) {
return this.getEvent('AgreementCreated', {
agreementId: zeroX(agreementId)
})
}
public getAgreementActorAddedEvent(agreementId: string) {
return this.getEvent('AgreementActorAdded', {
agreementId: zeroX(agreementId)
})
}
}