Skip to content

Instantly share code, notes, and snippets.

@0xdeepmehta
Created November 22, 2024 20:29
Show Gist options
  • Save 0xdeepmehta/cd0d4693bb94d0ec743db85f4b8c9391 to your computer and use it in GitHub Desktop.
Save 0xdeepmehta/cd0d4693bb94d0ec743db85f4b8c9391 to your computer and use it in GitHub Desktop.
import { AnchorProvider, Program } from '@coral-xyz/anchor'
import {
calculateBorrowRate,
calculateDepositRate,
getSpotMarketPublicKey,
SpotMarketAccount
} from '@drift-labs/sdk'
import { Connection, PublicKey } from '@solana/web3.js'
import * as driftIdl from "./drift_idl_2.json";
import { Drift } from "./drift_idl_type_2";
export const connection = new Connection(Bun.env.SYNC_RPC_ENDPOINT!, "processed");
export const provider = new AnchorProvider(connection, {} as any, { commitment: 'processed' });
const DRIFT_PROGRAM_ID = new PublicKey("dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH")
export type DriftIdlType = Drift;
export type DriftProgram = Program<Drift>;
const idl = { ...(driftIdl as unknown as DriftIdlType), address: "dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH" };
const program = new Program(idl, provider) as Program<Drift>;
// Spot Market Indices
const SPOT_MARKETS = {
USDC: 0,
SOL: 1,
mSOL: 2,
wBTC: 3,
USDT: 5,
jitoSOL: 6,
bSOL: 8,
PYUSD: 22,
USDe: 23,
sUSDe: 24,
BNSOL: 25,
cbBTC: 27,
USDS: 28
};
interface SpotMarketInfo {
name: string;
index: number;
publicKey?: PublicKey;
depositRate?: number;
borrowRate?: number;
}
export const fetchSpotMarkets = async (): Promise<SpotMarketInfo[]> => {
const spotMarkets: SpotMarketInfo[] = [];
for (const [name, index] of Object.entries(SPOT_MARKETS)) {
try {
const spotMarketPublicKey = await getSpotMarketPublicKey(DRIFT_PROGRAM_ID, index);
const spotMarketAccount = await program.account.spotMarket.fetch(spotMarketPublicKey) as SpotMarketAccount;
const depositRate = calculateDepositRate(spotMarketAccount).toNumber() / 10_000;
const borrowRate = calculateBorrowRate(spotMarketAccount).toNumber() / 10_000;
spotMarkets.push({
name,
index,
publicKey: spotMarketPublicKey,
depositRate,
borrowRate
});
console.log(`${name} Spot Market:
- Public Key: ${spotMarketPublicKey.toBase58()}
- Deposit Rate: ${depositRate}%
- Borrow Rate: ${borrowRate}%`);
} catch (error) {
console.error(`Error fetching ${name} spot market:`, error);
spotMarkets.push({
name,
index,
publicKey: undefined,
depositRate: undefined,
borrowRate: undefined
});
}
}
return spotMarkets;
};
export const driftEntryPoint = async (connection: Connection) => {
console.log("Fetching Drift Protocol Spot Markets...");
const markets = await fetchSpotMarkets();
return markets;
};
// Uncomment to run directly
driftEntryPoint(connection);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment