From 2196717b145c4fa6a7e89bd81731b448ebdd3908 Mon Sep 17 00:00:00 2001 From: juanmardefago Date: Wed, 20 Mar 2024 04:57:38 -0300 Subject: [PATCH] fix: parsing issues --- src/mappings/ipfs.ts | 23 ++++++++++++++++++++--- tests/yaml.test.ts | 14 ++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 tests/yaml.test.ts diff --git a/src/mappings/ipfs.ts b/src/mappings/ipfs.ts index 953f6f7..8e4f2c9 100644 --- a/src/mappings/ipfs.ts +++ b/src/mappings/ipfs.ts @@ -174,7 +174,8 @@ function createDataSources(dataSourceStrings: String[], manifestId: string): voi dataSource.manifest = manifestId dataSource.name = getManifestFieldFromExtract(dataSourceString, "name") dataSource.address = Bytes.fromHexString(getManifestFieldFromExtract(dataSourceString, "address")) - dataSource.startBlock = BigInt.fromString(getManifestFieldFromExtract(dataSourceString, "startBlock")) + let startBlock = getManifestFieldFromExtract(dataSourceString, "startBlock") + dataSource.startBlock = BigInt.fromString(startBlock == "" ? "0" : startBlock) dataSource.network = getManifestFieldFromExtract(dataSourceString, "network") dataSource.apiVersion = getManifestFieldFromExtract(dataSourceString, "apiVersion") dataSource.isTemplate = false @@ -209,6 +210,22 @@ function createBlockHandlers(): void { } -function getManifestFieldFromExtract(manifestExtract: String, fieldName: String): string { - return manifestExtract.split(fieldName+": ", 2)[1].split("\n", 1)[0].toString() +export function getManifestFieldFromExtract(manifestExtract: String, fieldName: String): string { + let res = "" + let fieldNameSplit = manifestExtract.split(fieldName+":") + if (fieldNameSplit.length > 1) { + res = cleanStr(fieldNameSplit[1].split("\n")[0]) + } + return res +} + +function cleanStr(str: String): string { + let res = str.trim() + if (res.startsWith("'") || res.startsWith("\"")) { + res = res.slice(1) + } + if (res.endsWith("'") || res.endsWith("\"")) { + res = res.slice(0, res.length - 1) + } + return res } \ No newline at end of file diff --git a/tests/yaml.test.ts b/tests/yaml.test.ts new file mode 100644 index 0000000..030d35b --- /dev/null +++ b/tests/yaml.test.ts @@ -0,0 +1,14 @@ +import { test, assert } from 'matchstick-as/assembly/index' +import { getManifestFieldFromExtract } from '../src/mappings/ipfs' + +test('Check get field from extract', () => { + let extract = "dataSources:\n - kind: ethereum\n mapping:\n abis:\n - file:\n /: /ipfs/QmdxvRJHRaDpLKYdiQJ294CxtEQExftBmnFxsqrMKcyja2\n name: milady\n apiVersion: 0.0.7\n entities:\n - Approval\n - ApprovalForAll\n - OwnershipTransferred\n - Transfer\n eventHandlers:\n - event: 'Approval(indexed address,indexed address,indexed uint256)'\n handler: handleApproval\n - event: 'ApprovalForAll(indexed address,indexed address,bool)'\n handler: handleApprovalForAll\n - event: 'OwnershipTransferred(indexed address,indexed address)'\n handler: handleOwnershipTransferred\n - event: 'Transfer(indexed address,indexed address,indexed uint256)'\n handler: handleTransfer\n file:\n /: /ipfs/QmXtmRDcZNNR2FnzAgULcLQVnV89fEAowJXipSpfCgKZEp\n kind: ethereum/events\n language: wasm/assemblyscript\n name: milady\n network: mainnet\n source:\n abi: milady\n address: '0x5af0d9827e0c53e4799bb226655a1de152a425a5'\n startBlock: 13090020\ndescription: Mailady NFT Subgraph\nschema:\n file:\n /: /ipfs/QmP8HWfXhWums3QTxMJ24e83ZnUkHmRVmHo6aTMYh31N72\nspecVersion: 0.0.5\n" + let name = getManifestFieldFromExtract(extract, "name") + let apiVersion = getManifestFieldFromExtract(extract, "apiVersion") + let network = getManifestFieldFromExtract(extract, "network") + let address = getManifestFieldFromExtract(extract, "address") + assert.stringEquals("milady", name) + assert.stringEquals("0.0.7", apiVersion) + assert.stringEquals("mainnet", network) + assert.stringEquals("0x5af0d9827e0c53e4799bb226655a1de152a425a5", address) +}) \ No newline at end of file