-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy path[action].js
More file actions
1491 lines (1380 loc) · 50.4 KB
/
Copy path[action].js
File metadata and controls
1491 lines (1380 loc) · 50.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Consolidated pumpfun action dispatcher.
*
* Routes (via vercel.json rewrites):
* POST /api/agents/:id/pumpfun/launch — create a pump.fun token from this agent
* POST /api/agents/:id/pumpfun/buy — bonding-curve buy
* POST /api/agents/:id/pumpfun/sell — bonding-curve sell
* GET /api/agents/:id/pumpfun/portfolio — aggregated positions + live PnL
* POST /api/agents/:id/pumpfun/swap — AMM swap (graduated tokens)
* POST /api/agents/:id/pumpfun/pay — @three-ws/agent-payments surface
*
* One bundle so Vercel doesn't re-bundle @solana/web3.js + @pump-fun/* per file.
*/
import { getSessionUser, authenticateBearer, extractBearer, isSameSiteOrigin } from '../../_lib/auth.js';
import {
cors,
json,
method,
readJson,
wrap,
error,
rateLimited,
serverError,
respondError,
} from '../../_lib/http.js';
import { limits, clientIp } from '../../_lib/rate-limit.js';
import { loadAgentForSigning, solanaConnection } from '../../_lib/agent-pumpfun.js';
import { submitProtected } from '../../_lib/execution-engine.js';
import { reserveSpend, finalizeSpend, releaseSpend } from '../../_lib/agent-spend-policy.js';
import { grindMintKeypair } from '../../_lib/pump-vanity.js';
import { sql } from '../../_lib/db.js';
import { Keypair, PublicKey } from '@solana/web3.js';
import { z } from 'zod';
const NATIVE_SOL_MINT = 'So11111111111111111111111111111111111111112';
async function resolveAuth(req) {
const session = await getSessionUser(req);
if (session) {
// CSRF defense-in-depth for the cookie path: these handlers sign real
// spends with platform-held custodial keys, so a cross-site POST riding
// the session cookie must never reach them. Reads stay open; bearer
// callers (agent keys, workers) are exempt since the token itself is
// the proof of intent.
const isRead = ['GET', 'HEAD', 'OPTIONS'].includes(String(req.method || '').toUpperCase());
if (!isRead && !isSameSiteOrigin(req)) {
throw Object.assign(new Error('cross-site request blocked'), { status: 403, code: 'forbidden' });
}
return { userId: session.id };
}
const bearer = await authenticateBearer(extractBearer(req));
if (bearer) return { userId: bearer.userId };
return null;
}
function extractAgentId(req) {
// Original URL pattern: /api/agents/:id/pumpfun/:action — id at parts[2].
const url = new URL(req.url, 'http://x');
const parts = url.pathname.split('/').filter(Boolean);
if (parts[1] === 'agents' && parts[3] === 'pumpfun') return parts[2];
// Fallback: query string (in case vercel.json passes it that way).
return req.query?.id || null;
}
export default wrap(async (req, res) => {
const action = req.query?.action;
const id = extractAgentId(req);
if (!id) {
if (cors(req, res)) return;
return error(res, 400, 'bad_request', 'missing agent id');
}
if (action === 'buy') return handleBuy(req, res, id);
if (action === 'launch') return handleLaunch(req, res, id);
if (action === 'pay') return handlePay(req, res, id);
if (action === 'portfolio') return handlePortfolio(req, res, id);
if (action === 'sell') return handleSell(req, res, id);
if (action === 'swap') return handleSwap(req, res, id);
if (cors(req, res)) return;
return error(res, 404, 'not_found', 'unknown pumpfun action');
});
// ── buy ────────────────────────────────────────────────────────────────────
const buyBodySchema = z
.object({
mint: z.string().min(32).max(64),
// A coin is paired against exactly one quote asset on-chain. SOL-paired
// coins spend SOL; USDC-paired coins spend USDC. The curve decides which,
// so the caller supplies the matching amount and the server validates it.
solAmount: z.number().positive().max(1000).optional(),
usdcAmount: z.number().positive().max(1_000_000).optional(),
slippageBps: z.number().int().min(0).max(10_000).default(500),
network: z.enum(['mainnet', 'devnet']).default('mainnet'),
})
.refine((v) => (v.solAmount ?? 0) > 0 || (v.usdcAmount ?? 0) > 0, {
message: 'solAmount or usdcAmount required',
path: ['solAmount'],
});
async function handleBuy(req, res, id) {
if (cors(req, res, { methods: 'POST,OPTIONS', credentials: true })) return;
if (!method(req, res, ['POST'])) return;
const auth = await resolveAuth(req);
if (!auth) return error(res, 401, 'unauthorized', 'sign in required');
const rl = await limits.authIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
let body;
try {
body = buyBodySchema.parse(await readJson(req));
} catch (e) {
return error(res, 400, 'validation_error', e.errors?.[0]?.message || 'invalid body');
}
const loaded = await loadAgentForSigning(id, auth.userId);
if (loaded.error) return error(res, loaded.error.status, loaded.error.code, loaded.error.msg);
const { keypair, meta } = loaded;
const [{ PumpSdk, OnlinePumpSdk, getBuyTokenAmountFromSolAmount }, BN] = await Promise.all([
import('@pump-fun/pump-sdk'),
import('bn.js').then((m) => m.default || m),
]);
const { slippagePercentFromBps, resolveTokenProgramForMintOwner, resolveCustodialQuote } =
await import('../../_lib/pump-trade-args.js');
const conn = solanaConnection(body.network);
const online = new OnlinePumpSdk(conn);
const sdk = new PumpSdk();
const mint = new PublicKey(body.mint);
let instructions;
let quote; // resolved quote asset { isSol, isUsdc, quoteSymbol, quoteMint }
let quoteAtomics; // BN — units of the quote asset this buy spends
try {
// base_token_program must match the mint owner — Token-2022 for create_v2
// coins (every coin pump.fun mints today), SPL Token for legacy coins.
const mintInfo = await conn.getAccountInfo(mint);
if (!mintInfo) return error(res, 404, 'mint_not_found', 'mint not found on this network');
const tokenProgram = resolveTokenProgramForMintOwner(mintInfo.owner);
const [global, feeConfig, state] = await Promise.all([
online.fetchGlobal(),
online.fetchFeeConfig().catch(() => null),
online.fetchBuyState(mint, keypair.publicKey, tokenProgram),
]);
// Resolve the coin's quote asset from the on-chain curve before building
// anything — the same resolution the wallet-signed path uses — so a
// USDC-paired coin is bought with USDC, not a SOL trade the curve refuses.
quote = resolveCustodialQuote(state.bondingCurve?.quoteMint, body.network);
if (!quote.isSol && !quote.isUsdc) {
return error(
res,
409,
'unsupported_quote',
`coin is ${quote.quoteSymbol}-paired — custodial trading supports SOL and USDC quote assets only`,
);
}
if (quote.isUsdc) {
if (body.usdcAmount == null)
return error(
res,
400,
'validation_error',
'usdcAmount required for USDC-paired coin',
);
const quoteMintPk = new PublicKey(quote.quoteMint);
// quote_token_program must match the quote mint's owner (USDC is SPL
// Token today; read it from chain so a future quote can't break us).
const quoteInfo = await conn.getAccountInfo(quoteMintPk);
if (!quoteInfo)
return error(
res,
404,
'quote_mint_not_found',
`quote mint ${quote.quoteMint} not found on ${body.network}`,
);
const quoteTokenProgram = resolveTokenProgramForMintOwner(quoteInfo.owner);
quoteAtomics = new BN(Math.round(body.usdcAmount * 1_000_000));
// Custodial USDC buys spend from the agent wallet's USDC ATA. Confirm
// it holds enough up front so the trade fails here with a clear error
// instead of a cryptic on-chain insufficient-funds revert.
const { getAssociatedTokenAddressSync } = await import('@solana/spl-token');
const usdcAta = getAssociatedTokenAddressSync(
quoteMintPk,
keypair.publicKey,
true,
quoteTokenProgram,
);
let haveAtomics = 0n;
try {
const bal = await conn.getTokenAccountBalance(usdcAta);
haveAtomics = BigInt(bal?.value?.amount ?? 0);
} catch {
haveAtomics = 0n; // missing/closed ATA → treat as zero balance
}
if (haveAtomics < BigInt(quoteAtomics.toString()))
return error(
res,
402,
'insufficient_usdc',
`agent wallet holds ${Number(haveAtomics) / 1e6} USDC, needs ${body.usdcAmount}`,
);
// buy_v2's `amount` is the base-token quantity to buy and must be > 0;
// derive it from the USDC input against the curve, passing quoteMint so
// the SDK prices in USDC (mirrors handleBuyPrep in api/pump/[action].js).
const tokenAmount = getBuyTokenAmountFromSolAmount({
global,
feeConfig,
mintSupply: state.bondingCurve.tokenTotalSupply,
bondingCurve: state.bondingCurve,
amount: quoteAtomics,
quoteMint: quoteMintPk,
});
if (!tokenAmount.gt(new BN(0)))
return error(
res,
400,
'amount_too_small',
'usdcAmount too small to buy any tokens',
);
instructions = await sdk.buyV2Instructions({
global,
bondingCurveAccountInfo: state.bondingCurveAccountInfo,
bondingCurve: state.bondingCurve,
associatedUserAccountInfo: state.associatedUserAccountInfo,
mint,
user: keypair.publicKey,
amount: tokenAmount,
quoteAmount: quoteAtomics,
slippage: slippagePercentFromBps(body.slippageBps, { defaultBps: 500 }),
tokenProgram,
quoteTokenProgram,
});
} else {
// SOL-paired buy (unchanged behaviour).
if (body.solAmount == null)
return error(
res,
400,
'validation_error',
'solAmount required for SOL-paired coin',
);
quoteAtomics = new BN(Math.floor(body.solAmount * 1e9));
const expected = getBuyTokenAmountFromSolAmount({
global,
feeConfig,
mintSupply: state.bondingCurve.tokenTotalSupply,
bondingCurve: state.bondingCurve,
amount: quoteAtomics,
});
if (!expected.gt(new BN(0)))
return error(res, 400, 'amount_too_small', 'solAmount too small to buy any tokens');
// Unified v2 buy (UPSTREAM-buy-sell-v2-announcement.md): SDK derives the
// wSOL quote from the curve, applies the percent slippage pad to
// max_quote_cost, and handles the mayhem fee pool.
instructions = await sdk.buyV2Instructions({
global,
bondingCurveAccountInfo: state.bondingCurveAccountInfo,
bondingCurve: state.bondingCurve,
associatedUserAccountInfo: state.associatedUserAccountInfo,
mint,
user: keypair.publicKey,
amount: expected,
quoteAmount: quoteAtomics,
slippage: slippagePercentFromBps(body.slippageBps, { defaultBps: 500 }),
tokenProgram,
});
}
} catch (err) {
console.error('[pumpfun/buy] build failed', err);
return respondError(res, err.status || 422, err.code || 'build_failed', err);
}
// Reserve against the daily SOL cap for SOL spends only — atomically, BEFORE
// sending, so two concurrent buys on a stolen session can't both slip past
// the limit. USDC buys move USDC, not SOL, so they are gated by the up-front
// USDC balance check above instead of the SOL cap.
let reservation = null;
if (quote.isSol) {
reservation = await reserveSpend({
agentId: id,
meta,
mint: body.mint,
solAmount: body.solAmount,
type: 'pumpfun.buy',
payload: { slippageBps: body.slippageBps, network: body.network },
});
if (!reservation.ok)
return error(res, reservation.status, reservation.code, reservation.msg);
}
let signature;
try {
// Protected send: priority fee + CU estimate, rebroadcast with blockhash
// refresh until it lands, hard throw on an on-chain revert.
({ signature } = await submitProtected({
network: body.network,
connection: conn,
payer: keypair,
instructions,
}));
} catch (err) {
console.error('[pumpfun/buy] send failed', err);
if (reservation) await releaseSpend(reservation.reservationId);
return serverError(res, 502, 'rpc_error', err);
}
if (reservation) {
await finalizeSpend(reservation.reservationId, {
mint: body.mint,
solAmount: body.solAmount,
slippageBps: body.slippageBps,
signature,
network: body.network,
});
} else {
// USDC buy: no SOL reservation row to finalize — record the action directly
// so it still appears in the agent's activity/audit trail.
await sql`
INSERT INTO agent_actions (agent_id, type, payload, source_skill)
VALUES (
${id},
${'pumpfun.buy'},
${JSON.stringify({
mint: body.mint,
usdcAmount: body.usdcAmount,
quoteMint: quote.quoteMint,
quoteSymbol: quote.quoteSymbol,
slippageBps: body.slippageBps,
signature,
network: body.network,
})}::jsonb,
${'pumpfun'}
)
`.catch((e) => console.error('[pumpfun/buy] log failed', e));
}
// Mirror into pump_agent_trades for cross-feature analytics (reputation,
// admin health, strategy backtests). Best-effort — agent may not have a
// pump_agent_mints row yet if the mint was launched outside three.ws.
try {
const [m] = await sql`
select id from pump_agent_mints where mint=${body.mint} and network=${body.network} limit 1
`;
if (m) {
// sol_amount stays lamports (SOL buys only); quote_amount carries the
// spent amount in the coin's own quote asset for both SOL and USDC.
const solLamports = quote.isSol
? BigInt(Math.floor(body.solAmount * 1_000_000_000)).toString()
: null;
await sql`
INSERT INTO pump_agent_trades
(mint_id, user_id, wallet, direction, route, sol_amount, slippage_bps, tx_signature, network,
quote_mint, quote_symbol, quote_amount)
VALUES
(${m.id}, ${auth.userId}, ${keypair.publicKey.toBase58()}, 'buy',
'bonding_curve', ${solLamports}, ${body.slippageBps || null},
${signature}, ${body.network},
${quote.quoteMint}, ${quote.quoteSymbol}, ${quoteAtomics.toString()})
ON CONFLICT (tx_signature, network) DO NOTHING
`;
}
} catch (e) {
console.error('[pumpfun/buy] trade index failed', e);
}
return json(res, 200, {
data: {
signature,
mint: body.mint,
...(quote.isSol
? { solAmount: body.solAmount }
: { usdcAmount: body.usdcAmount, quoteMint: quote.quoteMint }),
explorer: `https://solscan.io/tx/${signature}${body.network === 'devnet' ? '?cluster=devnet' : ''}`,
},
});
}
// ── launch ─────────────────────────────────────────────────────────────────
const launchBodySchema = z.object({
name: z.string().trim().min(1).max(32),
symbol: z.string().trim().min(1).max(10),
uri: z.string().url().max(2048),
solAmount: z.number().nonnegative().max(1000).optional(),
tokenAmount: z.number().nonnegative().optional(),
vanityPrefix: z.string().min(1).max(6).optional(),
vanitySuffix: z.string().min(1).max(6).optional(),
vanityIgnoreCase: z.boolean().optional(),
mintSecretKey: z.array(z.number().int().min(0).max(255)).length(64).optional(),
network: z.enum(['mainnet', 'devnet']).default('mainnet'),
});
async function handleLaunch(req, res, id) {
if (cors(req, res, { methods: 'POST,OPTIONS', credentials: true })) return;
if (!method(req, res, ['POST'])) return;
const auth = await resolveAuth(req);
if (!auth) return error(res, 401, 'unauthorized', 'sign in required');
const rl = await limits.authIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
let body;
try {
body = launchBodySchema.parse(await readJson(req));
} catch (e) {
return error(res, 400, 'validation_error', e.errors?.[0]?.message || 'invalid body');
}
const loaded = await loadAgentForSigning(id, auth.userId);
if (loaded.error) return error(res, loaded.error.status, loaded.error.code, loaded.error.msg);
const { keypair, meta } = loaded;
const { PumpSdk, OnlinePumpSdk } = await import('@pump-fun/pump-sdk');
const BN = (await import('bn.js')).default;
const conn = solanaConnection(body.network);
const online = new OnlinePumpSdk(conn);
const sdk = new PumpSdk();
let mint;
let vanityIterations = 1;
let vanityDurationMs = 0;
if (body.mintSecretKey) {
try {
mint = Keypair.fromSecretKey(Uint8Array.from(body.mintSecretKey));
} catch (e) {
return error(
res,
400,
'validation_error',
'mintSecretKey did not parse as a valid Solana keypair',
);
}
const addr = mint.publicKey.toBase58();
const ic = !!body.vanityIgnoreCase;
const cmp = (a, b) => (ic ? a.toLowerCase() === b.toLowerCase() : a === b);
if (body.vanityPrefix && !cmp(addr.slice(0, body.vanityPrefix.length), body.vanityPrefix)) {
return error(res, 400, 'validation_error', 'mintSecretKey does not match vanityPrefix');
}
if (body.vanitySuffix && !cmp(addr.slice(-body.vanitySuffix.length), body.vanitySuffix)) {
return error(res, 400, 'validation_error', 'mintSecretKey does not match vanitySuffix');
}
} else {
try {
const ground = await grindMintKeypair({
prefix: body.vanityPrefix,
suffix: body.vanitySuffix,
ignoreCase: body.vanityIgnoreCase,
});
mint = ground.keypair;
vanityIterations = ground.iterations;
vanityDurationMs = ground.durationMs;
} catch (err) {
console.error('[pumpfun/launch] vanity grind failed', err?.message);
return respondError(res, err.status || 500, err.code || 'internal', err);
}
}
const solLamports = new BN(Math.floor((body.solAmount || 0) * 1e9));
const tokenAmount = new BN(body.tokenAmount || 0);
let instructions;
if (solLamports.gtn(0)) {
const global = await online.fetchGlobal();
instructions = await sdk.createV2AndBuyInstructions({
global,
mint: mint.publicKey,
name: body.name,
symbol: body.symbol,
uri: body.uri,
creator: keypair.publicKey,
user: keypair.publicKey,
amount: tokenAmount,
solAmount: solLamports,
mayhemMode: false,
});
} else {
const ix = await sdk.createV2Instruction({
mint: mint.publicKey,
name: body.name,
symbol: body.symbol,
uri: body.uri,
creator: keypair.publicKey,
user: keypair.publicKey,
mayhemMode: false,
});
instructions = [ix];
}
const mintAddr = mint.publicKey.toBase58();
// Gate the launch's initial buy against the agent's spend policy. Previously
// a launch could spend up to 1000 SOL with no per-tx or daily cap, so a stolen
// session could drain the custodial wallet through a single large initial buy.
// A create-only launch (solAmount 0) passes the cap and is still recorded.
const reservation = await reserveSpend({
agentId: id,
meta,
mint: mintAddr,
solAmount: body.solAmount || 0,
type: 'pumpfun.launch',
payload: {
name: body.name,
symbol: body.symbol,
uri: body.uri,
network: body.network,
vanity_prefix: body.vanityPrefix || null,
vanity_suffix: body.vanitySuffix || null,
vanity_ignore_case: body.vanityIgnoreCase || false,
vanity_iterations: vanityIterations,
vanity_duration_ms: vanityDurationMs,
},
});
if (!reservation.ok) return error(res, reservation.status, reservation.code, reservation.msg);
let signature;
try {
// Protected send: the agent keypair pays + signs, the new mint co-signs.
// Priority fee + CU estimate, rebroadcast with blockhash refresh, hard throw
// on an on-chain revert.
({ signature } = await submitProtected({
network: body.network,
connection: conn,
payer: keypair,
instructions,
opts: { extraSigners: [mint] },
}));
} catch (err) {
console.error('[pumpfun/launch] send failed', err);
await releaseSpend(reservation.reservationId);
return serverError(res, 502, 'rpc_error', err);
}
await finalizeSpend(reservation.reservationId, { mint: mintAddr, signature });
// Register the mint in the indexer table so pump-agent-stats cron picks it up.
await sql`
INSERT INTO pump_agent_mints
(agent_id, user_id, network, mint, name, symbol, metadata_uri, agent_authority)
VALUES
(${id}, ${auth.userId}, ${body.network}, ${mintAddr},
${body.name}, ${body.symbol}, ${body.uri}, ${keypair.publicKey.toBase58()})
ON CONFLICT (mint, network) DO NOTHING
`.catch((e) => console.error('[pumpfun/launch] mint index failed', e));
return json(res, 201, {
data: {
mint: mint.publicKey.toBase58(),
signature,
explorer: `https://solscan.io/tx/${signature}${body.network === 'devnet' ? '?cluster=devnet' : ''}`,
pumpfun_url: `https://pump.fun/${mint.publicKey.toBase58()}`,
vanity_prefix: body.vanityPrefix || null,
vanity_suffix: body.vanitySuffix || null,
vanity_iterations: vanityIterations,
vanity_duration_ms: vanityDurationMs,
},
});
}
// ── pay ────────────────────────────────────────────────────────────────────
const payBodySchema = z.object({
action: z.enum([
'create',
'accept',
'withdraw',
'distribute',
'extend_account',
'update_authority',
'update_buyback',
'balances',
]),
tokenMint: z.string().min(32).max(64),
currencyMint: z.string().min(32).max(64).default(NATIVE_SOL_MINT),
amount: z.string().regex(/^\d+$/).optional(),
memo: z.string().regex(/^\d+$/).optional(),
startTime: z.number().int().nonnegative().optional(),
endTime: z.number().int().nonnegative().optional(),
receiverAta: z.string().min(32).max(64).optional(),
userTokenAccount: z.string().min(32).max(64).optional(),
account: z.string().min(32).max(64).optional(),
newAuthority: z.string().min(32).max(64).optional(),
buybackBps: z.number().int().min(0).max(10_000).optional(),
network: z.enum(['mainnet', 'devnet']).default('mainnet'),
});
function payNeed(body, ...fields) {
const missing = fields.filter((f) => body[f] == null || body[f] === '');
if (missing.length)
return { status: 400, code: 'validation_error', msg: `missing: ${missing.join(', ')}` };
return null;
}
function explorerUrl(sig, network) {
return `https://solscan.io/tx/${sig}${network === 'devnet' ? '?cluster=devnet' : ''}`;
}
async function handlePay(req, res, id) {
if (cors(req, res, { methods: 'POST,OPTIONS', credentials: true })) return;
if (!method(req, res, ['POST'])) return;
const auth = await resolveAuth(req);
if (!auth) return error(res, 401, 'unauthorized', 'sign in required');
const rl = await limits.authIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
let body;
try {
body = payBodySchema.parse(await readJson(req));
} catch (e) {
return error(res, 400, 'validation_error', e.errors?.[0]?.message || 'invalid body');
}
const loaded = await loadAgentForSigning(id, auth.userId, {
reason: `pumpfun.pay.${body.action}`,
meta: { tokenMint: body.tokenMint, network: body.network },
});
if (loaded.error) return error(res, loaded.error.status, loaded.error.code, loaded.error.msg);
const { keypair } = loaded;
const conn = solanaConnection(body.network);
const tokenMint = new PublicKey(body.tokenMint);
const currencyMint = new PublicKey(body.currencyMint);
const { PumpAgent } = await import('@three-ws/agent-payments');
const agent = new PumpAgent(tokenMint, body.network, conn);
// ── Read-only ──────────────────────────────────────────────────────────
if (body.action === 'balances') {
try {
const balances = await agent.getBalances(currencyMint);
return json(res, 200, {
data: {
tokenMint: body.tokenMint,
currencyMint: body.currencyMint,
balances: {
paymentVault: {
address: balances.paymentVault.address.toBase58(),
balance: balances.paymentVault.balance.toString(),
},
buybackVault: {
address: balances.buybackVault.address.toBase58(),
balance: balances.buybackVault.balance.toString(),
},
withdrawVault: {
address: balances.withdrawVault.address.toBase58(),
balance: balances.withdrawVault.balance.toString(),
},
},
},
});
} catch (err) {
console.error('[pumpfun/pay] balances failed', err);
return serverError(res, 502, 'rpc_error', err);
}
}
// ── Build instructions per action ──────────────────────────────────────
let instructions;
let extra = {};
try {
switch (body.action) {
case 'create': {
const miss = payNeed(body, 'buybackBps');
if (miss) return error(res, miss.status, miss.code, miss.msg);
const ix = await agent.create({
authority: keypair.publicKey,
mint: tokenMint,
agentAuthority: keypair.publicKey,
buybackBps: body.buybackBps,
});
instructions = [ix];
extra = { buybackBps: body.buybackBps };
break;
}
case 'accept': {
const miss = payNeed(body, 'amount', 'memo', 'startTime', 'endTime');
if (miss) return error(res, miss.status, miss.code, miss.msg);
instructions = await agent.buildAcceptPaymentInstructions({
user: keypair.publicKey,
currencyMint,
amount: body.amount,
memo: body.memo,
startTime: body.startTime,
endTime: body.endTime,
});
extra = {
amount: body.amount,
memo: body.memo,
startTime: body.startTime,
endTime: body.endTime,
};
break;
}
case 'withdraw': {
const miss = payNeed(body, 'receiverAta');
if (miss) return error(res, miss.status, miss.code, miss.msg);
const ix = await agent.withdraw({
authority: keypair.publicKey,
currencyMint,
receiverAta: new PublicKey(body.receiverAta),
});
instructions = [ix];
extra = { receiverAta: body.receiverAta };
break;
}
case 'distribute': {
instructions = await agent.distributePayments({
user: keypair.publicKey,
currencyMint,
});
break;
}
case 'extend_account': {
const miss = payNeed(body, 'account');
if (miss) return error(res, miss.status, miss.code, miss.msg);
const ix = await agent.extendAccount({
account: new PublicKey(body.account),
user: keypair.publicKey,
});
instructions = [ix];
extra = { account: body.account };
break;
}
case 'update_authority': {
const miss = payNeed(body, 'newAuthority');
if (miss) return error(res, miss.status, miss.code, miss.msg);
const ix = await agent.updateAuthority({
authority: keypair.publicKey,
newAuthority: new PublicKey(body.newAuthority),
});
instructions = [ix];
extra = { newAuthority: body.newAuthority };
break;
}
case 'update_buyback': {
const miss = payNeed(body, 'buybackBps');
if (miss) return error(res, miss.status, miss.code, miss.msg);
const ix = await agent.updateBuybackBps({
authority: keypair.publicKey,
buybackBps: body.buybackBps,
});
instructions = [ix];
extra = { buybackBps: body.buybackBps };
break;
}
}
} catch (err) {
console.error(`[pumpfun/pay] ${body.action} build failed`, err);
return error(res, 422, 'build_failed', err.message || 'could not build instruction');
}
let signature;
try {
// Protected send: priority fee + CU estimate, rebroadcast with blockhash
// refresh until it lands, hard throw on an on-chain revert.
({ signature } = await submitProtected({
network: body.network,
connection: conn,
payer: keypair,
instructions,
}));
} catch (err) {
console.error(`[pumpfun/pay] ${body.action} send failed`, err);
return serverError(res, 502, 'rpc_error', err);
}
await sql`
INSERT INTO agent_actions (agent_id, type, payload, source_skill)
VALUES (
${id},
${`pumpfun.pay.${body.action}`},
${JSON.stringify({
tokenMint: body.tokenMint,
currencyMint: body.currencyMint,
signature,
network: body.network,
...extra,
})}::jsonb,
${'pumpfun'}
)
`.catch((e) => console.error('[pumpfun/pay] log failed', e));
return json(res, 200, {
data: {
signature,
action: body.action,
tokenMint: body.tokenMint,
currencyMint: body.currencyMint,
explorer: explorerUrl(signature, body.network),
...extra,
},
});
}
// ── portfolio ──────────────────────────────────────────────────────────────
function lamportsToSol(bnLike) {
if (!bnLike) return 0;
const s = typeof bnLike === 'string' ? bnLike : bnLike.toString();
return Number(s) / 1e9;
}
async function handlePortfolio(req, res, id) {
if (cors(req, res, { methods: 'GET,OPTIONS', credentials: true })) return;
if (!method(req, res, ['GET'])) return;
const auth = await resolveAuth(req);
if (!auth) return error(res, 401, 'unauthorized', 'sign in required');
const rl = await limits.authedReadIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
const url = new URL(req.url, 'http://x');
const network = url.searchParams.get('network') === 'devnet' ? 'devnet' : 'mainnet';
const loaded = await loadAgentForSigning(id, auth.userId);
if (loaded.error) return error(res, loaded.error.status, loaded.error.code, loaded.error.msg);
const { keypair, meta } = loaded;
const rows = await sql`
SELECT type, payload
FROM agent_actions
WHERE agent_id = ${id}
AND type IN ('pumpfun.buy', 'pumpfun.sell', 'pumpfun.launch',
'pumpfun.swap.buy', 'pumpfun.swap.sell')
AND (payload->>'network') = ${network}
`;
// Aggregate per mint
const byMint = new Map();
for (const row of rows) {
const p = row.payload || {};
const mint = p.mint;
if (!mint) continue;
const e = byMint.get(mint) || { mint, sol_in: 0, sol_out: 0 };
if (
row.type === 'pumpfun.buy' ||
row.type === 'pumpfun.launch' ||
row.type === 'pumpfun.swap.buy'
) {
e.sol_in += Number(p.solAmount) || 0;
} else if (row.type === 'pumpfun.sell' || row.type === 'pumpfun.swap.sell') {
e.sol_out += lamportsToSol(p.expectedSolLamports);
}
byMint.set(mint, e);
}
if (byMint.size === 0) {
return json(res, 200, {
data: {
wallet: meta.solana_address,
network,
positions: [],
totals: { sol_in: 0, sol_out: 0, estimated_value_sol: 0, unrealized_pnl_sol: 0 },
},
});
}
const conn = solanaConnection(network);
const [{ PumpSdk, OnlinePumpSdk, getSellSolAmountFromTokenAmount }, ammMod, splToken, BN] =
await Promise.all([
import('@pump-fun/pump-sdk'),
import('@pump-fun/pump-swap-sdk'),
import('@solana/spl-token'),
import('bn.js').then((m) => m.default || m),
]);
const online = new OnlinePumpSdk(conn);
const onlineAmm = new ammMod.OnlinePumpAmmSdk(conn);
let global;
try {
global = await online.fetchGlobal();
} catch (err) {
console.error('[pumpfun/portfolio] fetchGlobal failed', err);
global = null;
}
const positions = await Promise.all(
[...byMint.values()].map(async (pos) => {
const out = {
...pos,
token_balance: '0',
estimated_sol_value: null,
unrealized_pnl_sol: null,
graduated: null,
};
let mintPk;
try {
mintPk = new PublicKey(pos.mint);
} catch {
out.error = 'invalid_mint';
return out;
}
let positionTokenProgram = splToken.TOKEN_PROGRAM_ID;
try {
// A mint is owned by exactly one token program; create_v2 coins are
// Token-2022, legacy coins are SPL. Check both ATAs and use the one
// that exists so v2 positions don't read as zero.
const ataLegacy = splToken.getAssociatedTokenAddressSync(
mintPk,
keypair.publicKey,
true,
splToken.TOKEN_PROGRAM_ID,
);
const ata2022 = splToken.getAssociatedTokenAddressSync(
mintPk,
keypair.publicKey,
true,
splToken.TOKEN_2022_PROGRAM_ID,
);
const [balLegacy, bal2022] = await Promise.all([
conn.getTokenAccountBalance(ataLegacy).catch(() => null),
conn.getTokenAccountBalance(ata2022).catch(() => null),
]);
if (bal2022?.value?.amount != null)
positionTokenProgram = splToken.TOKEN_2022_PROGRAM_ID;
out.token_balance = bal2022?.value?.amount ?? balLegacy?.value?.amount ?? '0';
} catch (err) {
out.error = 'balance_fetch_failed';
}
if (out.token_balance === '0' || !global) {
out.unrealized_pnl_sol = out.sol_out - out.sol_in;
out.estimated_sol_value = 0;
return out;
}
try {
const state = await online.fetchSellState(
mintPk,
keypair.publicKey,
positionTokenProgram,
);
out.graduated = !!state.bondingCurve.complete;
if (!out.graduated) {
const expectedSol = getSellSolAmountFromTokenAmount({
global,
feeConfig: null,
mintSupply: state.bondingCurve.tokenTotalSupply,
bondingCurve: state.bondingCurve,
amount: new BN(out.token_balance),
});
out.estimated_sol_value = lamportsToSol(expectedSol);
out.venue = 'curve';
} else {
// AMM quote: simulate sellBaseInput to get expected SOL out.
try {
const poolKey = ammMod.canonicalPumpPoolPda(mintPk);
const swapState = await onlineAmm.swapSolanaState(
poolKey,
keypair.publicKey,
);
// Fee-aware quote off the live pool. Reserves come from the
// swap state (the vault balances); `virtualQuoteReserves`
// is added to the quote side by the SDK itself, so it is
// passed separately rather than pre-summed.
const result = ammMod.sellBaseInput({
base: new BN(out.token_balance),
slippage: 0,
baseReserve: swapState.poolBaseAmount,
quoteReserve: swapState.poolQuoteAmount,
virtualQuoteReserves: swapState.pool.virtualQuoteReserves,
baseMintAccount: swapState.baseMintAccount,
baseMint: swapState.baseMint,
coinCreator: swapState.pool.coinCreator,
creator: swapState.pool.creator,
feeConfig: swapState.feeConfig,
globalConfig: swapState.globalConfig,
});
const expectedQuote = result?.uiQuote ?? result?.minQuote ?? null;
out.estimated_sol_value =
expectedQuote != null ? lamportsToSol(expectedQuote) : null;
out.venue = 'amm';
out.pool = poolKey.toBase58();
} catch (e) {
console.error('[pumpfun/portfolio] amm quote failed', e);
out.estimated_sol_value = null;
out.error = 'amm_quote_failed';
}
}
if (typeof out.estimated_sol_value === 'number') {
out.unrealized_pnl_sol = out.estimated_sol_value + out.sol_out - out.sol_in;
} else {
out.unrealized_pnl_sol = null;
}
} catch (err) {
out.error = out.error || 'curve_quote_failed';
}
return out;
}),
);