-
Notifications
You must be signed in to change notification settings - Fork 58
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): web3 v2, codama clients, and events retrieval #866
base: master
Are you sure you want to change the base?
Changes from 7 commits
ebc0a98
703581e
06316e6
e36e43d
a105f91
98f5fa9
6aafec8
0135718
f9216c3
9f09497
c0860cc
312c95a
18e794c
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 |
---|---|---|
|
@@ -33,3 +33,5 @@ target | |
test-ledger | ||
idls | ||
src/svm/assets | ||
src/svm/clients/* | ||
!src/svm/clients/index.ts |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { createFromRoot } from "codama"; | ||
import { rootNodeFromAnchor, AnchorIdl } from "@codama/nodes-from-anchor"; | ||
import { renderVisitor as renderJavaScriptVisitor } from "@codama/renderers-js"; | ||
import { SvmSpokeIdl, MulticallHandlerIdl } from "../../../src/svm/assets"; | ||
import path from "path"; | ||
export const clientsPath = path.join(__dirname, "..", "..", "..", "src", "svm", "clients"); | ||
|
||
// Generate SvmSpoke clients | ||
let codama = createFromRoot(rootNodeFromAnchor(SvmSpokeIdl as AnchorIdl)); | ||
codama.accept(renderJavaScriptVisitor(path.join(clientsPath, "SvmSpoke"))); | ||
|
||
// Generate MulticallHandler clients | ||
codama = createFromRoot(rootNodeFromAnchor(MulticallHandlerIdl as AnchorIdl)); | ||
codama.accept(renderJavaScriptVisitor(path.join(clientsPath, "MulticallHandler"))); | ||
|
||
// codama = createFromRoot(rootNodeFromAnchor(MessageTransmitterIdl as AnchorIdl)); | ||
// codama.accept( | ||
// renderJavaScriptVisitor(path.join(clientsPath, "MessageTransmitter")) | ||
// ); | ||
|
||
// codama = createFromRoot(rootNodeFromAnchor(TokenMessengerMinterIdl as AnchorIdl)); | ||
// codama.accept( | ||
// renderJavaScriptVisitor(path.join(clientsPath, "TokenMessengerMinter")) | ||
// ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
const clientsPath = path.join(__dirname, "..", "..", "..", "src", "svm", "clients"); | ||
|
||
function replaceInFiles(dir: string): void { | ||
const files = fs.readdirSync(dir); | ||
files.forEach((file: string) => { | ||
const filePath = path.join(dir, file); | ||
const stat = fs.statSync(filePath); | ||
|
||
if (stat.isDirectory()) { | ||
replaceInFiles(filePath); | ||
} else if (file.endsWith(".ts")) { | ||
const fileContent = fs.readFileSync(filePath, "utf8"); | ||
const updatedContent = fileContent.replace("@solana/web3.js", "@solana/web3-v2.js"); | ||
fs.writeFileSync(filePath, updatedContent); | ||
} | ||
}); | ||
} | ||
|
||
replaceInFiles(clientsPath); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import * as MulticallHandlerClient from "./MulticallHandler"; | ||
import * as SvmSpokeClient from "./SvmSpoke"; | ||
|
||
export { MulticallHandlerClient, SvmSpokeClient }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,3 @@ | ||
export * from "./relayHashUtils"; | ||
export * from "./instructionParamsUtils"; | ||
export * from "./conversionUtils"; | ||
export * from "./transactionUtils"; | ||
export * from "./solanaProgramUtils"; | ||
export * from "./coders"; | ||
export * from "./programConnectors"; | ||
export * from "./web3-v1"; | ||
export * from "./assets"; | ||
export * from "./constants"; | ||
export * from "./helpers"; | ||
export * from "./cctpHelpers"; | ||
export * from "./clients"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export * from "./relayHashUtils"; | ||
export * from "./instructionParamsUtils"; | ||
export * from "./conversionUtils"; | ||
export * from "./transactionUtils"; | ||
export * from "./solanaProgramUtils"; | ||
export * from "./coders"; | ||
export * from "./programConnectors"; | ||
export * from "./constants"; | ||
export * from "./helpers"; | ||
export * from "./cctpHelpers"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { BorshEventCoder, Idl, utils } from "@coral-xyz/anchor"; | ||
import web3, { | ||
Address, | ||
Commitment, | ||
GetTransactionApi, | ||
Signature, | ||
Slot, | ||
TransactionError, | ||
UnixTimestamp, | ||
} from "@solana/web3-v2.js"; | ||
|
||
type GetTransactionReturnType = ReturnType<GetTransactionApi["getTransaction"]>; | ||
|
||
type GetSignaturesForAddressConfig = Readonly<{ | ||
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 can infer this type without replicating upstream:
|
||
before?: Signature; | ||
commitment?: Exclude<Commitment, "processed">; | ||
limit?: number; | ||
minContextSlot?: Slot; | ||
until?: Signature; | ||
}>; | ||
|
||
type GetSignaturesForAddressTransaction = { | ||
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 can also infer this:
|
||
blockTime: UnixTimestamp | null; | ||
confirmationStatus: Commitment | null; | ||
err: TransactionError | null; | ||
memo: string | null; | ||
signature: Signature; | ||
slot: Slot; | ||
}; | ||
|
||
/** | ||
* Reads all events for a specific program. | ||
*/ | ||
export async function readProgramEvents( | ||
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. how did you test this? |
||
rpc: web3.Rpc<web3.SolanaRpcApiFromTransport<any>>, | ||
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 replace |
||
program: Address, | ||
anchorIdl: Idl, | ||
finality: Commitment = "confirmed", | ||
options: GetSignaturesForAddressConfig = { limit: 1000 } | ||
) { | ||
const allSignatures: GetSignaturesForAddressTransaction[] = []; | ||
|
||
// Fetch all signatures in sequential batches | ||
while (true) { | ||
const signatures = await rpc.getSignaturesForAddress(program, options).send(); | ||
allSignatures.push(...signatures); | ||
|
||
// Update options for the next batch. Set before to the last fetched signature. | ||
if (signatures.length > 0) { | ||
options = { ...options, before: signatures[signatures.length - 1].signature }; | ||
} | ||
|
||
if (options.limit && signatures.length < options.limit) break; // Exit early if the number of signatures < limit | ||
} | ||
|
||
// Fetch events for all signatures in parallel | ||
const eventsWithSlots = await Promise.all( | ||
allSignatures.map(async (signatureTransaction) => { | ||
const events = await readEvents(rpc, signatureTransaction.signature, program, anchorIdl, finality); | ||
|
||
return events.map((event) => ({ | ||
...event, | ||
confirmationStatus: signatureTransaction.confirmationStatus || "Unknown", | ||
blockTime: signatureTransaction.blockTime || 0, | ||
signature: signatureTransaction.signature, | ||
slot: signatureTransaction.slot, | ||
name: event.name || "Unknown", | ||
})); | ||
}) | ||
); | ||
return eventsWithSlots.flat(); | ||
} | ||
|
||
/** | ||
* Reads events from a transaction. | ||
*/ | ||
export async function readEvents( | ||
rpc: web3.Rpc<web3.SolanaRpcApiFromTransport<any>>, | ||
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 replace |
||
txSignature: Signature, | ||
programId: Address, | ||
programIdl: Idl, | ||
commitment: Commitment = "confirmed" | ||
) { | ||
const txResult = await rpc.getTransaction(txSignature, { commitment, maxSupportedTransactionVersion: 0 }).send(); | ||
|
||
if (txResult === null) return []; | ||
|
||
return processEventFromTx(txResult, programId, programIdl); | ||
} | ||
|
||
/** | ||
* Processes events from a transaction. | ||
*/ | ||
async function processEventFromTx( | ||
txResult: GetTransactionReturnType, | ||
programId: Address, | ||
programIdl: Idl | ||
): Promise<{ program: Address; data: any; name: string | undefined }[]> { | ||
if (!txResult) return []; | ||
const eventAuthorities: Map<string, Address> = new Map(); | ||
const events: { program: Address; data: any; name: string | undefined }[] = []; | ||
const [pda] = await web3.getProgramDerivedAddress({ programAddress: programId, seeds: ["__event_authority"] }); | ||
eventAuthorities.set(programId, pda); | ||
|
||
const messageAccountKeys = txResult.transaction.message.accountKeys; | ||
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. have you tested this with V0 transactions? does |
||
|
||
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 | ||
) { | ||
const ixData = utils.bytes.bs58.decode(ix.data); | ||
const eventData = utils.bytes.base64.encode(Buffer.from(new Uint8Array(ixData).slice(8))); | ||
let event = new BorshEventCoder(programIdl).decode(eventData); | ||
events.push({ | ||
program: programId, | ||
data: event?.data, | ||
name: event?.name, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
return events; | ||
} |
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.
Pending to fix.