@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.
@@ -0,0 +1,124 @@
1
+ import * as web3 from "@solana/web3.js";
2
+ import type { ZeroBlockContext } from "./context.js";
3
+ /** Anywhere a key is accepted, both base58 strings and PublicKeys work. */
4
+ export type Address = string | web3.PublicKey;
5
+ export type SwapSide = "buy" | "sell";
6
+ /**
7
+ * Everything chain-derived that a pump.fun swap needs, resolved once
8
+ * (server-side, close to an RPC) and streamed to clients as plain strings.
9
+ * The frontend then builds transactions fully offline.
10
+ */
11
+ export interface PumpfunMarketContext {
12
+ /** The pump.fun token mint being traded. */
13
+ mint: string;
14
+ /** SPL token program owning the mint (classic or Token-2022). */
15
+ mintTokenProgram: string;
16
+ pumpfunProgram: string;
17
+ pumpfunFeeProgram: string;
18
+ global: string;
19
+ bondingCurve: string;
20
+ associatedBondingCurve: string;
21
+ bondingCurveV2: string;
22
+ creatorVault: string;
23
+ /** Current Global.fee_recipient. */
24
+ feeRecipient: string;
25
+ /** Non-zero Global.buyback_fee_recipients entries (builder picks one). */
26
+ buybackFeeRecipients: string[];
27
+ eventAuthority: string;
28
+ globalVolumeAccumulator: string;
29
+ feeConfig: string;
30
+ }
31
+ /** Serializable address-lookup-table contents, streamable over a WS. */
32
+ export interface LookupTableData {
33
+ key: string;
34
+ addresses: string[];
35
+ }
36
+ /**
37
+ * A trading preset as a tenant UI typically models it: slippage %, priority
38
+ * fee in SOL, bribe (gateway tip) in SOL. `mevMode` is carried through
39
+ * untouched — it is gateway-side behavior, not transaction content.
40
+ */
41
+ export interface TradingPreset {
42
+ /** Max acceptable slippage, percent (e.g. 20 for 20%). */
43
+ slippagePct: number;
44
+ /** Total priority fee in SOL (e.g. 0.001). */
45
+ prioritySol: number;
46
+ /** Gateway tip ("bribe") in SOL (e.g. 0.001). */
47
+ bribeSol: number;
48
+ mevMode?: "off" | "reduced" | "secure";
49
+ }
50
+ export interface TipParams {
51
+ /** Allowlisted gateway tip account. MUST stay a static message key. */
52
+ account: Address;
53
+ lamports: bigint;
54
+ }
55
+ export interface ComputeBudgetParams {
56
+ /** Total priority fee in SOL, converted to a per-CU price. */
57
+ prioritySol: number;
58
+ /** Compute-unit limit the price is spread over. */
59
+ computeUnitLimit?: number;
60
+ }
61
+ export interface BuildPumpfunSwapParams {
62
+ /** From createContext(): tenant-wrapped or direct-to-router. */
63
+ context: ZeroBlockContext;
64
+ market: PumpfunMarketContext;
65
+ /** The end user: fee payer, token-account owner, swap authority. */
66
+ user: Address;
67
+ side: SwapSide;
68
+ /** buy: lamports of wSOL in. sell: raw token amount in. */
69
+ amountIn: bigint;
70
+ /** Minimum acceptable output (quote minus slippage), raw units. */
71
+ minReturn: bigint;
72
+ /** Correlates the on-chain SwapResultEvent with an off-chain order. Random when omitted. */
73
+ orderId?: bigint;
74
+ /** A FINALIZED blockhash (gateway forwards to a different staked node). */
75
+ recentBlockhash: string;
76
+ /** Shared static-accounts tables (see buildLookupTableAddresses). */
77
+ lookupTables?: LookupTableData[];
78
+ /** Gateway tip. Omit only for plain-RPC submission paths. */
79
+ tip?: TipParams | null;
80
+ computeBudget?: ComputeBudgetParams | null;
81
+ /**
82
+ * buy only: prepend create-wSOL-ATA (idempotent) + transfer + syncNative so
83
+ * the user trades from native SOL without holding wSOL. Default true on buy.
84
+ */
85
+ wrapSol?: boolean;
86
+ /** sell only: append close-wSOL-ATA so proceeds land as native SOL. Default true on sell. */
87
+ unwrapWsol?: boolean;
88
+ }
89
+ export interface BuildSwapResult {
90
+ /** The unsigned v0 transaction. */
91
+ transaction: web3.VersionedTransaction;
92
+ /** Exactly what each required signer signs (serialized v0 message). */
93
+ messageBytes: Uint8Array;
94
+ /** Signers the message requires, in fee-payer-first order. */
95
+ requiredSigners: web3.PublicKey[];
96
+ /** order_id embedded in the swap args (u128). */
97
+ orderId: bigint;
98
+ /** Wire size once all signatures are attached; already checked ≤ 1232. */
99
+ estimatedSize: number;
100
+ }
101
+ /** Decoded router SwapResultEvent, normalized for accounting consumers. */
102
+ export interface DecodedSwapResult {
103
+ orderId: bigint;
104
+ status: "success" | "failure";
105
+ /** Present on success. */
106
+ commissions?: {
107
+ feeMint: string;
108
+ feeSource: "input" | "output";
109
+ feeBaseAmount: bigint;
110
+ totalFeeAmount: bigint;
111
+ items: Array<{
112
+ kind: "platform" | "tenant";
113
+ bps: number;
114
+ amount: bigint;
115
+ /** Tenant wrapper program id for tenant items; default pubkey for platform. */
116
+ tenantProgram: string;
117
+ }>;
118
+ };
119
+ /** Present on failure. */
120
+ failure?: {
121
+ code: bigint;
122
+ message: string;
123
+ };
124
+ }
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,32 @@
1
+ import * as web3 from "@solana/web3.js";
2
+ import type { SwapSide } from "../types.js";
3
+ export interface PumpfunRemainingAccountsParams {
4
+ dexProgramId: web3.PublicKey;
5
+ /** The swap authority — the user (signs the transaction). */
6
+ swapAuthority: web3.PublicKey;
7
+ swapSourceToken: web3.PublicKey;
8
+ swapDestinationToken: web3.PublicKey;
9
+ pumpfunGlobal: web3.PublicKey;
10
+ pumpfunFeeRecipient: web3.PublicKey;
11
+ pumpfunMint: web3.PublicKey;
12
+ pumpfunBondingCurve: web3.PublicKey;
13
+ pumpfunAssociatedBondingCurve: web3.PublicKey;
14
+ systemProgramId: web3.PublicKey;
15
+ tokenProgramId: web3.PublicKey;
16
+ pumpfunCreatorVault: web3.PublicKey;
17
+ pumpfunEventAuthority: web3.PublicKey;
18
+ pumpfunGlobalVolumeAccumulator: web3.PublicKey;
19
+ pumpfunUserVolumeAccumulator: web3.PublicKey;
20
+ pumpfunFeeConfig: web3.PublicKey;
21
+ pumpfunFeeProgram: web3.PublicKey;
22
+ pumpfunBondingCurveV2: web3.PublicKey;
23
+ pumpfunBuybackFeeRecipient: web3.PublicKey;
24
+ /** Classic token program, appended when the mint lives on Token-2022. */
25
+ extraTokenProgramId?: web3.PublicKey | null;
26
+ }
27
+ /**
28
+ * Remaining accounts for one pump.fun leg, in the exact order the router's
29
+ * adapter parses them (PumpfunBuyAccounts / PumpfunSellAccounts). The dex
30
+ * program id is always index 0 and is hard-checked on-chain.
31
+ */
32
+ export declare function buildPumpfunRemainingAccounts(side: SwapSide, p: PumpfunRemainingAccountsParams): web3.AccountMeta[];
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Remaining accounts for one pump.fun leg, in the exact order the router's
3
+ * adapter parses them (PumpfunBuyAccounts / PumpfunSellAccounts). The dex
4
+ * program id is always index 0 and is hard-checked on-chain.
5
+ */
6
+ export function buildPumpfunRemainingAccounts(side, p) {
7
+ const metas = [];
8
+ const push = (pubkey, isWritable, isSigner = false) => metas.push({ pubkey, isWritable, isSigner });
9
+ if (side === "buy") {
10
+ push(p.dexProgramId, false);
11
+ push(p.swapAuthority, false, true);
12
+ push(p.swapSourceToken, true);
13
+ push(p.swapDestinationToken, true);
14
+ push(p.pumpfunGlobal, false);
15
+ push(p.pumpfunFeeRecipient, true);
16
+ push(p.pumpfunMint, false);
17
+ push(p.pumpfunBondingCurve, true);
18
+ push(p.pumpfunAssociatedBondingCurve, true);
19
+ push(p.systemProgramId, false);
20
+ push(p.tokenProgramId, false);
21
+ push(p.pumpfunCreatorVault, true);
22
+ push(p.pumpfunEventAuthority, false);
23
+ push(p.pumpfunGlobalVolumeAccumulator, false);
24
+ push(p.pumpfunUserVolumeAccumulator, true);
25
+ push(p.pumpfunFeeConfig, false);
26
+ push(p.pumpfunFeeProgram, false);
27
+ push(p.pumpfunBondingCurveV2, false);
28
+ push(p.pumpfunBuybackFeeRecipient, true);
29
+ if (p.extraTokenProgramId)
30
+ push(p.extraTokenProgramId, false);
31
+ return metas;
32
+ }
33
+ if (side === "sell") {
34
+ push(p.dexProgramId, false);
35
+ push(p.swapAuthority, false, true);
36
+ push(p.swapSourceToken, true);
37
+ push(p.swapDestinationToken, true);
38
+ push(p.pumpfunGlobal, false);
39
+ push(p.pumpfunFeeRecipient, true);
40
+ push(p.pumpfunMint, false);
41
+ push(p.pumpfunBondingCurve, true);
42
+ push(p.pumpfunAssociatedBondingCurve, true);
43
+ push(p.systemProgramId, false);
44
+ push(p.pumpfunCreatorVault, true);
45
+ push(p.tokenProgramId, false);
46
+ push(p.pumpfunEventAuthority, false);
47
+ push(p.pumpfunFeeConfig, false);
48
+ push(p.pumpfunFeeProgram, false);
49
+ push(p.pumpfunBondingCurveV2, false);
50
+ push(p.pumpfunBuybackFeeRecipient, true);
51
+ if (p.extraTokenProgramId)
52
+ push(p.extraTokenProgramId, false);
53
+ return metas;
54
+ }
55
+ throw new Error(`Unsupported pump.fun side: ${side}`);
56
+ }
package/dist/wsol.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ import * as web3 from "@solana/web3.js";
2
+ /**
3
+ * Create an associated token account if it does not exist (ATA program
4
+ * instruction 1, "CreateIdempotent"). Safe to include on every transaction.
5
+ */
6
+ export declare function createAtaIdempotentInstruction(params: {
7
+ payer: web3.PublicKey;
8
+ owner: web3.PublicKey;
9
+ mint: web3.PublicKey;
10
+ tokenProgram?: web3.PublicKey;
11
+ }): web3.TransactionInstruction;
12
+ /** SPL Token "SyncNative" (instruction 17) — refresh a wSOL account balance. */
13
+ export declare function syncNativeInstruction(account: web3.PublicKey): web3.TransactionInstruction;
14
+ /** SPL Token "CloseAccount" (instruction 9) — reclaim lamports (unwrap wSOL). */
15
+ export declare function closeAccountInstruction(params: {
16
+ account: web3.PublicKey;
17
+ destination: web3.PublicKey;
18
+ owner: web3.PublicKey;
19
+ }): web3.TransactionInstruction;
20
+ /**
21
+ * Fund a user's wSOL ATA from native SOL: create the ATA if missing, transfer
22
+ * lamports in, then SyncNative so the token balance reflects them. Lets users
23
+ * buy from plain SOL without ever holding wSOL between trades.
24
+ */
25
+ export declare function wrapSolInstructions(params: {
26
+ user: web3.PublicKey;
27
+ lamports: bigint;
28
+ }): web3.TransactionInstruction[];
29
+ /** Close the user's wSOL ATA back into native SOL (after a sell). */
30
+ export declare function unwrapWsolInstruction(user: web3.PublicKey): web3.TransactionInstruction;
package/dist/wsol.js ADDED
@@ -0,0 +1,69 @@
1
+ import * as web3 from "@solana/web3.js";
2
+ import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, WSOL_MINT, } from "./constants.js";
3
+ import { deriveAta } from "./pda.js";
4
+ /**
5
+ * Create an associated token account if it does not exist (ATA program
6
+ * instruction 1, "CreateIdempotent"). Safe to include on every transaction.
7
+ */
8
+ export function createAtaIdempotentInstruction(params) {
9
+ const tokenProgram = params.tokenProgram ?? TOKEN_PROGRAM_ID;
10
+ const ata = deriveAta(params.owner, params.mint, tokenProgram);
11
+ return new web3.TransactionInstruction({
12
+ programId: ASSOCIATED_TOKEN_PROGRAM_ID,
13
+ keys: [
14
+ { pubkey: params.payer, isSigner: true, isWritable: true },
15
+ { pubkey: ata, isSigner: false, isWritable: true },
16
+ { pubkey: params.owner, isSigner: false, isWritable: false },
17
+ { pubkey: params.mint, isSigner: false, isWritable: false },
18
+ { pubkey: web3.SystemProgram.programId, isSigner: false, isWritable: false },
19
+ { pubkey: tokenProgram, isSigner: false, isWritable: false },
20
+ ],
21
+ data: Buffer.from([1]),
22
+ });
23
+ }
24
+ /** SPL Token "SyncNative" (instruction 17) — refresh a wSOL account balance. */
25
+ export function syncNativeInstruction(account) {
26
+ return new web3.TransactionInstruction({
27
+ programId: TOKEN_PROGRAM_ID,
28
+ keys: [{ pubkey: account, isSigner: false, isWritable: true }],
29
+ data: Buffer.from([17]),
30
+ });
31
+ }
32
+ /** SPL Token "CloseAccount" (instruction 9) — reclaim lamports (unwrap wSOL). */
33
+ export function closeAccountInstruction(params) {
34
+ return new web3.TransactionInstruction({
35
+ programId: TOKEN_PROGRAM_ID,
36
+ keys: [
37
+ { pubkey: params.account, isSigner: false, isWritable: true },
38
+ { pubkey: params.destination, isSigner: false, isWritable: true },
39
+ { pubkey: params.owner, isSigner: true, isWritable: false },
40
+ ],
41
+ data: Buffer.from([9]),
42
+ });
43
+ }
44
+ /**
45
+ * Fund a user's wSOL ATA from native SOL: create the ATA if missing, transfer
46
+ * lamports in, then SyncNative so the token balance reflects them. Lets users
47
+ * buy from plain SOL without ever holding wSOL between trades.
48
+ */
49
+ export function wrapSolInstructions(params) {
50
+ const ata = deriveAta(params.user, WSOL_MINT, TOKEN_PROGRAM_ID);
51
+ return [
52
+ createAtaIdempotentInstruction({
53
+ payer: params.user,
54
+ owner: params.user,
55
+ mint: WSOL_MINT,
56
+ }),
57
+ web3.SystemProgram.transfer({
58
+ fromPubkey: params.user,
59
+ toPubkey: ata,
60
+ lamports: params.lamports,
61
+ }),
62
+ syncNativeInstruction(ata),
63
+ ];
64
+ }
65
+ /** Close the user's wSOL ATA back into native SOL (after a sell). */
66
+ export function unwrapWsolInstruction(user) {
67
+ const ata = deriveAta(user, WSOL_MINT, TOKEN_PROGRAM_ID);
68
+ return closeAccountInstruction({ account: ata, destination: user, owner: user });
69
+ }
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@0block.io/sdk",
3
+ "version": "0.1.0",
4
+ "description": "Client-side transaction builder for the 0Block DEX router. Builds unsigned v0 swap transactions entirely offline — tenant-wrapped or direct — with shared address lookup tables, gateway tips, and on-chain fee-event decoding for accounting.",
5
+ "keywords": [
6
+ "solana",
7
+ "dex",
8
+ "router",
9
+ "swap",
10
+ "0block",
11
+ "transaction-builder",
12
+ "versioned-transactions",
13
+ "address-lookup-tables"
14
+ ],
15
+ "license": "UNLICENSED",
16
+ "type": "module",
17
+ "main": "./dist/index.js",
18
+ "types": "./dist/index.d.ts",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "import": "./dist/index.js"
23
+ }
24
+ },
25
+ "files": [
26
+ "dist"
27
+ ],
28
+ "sideEffects": false,
29
+ "engines": {
30
+ "node": ">=20"
31
+ },
32
+ "publishConfig": {
33
+ "access": "public"
34
+ },
35
+ "scripts": {
36
+ "build": "tsc -p tsconfig.json",
37
+ "test": "npm run build && node test/smoke.mjs",
38
+ "prepublishOnly": "npm test"
39
+ },
40
+ "dependencies": {
41
+ "@coral-xyz/anchor": "^0.31.1",
42
+ "@solana/web3.js": "^1.98.0"
43
+ },
44
+ "devDependencies": {
45
+ "typescript": "^5.5.4"
46
+ }
47
+ }