diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index 9df0849..194bc7a 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -172,21 +172,23 @@ export default class Ocean { const accessService: Service = ddo.findServiceByType("Access") const metadataService: Service = ddo.findServiceByType("Metadata") - const event: ContractEvent = EventListener.subscribe(accessService.serviceAgreementContract.contractName, - accessService.serviceAgreementContract.events[0].name, { - serviceAgreementId, - }) - const price = metadataService.metadata.base.price const balance = await consumer.getOceanBalance() if (balance < price) { throw new Error(`Not enough ocean tokens! Should have ${price} but has ${balance}`) } + const event: ContractEvent = EventListener.subscribe( + accessService.serviceAgreementContract.contractName, + accessService.serviceAgreementContract.events[0].name, { + serviceAgreementId, + }) + event.listenOnce((data) => { - new ServiceAgreement(serviceAgreementId) - .buyAsset(id, + Logger.log("Paying asset") + new ServiceAgreement(data.returnValues.serviceAgreementId) + .payAsset(id, metadataService.metadata.base.price, consumer, ) diff --git a/src/ocean/ServiceAgreements/ServiceAgreement.ts b/src/ocean/ServiceAgreements/ServiceAgreement.ts index 6c8faa5..b7fe848 100644 --- a/src/ocean/ServiceAgreements/ServiceAgreement.ts +++ b/src/ocean/ServiceAgreements/ServiceAgreement.ts @@ -195,8 +195,7 @@ export default class ServiceAgreement extends OceanBase { super(serviceAgreementId) } - // todo: sync with samer - public async buyAsset(assetId: string, price: number, consumer: Account): Promise { + public async payAsset(assetId: string, price: number, consumer: Account): Promise { const {paymentConditions, token} = await Keeper.getInstance() await token.approve(paymentConditions.getAddress(), price, consumer.getId()) diff --git a/test/ocean/ServiceAgreement.test.ts b/test/ocean/ServiceAgreement.test.ts index 1472dba..c2b8385 100644 --- a/test/ocean/ServiceAgreement.test.ts +++ b/test/ocean/ServiceAgreement.test.ts @@ -122,7 +122,7 @@ describe("ServiceAgreement", () => { }) }) - describe("#buyAsset()", () => { + describe("#payAsset()", () => { it("should lock the payment in that service agreement", async () => { const did: string = `did:op:${assetId}` @@ -145,7 +145,7 @@ describe("ServiceAgreement", () => { // get funds await consumerAccount.requestTokens(metaDataService.metadata.base.price) - const paid: boolean = await serviceAgreement.buyAsset(assetId, metaDataService.metadata.base.price, + const paid: boolean = await serviceAgreement.payAsset(assetId, metaDataService.metadata.base.price, consumerAccount) assert(paid) }) @@ -173,7 +173,7 @@ describe("ServiceAgreement", () => { // get funds await consumerAccount.requestTokens(metaDataService.metadata.base.price) - const paid: boolean = await serviceAgreement.buyAsset(assetId, metaDataService.metadata.base.price, + const paid: boolean = await serviceAgreement.payAsset(assetId, metaDataService.metadata.base.price, consumerAccount) assert(paid)