ponder-sh / ponder

A backend framework for crypto apps
https://ponder.sh
MIT License
629 stars 97 forks source link

feat: Enrich context to contracts created by factories #1203

Open jjranalli opened 3 days ago

jjranalli commented 3 days ago

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?

0xOlias commented 2 days ago

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 commented 2 days ago

@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:

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!