Skip to content

Instantly share code, notes, and snippets.

@xeroc
Created May 3, 2024 07:42
Show Gist options
  • Save xeroc/b098d84b427ce6b402e7f947b27425ef to your computer and use it in GitHub Desktop.
Save xeroc/b098d84b427ce6b402e7f947b27425ef to your computer and use it in GitHub Desktop.
import idl from '../protocol-v2/sdk/src/idl/drift.json';
import { Connection, Keypair, PublicKey } from '@solana/web3.js';
import { Program, ProgramAccount } from '@coral-xyz/anchor';
import * as anchor from '@coral-xyz/anchor';
const wallet = new anchor.Wallet(Keypair.generate());
const connection = new Connection("https://api.devnet.solana.com");
//const connection = new Connection("https://api.mainnet-beta.solana.com");
const provider = new anchor.AnchorProvider(
connection,
wallet,
anchor.AnchorProvider.defaultOptions()
);
const programId = new PublicKey("JujUaGqUE7bEmptM5ta9mDcFxheSFmJYbUhWdphb31E")
const program = new anchor.Program(idl as anchor.Idl, programId, provider);
async function main() {
// Get state address
const state_address = (await PublicKey.findProgramAddress(
[Buffer.from(anchor.utils.bytes.utf8.encode('drift_state'))],
programId
))[0];
// Raw data (not usable)
const raw_state_info = await connection.getAccountInfo(state_address);
console.log("raw state data: ", raw_state_info.data);
const state = await program.account.state.fetch(state_address);
console.log(state);
const perp_market_index = 0;
const perp_market_address = (await PublicKey.findProgramAddress(
[
Buffer.from(anchor.utils.bytes.utf8.encode('perp_market')),
new anchor.BN(perp_market_index).toArrayLike(Buffer, 'le', 2),
],
programId
))[0];
const perp_market = await program.account.perpMarket.fetch(perp_market_address);
console.log(perp_market);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment