From 3f397866a928d3ffa1426342748c0f763cd43a40 Mon Sep 17 00:00:00 2001 From: Jay Ji Date: Tue, 22 Aug 2023 17:26:24 +1200 Subject: [PATCH] Fix option address field (#146) * Convert option address to 'to' * typo --- .../node/src/indexer/fetch.service.spec.ts | 42 ++++++++++++++++++- packages/node/src/indexer/fetch.service.ts | 38 +++++++++++------ 2 files changed, 67 insertions(+), 13 deletions(-) diff --git a/packages/node/src/indexer/fetch.service.spec.ts b/packages/node/src/indexer/fetch.service.spec.ts index 61e83f0841..e797b65c47 100644 --- a/packages/node/src/indexer/fetch.service.spec.ts +++ b/packages/node/src/indexer/fetch.service.spec.ts @@ -192,7 +192,47 @@ describe('Dictionary queries', () => { entity: 'evmTransactions', conditions: [ { - field: 'address', + field: 'to', + matcher: 'equalTo', + value: '0x7ceb23fd6bc0add59e62ac25578270cff1b9f619', + }, + { field: 'func', matcher: 'equalTo', value: '0x095ea7b3' }, + ], + }, + ]); + }); + + it('If ds option and filter both provide contract address, it should use ds options one ', () => { + const ds: SubqlRuntimeDatasource = { + kind: EthereumDatasourceKind.Runtime, + assets: new Map(), + options: { + abi: 'erc20', + address: '0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619', + }, + startBlock: 1, + mapping: { + file: '', + handlers: [ + { + handler: 'handleTransfer', + kind: EthereumHandlerKind.Call, + filter: { + to: '0xabcde', + function: 'approve(address spender, uint256 rawAmount)', + }, + }, + ], + }, + }; + + const result = buildDictionaryQueryEntries([ds], 1); + expect(result).toEqual([ + { + entity: 'evmTransactions', + conditions: [ + { + field: 'to', matcher: 'equalTo', value: '0x7ceb23fd6bc0add59e62ac25578270cff1b9f619', }, diff --git a/packages/node/src/indexer/fetch.service.ts b/packages/node/src/indexer/fetch.service.ts index 60e0046e21..7e863ecc97 100644 --- a/packages/node/src/indexer/fetch.service.ts +++ b/packages/node/src/indexer/fetch.service.ts @@ -116,6 +116,12 @@ function callFilterToQueryEntry( ): DictionaryQueryEntry { const conditions: DictionaryQueryCondition[] = []; appendDsOptions(dsOptions, conditions); + + for (const condition of conditions) { + if (condition.field === 'address') { + condition.field = 'to'; + } + } if (filter.from) { conditions.push({ field: 'from', @@ -123,19 +129,27 @@ function callFilterToQueryEntry( matcher: 'equalTo', }); } - if (filter.to) { - conditions.push({ - field: 'to', - value: filter.to.toLowerCase(), - matcher: 'equalTo', - }); - } else if (filter.to === null) { - conditions.push({ - field: 'to', - value: true as any, // TODO update types to allow boolean - matcher: 'isNull', - }); + const optionsAddresses = conditions.find((c) => c.field === 'to'); + if (!optionsAddresses) { + if (filter.to) { + conditions.push({ + field: 'to', + value: filter.to.toLowerCase(), + matcher: 'equalTo', + }); + } else if (filter.to === null) { + conditions.push({ + field: 'to', + value: true as any, // TODO update types to allow boolean + matcher: 'isNull', + }); + } + } else { + logger.warn( + `TransactionFilter 'to' conflict with 'address' in data source options`, + ); } + if (filter.function) { conditions.push({ field: 'func',