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

98 lines
3.7 KiB
TypeScript
Raw Normal View History

import Keeper from "../keeper/Keeper"
import Account from "./Account"
/**
* Agreements Conditions submodule of Ocean Protocol.
*/
export class OceanAgreementsConditions {
/**
* Returns the instance of OceanAgreementsConditions.
* @return {Promise<OceanAgreementsConditions>}
*/
public static async getInstance(): Promise<OceanAgreementsConditions> {
if (!OceanAgreementsConditions.instance) {
OceanAgreementsConditions.instance = new OceanAgreementsConditions()
OceanAgreementsConditions.instance.keeper = await Keeper.getInstance()
}
return OceanAgreementsConditions.instance
}
/**
* OceanAgreementsConditions instance.
* @type {OceanAgreementsConditions}
*/
private static instance: OceanAgreementsConditions = null
private keeper: Keeper
/**
* Transfers tokens to the EscrowRewardCondition contract as an escrow payment.
* This is required before access can be given to the asset data.
* @param {string} agreementId Agreement ID.
* @param {number} amount Asset amount.
* @param {Account} from Account of sender.
*/
public async lockReward(agreementId: string, amount: number, from: Account = new Account()) {
const {lockRewardCondition, escrowReward} = this.keeper.conditions
await this.keeper.token.approve(lockRewardCondition.getAddress(), amount, from.getId())
const receipt = await lockRewardCondition.fulfill(agreementId, escrowReward.getAddress(), amount, from.getId())
return !!receipt.events.Fulfilled
}
/**
* Authorize the consumer defined in the agreement to access (consume) this asset.
* @param {string} agreementId Agreement ID.
* @param {string} did Asset ID.
* @param {string} grantee Consumer address.
* @param {Account} from Account of sender.
*/
public async grantAccess(agreementId: string, did: string, grantee: string, from: Account = new Account()) {
const {accessSecretStoreCondition} = this.keeper.conditions
const receipt = await accessSecretStoreCondition.fulfill(agreementId, did, grantee, from.getId())
return !!receipt.events.Fulfilled
}
/**
* Transfer the escrow or locked tokens from the LockRewardCondition contract to the publisher's account.
* This should be allowed after access has been given to the consumer and the asset data is downloaded.
*
* If the AccessSecretStoreCondition already timed out, this function will do a refund by transferring
* the token amount to the original consumer.
* @param {string} agreementId Agreement ID.
* @param {number} amount Asset amount.
* @param {string} did Asset ID.
* @param {string} consumer Consumer address.
* @param {string} publisher Publisher address.
* @param {Account} from Account of sender.
*/
public async releaseReward(
agreementId: string,
amount: number,
did: string,
consumer: string,
publisher: string,
from: Account = new Account(),
) {
const {escrowReward, accessSecretStoreCondition, lockRewardCondition} = this.keeper.conditions
const conditionIdAccess = await accessSecretStoreCondition.generateIdHash(agreementId, did, consumer)
2019-03-15 15:29:47 +01:00
const conditionIdLock = await lockRewardCondition.generateIdHash(agreementId, escrowReward.getAddress(), amount)
const receipt = await escrowReward.fulfill(
agreementId,
amount,
publisher,
2019-03-14 19:02:41 +01:00
consumer,
conditionIdLock,
conditionIdAccess,
from.getId(),
)
return !!receipt.events.Fulfilled
}
}