Skip to content

Commit

Permalink
show how mock response was generated
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-gray committed Mar 14, 2024
1 parent 30f31db commit 03d819d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/StakePoolRate.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ contract CounterTest is Test {
uint256 THIRTY_DAYS = 60*60*24*30;
bytes32 mockPoolAccount = 0x048a3e08c3b495be17f45427d89bec5b80c7e2695c1864d76743db39bed346d6;

// NOTE: These values are taken from a mock response. `npx tsx ts-test/gen-test.ts`
// some happy case defaults
bytes mockMainnetResponse = hex"010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000073010000002a01000104000000660000000966696e616c697a656400000000000000000000000000000000000000000000011a02048a3e08c3b495be17f45427d89bec5b80c7e2695c1864d76743db39bed346d606a7d51718c774c928566398691d5eb68b5eb8a39b4b6d5c73555b210000000001000104000001dd000000000e8a333900060fbc482645c089cb4c158a7c669dfe20bef457ae808d671dc9a32f28cbc4b1f0ad3d406ea9380200000000004e7b9000000000000000000006814ed4caf68a174672fdac86031a63e84ea15efa1d44b72293f6dbdb0016500000011a01451e3dd50d3b7b8536045c2b7ac2ec259473ebc25ae3bcbe1fbeb17d52fbc7be0d095d453d5883bfeeb42269657a79abd0ed08bb66f986591ce4f00f950bd5c85a1fcd5de2beec843fe794ddc95faf466d40451c9faa569e7822d92c7e6ae13afd23e07509baddedfdb516a90b9197bb504743255d0e37c5ff5dce8a241eedc4319ea768fedf644c8aae9b8e2188add06bc550fbf716c822b9ce63c7783d952e1ffcd141e9832caf10ad917495ca0f271b5b293cd47027ea737007ed40eb39a0bd09e6a3feecf99032e1c1df6b9722dcb3634e7b8e3440936bc34b0cc1c8eb521f06ddf6e1d765a193d9cbe146ceeb79ac1cb485ed5f5b37913a8cf5857eff00a9c2dba43ab1ac1800eb5e0a9753bf16003402000000000000000000000011d78000000000000001690006a7d5171875f729c73d93408f216120067ed88c76e08c287fc1946000000000000000283c338a0e000000002af7af65000000003402000000000000350200000000000021cdb16500000000";
uint8 mockMainnetSigV = 0x1c;
Expand Down
71 changes: 71 additions & 0 deletions ts-test/gen-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {
PerChainQueryRequest,
QueryProxyMock,
QueryRequest,
SolanaAccountQueryRequest,
signaturesToEvmStruct,
} from "@wormhole-foundation/wormhole-query-sdk";
import base58 from "bs58";
import { DATA_SLICE_LENGTH, DATA_SLICE_OFFSET } from "./consts";
import { logQueryResponseInfo } from "./utils";

(async () => {
const SOLANA_RPC = "https://api.mainnet-beta.solana.com";
const JITO_SOL_POOL = "Jito4APyf642JPZPx3hGc6WWJ8zPKtRbRs4P815Awbb"; // https://solanacompass.com/stake-pools/Jito4APyf642JPZPx3hGc6WWJ8zPKtRbRs4P815Awbbs
const SYSVAR_CLOCK = "SysvarC1ock11111111111111111111111111111111";
const JITO_ADDRESS_HEX = `0x${Buffer.from(
base58.decode(JITO_SOL_POOL)
).toString("hex")}`;
const THIRTY_MINUTES = 60 * 30;
const THIRTY_DAYS = 60 * 60 * 24 * 30;

console.log(`Mocking query using ${SOLANA_RPC}\n`);

const mock = new QueryProxyMock({
1: SOLANA_RPC,
});

const accounts = [JITO_SOL_POOL, SYSVAR_CLOCK];

const query = new QueryRequest(42, [
new PerChainQueryRequest(
1,
new SolanaAccountQueryRequest(
"finalized",
accounts,
undefined,
BigInt(DATA_SLICE_OFFSET),
BigInt(DATA_SLICE_LENGTH)
)
),
]);
const resp = await mock.mock(query);
const {
slotNumber,
blockTime,
totalActiveStake,
poolTokenSupply,
clockEpoch,
} = logQueryResponseInfo(resp.bytes);
const sig = signaturesToEvmStruct(resp.signatures);
console.log("\n\n*****\nmock result for Solidity\n*****\n\n");
console.log(` // some happy case defaults`);
console.log(` bytes mockMainnetResponse = hex"${resp.bytes}";`);
console.log(` uint8 mockMainnetSigV = 0x${sig[0].v};`);
console.log(` bytes32 mockMainnetSigR = 0x${sig[0].r};`);
console.log(` bytes32 mockMainnetSigS = 0x${sig[0].s};`);
console.log(` uint64 mockSlot = ${slotNumber.toString()};`);
console.log(` uint64 mockBlockTime = ${blockTime.toString()};`);
console.log(` uint64 mockEpoch = ${clockEpoch.toString()};`);
console.log(
` uint64 mockTotalActiveStake = ${totalActiveStake.toString()};`
);
console.log(
` uint64 mockPoolTokenSupply = ${poolTokenSupply.toString()};`
);
console.log(
` uint256 mockRate = ${
(totalActiveStake * BigInt(10) ** BigInt(18)) / poolTokenSupply
}; // (mockTotalActiveStake * (10 ** 18)) / mockPoolTokenSupply\n`
);
})();
1 change: 1 addition & 0 deletions ts-test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ export function logQueryResponseInfo(bytes: string) {
blockTime: solResponse.blockTime,
totalActiveStake,
poolTokenSupply,
clockEpoch,
};
}

0 comments on commit 03d819d

Please sign in to comment.