@cobaltx/sdk-v2
Version:
An SDK for building applications on top of CobaltX.
99 lines (83 loc) • 3.36 kB
text/typescript
import { getNetworkConfig, NetworkName } from "@/config";
import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
import { AccountMeta, PublicKey, SystemProgram, SYSVAR_RENT_PUBKEY } from "@solana/web3.js";
interface AccountMetaProps {
pubkey: PublicKey;
isSigner?: boolean;
isWritable?: boolean;
}
export function accountMeta({ pubkey, isSigner = false, isWritable = true }: AccountMetaProps): AccountMeta {
return {
pubkey,
isWritable,
isSigner,
};
}
export const commonSystemAccountMeta = [
accountMeta({ pubkey: TOKEN_PROGRAM_ID, isWritable: false }),
accountMeta({ pubkey: SystemProgram.programId, isWritable: false }),
accountMeta({ pubkey: SYSVAR_RENT_PUBKEY, isWritable: false }),
];
export type PublicKeyish = PublicKey | string;
export function validateAndParsePublicKey({
publicKey: orgPubKey,
transformSol,
network,
}: {
publicKey: PublicKeyish;
transformSol?: boolean;
network?: NetworkName;
}): PublicKey {
const publicKey = tryParsePublicKey(orgPubKey.toString());
if (transformSol && network) {
const { WSOLMint } = getMintAddresses(network);
if (publicKey instanceof PublicKey) {
if (transformSol && publicKey.equals(SOLMint)) return WSOLMint;
return publicKey;
}
if (transformSol && publicKey.toString() === SOLMint.toBase58()) return WSOLMint;
} else if (publicKey instanceof PublicKey) {
return publicKey;
}
if (typeof publicKey === "string") {
if (publicKey === PublicKey.default.toBase58()) return PublicKey.default;
try {
const key = new PublicKey(publicKey);
return key;
} catch {
throw new Error("invalid public key");
}
}
throw new Error("invalid public key");
}
export function tryParsePublicKey(v: string): PublicKey | string {
try {
return new PublicKey(v);
} catch (e) {
return v;
}
}
export const MEMO_PROGRAM_ID = new PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr");
export const MEMO_PROGRAM_ID2 = new PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr");
export const RENT_PROGRAM_ID = new PublicKey("SysvarRent111111111111111111111111111111111");
export const CLOCK_PROGRAM_ID = new PublicKey("SysvarC1ock11111111111111111111111111111111");
export const METADATA_PROGRAM_ID = new PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s");
export const INSTRUCTION_PROGRAM_ID = new PublicKey("Sysvar1nstructions1111111111111111111111111");
export const SYSTEM_PROGRAM_ID = SystemProgram.programId;
export const SOLMint = PublicKey.default;
export function getMintAddresses(network: NetworkName) {
const config = getNetworkConfig(network);
return {
USDCMint: new PublicKey(config.USDC_MINT_ADDRESS),
USDTMint: new PublicKey(config.USDT_MINT_ADDRESS),
WSOLMint: new PublicKey(config.WSOL_MINT_ADDRESS),
ETHMint: new PublicKey(config.ETH_MINT_ADDRESS),
BONK_Mint: config.BONK_MINT_ADDRESS ? new PublicKey(config.BONK_MINT_ADDRESS) : PublicKey.default,
SOL_Mint: config.SOL_MINT_ADDRESS ? new PublicKey(config.SOL_MINT_ADDRESS) : PublicKey.default,
INPUT_MINT: new PublicKey(config.INPUT_MINT_ADDRESS),
OUTPUT_MINT: new PublicKey(config.OUTPUT_MINT_ADDRESS),
};
}
export function solToWSol(mint: PublicKeyish, network: NetworkName): PublicKey {
return validateAndParsePublicKey({ publicKey: mint, transformSol: true, network });
}