Skip to content

Commit f20b7bc

Browse files
chore: merge
2 parents d0bde64 + 3e20d7e commit f20b7bc

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
@@ -31,6 +31,7 @@ export class SvmCpiEventsClient {
3131
private programAddress: Address;
3232
private programEventAuthority: Address;
3333
private idl: Idl;
34+
private derivedAddress?: Address;
3435

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

5053
/**
@@ -60,14 +63,16 @@ export class SvmCpiEventsClient {
6063
public static async createFor(
6164
rpc: web3.Rpc<web3.SolanaRpcApiFromTransport<RpcTransport>>,
6265
programId: string,
63-
idl: Idl
66+
idl: Idl,
67+
pda?: string
6468
): Promise<SvmCpiEventsClient> {
6569
const address = web3.address(programId);
70+
const derivedAddress = pda ? web3.address(pda) : undefined;
6671
const [eventAuthority] = await web3.getProgramDerivedAddress({
6772
programAddress: address,
6873
seeds: ["__event_authority"],
6974
});
70-
return new SvmCpiEventsClient(rpc, address, eventAuthority, idl);
75+
return new SvmCpiEventsClient(rpc, address, eventAuthority, idl, derivedAddress);
7176
}
7277

7378
/**
@@ -89,26 +94,52 @@ export class SvmCpiEventsClient {
8994
return events.filter((event) => event.name === eventName) as EventWithData[];
9095
}
9196

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

135+
if (forDerivedAddress && !this.derivedAddress) {
136+
throw new Error("Unable to query PDA events. Derived address not set.");
137+
}
138+
const addressToQuery = forDerivedAddress ? this.derivedAddress : this.programAddress;
139+
109140
while (hasMoreSignatures) {
110141
const signatures: GetSignaturesForAddressApiResponse = await this.rpc
111-
.getSignaturesForAddress(this.programAddress, currentOptions)
142+
.getSignaturesForAddress(addressToQuery!, currentOptions)
112143
.send();
113144
// Signatures are sorted by slot in descending order.
114145
allSignatures.push(...signatures);

0 commit comments

Comments
 (0)