coral-xyz / anchor

⚓ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.68k stars 1.35k forks source link

Listening for events from inner program on outer program fatals #1743

Open steveluscher opened 2 years ago

steveluscher commented 2 years ago

Problem statement

Imagine you have an outer program A. Program A has no defined events. Program A CPIs to an inner program B that does have events defined on it. If I try to listen to one of B's events on program A, Anchor will fatal:

// FATAL!
programA.addEventListener('SomeEventFromB', (event, slot) => { /* ... */ });

This is surely because the IDL of program A does not contain the event. When Anchor tries to deserialize the event, it bombs.

This works:

const programB = /* Load B's IDL and `new` up a `Program` */;
programB.addEventListener('SomeEventFromB', (event, slot) => { /* ... */ });

…but the downside is that I have to pull in a ton of program-B-related code to my dApp just to be able to parse its events.

Possible solution (RFC)

What would you think about teaching the Anchor compiler to include the Event definitions from program B's IDL in the IDL of program A, simply by virtue of the fact that there is a CPI to B from A?

Pros:

Cons:

paul-schaaf commented 2 years ago

related https://github.com/project-serum/anchor/issues/454