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): web3 v2, codama clients, and events retrieval #866

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ target
test-ledger
idls
src/svm/assets
src/svm/clients/*
!src/svm/clients/index.ts
1 change: 1 addition & 0 deletions Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ wallet = "test/svm/keys/localnet-wallet.json"
[scripts]
test = "anchor run generateExternalTypes && yarn run ts-mocha -p ./tsconfig.json -t 1000000 test/svm/**/*.ts"
queryEvents = "NODE_NO_WARNINGS=1 yarn run ts-node ./scripts/svm/queryEvents.ts"
queryEventsV2 = "NODE_NO_WARNINGS=1 yarn run ts-node ./scripts/svm/queryEventsV2.ts"
initialize = "NODE_NO_WARNINGS=1 yarn run ts-node ./scripts/svm/initialize.ts"
queryState = "NODE_NO_WARNINGS=1 yarn run ts-node ./scripts/svm/queryState.ts"
enableRoute = "NODE_NO_WARNINGS=1 yarn run ts-node ./scripts/svm/enableRoute.ts"
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"lint-fix": "yarn prettier --write **/*.js **/*.ts ./programs/**/*.rs ./contracts**/*.sol && cargo +nightly fmt --all && cargo clippy",
"clean-fast": "for dir in node_modules cache cache-zk artifacts artifacts-zk dist typechain; do mv \"${dir}\" \"_${dir}\"; rm -rf \"_${dir}\" &; done",
"clean": "rm -rf node_modules cache cache-zk artifacts artifacts-zk dist typechain",
"generate-svm-assets": "sh ./scripts/generate-svm-assets.sh",
"generate-svm-assets": "sh ./scripts/generate-svm-assets.sh && yarn generate-svm-clients",
"generate-svm-clients": "yarn ts-node ./scripts/svm/utils/generate-svm-clients.ts && yarn ts-node ./scripts/svm/utils/rename-clients-imports.ts",
"build-evm": "hardhat compile",
"build-svm": "echo 'Generating IDLs...' && anchor build > /dev/null 2>&1 || true && anchor run generateExternalTypes && anchor build",
"build-ts": "tsc && rsync -a --include '*/' --include '*.d.ts' --exclude '*' ./typechain ./dist/",
Expand Down Expand Up @@ -54,6 +55,7 @@
"@solana-developers/helpers": "^2.4.0",
"@solana/spl-token": "^0.4.6",
"@solana/web3.js": "^1.31.0",
"@solana/web3-v2.js": "npm:@solana/web3.js@2",
"@types/yargs": "^17.0.33",
"@uma/common": "^2.37.3",
"@uma/contracts-node": "^0.4.17",
Expand All @@ -65,6 +67,9 @@
"zksync-web3": "^0.14.3"
},
"devDependencies": {
"@codama/nodes-from-anchor": "^1.1.0",
"@codama/renderers-js": "^1.1.1",
"codama": "^1.2.0",
"@consensys/linea-sdk": "^0.1.6",
"@matterlabs/hardhat-zksync-deploy": "^0.6.3",
"@matterlabs/hardhat-zksync-solc": "^1.1.4",
Expand Down
52 changes: 52 additions & 0 deletions scripts/svm/queryEventsV2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// This script queries the events of the spoke pool and prints them in a human readable format.
import { AnchorProvider } from "@coral-xyz/anchor";
import { address, createSolanaRpc } from "@solana/web3-v2.js";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { readProgramEventsV2, stringifyCpiEvent, SvmSpokeIdl } from "../../src/svm";

// Set up the provider
const provider = AnchorProvider.env();

const argvPromise = yargs(hideBin(process.argv))
.option("eventName", {
type: "string",
demandOption: false,
describe: "Name of the event to query",
choices: [
"any",
"FilledV3Relay",
"V3FundsDeposited",
"EnabledDepositRoute",
"RelayedRootBundle",
"ExecutedRelayerRefundRoot",
"BridgedToHubPool",
"PausedDeposits",
"PausedFills",
"SetXDomainAdmin",
"EmergencyDeletedRootBundle",
"RequestedV3SlowFill",
"ClaimedRelayerRefund",
"TokensBridged",
],
})
.option("programId", {
type: "string",
demandOption: true,
describe: "SvmSpokeProgram ID to query events from",
}).argv;

async function queryEvents(): Promise<void> {
const argv = await argvPromise;
const eventName = argv.eventName || "any";
const programId = argv.programId;
const rpc = createSolanaRpc(provider.connection.rpcEndpoint);
const events = await readProgramEventsV2(rpc, address(programId), SvmSpokeIdl, "confirmed");
const filteredEvents = events.filter((event) => (eventName == "any" ? true : event.name == eventName));
const formattedEvents = filteredEvents.map((event) => stringifyCpiEvent(event));

console.log(JSON.stringify(formattedEvents, null, 2));
}

// Run the queryEvents function
queryEvents();
24 changes: 24 additions & 0 deletions scripts/svm/utils/generate-svm-clients.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));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pending to fix.

// codama.accept(
// renderJavaScriptVisitor(path.join(clientsPath, "MessageTransmitter"))
// );

