diff --git a/CHANGELOG.md b/CHANGELOG.md index 13f6b1567..7a1bd6d2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file. Dates are d Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). +#### [v3.0.12](https://github.com/nevermined-io/sdk-js/compare/v3.0.11...v3.0.12) + +> 6 June 2024 + +- fix: events improvements [`ccbe246`](https://github.com/nevermined-io/sdk-js/commit/ccbe24699c1a1d7a94104b73e95453a7f975e004) +- chore: adding v3.0.11 Changelog updates [`cb289f2`](https://github.com/nevermined-io/sdk-js/commit/cb289f2c5cbbdc55ee3399534c9e628a2d6cbd95) + #### [v3.0.11](https://github.com/nevermined-io/sdk-js/compare/v3.0.10...v3.0.11) > 5 June 2024 diff --git a/package.json b/package.json index 9b60964a9..e7c461891 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nevermined-io/sdk", - "version": "3.0.11", + "version": "3.0.12", "description": "Javascript SDK for connecting with Nevermined Data Platform ", "main": "./dist/node/sdk.js", "typings": "./dist/node/sdk.d.ts", diff --git a/src/events/ContractEvent.ts b/src/events/ContractEvent.ts index 9cf3baad8..2ce97eaec 100644 --- a/src/events/ContractEvent.ts +++ b/src/events/ContractEvent.ts @@ -29,31 +29,22 @@ export class ContractEvent extends NeverminedEvent { ) } - const args = this.filterToArgs(options.eventName, options.filterJsonRpc) - - return await this.contract.publicClient.getContractEvents({ - address: this.contract.contract.address, - abi: this.contract.contract.abi, - eventName: options.eventName, - args, - fromBlock: options.fromBlock, - toBlock: options.toBlock, - }) - - // const eventFilter = this.contract.contract.filters[options.eventName](...args) + try { + const args = this.filterToArgs(options.eventName, options.filterJsonRpc) - // const logs = await publicClient.getContractEvents({ - // address: usdcContractAddress, - // abi: erc20Abi, - // eventName: 'Transfer', - // args: { - // from: '0xd8da6bf26964af9d7eed9e03e53415d37aa96045', - // to: '0xa5cc3c03994db5b0d9a5eedd10cabab0813678ac' - // }, - // fromBlock: 16330000n, - // toBlock: 16330050n - // }) - // return this.contract.contract.queryFilter(eventFilter, options.fromBlock, options.toBlock) + const contractEvents = await this.contract.publicClient.getContractEvents({ + address: this.contract.contract.address, + abi: this.contract.contract.abi, + eventName: options.eventName, + args, + fromBlock: options.fromBlock, + toBlock: options.toBlock, + }) + return contractEvents + } catch (error) { + const errorMessage = `Error getting event: ${options.eventName} - ${error}` + throw new KeeperError(errorMessage) + } } public async getPastEvents(options: EventOptions): EventResult { @@ -73,9 +64,11 @@ export class ContractEvent extends NeverminedEvent { ) { const latestBlock = await this.client.public.getBlockNumber() options.fromBlock = latestBlock - 999n + this.logger.debug(`Getting events from block ${options.fromBlock} to ${latestBlock}`) } return await this.getEventData(options) - } catch { + } catch (error) { + this.logger.warn(`Error getting past events for ${options.eventName}: ${error}`) return [] } } diff --git a/src/events/SubgraphEvent.ts b/src/events/SubgraphEvent.ts index 30510f255..c8ca58e4c 100644 --- a/src/events/SubgraphEvent.ts +++ b/src/events/SubgraphEvent.ts @@ -52,13 +52,12 @@ export class SubgraphEvent extends NeverminedEvent { try { const methodName = getMethodName(options.eventName) const query = generateGql(methodName, options.filterSubgraph, options.result as GqlArgs) - const response = await this.subgraph.query({ - query: gql`query ${query}`, - }) + const response = await this.subgraph.query({ query: gql`query ${query}` }) return response.data[methodName] } catch (error) { - throw new GraphError(`Error calling executing query ${error}`) + const errorMessage = `Error getting subgraph event: ${options.eventName} - ${error}` + throw new GraphError(errorMessage) } } diff --git a/src/keeper/contracts/ContractBase.ts b/src/keeper/contracts/ContractBase.ts index 81f232526..e9e11c470 100644 --- a/src/keeper/contracts/ContractBase.ts +++ b/src/keeper/contracts/ContractBase.ts @@ -308,13 +308,22 @@ export abstract class ContractBase extends Instantiable { ) { const functionInputs = getInputsOfFunctionFormatted(this.contract.abi, name, args) // Uncomment to debug contract calls - if (name === 'XXXXX') { + if (name === 'XXXXXX') { const functionSignature = getSignatureOfFunction(this.contract.abi, name, args) console.debug(`Making contract call ....: ${name} - ${from}`) console.debug(`With args`, args) console.debug(`And signature - ${JSON.stringify(functionSignature, jsonReplacer)}`) console.debug('And amount: ', txparams.value) } + // abi.filter(({name, inputs}) => name === 'closeSignals' && inputs.length === 1) + // const abiFilter = this.contract.abi.filter((abi) => abi.name === name && abi.inputs.length === args.length) + // const abiFilter = this.contract.abi.filter((abi) => abi.name === name && abi.type === 'function') + // console.debug('abi', this.contract.abi.filter((abi) => abi.name === name)) + // console.debug('abiFilter', abiFilter) + // if (!abiFilter) + // throw new KeeperError( + // `No ABI found for method "${name}" with ${args.length} arguments on contract "${this.contractName}"`, + // ) const { gasLimit, value } = txparams // make the call diff --git a/src/keeper/contracts/Nft721Contract.ts b/src/keeper/contracts/Nft721Contract.ts index 3c2622534..ba633a4f7 100644 --- a/src/keeper/contracts/Nft721Contract.ts +++ b/src/keeper/contracts/Nft721Contract.ts @@ -88,7 +88,7 @@ export class Nft721Contract extends NFTContractsBase { from: NvmAccount, txParams?: TxParameters, ) { - return this.sendFrom('mint', [to, didToTokenId(did), url], from, txParams) + return this.send('mint', from, [to, didToTokenId(did), url], txParams) } /** diff --git a/src/keeper/contracts/managers/AgreementStoreManager.ts b/src/keeper/contracts/managers/AgreementStoreManager.ts index 7fdc25fd1..95582292a 100644 --- a/src/keeper/contracts/managers/AgreementStoreManager.ts +++ b/src/keeper/contracts/managers/AgreementStoreManager.ts @@ -40,10 +40,11 @@ export class AgreementStoreManager extends ContractBase { return this.call('owner', []) } - public async getAgreement(agreementId: string): Promise { + public async getAgreement(agreementId: string, tryOnchain = false): Promise { const templateId: string = getChecksumAddress( await this.call('getAgreementTemplate', [zeroX(agreementId)]), ) + const template = this.templates[templateId] if (!template) { @@ -55,18 +56,22 @@ export class AgreementStoreManager extends ContractBase { const events = await template.getAgreementCreatedEvent(agreementId) if (!Array.isArray(events) || events.length == 0) { - //Could not find agreement with id: ${agreementId} - getting agreement data from the contract - const agreementData = await template.getAgreementData(agreementId) - return { - did: agreementData.did, - agreementId, - agreementIdSeed: '', - creator: agreementData.accessProvider, - didOwner: agreementData.accessProvider, - templateId, - conditionIdSeeds: [], - conditionIds: [], - } as AgreementData + if (tryOnchain) { + //Could not find agreement with id: ${agreementId} - getting agreement data from the contract + const agreementData = await template.getAgreementData(agreementId) + return { + did: agreementData.did, + agreementId, + agreementIdSeed: '', + creator: agreementData.accessProvider, + didOwner: agreementData.accessProvider, + templateId, + conditionIdSeeds: [], + conditionIds: [], + } as AgreementData + } else { + throw new KeeperError(`Could not find agreement with id: ${agreementId}`) + } } else { const values = events.map((e) => e.args || e) const [{ _did, _didOwner, _conditionIds, _conditionIdSeeds, _idSeed, _creator }] = values diff --git a/src/keeper/contracts/templates/BaseTemplate.abstract.ts b/src/keeper/contracts/templates/BaseTemplate.abstract.ts index 72723ee3a..887c3d2ea 100644 --- a/src/keeper/contracts/templates/BaseTemplate.abstract.ts +++ b/src/keeper/contracts/templates/BaseTemplate.abstract.ts @@ -147,6 +147,9 @@ export abstract class BaseTemplate txparams?: TxParameters, ): Promise { const ddo = await this.nevermined.assets.resolve(did) + if (!ddo) { + throw new Error(`Asset ${did} not found`) + } const agreement = await this.nevermined.keeper.agreementStoreManager.getAgreement(agreement_id) const agreementData = await this.instanceFromDDO( @@ -156,6 +159,7 @@ export abstract class BaseTemplate params, extra.service_index, ) + if (agreementData.agreementId !== agreement_id) { throw new Error( `Agreement doesn't match ${agreement_id} should be ${agreementData.agreementId}`,