Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enrich context to contracts created by factories #1203

Open
jjranalli opened this issue Oct 30, 2024 · 2 comments
Open

feat: Enrich context to contracts created by factories #1203

jjranalli opened this issue Oct 30, 2024 · 2 comments

Comments

@jjranalli
Copy link

currently when a factory creates a contract it's not possible to enrich its context arbitrarily.

this is often useful when events on the child contract require some data different than its address, like an id, which could be initialised in its context upon creation.

on the graph, this happens via createWithContext.

would it be possible to add the feature to ponder?

@jjranalli jjranalli changed the title [Feature Request] Enrich context to contracts created by factories feat: Enrich context to contracts created by factories Oct 30, 2024
@typedarray
Copy link
Collaborator

As you probably noticed, in Ponder the factory concept happens the config/sync layer, which makes this context idea a bit more difficult.

Could you share specific details about your use case for this feature (e.g. what does the factory event look like, what context are you trying to include, do you currently have a workaround, etc. )? That often sparks an idea on our end.

@jjranalli
Copy link
Author

jjranalli commented Oct 31, 2024

@0xOlias in our case each child contract is also linked to a tokenId which we use as the id everywhere instead of the address. not just on the indexer but pretty much in all of our code, so would prefer not changing it.

my current workaround is retrieving said ID from the address at the beginning in each handler, and then use it as needed

const getSlicerId = async (slicerAddress: `0x${string}`) => {
  const { items: slicers } = await db.Slicer.findMany({
    where: {
      address: slicerAddress
    },
    limit: 1
  })
  return slicers[0]!.id
}

I figured it would be tricky to implement in ponder given how it's setup in the config. On the top of my head I can think of two possible solutions:

  • Allow specifying additional parameters in the factory config, coming from the same event, which I can then retrieve in the context. for example:
export default createConfig({
  // ...
  contracts: {
    Slicer: {
      abi: SlicerAbi,
      network: "base",
      factory: {
        address: "0x...",
        event: TokenSlicedEvent,
        // The name of the parameter that contains the address of the new child contract.
        parameter: "slicerAddress",
        // additional context object
        context: {
          // The name of the parameter that contains the id of the new child contract.
          id: "slicerId"
        }
      }
    }
  }
  • add a helper function to arbitrarily enrich the context of an entity from its id

former is likely easier and more efficient, but requires the additional context to be present in the same event containing the address. this works in my case, but may not work in others. latter would be more customisable

hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants