Skip to content

Commit 3e20d7e

Browse files
improve(svmEventsClient): enable pda events querying (#989)
1 parent c9ee25e commit 3e20d7e

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

src/arch/svm/eventsClient.ts

+36-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export class SvmCpiEventsClient {
3232
private programAddress: Address;
3333
private programEventAuthority: Address;
3434
private idl: Idl;
35+
private derivedAddress?: Address;
3536

3637
/**
3738
* Private constructor. Use the async create() method to instantiate.
@@ -40,12 +41,14 @@ export class SvmCpiEventsClient {
4041
rpc: web3.Rpc<web3.SolanaRpcApiFromTransport<RpcTransport>>,
4142
address: Address,
4243
eventAuthority: Address,
43-
idl: Idl
44+
idl: Idl,
45+
derivedAddress?: Address
4446
) {
4547
this.rpc = rpc;
4648
this.programAddress = address;
4749
this.programEventAuthority = eventAuthority;
4850
this.idl = idl;
51+
this.derivedAddress = derivedAddress;
4952
}
5053

5154
/**
@@ -61,14 +64,16 @@ export class SvmCpiEventsClient {
6164
public static async createFor(
6265
rpc: web3.Rpc<web3.SolanaRpcApiFromTransport<RpcTransport>>,
6366
programId: string,
64-
idl: Idl
67+
idl: Idl,
68+
pda?: string
6569
): Promise<SvmCpiEventsClient> {
6670
const address = web3.address(programId);
71+
const derivedAddress = pda ? web3.address(pda) : undefined;
6772
const [eventAuthority] = await web3.getProgramDerivedAddress({
6873
programAddress: address,
6974
seeds: ["__event_authority"],
7075
});
71-
return new SvmCpiEventsClient(rpc, address, eventAuthority, idl);
76+
return new SvmCpiEventsClient(rpc, address, eventAuthority, idl, derivedAddress);
7277
}
7378

7479
/**
@@ -90,18 +95,39 @@ export class SvmCpiEventsClient {
9095
return events.filter((event) => event.name === eventName) as EventWithData[];
9196
}
9297

98+
/**
99+
* Queries events for the provided derived address at instantiation filtered by event name.
100+
*
101+
* @param eventName - The name of the event to filter by.
102+
* @param fromBlock - Optional starting block.
103+
* @param toBlock - Optional ending block.
104+
* @param options - Options for fetching signatures.
105+
* @returns A promise that resolves to an array of events matching the eventName.
106+
*/
107+
public async queryDerivedAddressEvents(
108+
eventName: string,
109+
fromBlock?: bigint,
110+
toBlock?: bigint,
111+
options: GetSignaturesForAddressConfig = { limit: 1000, commitment: "confirmed" }
112+
): Promise<EventWithData[]> {
113+
const events = await this.queryAllEvents(fromBlock, toBlock, options, true);
114+
return events.filter((event) => event.name === eventName) as EventWithData[];
115+
}
116+
93117
/**
94118
* Queries all events for a specific program.
95119
*
96120
* @param fromBlock - Optional starting block.
97121
* @param toBlock - Optional ending block.
98122
* @param options - Options for fetching signatures.
123+
* @param forDerivedAddress - Whether to query events for the program or the derived address.
99124
* @returns A promise that resolves to an array of all events with additional metadata.
100125
*/
101126
private async queryAllEvents(
102127
fromBlock?: bigint,
103128
toBlock?: bigint,
104-
options: GetSignaturesForAddressConfig = { limit: 1000, commitment: "confirmed" }
129+
options: GetSignaturesForAddressConfig = { limit: 1000, commitment: "confirmed" },
130+
forDerivedAddress: boolean = false
105131
): Promise<EventWithData[]> {
106132
const allSignatures: GetSignaturesForAddressTransaction[] = [];
107133
let hasMoreSignatures = true;
@@ -110,6 +136,11 @@ export class SvmCpiEventsClient {
110136
let fromSlot: bigint | undefined;
111137
let toSlot: bigint | undefined;
112138

139+
if (forDerivedAddress && !this.derivedAddress) {
140+
throw new Error("Unable to query PDA events. Derived address not set.");
141+
}
142+
const addressToQuery = forDerivedAddress ? this.derivedAddress : this.programAddress;
143+
113144
if (fromBlock) {
114145
const slot = await getSlotForBlock(this.rpc, fromBlock, BigInt(0));
115146
fromSlot = slot;
@@ -122,7 +153,7 @@ export class SvmCpiEventsClient {
122153

123154
while (hasMoreSignatures) {
124155
const signatures: GetSignaturesForAddressApiResponse = await this.rpc
125-
.getSignaturesForAddress(this.programAddress, currentOptions)
156+
.getSignaturesForAddress(addressToQuery!, currentOptions)
126157
.send();
127158
// Signatures are sorted by slot in descending order.
128159
allSignatures.push(...signatures);

0 commit comments

Comments
 (0)