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(svm): svm spoke events client #899

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@across-protocol/sdk",
"author": "UMA Team",
"version": "4.1.17",
"version": "4.1.18",
"license": "AGPL-3.0",
"homepage": "https://docs.across.to/reference/sdk",
"files": [
Expand All @@ -12,6 +12,8 @@
"node": ">=20.18.0"
},
"scripts": {
"build-bigint-buffer": "[ -d node_modules/bigint-buffer ] && command -v node-gyp > /dev/null && cd node_modules/bigint-buffer && node-gyp configure && node-gyp build || echo 'Skipping bigint-buffer build: folder or node-gyp not found'",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as in relayer to get rid of the bigint-buffer warning

"postinstall": "yarn build-bigint-buffer",
"start": "yarn typechain && nodemon -e ts,tsx,json,js,jsx --watch ./src --ignore ./dist --exec 'yarn dev'",
"build": "yarn run clean && yarn typechain && yarn run build:cjs & yarn run build:esm & yarn run build:types; wait",
"dev": "yarn run build:cjs & yarn run build:esm & yarn run build:types; wait",
Expand Down Expand Up @@ -105,7 +107,9 @@
"@eth-optimism/sdk": "^3.3.1",
"@ethersproject/bignumber": "^5.7.0",
"@pinata/sdk": "^2.1.0",
"@solana/web3.js": "^2.0.0",
"@solana/web3-v2.js": "npm:@solana/web3.js@2",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to have both versions because the BorshEncoder uses v1 internally

"@solana/web3.js": "^1.31.0",
"@coral-xyz/anchor": "^0.30.1",
"@types/mocha": "^10.0.1",
"@uma/sdk": "^0.34.10",
"arweave": "^1.14.4",
Expand All @@ -116,6 +120,7 @@
"ethers": "^5.7.2",
"lodash": "^4.17.21",
"lodash.get": "^4.4.2",
"node-gyp": "^11.0.0",
"superstruct": "^0.15.4",
"tslib": "^2.6.2",
"viem": "^2.21.15"
Expand Down Expand Up @@ -161,4 +166,4 @@
"[email protected]": "4.0.4",
"[email protected]": "5.0.1"
}
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export * as clients from "./clients";
export * as typechain from "./typechain";
export * as caching from "./caching";
export * as providers from "./providers";
export * as svm from "./svm";
4 changes: 2 additions & 2 deletions src/providers/solana/baseRpcFactories.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ClusterUrl, createSolanaRpcFromTransport, RpcTransport } from "@solana/web3.js";
import { ClusterUrl, createSolanaRpcFromTransport, RpcTransport } from "@solana/web3-v2.js";

// This is abstract base class for creating Solana RPC clients and transports.
export abstract class SolanaBaseRpcFactory {
constructor(readonly chainId: number) {}
constructor(readonly chainId: number) { }

Check warning on line 5 in src/providers/solana/baseRpcFactories.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `·`

// This method must be implemented by the derived class to create a transport.
public abstract createTransport(): RpcTransport;
Expand Down
2 changes: 1 addition & 1 deletion src/providers/solana/defaultRpcFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createDefaultRpcTransport, RpcTransport } from "@solana/web3.js";
import { createDefaultRpcTransport, RpcTransport } from "@solana/web3-v2.js";
import { SolanaClusterRpcFactory } from "./baseRpcFactories";

