codegen: generate file with contract addresses #872
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello!
I should probably have opened an issue to discuss if this is a feature you would like to have, but the implementation was so quick that I thought it would be better if I make this request with a working example.
I'll describe below why I want this feature and implementation details.
The Problem
I have multiple subgraph manifests, one for ethereum mainnet, another for rinkeby, and another for local development. Most of the manifest contains the same configuration, but the data inside
dataSources[].source
is different. For example, on mainnet the contract address is0x123...456
, on rinkeby it is0x234...567
and on local it is0x345...678
.In my specific case, I have some conditional logic that uses the contract address to decide what to do.
https://github.com/oddworx/genzee-subgraph/blob/d40d044c5eaf4f480d66c399b59d171fbafadbc5/src/genzee.ts#L12-L27
And I currently store the contract addresses on code:
https://github.com/oddworx/genzee-subgraph/blob/d40d044c5eaf4f480d66c399b59d171fbafadbc5/src/common.ts#L6-L18
This approach is really painful, it requires me to comment/uncomment that code all the time and requires me to keep in sync addresses (if I deploy it to rinkeby again, and want to update addresses, I need to update in multiple places).
Solution
The solution I want to propose here is for the codegen tool to generate a file
addresses.ts
that exports the addresses like so:Projects with multiple manifest can then just run
graph codegen mymanifest.yml
and have the values updated.Implementation
The implementation is pretty simple, just loop through
dataSources
and create each export line usingdataSource.name
anddataSource.source.address
.I'm not very familiar with the codebase, but I think there might have an issue with
L60
ofsrc/type-generator.js
because not every source is an ABI. Instead of reading all the code and getting familiar with everything, I thought would be better if a maintainer can guide me on what we should do there.