Skip to content

Commit

Permalink
Merge pull request #672 from nevermined-io/fix/key_def
Browse files Browse the repository at this point in the history
Small events improvements
  • Loading branch information
aaitor authored Jun 6, 2024
2 parents 46ec041 + 6aed490 commit 89b2e7b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 45 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
43 changes: 18 additions & 25 deletions src/events/ContractEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 []
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/events/SubgraphEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
11 changes: 10 additions & 1 deletion src/keeper/contracts/ContractBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/keeper/contracts/Nft721Contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down
31 changes: 18 additions & 13 deletions src/keeper/contracts/managers/AgreementStoreManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ export class AgreementStoreManager extends ContractBase {
return this.call('owner', [])
}

public async getAgreement(agreementId: string): Promise<AgreementData> {
public async getAgreement(agreementId: string, tryOnchain = false): Promise<AgreementData> {
const templateId: string = getChecksumAddress(
await this.call('getAgreementTemplate', [zeroX(agreementId)]),
)

const template = this.templates[templateId]

if (!template) {
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/keeper/contracts/templates/BaseTemplate.abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ export abstract class BaseTemplate<Params, S extends Service>
txparams?: TxParameters,
): Promise<void> {
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(
Expand All @@ -156,6 +159,7 @@ export abstract class BaseTemplate<Params, S extends Service>
params,
extra.service_index,
)

if (agreementData.agreementId !== agreement_id) {
throw new Error(
`Agreement doesn't match ${agreement_id} should be ${agreementData.agreementId}`,
Expand Down

0 comments on commit 89b2e7b

Please sign in to comment.