// Exposes default RPC transport for Solana in the SolanaClusterRpcFactory class.
Expand Down
2 changes: 1 addition & 1 deletion src/providers/solana/rateLimitedRpcFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RpcResponse, RpcTransport } from "@solana/web3.js";
import { RpcResponse, RpcTransport } from "@solana/web3-v2.js";
import { QueueObject, queue } from "async";
import winston, { Logger } from "winston";
import { SolanaClusterRpcFactory } from "./baseRpcFactories";
Expand Down
233 changes: 233 additions & 0 deletions src/svm/eventsClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
import { getDeployedAddress, SvmSpokeIdl } from "@across-protocol/contracts";
import { getSolanaChainId } from "@across-protocol/contracts/dist/src/svm/web3-v1";
import { BorshEventCoder, Idl, utils } from "@coral-xyz/anchor";
import web3, {
Address,
Commitment,
GetSignaturesForAddressApi,
GetTransactionApi,
RpcTransport,
Signature,
unixTimestamp

Check warning on line 11 in src/svm/eventsClient.ts

View workflow job for this annotation

GitHub Actions / Lint

Insert `,`
} from "@solana/web3-v2.js";
import { EventData, EventName, EventWithData } from "./types";
import { getEventName, mapEventData, parseEventData } from "./utils/events";
import { isDevnet } from "./utils/helpers";

type GetTransactionReturnType = ReturnType<GetTransactionApi["getTransaction"]>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be bit risky as this infers the union of all overloads. E.g. if the caller passed { encoding: "base58" } then returned transaction property would be of type Base58EncodedDataResponse and the event client would error when trying to access something like txResult.transaction.message.accountKeys.

I think the above does not exactly apply to us as we are calling getTransaction without setting encoding, so it matches the optional json overload which has the props we are trying to access. But maybe its worth adding a comment on this for safer code maintenance.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. I've added a type wrapper to extract only the "json" encoded return type.

type GetSignaturesForAddressConfig = Parameters<GetSignaturesForAddressApi["getSignaturesForAddress"]>[1];
type GetSignaturesForAddressTransaction = ReturnType<GetSignaturesForAddressApi["getSignaturesForAddress"]>[number];
type GetSignaturesForAddressApiResponse = readonly GetSignaturesForAddressTransaction[];

