Skip to content

Commit

Permalink
fix: parsing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmardefago committed Mar 20, 2024
1 parent b554d7a commit 2196717
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/mappings/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
14 changes: 14 additions & 0 deletions tests/yaml.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})

0 comments on commit 2196717

Please sign in to comment.