Skip to content

Commit

Permalink
mustache custom script
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Lima committed Oct 31, 2023
1 parent 89731bd commit 24a0cd8
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 15 deletions.
3 changes: 1 addition & 2 deletions config/goerli.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
{
"name": "AavegotchiDiamond",
"address": "0x450c91d1fe9f3d57b91218f6ff96f7994eec4d32",
"startBlock": 8099655,
"isFirstElement": true
"startBlock": 8099655
}
]
}
6 changes: 2 additions & 4 deletions config/mumbai.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
{
"name": "AavegotchiDiamond",
"address": "0x83e73D9CF22dFc3A767EA1cE0611F7f50306622e",
"startBlock": 34467860,
"isFirstElement": true
"startBlock": 34467860
},
{
"name": "RealmDiamond",
Expand All @@ -17,8 +16,7 @@
{
"name": "WearableDiamond",
"address": "0x1b1bcB49A744a09aEd636CDD9893508BdF1431A8",
"startBlock": 34467877,
"isFirstElement": true
"startBlock": 34467877
}
]
}
6 changes: 2 additions & 4 deletions config/polygon.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
{
"name": "AavegotchiDiamond",
"address": "0x86935f11c86623dec8a25696e1c19a8659cbf95d",
"startBlock": 11516320,
"isFirstElement": true
"startBlock": 11516320
},
{
"name": "RealmDiamond",
Expand All @@ -22,8 +21,7 @@
{
"name": "WearableDiamond",
"address": "0x58de9aabcaeec0f69883c94318810ad79cc6a44f",
"startBlock": 35999793,
"isFirstElement": true
"startBlock": 35999793
}
]
}
60 changes: 60 additions & 0 deletions mustache.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const Mustache = require('mustache')
const fs = require('fs')

// receive network name as argument
async function main(args) {
const networkName = getNetworkName(args)
const networkData = require(`./config/${networkName}.json`)
const template = fs.readFileSync('./subgraph.template.yaml', 'utf8')
const parsedData = putFirstElementFlag(networkData)
const output = Mustache.render(template, parsedData)
writeOutput(output)
}

function getNetworkName(args) {
if (args.length < 3) {
throw new Error('Missing network name argument')
}
return args[2]
}

function writeOutput(output) {
fs.writeFileSync('./subgraph.yaml', output)
}

function putFirstElementFlag(data) {
const dataWithFirstElementFlag = replaceFirstElementFlag(data)
return arrayToObj(dataWithFirstElementFlag)
}
function replaceFirstElementFlag(data) {
return Object.keys(data).map((key, index) => {
if (Array.isArray(data[key]) && data[key].length > 0) {
return {
[key]: data[key].map((item, index) => {
if (index === 0) {
return { ...item, isFirstElement: true }
} else {
return item
}
}),
}
}
return { [key]: data[key] }
})
}

function arrayToObj(data) {
return data.reduce((acc, curr) => {
return { ...acc, ...curr }
}, {})
}

main(process.argv)
.then(() => {
console.log('Done')
process.exit()
})
.catch(err => {
console.error(err)
process.exit(1)
})
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
},
"scripts": {
"graph:auth": "graph auth --product hosted-service",
"build:goerli": "mustache ./config/goerli.json subgraph.template.yaml > subgraph.yaml && graph codegen subgraph.yaml && graph build subgraph.yaml",
"build:mumbai": "mustache ./config/mumbai.json subgraph.template.yaml > subgraph.yaml && graph codegen subgraph.yaml && graph build subgraph.yaml",
"build:polygon": "mustache ./config/polygon.json subgraph.template.yaml > subgraph.yaml && graph codegen subgraph.yaml && graph build subgraph.yaml",
"build:goerli": "node mustache.config.js goerli && graph codegen subgraph.yaml && graph build subgraph.yaml",
"build:mumbai": "node mustache.config.js mumbai && graph codegen subgraph.yaml && graph build subgraph.yaml",
"build:polygon": "node mustache.config.js polygon && graph codegen subgraph.yaml && graph build subgraph.yaml",
"deploy:goerli": "graph deploy --node https://api.thegraph.com/deploy/orium-network/nft-roles-goerli",
"deploy:mumbai": "graph deploy --node https://api.thegraph.com/deploy/orium-network/nft-roles-mumbai",
"deploy:polygon": "graph deploy --node https://api.thegraph.com/deploy/orium-network/nft-roles-polygon",
Expand Down
4 changes: 2 additions & 2 deletions subgraph.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ schema:
file: ./schema.graphql
dataSources:

{{#erc721}}
{{#erc721}}
{{#isFirstElement}}
- name: ERC721
{{/isFirstElement}}
Expand All @@ -30,7 +30,7 @@ dataSources:
- event: Transfer(indexed address,indexed address,indexed uint256)
handler: handleTransfer
file: ./src/erc721/index.ts
{{/erc721}}
{{/erc721}}

{{#erc1155}}
{{#isFirstElement}}
Expand Down

0 comments on commit 24a0cd8

Please sign in to comment.