Skip to content

Commit

Permalink
Add min context slot
Browse files Browse the repository at this point in the history
  • Loading branch information
bruce-riley authored and evan-gray committed Jan 30, 2024
1 parent 5e3f219 commit ad2609e
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 3 deletions.
85 changes: 85 additions & 0 deletions ts-test/mainnet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Usage: API_KEY=<> npx tsx ts-test/mainnet.ts
import {
PerChainQueryRequest,
QueryProxyQueryResponse,
QueryRequest,
SolanaAccountQueryRequest,
} from "@wormhole-foundation/wormhole-query-sdk";
import axios from "axios";
import "dotenv/config";
import { logQueryResponseInfo } from "./utils";
import { DATA_SLICE_LENGTH, DATA_SLICE_OFFSET } from "./consts";

const SOLANA_NODE_URL = "https://api.mainnet-beta.solana.com";

async function getSolanaSlot(comm: string): Promise<bigint> {
const response = await axios.post(SOLANA_NODE_URL, {
jsonrpc: "2.0",
id: 1,
method: "getSlot",
params: [{ commitment: comm, transactionDetails: "none" }],
});

return response.data.result;
}

(async () => {
const QUERY_URL = "https://api.wormholelabs.xyz/v1/query";
const API_KEY = process.env.API_KEY;
if (!API_KEY) {
throw new Error("API_KEY is required");
}

const currSlot = await getSolanaSlot("finalized");
const minContextSlot = BigInt(currSlot) + BigInt(2);

console.log(
`Performing query against Wormhole mainnet, currentSlot: `,
currSlot,
` using minContext Slot: `,
minContextSlot,
`\n`
);

// devnet stake pool from
// solana-program-library % ./target/release/spl-stake-pool --url "https://api.devnet.solana.com" list-all
const accounts = [
"Jito4APyf642JPZPx3hGc6WWJ8zPKtRbRs4P815Awbb",
"SysvarC1ock11111111111111111111111111111111",
];

const query = new QueryRequest(42, [
new PerChainQueryRequest(
1,
new SolanaAccountQueryRequest(
"finalized",
accounts,
minContextSlot,
BigInt(DATA_SLICE_OFFSET),
BigInt(DATA_SLICE_LENGTH)
)
),
]);
const serialized = Buffer.from(query.serialize()).toString("hex");
const before = performance.now();
const resp = (
await axios.post<QueryProxyQueryResponse>(
QUERY_URL,
{ bytes: serialized },
{ headers: { "X-API-Key": API_KEY } }
)
).data;
const after = performance.now();
const logResp = logQueryResponseInfo(resp.bytes);
if (minContextSlot == logResp.slotNumber) {
console.log("\nReturned slot matches requested slot.");
} else {
console.error(
"\nSlot mismatch: slotNumber: ",
logResp.slotNumber,
", minContextSlot: ",
minContextSlot
);
}
console.log(`\nQuery completed in ${(after - before).toFixed(2)}ms.`);
})();
39 changes: 36 additions & 3 deletions ts-test/testnet.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Usage: API_KEY=<> npx tsx ts-test/testnet.ts
import {
PerChainQueryRequest,
QueryProxyQueryResponse,
Expand All @@ -9,14 +10,36 @@ import "dotenv/config";
import { logQueryResponseInfo } from "./utils";
import { DATA_SLICE_LENGTH, DATA_SLICE_OFFSET } from "./consts";

const SOLANA_NODE_URL = "https://api.devnet.solana.com";

async function getSolanaSlot(comm: string): Promise<bigint> {
const response = await axios.post(SOLANA_NODE_URL, {
jsonrpc: "2.0",
id: 1,
method: "getSlot",
params: [{ commitment: comm, transactionDetails: "none" }],
});

return response.data.result;
}

(async () => {
const QUERY_URL = "https://testnet.ccq.vaa.dev/v1/query";
const API_KEY = process.env.API_KEY;
if (!API_KEY) {
throw new Error("API_KEY is required");
}

console.log(`Performing query against Wormhole testnet\n`);
const currSlot = await getSolanaSlot("finalized");
const minContextSlot = BigInt(currSlot) + BigInt(2);

console.log(
`Performing query against Wormhole testnet, currentSlot: `,
currSlot,
` using minContext Slot: `,
minContextSlot,
`\n`
);

// devnet stake pool from
// solana-program-library % ./target/release/spl-stake-pool --url "https://api.devnet.solana.com" list-all
Expand All @@ -31,7 +54,7 @@ import { DATA_SLICE_LENGTH, DATA_SLICE_OFFSET } from "./consts";
new SolanaAccountQueryRequest(
"finalized",
accounts,
undefined,
minContextSlot,
BigInt(DATA_SLICE_OFFSET),
BigInt(DATA_SLICE_LENGTH)
)
Expand All @@ -47,6 +70,16 @@ import { DATA_SLICE_LENGTH, DATA_SLICE_OFFSET } from "./consts";
)
).data;
const after = performance.now();
logQueryResponseInfo(resp.bytes);
const logResp = logQueryResponseInfo(resp.bytes);
if (minContextSlot == logResp.slotNumber) {
console.log("\nReturned slot matches requested slot.");
} else {
console.error(
"\nSlot mismatch: slotNumber: ",
logResp.slotNumber,
", minContextSlot: ",
minContextSlot
);
}
console.log(`\nQuery completed in ${(after - before).toFixed(2)}ms.`);
})();

0 comments on commit ad2609e

Please sign in to comment.