// codama = createFromRoot(rootNodeFromAnchor(TokenMessengerMinterIdl as AnchorIdl));
// codama.accept(
// renderJavaScriptVisitor(path.join(clientsPath, "TokenMessengerMinter"))
// );
22 changes: 22 additions & 0 deletions scripts/svm/utils/rename-clients-imports.ts
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);
4 changes: 4 additions & 0 deletions src/svm/clients/index.ts
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 };
13 changes: 3 additions & 10 deletions src/svm/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
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 "./web3-v2";
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
@@ -1,6 +1,6 @@
import * as anchor from "@coral-xyz/anchor";
import { array, object, optional, string, Struct } from "superstruct";
import { readUInt256BE } from "../../src/svm";
import { readUInt256BE } from "./relayHashUtils";

// Index positions to decode Message Header from
// https://developers.circle.com/stablecoins/docs/message-format#message-header
Expand Down
2 changes: 1 addition & 1 deletion src/svm/coders.ts → src/svm/web3-v1/coders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "@solana/web3.js";
import bs58 from "bs58";
import { Layout } from "buffer-layout";
import { AcrossPlusMessage } from "../types/svm";
import { AcrossPlusMessage } from "../../types/svm";

/**
* Extended Anchor accounts coder to handle large account data.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions src/svm/web3-v1/index.ts
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
@@ -1,7 +1,7 @@
import { Keypair, TransactionInstruction, Transaction, sendAndConfirmTransaction, PublicKey } from "@solana/web3.js";
import { Program, BN } from "@coral-xyz/anchor";
import { RelayData, SlowFillLeaf, RelayerRefundLeafSolana } from "../types/svm";
import { SvmSpoke } from "../../target/types/svm_spoke";
import { RelayData, SlowFillLeaf, RelayerRefundLeafSolana } from "../../types/svm";
import { SvmSpoke } from "../../../target/types/svm_spoke";
import { LargeAccountsCoder } from "./coders";

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AnchorProvider, Idl, Program } from "@coral-xyz/anchor";
import { getDeployedAddress } from "../DeploymentUtils";
import { SupportedNetworks } from "../types/svm";
import { getDeployedAddress } from "../../DeploymentUtils";
import { SupportedNetworks } from "../../types/svm";
import {
MessageTransmitterAnchor,
MessageTransmitterIdl,
Expand All @@ -10,7 +10,7 @@ import {
SvmSpokeIdl,
TokenMessengerMinterAnchor,
TokenMessengerMinterIdl,
} from "./assets";
} from "../assets";
import { getSolanaChainId, isSolanaDevnet } from "./helpers";

type ProgramOptions = { network?: SupportedNetworks; programId?: string };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BN } from "@coral-xyz/anchor";
import { ethers } from "ethers";
import { RelayerRefundLeaf, RelayerRefundLeafSolana, SlowFillLeaf } from "../types/svm";
import { RelayerRefundLeaf, RelayerRefundLeafSolana, SlowFillLeaf } from "../../types/svm";
import { serialize } from "borsh";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
SignaturesForAddressOptions,
} from "@solana/web3.js";
import { deserialize } from "borsh";
import { EventType } from "../types/svm";
import { EventType } from "../../types/svm";
import { publicKeyToEvmAddress } from "./conversionUtils";

/**
Expand Down Expand Up @@ -239,6 +239,8 @@ export function stringifyCpiEvent(obj: any): any {
return obj.toString();
} else if (BN.isBN(obj)) {
return obj.toString();
} else if (typeof obj === "bigint" && obj !== 0n) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why 0n is excluded?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Big bad, i put that during some tests and forgot about it.
Thanks for catchinng!

return obj.toString();
} else if (Array.isArray(obj) && obj.length == 32) {
return Buffer.from(obj).toString("hex"); // Hex representation for fixed-length arrays
} else if (Array.isArray(obj)) {
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions src/svm/web3-v2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./solanaProgramUtils";
121 changes: 121 additions & 0 deletions src/svm/web3-v2/solanaProgramUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { BorshEventCoder, Idl, utils } from "@coral-xyz/anchor";
import web3, {
Address,
Commitment,
GetSignaturesForAddressApi,
GetTransactionApi,
RpcTransport,
Signature,
} from "@solana/web3-v2.js";

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

type GetSignaturesForAddressConfig = Parameters<GetSignaturesForAddressApi["getSignaturesForAddress"]>[1];

type GetSignaturesForAddressTransaction = ReturnType<GetSignaturesForAddressApi["getSignaturesForAddress"]>[number];

/**
* Reads all events for a specific program.
*/
export async function readProgramEventsV2(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed to readProgramEventsV2 so they do not conflict with the v1 exports. I don't think this should be the final name as we want to prioritize V2 but for now I think it's fine until we start exposing this functions

rpc: web3.Rpc<web3.SolanaRpcApiFromTransport<RpcTransport>>,
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 readEventsV2(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 readEventsV2(
rpc: web3.Rpc<web3.SolanaRpcApiFromTransport<RpcTransport>>,
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 accountKeys = txResult.transaction.message.accountKeys;
const messageAccountKeys = [...accountKeys];
// Order matters here. writable accounts must be processed before readonly accounts.
// 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
) {
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;
}
Loading
Loading