|
| 1 | +import { |
| 2 | + AccountInfo, |
| 3 | + ParsedAccountData, |
| 4 | + ParsedTransactionMeta, |
| 5 | + ParsedTransactionWithMeta, |
| 6 | + PublicKey, |
| 7 | +} from "@solana/web3.js"; |
| 8 | +import BigNumber from "bignumber.js"; |
| 9 | +import { v4, v5, parse } from "uuid"; |
| 10 | + |
| 11 | +const seed = v4(); |
| 12 | + |
| 13 | +export function publicKeyOf(str: string) { |
| 14 | + return new PublicKey(parse(v5(str, seed))); |
| 15 | +} |
| 16 | + |
| 17 | +export function parsedAccountInfo({ |
| 18 | + lamports, |
| 19 | + owner, |
| 20 | + program, |
| 21 | + info, |
| 22 | + executable, |
| 23 | + type, |
| 24 | +}: { |
| 25 | + lamports?: number; |
| 26 | + program: "stake" | "sysvar" | "spl-token" | "spl-token-2022"; |
| 27 | + owner?: PublicKey; |
| 28 | + info?: unknown; |
| 29 | + executable?: boolean; |
| 30 | + type: "account" | "delegated" | "stakeHistory"; |
| 31 | +}): AccountInfo<ParsedAccountData> { |
| 32 | + return { |
| 33 | + executable: executable ?? false, |
| 34 | + owner: owner ?? publicKeyOf("owner"), |
| 35 | + lamports: lamports ?? 20, |
| 36 | + data: { |
| 37 | + program, |
| 38 | + parsed: { info, type }, |
| 39 | + space: 200, |
| 40 | + }, |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +export function parsedStakeInfo({ |
| 45 | + stakingCredit, |
| 46 | + authorized, |
| 47 | + delegation, |
| 48 | +}: { |
| 49 | + stakingCredit: number; |
| 50 | + authorized: { |
| 51 | + staker: PublicKey; |
| 52 | + withdrawer: PublicKey; |
| 53 | + }; |
| 54 | + delegation: { |
| 55 | + activationEpoch: string; |
| 56 | + deactivationEpoch: string; |
| 57 | + stake: string; |
| 58 | + }; |
| 59 | +}) { |
| 60 | + return { |
| 61 | + meta: { |
| 62 | + rentExemptReserve: new BigNumber(33000), |
| 63 | + authorized, |
| 64 | + lockup: { |
| 65 | + custodian: publicKeyOf(""), |
| 66 | + epoch: 0, |
| 67 | + unixTimestamp: 0, |
| 68 | + }, |
| 69 | + }, |
| 70 | + stake: { |
| 71 | + creditsObserved: stakingCredit, |
| 72 | + delegation: { ...delegation, voter: publicKeyOf("voter"), warmupCooldownRate: 0.25 }, |
| 73 | + }, |
| 74 | + }; |
| 75 | +} |
| 76 | + |
| 77 | +export function parsedHistoryInfo({ |
| 78 | + minEpoch, |
| 79 | + history, |
| 80 | +}: { |
| 81 | + minEpoch: number; |
| 82 | + history: Array<{ activating: number; deactivating: number; effective: number }>; |
| 83 | +}) { |
| 84 | + return history.map((entry, index) => ({ |
| 85 | + epoch: minEpoch + index, |
| 86 | + stakeHistory: { |
| 87 | + activating: entry.activating, |
| 88 | + deactivating: entry.deactivating, |
| 89 | + effective: entry.effective, |
| 90 | + }, |
| 91 | + })); |
| 92 | +} |
| 93 | + |
| 94 | +export function parsedTokenInfo({ |
| 95 | + state, |
| 96 | + owner, |
| 97 | + mint, |
| 98 | + isNative, |
| 99 | + amount, |
| 100 | + decimals, |
| 101 | +}: { |
| 102 | + state?: "initialized" | "uninitialized" | "frozen"; |
| 103 | + owner?: PublicKey; |
| 104 | + mint?: PublicKey; |
| 105 | + isNative?: boolean; |
| 106 | + amount?: number; |
| 107 | + decimals?: number; |
| 108 | +}) { |
| 109 | + const amountOrDefault = amount ?? 2000; |
| 110 | + const decimalsOrDefault = decimals ?? 3; |
| 111 | + const uiAmount = amountOrDefault / 10 ** decimalsOrDefault; |
| 112 | + return { |
| 113 | + isNative: !!isNative, |
| 114 | + owner: owner ?? publicKeyOf("owner"), |
| 115 | + mint: mint ?? publicKeyOf("mint"), |
| 116 | + state: state ?? "initialized", |
| 117 | + tokenAmount: { |
| 118 | + amount: amountOrDefault.toString(), |
| 119 | + decimals: decimalsOrDefault, |
| 120 | + uiAmount, |
| 121 | + uiAmountString: uiAmount.toFixed(decimalsOrDefault), |
| 122 | + }, |
| 123 | + }; |
| 124 | +} |
| 125 | + |
| 126 | +export function parsedTransaction({ |
| 127 | + slot, |
| 128 | + meta, |
| 129 | + signature, |
| 130 | + blockTime, |
| 131 | +}: { |
| 132 | + slot?: number; |
| 133 | + meta?: ParsedTransactionMeta; |
| 134 | + signature: string; |
| 135 | + blockTime?: Date; |
| 136 | +}): ParsedTransactionWithMeta { |
| 137 | + return { |
| 138 | + blockTime: blockTime ? Math.floor(blockTime.getTime() / 1000) : NaN, |
| 139 | + slot: slot ?? 0, |
| 140 | + meta: meta ?? null, |
| 141 | + transaction: { |
| 142 | + signatures: [signature], |
| 143 | + message: { accountKeys: [], instructions: [], recentBlockhash: "" }, |
| 144 | + }, |
| 145 | + }; |
| 146 | +} |
| 147 | + |
| 148 | +export function epochInfo({ epoch }: { epoch: number }) { |
| 149 | + return { |
| 150 | + absoluteSlot: 166598, |
| 151 | + blockHeight: 166500, |
| 152 | + epoch, |
| 153 | + slotIndex: 2790, |
| 154 | + slotsInEpoch: 8192, |
| 155 | + transactionCount: 22661093, |
| 156 | + }; |
| 157 | +} |
0 commit comments