@coinbarrel/sdk 1.1.0 → 1.3.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/README.md CHANGED
@@ -1,253 +1,305 @@
1
- # @coinbarrel/sdk
2
-
3
- SDK for integrating with CoinBarrel AMM - bonding curves and liquidity pools on Solana.
4
-
5
- ## Installation
6
-
7
- ```bash
8
- npm install @coinbarrel/sdk @solana/web3.js @solana/spl-token
9
- ```
10
-
11
- ## Quick Start
12
-
13
- ```typescript
14
- import { Connection, Keypair, Transaction } from '@solana/web3.js';
15
- import { CoinBarrel, solToLamports } from '@coinbarrel/sdk';
16
-
17
- // Initialize SDK
18
- const connection = new Connection('https://api.mainnet-beta.solana.com');
19
- const sdk = new CoinBarrel({
20
- connection,
21
- network: 'mainnet', // or 'devnet'
22
- });
23
-
24
- // Token to trade
25
- const tokenMint = new PublicKey('YOUR_TOKEN_MINT');
26
- ```
27
-
28
- ## Bonding Curve Trading (Pre-Graduation)
29
-
30
- ### Get Buy Quote
31
-
32
- ```typescript
33
- const quote = await sdk.curve.getBuyQuote(
34
- tokenMint,
35
- solToLamports(0.1) // 0.1 SOL
36
- );
37
-
38
- if (quote) {
39
- console.log(`Tokens out: ${quote.tokensOut}`);
40
- console.log(`Price impact: ${quote.priceImpactBps / 100}%`);
41
- }
42
- ```
43
-
44
- ### Buy Tokens
45
-
46
- ```typescript
47
- import { Transaction, sendAndConfirmTransaction } from '@solana/web3.js';
48
-
49
- const instructions = await sdk.curve.buildBuyInstruction({
50
- tokenMint,
51
- buyer: wallet.publicKey,
52
- lamportsIn: solToLamports(0.1),
53
- });
54
-
55
- const tx = new Transaction().add(...instructions);
56
- const signature = await sendAndConfirmTransaction(connection, tx, [wallet]);
57
- ```
58
-
59
- ### Sell Tokens
60
-
61
- ```typescript
62
- const instructions = await sdk.curve.buildSellInstruction({
63
- tokenMint,
64
- seller: wallet.publicKey,
65
- tokenAmount: BigInt(1_000_000), // 1 token (6 decimals)
66
- });
67
-
68
- // Or sell entire balance
69
- const sellMaxIx = await sdk.curve.buildSellMaxInstruction({
70
- tokenMint,
71
- seller: wallet.publicKey,
72
- });
73
- ```
74
-
75
- ## AMM Pool Trading (Post-Graduation)
76
-
77
- ### Get Swap Quote
78
-
79
- ```typescript
80
- const quote = await sdk.pool.getSwapSolToTokenQuote(
81
- tokenMint,
82
- solToLamports(1.0) // 1 SOL
83
- );
84
-
85
- if (quote) {
86
- console.log(`Tokens out: ${quote.amountOut}`);
87
- console.log(`Fee: ${quote.fee} lamports`);
88
- }
89
- ```
90
-
91
- ### Swap SOL for Tokens
92
-
93
- ```typescript
94
- const instructions = await sdk.pool.buildSwapSolToTokenInstruction({
95
- tokenMint,
96
- user: wallet.publicKey,
97
- lamportsIn: solToLamports(1.0),
98
- minimumAmountOut: BigInt(900_000), // Slippage protection
99
- });
100
- ```
101
-
102
- ### Swap Tokens for SOL
103
-
104
- ```typescript
105
- const instructions = await sdk.pool.buildSwapTokenToSolInstruction({
106
- tokenMint,
107
- user: wallet.publicKey,
108
- amountIn: BigInt(1_000_000),
109
- minimumLamportsOut: solToLamports(0.09),
110
- });
111
-
112
- // Or sell entire balance
113
- const sellMaxIx = await sdk.pool.buildSwapTokenToSolMaxInstruction({
114
- tokenMint,
115
- user: wallet.publicKey,
116
- });
117
- ```
118
-
119
- ## Market Info
120
-
121
- ### Get Token Market State
122
-
123
- ```typescript
124
- const info = await sdk.getMarketInfo(tokenMint);
125
-
126
- if (info) {
127
- console.log(`Phase: ${info.phase}`); // 'curve' or 'pool'
128
- console.log(`Price: ${info.price} lamports/token`);
129
- console.log(`Market Cap: ${info.marketCapSol} lamports`);
130
- console.log(`Fee: ${info.feeBps / 100}%`);
131
- }
132
- ```
133
-
134
- ### Check Token Phase
135
-
136
- ```typescript
137
- if (await sdk.isOnCurve(tokenMint)) {
138
- // Use curve methods
139
- } else if (await sdk.isOnPool(tokenMint)) {
140
- // Use pool methods
141
- }
142
- ```
143
-
144
- ## Networks
145
-
146
- The SDK supports both devnet and mainnet:
147
-
148
- ```typescript
149
- // Devnet
150
- const devnetSdk = new CoinBarrel({
151
- connection: new Connection('https://api.devnet.solana.com'),
152
- network: 'devnet',
153
- });
154
-
155
- // Mainnet
156
- const mainnetSdk = new CoinBarrel({
157
- connection: new Connection('https://api.mainnet-beta.solana.com'),
158
- network: 'mainnet',
159
- });
160
- ```
161
-
162
- ## Utility Functions
163
-
164
- ```typescript
165
- import { solToLamports, lamportsToSol, toRawAmount, toUiAmount } from '@coinbarrel/sdk';
166
-
167
- // SOL conversions
168
- const lamports = solToLamports(1.5); // 1_500_000_000n
169
- const sol = lamportsToSol(1_500_000_000n); // 1.5
170
-
171
- // Token amount conversions (default 6 decimals)
172
- const raw = toRawAmount(100); // 100_000_000n
173
- const ui = toUiAmount(100_000_000n); // 100
174
- ```
175
-
176
- ## Token Metadata
177
-
178
- Fetch token metadata from the Metaplex Token Metadata program:
179
-
180
- ```typescript
181
- // Get on-chain metadata (fast - just name, symbol, uri)
182
- const metadata = await sdk.getTokenMetadata(tokenMint);
183
- if (metadata) {
184
- console.log(metadata.name); // "My Token"
185
- console.log(metadata.symbol); // "MTK"
186
- console.log(metadata.uri); // "https://..."
187
- }
188
-
189
- // Get full metadata including off-chain JSON (image, socials)
190
- const fullMetadata = await sdk.getTokenMetadataJson(tokenMint);
191
- if (fullMetadata) {
192
- console.log(fullMetadata.image); // "https://..." or "ipfs://..."
193
- console.log(fullMetadata.description); // "A cool token"
194
- console.log(fullMetadata.twitter); // "@mytoken"
195
- console.log(fullMetadata.website); // "https://..."
196
- }
197
-
198
- // Batch fetch for multiple tokens (more efficient)
199
- const mints = [mint1, mint2, mint3];
200
- const metadataMap = await sdk.batchGetTokenMetadata(mints);
201
- for (const [mintStr, meta] of metadataMap) {
202
- console.log(`${mintStr}: ${meta?.name}`);
203
- }
204
- ```
205
-
206
- ## PDA Helpers
207
-
208
- ```typescript
209
- // Get PDAs for a token
210
- const bondingCurve = sdk.getBondingCurvePda(tokenMint);
211
- const pool = sdk.getPoolPda(tokenMint);
212
- const holderReward = sdk.getHolderRewardPda(tokenMint, wallet.publicKey);
213
- const metadataPda = sdk.getMetadataPda(tokenMint); // Metaplex metadata PDA
214
- ```
215
-
216
- ## Fee Structure
217
-
218
- CoinBarrel fees are enforced at the program level and cannot be bypassed:
219
-
220
- - **1% total fee** on all trades
221
- - **50%** goes to CoinBarrel (protocol) - fixed
222
- - **25-40%** goes to holder rewards pool (of total fee, configured by token launcher via `trader_rewards_share_bps`)
223
- - **10-25%** goes to token creator (remainder: 50% - holder%)
224
-
225
- Example: If `trader_rewards_share_bps = 3000` (30%):
226
-
227
- - Platform: 50% of fee
228
- - Holders: 30% of fee
229
- - Creator: 20% of fee
230
-
231
- The split is set when the token is launched (`trader_rewards_share_bps` must be 2500-4000). The SDK reads fee recipients from on-chain state. Integrators cannot modify or bypass fees.
232
-
233
- ## TypeScript
234
-
235
- The SDK is fully typed:
236
-
237
- ```typescript
238
- import type {
239
- BondingCurveState,
240
- PoolState,
241
- MarketInfo,
242
- BuyQuote,
243
- SellQuote,
244
- SwapQuote,
245
- Network,
246
- TokenMetadata,
247
- TokenMetadataJson,
248
- } from '@coinbarrel/sdk';
249
- ```
250
-
251
- ## License
252
-
253
- MIT
1
+ # @coinbarrel/sdk
2
+
3
+ SDK for integrating with CoinBarrel AMM - bonding curves and liquidity pools on Solana.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @coinbarrel/sdk @solana/web3.js @solana/spl-token
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```typescript
14
+ import { Connection, Keypair, Transaction } from '@solana/web3.js';
15
+ import { CoinBarrel, solToLamports } from '@coinbarrel/sdk';
16
+
17
+ // Initialize SDK
18
+ const connection = new Connection('https://api.mainnet-beta.solana.com');
19
+ const sdk = new CoinBarrel({
20
+ connection,
21
+ network: 'mainnet', // or 'devnet'
22
+ });
23
+
24
+ // Token to trade
25
+ const tokenMint = new PublicKey('YOUR_TOKEN_MINT');
26
+ ```
27
+
28
+ ## Bonding Curve Trading (Pre-Graduation)
29
+
30
+ ### Get Buy Quote
31
+
32
+ ```typescript
33
+ const quote = await sdk.curve.getBuyQuote(
34
+ tokenMint,
35
+ solToLamports(0.1) // 0.1 SOL
36
+ );
37
+
38
+ if (quote) {
39
+ console.log(`Tokens out: ${quote.tokensOut}`);
40
+ console.log(`Price impact: ${quote.priceImpactBps / 100}%`);
41
+ }
42
+ ```
43
+
44
+ ### Buy Tokens
45
+
46
+ ```typescript
47
+ import { Transaction, sendAndConfirmTransaction } from '@solana/web3.js';
48
+
49
+ const instructions = await sdk.curve.buildBuyInstruction({
50
+ tokenMint,
51
+ buyer: wallet.publicKey,
52
+ lamportsIn: solToLamports(0.1),
53
+ });
54
+
55
+ const tx = new Transaction().add(...instructions);
56
+ const signature = await sendAndConfirmTransaction(connection, tx, [wallet]);
57
+ ```
58
+
59
+ ### Sell Tokens
60
+
61
+ ```typescript
62
+ const instructions = await sdk.curve.buildSellInstruction({
63
+ tokenMint,
64
+ seller: wallet.publicKey,
65
+ tokenAmount: BigInt(1_000_000), // 1 token (6 decimals)
66
+ });
67
+
68
+ // Or sell entire balance
69
+ const sellMaxIx = await sdk.curve.buildSellMaxInstruction({
70
+ tokenMint,
71
+ seller: wallet.publicKey,
72
+ });
73
+ ```
74
+
75
+ ## AMM Pool Trading (Post-Graduation)
76
+
77
+ ### Get Swap Quote
78
+
79
+ ```typescript
80
+ const quote = await sdk.pool.getSwapSolToTokenQuote(
81
+ tokenMint,
82
+ solToLamports(1.0) // 1 SOL
83
+ );
84
+
85
+ if (quote) {
86
+ console.log(`Tokens out: ${quote.amountOut}`);
87
+ console.log(`Fee: ${quote.fee} lamports`);
88
+ }
89
+ ```
90
+
91
+ ### Swap SOL for Tokens
92
+
93
+ ```typescript
94
+ const instructions = await sdk.pool.buildSwapSolToTokenInstruction({
95
+ tokenMint,
96
+ user: wallet.publicKey,
97
+ lamportsIn: solToLamports(1.0),
98
+ minimumAmountOut: BigInt(900_000), // Slippage protection
99
+ });
100
+ ```
101
+
102
+ ### Swap Tokens for SOL
103
+
104
+ ```typescript
105
+ const instructions = await sdk.pool.buildSwapTokenToSolInstruction({
106
+ tokenMint,
107
+ user: wallet.publicKey,
108
+ amountIn: BigInt(1_000_000),
109
+ minimumLamportsOut: solToLamports(0.09),
110
+ });
111
+
112
+ // Or sell entire balance
113
+ const sellMaxIx = await sdk.pool.buildSwapTokenToSolMaxInstruction({
114
+ tokenMint,
115
+ user: wallet.publicKey,
116
+ });
117
+ ```
118
+
119
+ ## Token Creation
120
+
121
+ ### Create a New Token
122
+
123
+ ```typescript
124
+ // Build create token instruction
125
+ const { instructions, mintKeypair } = sdk.buildCreateTokenInstruction({
126
+ payer: wallet.publicKey,
127
+ name: 'My Token',
128
+ symbol: 'MTK',
129
+ uri: 'https://arweave.net/...', // Metadata JSON URI
130
+ creatorFeeRecipient: wallet.publicKey,
131
+ traderRewardsShareBps: 3000, // 30% of fees to holders (optional, default 2500)
132
+ });
133
+
134
+ // Build and send transaction
135
+ const tx = new Transaction().add(...instructions);
136
+ const signature = await sendAndConfirmTransaction(connection, tx, [wallet, mintKeypair]);
137
+
138
+ console.log(`Token created: ${mintKeypair.publicKey.toBase58()}`);
139
+ ```
140
+
141
+ The token is created with:
142
+
143
+ - **Mint authority**: Revoked immediately after minting 1B tokens
144
+ - **Freeze authority**: Never set (null from creation)
145
+ - **Total supply**: 1,000,000,000 tokens (6 decimals)
146
+ - **Metadata**: Created via Metaplex (update_authority = creator)
147
+
148
+ ## Holder Rewards
149
+
150
+ ### Claim Rewards from Bonding Curve
151
+
152
+ ```typescript
153
+ const instructions = await sdk.buildClaimRewardsCurveInstruction(tokenMint, wallet.publicKey);
154
+
155
+ const tx = new Transaction().add(...instructions);
156
+ const signature = await sendAndConfirmTransaction(connection, tx, [wallet]);
157
+ ```
158
+
159
+ ### Claim Rewards from AMM Pool (Post-Graduation)
160
+
161
+ ```typescript
162
+ const instructions = await sdk.buildClaimRewardsPoolInstruction(tokenMint, wallet.publicKey);
163
+
164
+ const tx = new Transaction().add(...instructions);
165
+ const signature = await sendAndConfirmTransaction(connection, tx, [wallet]);
166
+ ```
167
+
168
+ ## Market Info
169
+
170
+ ### Get Token Market State
171
+
172
+ ```typescript
173
+ const info = await sdk.getMarketInfo(tokenMint);
174
+
175
+ if (info) {
176
+ console.log(`Phase: ${info.phase}`); // 'curve' or 'pool'
177
+ console.log(`Price: ${info.price} lamports/token`);
178
+ console.log(`Market Cap: ${info.marketCapSol} lamports`);
179
+ console.log(`Fee: ${info.feeBps / 100}%`);
180
+ }
181
+ ```
182
+
183
+ ### Check Token Phase
184
+
185
+ ```typescript
186
+ if (await sdk.isOnCurve(tokenMint)) {
187
+ // Use curve methods
188
+ } else if (await sdk.isOnPool(tokenMint)) {
189
+ // Use pool methods
190
+ }
191
+ ```
192
+
193
+ ## Networks
194
+
195
+ The SDK supports both devnet and mainnet:
196
+
197
+ ```typescript
198
+ // Devnet
199
+ const devnetSdk = new CoinBarrel({
200
+ connection: new Connection('https://api.devnet.solana.com'),
201
+ network: 'devnet',
202
+ });
203
+
204
+ // Mainnet
205
+ const mainnetSdk = new CoinBarrel({
206
+ connection: new Connection('https://api.mainnet-beta.solana.com'),
207
+ network: 'mainnet',
208
+ });
209
+ ```
210
+
211
+ ## Utility Functions
212
+
213
+ ```typescript
214
+ import { solToLamports, lamportsToSol, toRawAmount, toUiAmount } from '@coinbarrel/sdk';
215
+
216
+ // SOL conversions
217
+ const lamports = solToLamports(1.5); // 1_500_000_000n
218
+ const sol = lamportsToSol(1_500_000_000n); // 1.5
219
+
220
+ // Token amount conversions (default 6 decimals)
221
+ const raw = toRawAmount(100); // 100_000_000n
222
+ const ui = toUiAmount(100_000_000n); // 100
223
+ ```
224
+
225
+ ## Token Metadata
226
+
227
+ Fetch token metadata from the Metaplex Token Metadata program:
228
+
229
+ ```typescript
230
+ // Get on-chain metadata (fast - just name, symbol, uri)
231
+ const metadata = await sdk.getTokenMetadata(tokenMint);
232
+ if (metadata) {
233
+ console.log(metadata.name); // "My Token"
234
+ console.log(metadata.symbol); // "MTK"
235
+ console.log(metadata.uri); // "https://..."
236
+ }
237
+
238
+ // Get full metadata including off-chain JSON (image, socials)
239
+ const fullMetadata = await sdk.getTokenMetadataJson(tokenMint);
240
+ if (fullMetadata) {
241
+ console.log(fullMetadata.image); // "https://..." or "ipfs://..."
242
+ console.log(fullMetadata.description); // "A cool token"
243
+ console.log(fullMetadata.twitter); // "@mytoken"
244
+ console.log(fullMetadata.website); // "https://..."
245
+ }
246
+
247
+ // Batch fetch for multiple tokens (more efficient)
248
+ const mints = [mint1, mint2, mint3];
249
+ const metadataMap = await sdk.batchGetTokenMetadata(mints);
250
+ for (const [mintStr, meta] of metadataMap) {
251
+ console.log(`${mintStr}: ${meta?.name}`);
252
+ }
253
+ ```
254
+
255
+ ## PDA Helpers
256
+
257
+ ```typescript
258
+ // Get PDAs for a token
259
+ const bondingCurve = sdk.getBondingCurvePda(tokenMint);
260
+ const pool = sdk.getPoolPda(tokenMint);
261
+ const holderReward = sdk.getHolderRewardPda(tokenMint, wallet.publicKey);
262
+ const metadataPda = sdk.getMetadataPda(tokenMint); // Metaplex metadata PDA
263
+ ```
264
+
265
+ ## Fee Structure
266
+
267
+ CoinBarrel fees are enforced at the program level and cannot be bypassed:
268
+
269
+ - **1% total fee** on all trades
270
+ - **50%** goes to CoinBarrel (protocol) - fixed
271
+ - **25-40%** goes to holder rewards pool (of total fee, configured by token launcher via `trader_rewards_share_bps`)
272
+ - **10-25%** goes to token creator (remainder: 50% - holder%)
273
+
274
+ Example: If `trader_rewards_share_bps = 3000` (30%):
275
+
276
+ - Platform: 50% of fee
277
+ - Holders: 30% of fee
278
+ - Creator: 20% of fee
279
+
280
+ The split is set when the token is launched (`trader_rewards_share_bps` must be 2500-4000). The SDK reads fee recipients from on-chain state. Integrators cannot modify or bypass fees.
281
+
282
+ ## TypeScript
283
+
284
+ The SDK is fully typed:
285
+
286
+ ```typescript
287
+ import type {
288
+ BondingCurveState,
289
+ PoolState,
290
+ MarketInfo,
291
+ BuyQuote,
292
+ SellQuote,
293
+ SwapQuote,
294
+ Network,
295
+ TokenMetadata,
296
+ TokenMetadataJson,
297
+ CreateBarrelTokenParams,
298
+ ClaimHolderRewardsParams,
299
+ ClaimHolderRewardsCurveParams,
300
+ } from '@coinbarrel/sdk';
301
+ ```
302
+
303
+ ## License
304
+
305
+ MIT