2019-03-04 13:05:27 +01:00
|
|
|
import Account from "./Account"
|
2019-03-21 02:56:58 +01:00
|
|
|
import { Instantiable, InstantiableConfig } from "../Instantiable.abstract"
|
2019-03-04 13:05:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Agreements Conditions submodule of Ocean Protocol.
|
|
|
|
*/
|
2019-03-21 02:56:58 +01:00
|
|
|
export class OceanAgreementsConditions extends Instantiable {
|
2019-03-04 13:05:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the instance of OceanAgreementsConditions.
|
|
|
|
* @return {Promise<OceanAgreementsConditions>}
|
|
|
|
*/
|
2019-03-21 02:56:58 +01:00
|
|
|
public static async getInstance(config: InstantiableConfig): Promise<OceanAgreementsConditions> {
|
|
|
|
const instance = new OceanAgreementsConditions()
|
|
|
|
instance.setInstanceConfig(config)
|
2019-03-04 13:05:27 +01:00
|
|
|
|
2019-03-21 02:56:58 +01:00
|
|
|
return instance
|
2019-03-04 13:05:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-05-23 11:52:36 +02:00
|
|
|
public async lockReward(agreementId: string, amount: number | string, from?: Account) {
|
2019-03-21 02:56:58 +01:00
|
|
|
const {lockRewardCondition, escrowReward} = this.ocean.keeper.conditions
|
2019-03-04 13:05:27 +01:00
|
|
|
|
2019-03-21 02:56:58 +01:00
|
|
|
await this.ocean.keeper.token.approve(lockRewardCondition.getAddress(), amount, from.getId())
|
2019-03-04 13:05:27 +01:00
|
|
|
|
2019-03-21 02:56:58 +01:00
|
|
|
const receipt = await lockRewardCondition.fulfill(agreementId, escrowReward.getAddress(), amount, from && from.getId())
|
2019-03-14 15:47:31 +01:00
|
|
|
return !!receipt.events.Fulfilled
|
2019-03-04 13:05:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-03-21 02:56:58 +01:00
|
|
|
public async grantAccess(agreementId: string, did: string, grantee: string, from?: Account) {
|
|
|
|
const {accessSecretStoreCondition} = this.ocean.keeper.conditions
|
2019-03-04 13:05:27 +01:00
|
|
|
|
2019-03-21 02:56:58 +01:00
|
|
|
const receipt = await accessSecretStoreCondition.fulfill(agreementId, did, grantee, from && from.getId())
|
2019-03-14 15:47:31 +01:00
|
|
|
return !!receipt.events.Fulfilled
|
2019-03-04 13:05:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
2019-03-21 02:56:58 +01:00
|
|
|
from?: Account,
|
2019-03-04 13:05:27 +01:00
|
|
|
) {
|
2019-03-21 02:56:58 +01:00
|
|
|
const {escrowReward, accessSecretStoreCondition, lockRewardCondition} = this.ocean.keeper.conditions
|
2019-03-04 13:05:27 +01:00
|
|
|
|
|
|
|
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)
|
2019-03-04 13:05:27 +01:00
|
|
|
|
2019-03-14 15:47:31 +01:00
|
|
|
const receipt = await escrowReward.fulfill(
|
2019-03-04 13:05:27 +01:00
|
|
|
agreementId,
|
|
|
|
amount,
|
|
|
|
publisher,
|
2019-03-14 19:02:41 +01:00
|
|
|
consumer,
|
2019-03-04 13:05:27 +01:00
|
|
|
conditionIdLock,
|
|
|
|
conditionIdAccess,
|
2019-03-21 02:56:58 +01:00
|
|
|
from && from.getId(),
|
2019-03-04 13:05:27 +01:00
|
|
|
)
|
2019-03-14 15:47:31 +01:00
|
|
|
return !!receipt.events.Fulfilled
|
2019-03-04 13:05:27 +01:00
|
|
|
}
|
|
|
|
}
|