mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
added executeAgreement event listener
This commit is contained in:
parent
a904649c33
commit
3bf09e63a1
@ -172,21 +172,23 @@ export default class Ocean {
|
|||||||
const accessService: Service = ddo.findServiceByType("Access")
|
const accessService: Service = ddo.findServiceByType("Access")
|
||||||
const metadataService: Service = ddo.findServiceByType("Metadata")
|
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 price = metadataService.metadata.base.price
|
||||||
const balance = await consumer.getOceanBalance()
|
const balance = await consumer.getOceanBalance()
|
||||||
if (balance < price) {
|
if (balance < price) {
|
||||||
throw new Error(`Not enough ocean tokens! Should have ${price} but has ${balance}`)
|
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) => {
|
event.listenOnce((data) => {
|
||||||
|
|
||||||
new ServiceAgreement(serviceAgreementId)
|
Logger.log("Paying asset")
|
||||||
.buyAsset(id,
|
new ServiceAgreement(data.returnValues.serviceAgreementId)
|
||||||
|
.payAsset(id,
|
||||||
metadataService.metadata.base.price,
|
metadataService.metadata.base.price,
|
||||||
consumer,
|
consumer,
|
||||||
)
|
)
|
||||||
|
@ -195,8 +195,7 @@ export default class ServiceAgreement extends OceanBase {
|
|||||||
super(serviceAgreementId)
|
super(serviceAgreementId)
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo: sync with samer
|
public async payAsset(assetId: string, price: number, consumer: Account): Promise<boolean> {
|
||||||
public async buyAsset(assetId: string, price: number, consumer: Account): Promise<boolean> {
|
|
||||||
const {paymentConditions, token} = await Keeper.getInstance()
|
const {paymentConditions, token} = await Keeper.getInstance()
|
||||||
|
|
||||||
await token.approve(paymentConditions.getAddress(), price, consumer.getId())
|
await token.approve(paymentConditions.getAddress(), price, consumer.getId())
|
||||||
|
@ -122,7 +122,7 @@ describe("ServiceAgreement", () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("#buyAsset()", () => {
|
describe("#payAsset()", () => {
|
||||||
it("should lock the payment in that service agreement", async () => {
|
it("should lock the payment in that service agreement", async () => {
|
||||||
|
|
||||||
const did: string = `did:op:${assetId}`
|
const did: string = `did:op:${assetId}`
|
||||||
@ -145,7 +145,7 @@ describe("ServiceAgreement", () => {
|
|||||||
// get funds
|
// get funds
|
||||||
await consumerAccount.requestTokens(metaDataService.metadata.base.price)
|
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)
|
consumerAccount)
|
||||||
assert(paid)
|
assert(paid)
|
||||||
})
|
})
|
||||||
@ -173,7 +173,7 @@ describe("ServiceAgreement", () => {
|
|||||||
// get funds
|
// get funds
|
||||||
await consumerAccount.requestTokens(metaDataService.metadata.base.price)
|
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)
|
consumerAccount)
|
||||||
assert(paid)
|
assert(paid)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user