-
Notifications
You must be signed in to change notification settings - Fork 17
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
base: master
Are you sure you want to change the base?
Changes from all commits
b02c143
600fcab
0e1dde4
c553a9f
0ff8eb7
bcf1fdb
9964733
b23a150
80d9af5
405c4e9
9bcad79
b07b9b4
3605de1
885b792
cf3ba96
716ad49
93ceb30
82e8d17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "@across-protocol/sdk", | ||
"author": "UMA Team", | ||
"version": "4.1.19", | ||
"version": "4.1.20", | ||
"license": "AGPL-3.0", | ||
"homepage": "https://docs.across.to/reference/sdk", | ||
"files": [ | ||
|
@@ -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'", | ||
"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", | ||
|
@@ -101,11 +103,13 @@ | |
"dependencies": { | ||
"@across-protocol/across-token": "^1.0.0", | ||
"@across-protocol/constants": "^3.1.38", | ||
"@across-protocol/contracts": "4.0.2", | ||
"@across-protocol/contracts": "4.0.3", | ||
"@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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
|
@@ -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" | ||
|
@@ -161,4 +166,4 @@ | |
"[email protected]": "4.0.4", | ||
"[email protected]": "5.0.1" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
import { getDeployedAddress, SvmSpokeIdl } from "@across-protocol/contracts"; | ||
import { getSolanaChainId } from "@across-protocol/contracts/dist/src/svm/web3-v1"; | ||
import { BorshEventCoder, utils } from "@coral-xyz/anchor"; | ||
import web3, { | ||
Address, | ||
Commitment, | ||
GetSignaturesForAddressApi, | ||
GetTransactionApi, | ||
RpcTransport, | ||
Signature, | ||
} from "@solana/web3-v2.js"; | ||
import { EventData, EventName, EventWithData } from "./types"; | ||
import { getEventName, parseEventData } from "./utils/events"; | ||
import { isDevnet } from "./utils/helpers"; | ||
|
||
// Utility type to extract the return type for the JSON encoding overload. We only care about the overload where the | ||
// configuration parameter (C) has the optional property 'encoding' set to 'json'. | ||
type ExtractJsonOverload<T> = T extends (signature: infer _S, config: infer C) => infer R | ||
? C extends { encoding?: "json" } | ||
? R | ||
: never | ||
: never; | ||
|
||
type GetTransactionReturnType = ExtractJsonOverload<GetTransactionApi["getTransaction"]>; | ||
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 svmSpokeEventAuthority: Address; | ||
|
||
/** | ||
* Private constructor. Use the async create() method to instantiate. | ||
*/ | ||
private constructor( | ||
rpc: web3.Rpc<web3.SolanaRpcApiFromTransport<RpcTransport>>, | ||
svmSpokeAddress: Address, | ||
eventAuthority: Address | ||
) { | ||
this.rpc = rpc; | ||
this.svmSpokeAddress = svmSpokeAddress; | ||
this.svmSpokeEventAuthority = eventAuthority; | ||
} | ||
|
||
/** | ||
* 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("SvmSpoke", getSolanaChainId(isTestnet ? "devnet" : "mainnet").toString()); | ||
if (!programId) throw new Error("Program not found"); | ||
const svmSpokeAddress = web3.address(programId); | ||
const [svmSpokeEventAuthority] = await web3.getProgramDerivedAddress({ | ||
programAddress: svmSpokeAddress, | ||
seeds: ["__event_authority"], | ||
}); | ||
return new SvmSpokeEventsClient(rpc, svmSpokeAddress, svmSpokeEventAuthority); | ||
} | ||
|
||
/** | ||
* 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. | ||
* @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, commitment: "confirmed" } | ||
): Promise<EventWithData<T>[]> { | ||
const events = await this.queryAllEvents(fromSlot, toSlot, options); | ||
return events.filter((event) => event.name === eventName) as EventWithData<T>[]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So there's no way to query the Seems expensive but if we're smart about setting from/toSlot we can cache the results There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 fromSlot - Optional starting slot. | ||
* @param toSlot - Optional ending slot. | ||
* @param options - Options for fetching signatures. | ||
* @returns A promise that resolves to an array of all events with additional metadata. | ||
*/ | ||
private async queryAllEvents( | ||
fromSlot?: bigint, | ||
toSlot?: bigint, | ||
options: GetSignaturesForAddressConfig = { limit: 1000, commitment: "confirmed" } | ||
): Promise<EventWithData<EventData>[]> { | ||
const allSignatures: GetSignaturesForAddressTransaction[] = []; | ||
let hasMoreSignatures = true; | ||
let currentOptions = options; | ||
|
||
while (hasMoreSignatures) { | ||
const signatures: GetSignaturesForAddressApiResponse = await this.rpc | ||
.getSignaturesForAddress(this.svmSpokeAddress, currentOptions) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this signatures are any transactions that involve the svmSpokeAddress, they could be direct calls to the spoke or cross program invocations. |
||
.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 | ||
); | ||
} | ||
|
||
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.flatMap(async (signatureTransaction) => { | ||
const events = await this.readEventsFromSignature(signatureTransaction.signature, options.commitment); | ||
return events.map((event) => ({ | ||
...event, | ||
confirmationStatus: signatureTransaction.confirmationStatus, | ||
blockTime: signatureTransaction.blockTime, | ||
signature: signatureTransaction.signature, | ||
slot: signatureTransaction.slot, | ||
})); | ||
}) | ||
); | ||
return eventsWithSlots.flat(); | ||
} | ||
|
||
/** | ||
* Reads events from a transaction signature. | ||
* | ||
* @param txSignature - The transaction signature. | ||
* @param commitment - Commitment level. | ||
* @returns A promise that resolves to an array of events. | ||
*/ | ||
private async readEventsFromSignature(txSignature: Signature, commitment: Commitment = "confirmed") { | ||
const txResult = await this.rpc | ||
.getTransaction(txSignature, { commitment, maxSupportedTransactionVersion: 0 }) | ||
.send(); | ||
|
||
if (txResult === null) return []; | ||
return this.processEventFromTx(txResult); | ||
} | ||
|
||
/** | ||
* Processes events from a transaction. | ||
* | ||
* @param txResult - The transaction result. | ||
* @returns A promise that resolves to an array of events with their data and name. | ||
*/ | ||
private processEventFromTx( | ||
txResult: GetTransactionReturnType | ||
): { program: Address; data: EventData; name: EventName }[] { | ||
if (!txResult) return []; | ||
const events: { program: Address; data: EventData; name: EventName }[] = []; | ||
|
||
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 = ix.accounts.length === 1 ? messageAccountKeys[ix.accounts[0]] : undefined; | ||
if ( | ||
ixProgramId !== undefined && | ||
singleIxAccount !== undefined && | ||
this.svmSpokeAddress === ixProgramId && | ||
this.svmSpokeEventAuthority === singleIxAccount | ||
) { | ||
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(Buffer.from(new Uint8Array(ixData).slice(8))); | ||
const event = new BorshEventCoder(SvmSpokeIdl).decode(eventData); | ||
if (!event?.name) throw new Error("Event name is undefined"); | ||
const name = getEventName(event.name); | ||
events.push({ | ||
program: this.svmSpokeAddress, | ||
data: parseEventData(event?.data), | ||
name, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
return events; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./eventsClient"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Signature, Address, UnixTimestamp } from "@solana/web3-v2.js"; | ||
import { SvmSpokeClient } from "@across-protocol/contracts"; | ||
|
||
export type EventData = | ||
| SvmSpokeClient.BridgedToHubPool | ||
| SvmSpokeClient.TokensBridged | ||
| SvmSpokeClient.ExecutedRelayerRefundRoot | ||
| SvmSpokeClient.RelayedRootBundle | ||
| SvmSpokeClient.PausedDeposits | ||
| SvmSpokeClient.PausedFills | ||
| SvmSpokeClient.SetXDomainAdmin | ||
| SvmSpokeClient.EnabledDepositRoute | ||
| SvmSpokeClient.FilledRelay | ||
| SvmSpokeClient.FundsDeposited | ||
| SvmSpokeClient.EmergencyDeletedRootBundle | ||
| SvmSpokeClient.RequestedSlowFill | ||
| SvmSpokeClient.ClaimedRelayerRefund | ||
| SvmSpokeClient.TransferredOwnership; | ||
|
||
export enum SVMEventNames { | ||
FilledRelay = "FilledRelay", | ||
FundsDeposited = "FundsDeposited", | ||
EnabledDepositRoute = "EnabledDepositRoute", | ||
RelayedRootBundle = "RelayedRootBundle", | ||
ExecutedRelayerRefundRoot = "ExecutedRelayerRefundRoot", | ||
BridgedToHubPool = "BridgedToHubPool", | ||
PausedDeposits = "PausedDeposits", | ||
PausedFills = "PausedFills", | ||
SetXDomainAdmin = "SetXDomainAdmin", | ||
EmergencyDeletedRootBundle = "EmergencyDeletedRootBundle", | ||
RequestedSlowFill = "RequestedSlowFill", | ||
ClaimedRelayerRefund = "ClaimedRelayerRefund", | ||
TokensBridged = "TokensBridged", | ||
TransferredOwnership = "TransferredOwnership", | ||
} | ||
|
||
export type EventName = keyof typeof SVMEventNames; | ||
|
||
export type EventWithData<T extends EventData> = { | ||
confirmationStatus: string | null; | ||
blockTime: UnixTimestamp | null; | ||
signature: Signature; | ||
slot: bigint; | ||
name: EventName; | ||
data: T; | ||
program: Address; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { BN } from "@coral-xyz/anchor"; | ||
import web3 from "@solana/web3-v2.js"; | ||
import { EventName, SVMEventNames } from "../types"; | ||
|
||
/** | ||
* Parses event data from a transaction. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function parseEventData(eventData: any): any { | ||
if (!eventData) return eventData; | ||
|
||
if (Array.isArray(eventData)) { | ||
return eventData.map(parseEventData); | ||
} | ||
|
||
if (typeof eventData === "object") { | ||
if (eventData.constructor.name === "PublicKey") { | ||
return web3.address(eventData.toString()); | ||
} | ||
if (BN.isBN(eventData)) { | ||
return BigInt(eventData.toString()); | ||
} | ||
|
||
// Convert each key from snake_case to camelCase and process the value recursively. | ||
return Object.fromEntries( | ||
Object.entries(eventData).map(([key, value]) => [snakeToCamel(key), parseEventData(value)]) | ||
); | ||
} | ||
|
||
return eventData; | ||
} | ||
|
||
/** | ||
* Converts a snake_case string to camelCase. | ||
*/ | ||
function snakeToCamel(s: string): string { | ||
return s.replace(/(_\w)/g, (match) => match[1].toUpperCase()); | ||
} | ||
|
||
/** | ||
* Gets the event name from a raw name. | ||
*/ | ||
export function getEventName(rawName: string): EventName { | ||
if (Object.values(SVMEventNames).some((name) => rawName.includes(name))) return rawName as EventName; | ||
throw new Error(`Unknown event name: ${rawName}`); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import web3, { RpcTransport } from "@solana/web3-v2.js"; | ||
|
||
/** | ||
* Helper to determine if the current RPC network is devnet. | ||
*/ | ||
export async function isDevnet(rpc: web3.Rpc<web3.SolanaRpcApiFromTransport<RpcTransport>>): Promise<boolean> { | ||
const genesisHash = await rpc.getGenesisHash().send(); | ||
return genesisHash === "EtWTRABZaYq6iMfeYKouRu166VU2xqa1wcaWoxPkrZBG"; | ||
} |
There was a problem hiding this comment.
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