export class SvmSpokeEventsClient {
private rpc: web3.Rpc<web3.SolanaRpcApiFromTransport<RpcTransport>>;
private svmSpokeAddress: Address;

/**
* Private constructor. Use the async create() method to instantiate.
*/
private constructor(

Check warning on line 29 in src/svm/eventsClient.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `⏎····rpc:·web3.Rpc<web3.SolanaRpcApiFromTransport<RpcTransport>>,⏎····svmSpokeAddress:·Address⏎··` with `rpc:·web3.Rpc<web3.SolanaRpcApiFromTransport<RpcTransport>>,·svmSpokeAddress:·Address`
rpc: web3.Rpc<web3.SolanaRpcApiFromTransport<RpcTransport>>,
svmSpokeAddress: Address
) {
this.rpc = rpc;
this.svmSpokeAddress = svmSpokeAddress;
}

/**
* Factory method to asynchronously create an instance of SvmSpokeEventsClient.
*/
public static async create(
rpc: web3.Rpc<web3.SolanaRpcApiFromTransport<RpcTransport>>
): Promise<SvmSpokeEventsClient> {
const isTestnet = await isDevnet(rpc);
const programId = getDeployedAddress(

Check warning on line 44 in src/svm/eventsClient.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `⏎······"SvmSpoke",⏎······getSolanaChainId(isTestnet·?·"devnet"·:·"mainnet").toString()⏎····` with `"SvmSpoke",·getSolanaChainId(isTestnet·?·"devnet"·:·"mainnet").toString()`
"SvmSpoke",
getSolanaChainId(isTestnet ? "devnet" : "mainnet").toString()
);
if (!programId) throw new Error("Program not found");
return new SvmSpokeEventsClient(rpc, web3.address(programId));
}

/**
* Queries events for the SvmSpoke program filtered by event name.
*
* @param eventName - The name of the event to filter by.
* @param fromSlot - Optional starting slot.
* @param toSlot - Optional ending slot.
* @param options - Options for fetching signatures.
* @param finality - Commitment level.
* @returns A promise that resolves to an array of events matching the eventName.
*/
public async queryEvents<T extends EventData>(
eventName: EventName,
fromSlot?: bigint,
toSlot?: bigint,
options: GetSignaturesForAddressConfig = { limit: 1000 },
finality: Commitment = "confirmed"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it make sense to pass separate commitment in the finality parameter (used in getTransaction calls) from options.commitment that gets used in getSignaturesForAddress calls?

): Promise<EventWithData<T>[]> {
const events = await this.queryAllEvents(

Check warning on line 69 in src/svm/eventsClient.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `⏎······this.svmSpokeAddress,⏎······SvmSpokeIdl,⏎······fromSlot,⏎······toSlot,⏎······options,⏎······finality⏎····` with `this.svmSpokeAddress,·SvmSpokeIdl,·fromSlot,·toSlot,·options,·finality`
this.svmSpokeAddress,
SvmSpokeIdl,
fromSlot,
toSlot,
options,
finality
);
return events.filter((event) => event.name === eventName) as EventWithData<T>[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So there's no way to query the rpc for just the eventName? You always need to query all events and then filter from there?

Seems expensive but if we're smart about setting from/toSlot we can cache the results

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, that's one of the trade-offs of using CPI Events.

}

/**
* Queries all events for a specific program.
*
* @param program - The program address.
* @param anchorIdl - The IDL describing the program events.
* @param fromSlot - Optional starting slot.
* @param toSlot - Optional ending slot.
* @param options - Options for fetching signatures.
* @param finality - Commitment level.
* @returns A promise that resolves to an array of all events with additional metadata.
*/
public async queryAllEvents(
program: Address,
anchorIdl: Idl,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these might be not necessary as we use them in private methods and it should work only for the spoke pool program. Except, maybe keep program address, but that is useful only if the public create method allowed passing a custom spoke address.

fromSlot?: bigint,
toSlot?: bigint,
options: GetSignaturesForAddressConfig = { limit: 1000 },
finality: Commitment = "confirmed"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we better use commitment for the parameter name.

): Promise<EventWithData<EventData>[]> {
const allSignatures: GetSignaturesForAddressTransaction[] = [];
let hasMoreSignatures = true;
let currentOptions = options;

while (hasMoreSignatures) {
const signatures: GetSignaturesForAddressApiResponse = await this.rpc
.getSignaturesForAddress(program, currentOptions)
.send();
// Signatures are sorted by slot in descending order.
allSignatures.push(...signatures);

// Update options for the next batch. Set "before" to the last fetched signature.
if (signatures.length > 0) {
currentOptions = { ...currentOptions, before: signatures[signatures.length - 1].signature };
}

if (fromSlot && allSignatures.length > 0 && allSignatures[allSignatures.length - 1].slot < fromSlot) {
hasMoreSignatures = false;
}

hasMoreSignatures = Boolean(hasMoreSignatures && currentOptions.limit && signatures.length === currentOptions.limit);

Check warning on line 119 in src/svm/eventsClient.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `hasMoreSignatures·&&·currentOptions.limit·&&·signatures.length·===·currentOptions.limit` with `⏎········hasMoreSignatures·&&·currentOptions.limit·&&·signatures.length·===·currentOptions.limit⏎······`
}

const filteredSignatures = allSignatures.filter((signatureTransaction) => {
if (fromSlot && signatureTransaction.slot < fromSlot) return false;
if (toSlot && signatureTransaction.slot > toSlot) return false;
return true;
});

// Fetch events for all signatures in parallel.
const eventsWithSlots = await Promise.all(
filteredSignatures.map(async (signatureTransaction) => {
const events = await this.readEventsFromSignature(

Check warning on line 131 in src/svm/eventsClient.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `⏎··········signatureTransaction.signature,⏎··········program,⏎··········anchorIdl,⏎··········finality⏎········` with `signatureTransaction.signature,·program,·anchorIdl,·finality`
signatureTransaction.signature,
program,
anchorIdl,
finality
);
return events.map((event) => ({
...event,
confirmationStatus: signatureTransaction.confirmationStatus || "Unknown",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe pass the Commitment | null type as is?

blockTime: signatureTransaction.blockTime || unixTimestamp(BigInt(0)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe pass UnixTimestamp | null type as is?

signature: signatureTransaction.signature,
slot: signatureTransaction.slot,
}));
})
);
return eventsWithSlots.flat();
}

/**
* Reads events from a transaction signature.
*
* @param txSignature - The transaction signature.
* @param programId - The program address.
* @param programIdl - The program IDL.
* @param commitment - Commitment level.
* @returns A promise that resolves to an array of events.
*/
private async readEventsFromSignature(
txSignature: Signature,
programId: Address,
programIdl: Idl,
commitment: Commitment = "confirmed"
) {
const txResult = await this.rpc
.getTransaction(txSignature, { commitment, maxSupportedTransactionVersion: 0 })
.send();

if (txResult === null) return [];
return this.processEventFromTx(txResult, programId, programIdl);
}

/**
* Processes events from a transaction.
*
* @param txResult - The transaction result.
* @param programId - The program address.
* @param programIdl - The program IDL.
* @returns A promise that resolves to an array of events with their data and name.
*/
private async processEventFromTx(
txResult: GetTransactionReturnType,
programId: Address,
programIdl: Idl
): Promise<{ program: Address; data: EventData; name: string }[]> {
if (!txResult) return [];

const eventAuthorities: Map<string, Address> = new Map();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is mapping necessary? we only have single program in this method

const events: { program: Address; data: EventData; name: string }[] = [];

// Derive the event authority PDA.
const [pda] = await web3.getProgramDerivedAddress({
programAddress: programId,
seeds: ["__event_authority"],
});
eventAuthorities.set(programId, pda);

const accountKeys = txResult.transaction.message.accountKeys;
const messageAccountKeys = [...accountKeys];
// Writable accounts come first, then readonly.
// See https://docs.anza.xyz/proposals/versioned-transactions#new-transaction-format
messageAccountKeys.push(...(txResult?.meta?.loadedAddresses?.writable ?? []));
messageAccountKeys.push(...(txResult?.meta?.loadedAddresses?.readonly ?? []));

for (const ixBlock of txResult.meta?.innerInstructions ?? []) {
for (const ix of ixBlock.instructions) {
const ixProgramId = messageAccountKeys[ix.programIdIndex];
const singleIxAccount =

Check warning on line 207 in src/svm/eventsClient.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎·········`
ix.accounts.length === 1 ? messageAccountKeys[ix.accounts[0]] : undefined;
if (
ixProgramId !== undefined &&
singleIxAccount !== undefined &&
programId == ixProgramId &&
eventAuthorities.get(ixProgramId.toString()) == singleIxAccount
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should these use strict evaluation ===?

) {
const ixData = utils.bytes.bs58.decode(ix.data);
// Skip the first 8 bytes (assumed header) and encode the rest.
const eventData = utils.bytes.base64.encode(

Check warning on line 217 in src/svm/eventsClient.ts

View workflow job for this annotation

GitHub Actions / Lint

Replace `⏎············Buffer.from(new·Uint8Array(ixData).slice(8))⏎··········` with `Buffer.from(new·Uint8Array(ixData).slice(8))`
Buffer.from(new Uint8Array(ixData).slice(8))
);
const event = new BorshEventCoder(programIdl).decode(eventData);
const name = getEventName(event?.name);
events.push({
program: programId,
data: mapEventData(parseEventData(event?.data), name),
name,
});
}
}
}

return events;
}
}

Check warning on line 233 in src/svm/eventsClient.ts

View workflow job for this annotation

GitHub Actions / Lint

Insert `⏎`
1 change: 1 addition & 0 deletions src/svm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./eventsClient";
Loading
Loading