Skip to content

Instantly share code, notes, and snippets.

View rubpy's full-sized avatar
💭
💥 Phosphorylating

rubpy rubpy

💭
💥 Phosphorylating
View GitHub Profile
import web3 from "@solana/web3.js";
import * as BufferLayout from "@solana/buffer-layout";
import bs58 from "bs58";
//////////////////////////////////////////////////
class BorshString extends BufferLayout.Layout<string> {
constructor(property?: string) {
super(-1, property);
}
@rubpy
rubpy / TokenAccountMonitor.ts
Created July 17, 2024 01:00
Crude example of a gRPC/WebSocket-based Solana token price monitor.
import * as web3 from "@solana/web3.js";
import * as spl from "@solana/spl-token";
import bs58 from "bs58";
import GeyserClient, {
SubscribeRequest as GeyserSubscribeRequest,
SubscribeUpdate as GeyserSubscribeUpdate,
CommitmentLevel as GeyserCommitmentLevel,
} from "@triton-one/yellowstone-grpc";
import { ClientDuplexStream } from "@grpc/grpc-js";
@rubpy
rubpy / rpc_fetch_pump_global_state.ts
Created June 22, 2024 19:35
Tracking changes in Pump.fun global state (initialVirtualTokenReserves, initialVirtualSolReserves, initialRealTokenReserves, tokenTotalSupply, feeBasisPoints).
import * as web3 from "@solana/web3.js";
import bs58 from "bs58";
//////////////////////////////////////////////////
function readBytes(buf: Buffer, offset: number, length: number): Buffer {
const end = offset + length;
if (buf.byteLength < end) throw new RangeError("range out of bounds");
return buf.subarray(offset, end);
}
@rubpy
rubpy / rpc_fetch_token_mint_info.ts
Created June 20, 2024 21:16
Fetching basic token (mint) info (through getAccountInfo), e.g.: total supply, mintAuthority, freezeAuthority...
import * as web3 from "@solana/web3.js";
import { TOKEN_PROGRAM_ID, unpackMint, Mint } from "@solana/spl-token";
//////////////////////////////////////////////////
async function getTokenInfo(conn: web3.Connection, tokenMint: web3.PublicKey): Promise<Mint | null> {
const info = await conn.getAccountInfo(tokenMint);
if (!info) return null;
try {
@rubpy
rubpy / rpc_fetch_metaplex_token_metadata.ts
Created June 20, 2024 20:46
Fetching on-chain Metaplex token metadata (through lightweight getAccountInfo).
import * as web3 from "@solana/web3.js";
import {
MPL_TOKEN_METADATA_PROGRAM_ID,
MetadataAccountData as MplMetadataAccountData,
getMetadataAccountDataSerializer,
} from "@metaplex-foundation/mpl-token-metadata";
//////////////////////////////////////////////////
const METAPLEX_PROGRAM_ID = new web3.PublicKey(MPL_TOKEN_METADATA_PROGRAM_ID);
@rubpy
rubpy / pump_idl.json
Created June 20, 2024 19:52
Pump.fun program IDL (v0.1.0)
{
"version": "0.1.0",
"name": "pump",
"instructions": [
{
"name": "initialize",
"docs": [ "Creates the global state." ],
"accounts": [
{ "name": "global" , "isMut": true , "isSigner": false },
{ "name": "user" , "isMut": true , "isSigner": true },
@rubpy
rubpy / rpc_fetch_pump_token_price.ts
Created June 20, 2024 18:39
Fetching Pump.fun bonding curve state and calculating price of token/SOL.
import * as web3 from "@solana/web3.js";
//////////////////////////////////////////////////
function readBytes(buf: Buffer, offset: number, length: number): Buffer {
const end = offset + length;
if (buf.byteLength < end) throw new RangeError("range out of bounds");
return buf.subarray(offset, end);
}
@rubpy
rubpy / rpc_fetch_raydium_pool_init.ts
Created June 6, 2024 19:40
Finds Raydium AMM pool initialization transaction (partially deserializes pool state, calls getSignaturesForAddress, searches for a Raydium innerInstruction, etc.).
import * as web3 from "@solana/web3.js";
import bs58 from "bs58";
//////////////////////////////////////////////////
function findInstructionsInTransaction(
tx: web3.ParsedTransactionWithMeta,
predicate?: (instruction: web3.ParsedInstruction | web3.PartiallyDecodedInstruction, outerInstruction: (web3.ParsedInstruction | web3.PartiallyDecodedInstruction) | null) => boolean,
): Array<web3.ParsedInstruction | web3.PartiallyDecodedInstruction> {
if (!tx || !tx.meta || !tx.transaction || !tx.transaction.message || !tx.transaction.message.instructions) {
@rubpy
rubpy / raydium_sdk_v2_example.ts
Created June 6, 2024 00:43
Fetching pool info and determining prices of token/SOL & SOL/USDC pairs via Raydium SDK V2.
import * as raydium from "@raydium-io/raydium-sdk-v2";
//////////////////////////////////////////////////
const RAYDIUM_CLMM_SOL_USDC_ID = "2QdhepnKRTLjjSqPL1PtKNwqrUkoLee5Gqs8bvZhRdMv";
function normalizeRaydiumBetaPoolInfoResponse(response: raydium.ApiV3PoolInfoItem[] | raydium.ApiV3PageIns<raydium.ApiV3PoolInfoItem>): raydium.ApiV3PoolInfoItem[] {
if (response === null || typeof response !== "object") {
return [];
}
@rubpy
rubpy / Makefile
Created January 16, 2023 18:18
A quick Makefile! 🧩
# ===== Directory structure =====
# build/
# foo/
# another.c.o
# main
# main.c.o
# other.c.o
#
# lib/
#