-
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 6 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.17", | ||
"version": "4.1.18", | ||
"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", | ||
|
@@ -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", | ||
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,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 | ||
} 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"]>; | ||
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. This might be bit risky as this infers the union of all overloads. E.g. if the caller passed I think the above does not exactly apply to us as we are calling 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. 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
|
||
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( | ||
"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" | ||
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. does it make sense to pass separate commitment in the |
||
): Promise<EventWithData<T>[]> { | ||
const events = await this.queryAllEvents( | ||
Check warning on line 69 in src/svm/eventsClient.ts
|
||
this.svmSpokeAddress, | ||
SvmSpokeIdl, | ||
fromSlot, | ||
toSlot, | ||
options, | ||
finality | ||
); | ||
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 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, | ||
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. 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 |
||
fromSlot?: bigint, | ||
toSlot?: bigint, | ||
options: GetSignaturesForAddressConfig = { limit: 1000 }, | ||
finality: Commitment = "confirmed" | ||
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. nit: can we better use |
||
): 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
|
||
} | ||
|
||
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
|
||
signatureTransaction.signature, | ||
program, | ||
anchorIdl, | ||
finality | ||
); | ||
return events.map((event) => ({ | ||
...event, | ||
confirmationStatus: signatureTransaction.confirmationStatus || "Unknown", | ||
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. maybe pass the |
||
blockTime: signatureTransaction.blockTime || unixTimestamp(BigInt(0)), | ||
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. maybe pass |
||
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(); | ||
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 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 = | ||
ix.accounts.length === 1 ? messageAccountKeys[ix.accounts[0]] : undefined; | ||
if ( | ||
ixProgramId !== undefined && | ||
singleIxAccount !== undefined && | ||
programId == ixProgramId && | ||
eventAuthorities.get(ixProgramId.toString()) == singleIxAccount | ||
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. 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( | ||
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; | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./eventsClient"; |
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