Factory contracts #264
Replies: 7 comments 5 replies
-
Hey @0xOlias any update on the status? |
Beta Was this translation helpful? Give feedback.
-
I like this proposal |
Beta Was this translation helpful? Give feedback.
-
Is the |
Beta Was this translation helpful? Give feedback.
-
I think most projects that use the factory contract pattern only need to be able to do something like UniswapV2Factory + UniswapV2Pool (V3 is the same) So I think what in this proposal is enough for most use cases Is there an ETA for this? |
Beta Was this translation helpful? Give feedback.
-
i've run into a protocol that's deploying multiple contracts as part of its factory and emitting an event like children: [{
factoryEventAddressArgument: "firstThing",
name: "FirstThing",
abi: FirstThingAbi,
}, {
factoryEventAddressArgument: "secondThing",
name: "SecondThing",
abi: SecondThingAbi,
}], |
Beta Was this translation helpful? Give feedback.
-
Thanks SO much for mentioning this, very timely. I also noticed this on a protocol I've been using for testing. Will update the API accordingly, and make sure the sync engine doesn't duplicate work when handling multiple children. I have factory contracts working and tested on this branch. Will release an alpha version within the next day or so. |
Beta Was this translation helpful? Give feedback.
-
Love to try this |
Beta Was this translation helpful? Give feedback.
-
Background
Quick background: Subgraphs allow you to register new child contracts at will inside event handler functions. This means you can use the database and generally write any logic you want around registering child contracts. (In subgraph lingo these are Data Source Templates https://thegraph.com/docs/en/developing/creating-a-subgraph/#data-source-templates).
This approach has a very large hidden cost which is that the "sync" service (fetch & cache raw blockchain data) becomes coupled with the "indexing" service (run user code). In Ponder these are completely decoupled. So, it would require major rework to support this way of doing factory contracts/dynamic data sources.
Proposal
Instead, we can support a slightly less powerful way of registering child contracts that looks like this:
Here, the
getAddress
required option is a synchronous function that extracts the child contract address from the factory contract event. Now, you can register event handlers that will run for every instance of the child contract:If you want, you could use the
getAddress
function to filter for specific hard-coded child contracts by returningundefined
. But note that you CANNOT use entity store API methods, and the function must be synchronous (cannot make HTTP requests etc).Edge cases / limitations
Factories of factories
Not supported. Key tradeoff. If your app/protocol uses this pattern, please say something.
I only want to index a subset of child contracts
Supported via a custom
getAddress
function.Also, this use case can often be solved by custom log filter with a list of known contract addresses rather than a factory.
I want to use the database to decide if/when to register a child contract
Not supported. The only way to register a child contract is an event log on a factory contract as described above.
Factories that create more than one kind of child contract
Supported. The
factory
field also accepts an array of child contract configs.Register child contracts based on transaction calls
Eventually, Ponder will support indexing based on transaction calls (in addition to event logs). Haven't fully explored this, but it seems likely that we could support a similar pattern here where the
getAddress
function gets the decoded transactioninput
as an argument.Beta Was this translation helpful? Give feedback.
All reactions