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

165 lines
5.0 KiB
TypeScript
Raw Normal View History

2019-06-20 00:20:09 +02:00
import Account from './Account'
import { Instantiable, InstantiableConfig } from '../Instantiable.abstract'
/**
* Agreements Conditions submodule of Ocean Protocol.
*/
export class OceanAgreementsConditions extends Instantiable {
/**
* Returns the instance of OceanAgreementsConditions.
* @return {Promise<OceanAgreementsConditions>}
*/
2019-11-15 00:00:10 +01:00
public static async getInstance(
config: InstantiableConfig
): Promise<OceanAgreementsConditions> {
const instance = new OceanAgreementsConditions()
instance.setInstanceConfig(config)
return instance
}
/**
* 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-11-15 00:00:10 +01:00
public async lockReward(
agreementId: string,
amount: number | string,
from?: Account
) {
2019-09-09 12:18:54 +02:00
const { lockRewardCondition, escrowReward } = this.ocean.keeper.conditions
try {
2019-11-15 00:00:10 +01:00
await this.ocean.keeper.token.approve(
lockRewardCondition.getAddress(),
amount,
from.getId()
)
const receipt = await lockRewardCondition.fulfill(
agreementId,
escrowReward.getAddress(),
amount,
from && from.getId()
)
return !!receipt.events.Fulfilled
} catch {
return false
}
}
/**
* 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-11-15 00:00:10 +01:00
public async grantAccess(
agreementId: string,
did: string,
grantee: string,
from?: Account
) {
try {
const { accessSecretStoreCondition } = this.ocean.keeper.conditions
2019-11-15 00:00:10 +01:00
const receipt = await accessSecretStoreCondition.fulfill(
agreementId,
did,
grantee,
from && from.getId()
)
2019-09-05 13:32:56 +02:00
return !!receipt.events.Fulfilled
} catch {
return false
}
}
/**
2020-01-29 16:29:26 +01:00
* Authorize the consumer defined in the agreement to compute on this asset.
2019-09-05 13:32:56 +02:00
* @param {string} agreementId Agreement ID.
* @param {string} did Asset ID.
* @param {string} grantee Consumer address.
* @param {Account} from Account of sender.
*/
2020-01-29 16:29:26 +01:00
public async grantCompute(
2019-11-15 00:00:10 +01:00
agreementId: string,
did: string,
grantee: string,
from?: Account
) {
2019-09-05 13:32:56 +02:00
try {
const { computeExecutionCondition } = this.ocean.keeper.conditions
2019-11-15 00:00:10 +01:00
const receipt = await computeExecutionCondition.fulfill(
agreementId,
did,
grantee,
from && from.getId()
)
return !!receipt.events.Fulfilled
} catch {
return false
}
}
/**
* 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-06-20 00:20:09 +02:00
from?: Account
) {
try {
2019-11-15 00:00:10 +01:00
const {
escrowReward,
accessSecretStoreCondition,
lockRewardCondition
} = this.ocean.keeper.conditions
2019-11-15 00:00:10 +01:00
const conditionIdAccess = await accessSecretStoreCondition.generateIdHash(
agreementId,
did,
consumer
)
const conditionIdLock = await lockRewardCondition.generateIdHash(
agreementId,
escrowReward.getAddress(),
amount
)
const receipt = await escrowReward.fulfill(
agreementId,
amount,
publisher,
consumer,
conditionIdLock,
conditionIdAccess,
from && from.getId()
)
return !!receipt.events.Fulfilled
} catch {
return false
}
}
}