@0block.io/sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/fetch.js ADDED
@@ -0,0 +1,104 @@
1
+ import * as web3 from "@solana/web3.js";
2
+ import { PUMPFUN_FEE_PROGRAM_ID, PUMPFUN_GLOBAL_BUYBACK_RECIPIENTS_OFFSET, PUMPFUN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID, } from "./constants.js";
3
+ import { deriveAta, derivePumpfunBondingCurve, derivePumpfunBondingCurveV2, derivePumpfunCreatorVault, derivePumpfunEventAuthority, derivePumpfunFeeConfig, derivePumpfunGlobal, derivePumpfunGlobalVolumeAccumulator, toPk, } from "./pda.js";
4
+ /** AccountFetcher backed by a @solana/web3.js Connection. */
5
+ export function connectionFetcher(connection) {
6
+ return {
7
+ async getAccountInfo(address) {
8
+ const info = await connection.getAccountInfo(address);
9
+ return info ? { data: info.data, owner: info.owner } : null;
10
+ },
11
+ };
12
+ }
13
+ /** Which token program owns a mint (classic SPL vs Token-2022). */
14
+ export async function resolveTokenProgram(fetcher, mint) {
15
+ const pk = toPk(mint);
16
+ if (pk.equals(web3.SystemProgram.programId))
17
+ return TOKEN_PROGRAM_ID;
18
+ const info = await fetcher.getAccountInfo(pk);
19
+ if (!info)
20
+ throw new Error(`Mint not found: ${pk.toBase58()}`);
21
+ return info.owner.equals(TOKEN_2022_PROGRAM_ID)
22
+ ? TOKEN_2022_PROGRAM_ID
23
+ : TOKEN_PROGRAM_ID;
24
+ }
25
+ /** Global.fee_recipient (offset: discriminator + initialized flag + authority). */
26
+ export async function fetchPumpfunGlobalFeeRecipient(fetcher, globalAccount) {
27
+ const info = await fetcher.getAccountInfo(globalAccount);
28
+ if (!info) {
29
+ throw new Error(`pump.fun global account not found: ${globalAccount.toBase58()}`);
30
+ }
31
+ const offset = 8 + 1 + 32;
32
+ if (info.data.length < offset + 32) {
33
+ throw new Error("pump.fun global account data too short");
34
+ }
35
+ return new web3.PublicKey(info.data.slice(offset, offset + 32));
36
+ }
37
+ /** Non-zero entries of Global.buyback_fee_recipients ([Pubkey; 8]). */
38
+ export async function fetchPumpfunBuybackFeeRecipients(fetcher, globalAccount) {
39
+ const info = await fetcher.getAccountInfo(globalAccount);
40
+ if (!info) {
41
+ throw new Error(`pump.fun global account not found: ${globalAccount.toBase58()}`);
42
+ }
43
+ const offset = PUMPFUN_GLOBAL_BUYBACK_RECIPIENTS_OFFSET;
44
+ if (info.data.length < offset + 8 * 32) {
45
+ throw new Error("pump.fun global account data too short for buyback_fee_recipients");
46
+ }
47
+ const recipients = [];
48
+ for (let i = 0; i < 8; i++) {
49
+ const key = new web3.PublicKey(info.data.slice(offset + i * 32, offset + (i + 1) * 32));
50
+ if (!key.equals(web3.PublicKey.default))
51
+ recipients.push(key);
52
+ }
53
+ if (!recipients.length) {
54
+ throw new Error("pump.fun global has no buyback fee recipients configured");
55
+ }
56
+ return recipients;
57
+ }
58
+ /** BondingCurve.creator (offset: discriminator + 5×u64 + bool). */
59
+ export async function fetchPumpfunBondingCurveCreator(fetcher, bondingCurve) {
60
+ const info = await fetcher.getAccountInfo(bondingCurve);
61
+ if (!info) {
62
+ throw new Error(`pump.fun bonding curve not found: ${bondingCurve.toBase58()}`);
63
+ }
64
+ const offset = 8 + 5 * 8 + 1;
65
+ if (info.data.length < offset + 32) {
66
+ throw new Error("pump.fun bonding curve data too short");
67
+ }
68
+ return new web3.PublicKey(info.data.slice(offset, offset + 32));
69
+ }
70
+ /**
71
+ * Resolve everything a pump.fun swap needs for one mint. Run this server-side
72
+ * next to an RPC (the tenant's backend) and stream the result to clients — every field
73
+ * is a plain base58 string. Only the per-user volume accumulator is left for
74
+ * build time (it depends on the user key).
75
+ */
76
+ export async function resolvePumpfunMarketContext(fetcher, params) {
77
+ const mint = toPk(params.mint);
78
+ const pumpProgram = toPk(params.pumpfunProgram ?? PUMPFUN_PROGRAM_ID);
79
+ const feeProgram = toPk(params.pumpfunFeeProgram ?? PUMPFUN_FEE_PROGRAM_ID);
80
+ const global = derivePumpfunGlobal(pumpProgram);
81
+ const bondingCurve = derivePumpfunBondingCurve(pumpProgram, mint);
82
+ const [mintTokenProgram, feeRecipient, buybackRecipients, creator] = await Promise.all([
83
+ resolveTokenProgram(fetcher, mint),
84
+ fetchPumpfunGlobalFeeRecipient(fetcher, global),
85
+ fetchPumpfunBuybackFeeRecipients(fetcher, global),
86
+ fetchPumpfunBondingCurveCreator(fetcher, bondingCurve),
87
+ ]);
88
+ return {
89
+ mint: mint.toBase58(),
90
+ mintTokenProgram: mintTokenProgram.toBase58(),
91
+ pumpfunProgram: pumpProgram.toBase58(),
92
+ pumpfunFeeProgram: feeProgram.toBase58(),
93
+ global: global.toBase58(),
94
+ bondingCurve: bondingCurve.toBase58(),
95
+ associatedBondingCurve: deriveAta(bondingCurve, mint, mintTokenProgram).toBase58(),
96
+ bondingCurveV2: derivePumpfunBondingCurveV2(pumpProgram, mint).toBase58(),
97
+ creatorVault: derivePumpfunCreatorVault(pumpProgram, creator).toBase58(),
98
+ feeRecipient: feeRecipient.toBase58(),
99
+ buybackFeeRecipients: buybackRecipients.map((r) => r.toBase58()),
100
+ eventAuthority: derivePumpfunEventAuthority(pumpProgram).toBase58(),
101
+ globalVolumeAccumulator: derivePumpfunGlobalVolumeAccumulator(pumpProgram).toBase58(),
102
+ feeConfig: derivePumpfunFeeConfig(feeProgram, pumpProgram).toBase58(),
103
+ };
104
+ }