@scalecrx/sdk 0.4.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,527 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ScaleSdkError = exports.ScaleVmm = void 0;
7
+ const anchor_1 = require("@coral-xyz/anchor");
8
+ const web3_js_1 = require("@solana/web3.js");
9
+ const spl_token_1 = require("@solana/spl-token");
10
+ const utils_1 = require("./utils");
11
+ const errors_1 = require("./errors");
12
+ Object.defineProperty(exports, "ScaleSdkError", { enumerable: true, get: function () { return errors_1.ScaleSdkError; } });
13
+ const scale_vmm_json_1 = __importDefault(require("./idl/scale_vmm.json"));
14
+ class ScaleVmm {
15
+ constructor(provider, programId, idlOverride, ammProgramId, hasWallet = true) {
16
+ this.provider = provider;
17
+ this.programId = programId;
18
+ const resolvedIdl = idlOverride ?? scale_vmm_json_1.default;
19
+ this.idl = { ...resolvedIdl, address: programId.toBase58() };
20
+ this.program = new anchor_1.Program(this.idl, provider);
21
+ this.idlErrors = (0, anchor_1.parseIdlErrors)(this.idl);
22
+ this.ammProgramId = ammProgramId;
23
+ this.hasWallet = hasWallet;
24
+ }
25
+ getConfigAddress() {
26
+ return (0, utils_1.getConfigAddress)(this.programId);
27
+ }
28
+ getPairAddress(mintA, mintB) {
29
+ return (0, utils_1.getPairAddress)(this.programId, mintA, mintB);
30
+ }
31
+ getVaultAddress(pair, mint) {
32
+ return (0, utils_1.getVaultAddress)(this.programId, pair, mint);
33
+ }
34
+ getAmmPoolAddress(pair, mintA, mintB) {
35
+ const programId = this.resolveAmmProgramId();
36
+ return (0, utils_1.getPoolAddress)(programId, pair, mintA, mintB);
37
+ }
38
+ getAmmVaultAddress(pool, mint) {
39
+ const programId = this.resolveAmmProgramId();
40
+ return (0, utils_1.getVaultAddress)(programId, pool, mint);
41
+ }
42
+ async getPlatformConfig() {
43
+ const config = this.getConfigAddress();
44
+ try {
45
+ return await this.program.account.platformConfig.fetch(config);
46
+ }
47
+ catch (err) {
48
+ return null;
49
+ }
50
+ }
51
+ async getPlatformConfigView() {
52
+ const config = this.getConfigAddress();
53
+ try {
54
+ return await this.program.methods
55
+ .getPlatformConfig()
56
+ .accounts({ config })
57
+ .view();
58
+ }
59
+ catch (err) {
60
+ return null;
61
+ }
62
+ }
63
+ async getPlatformBaseToken() {
64
+ const config = await this.getPlatformConfig();
65
+ return config ? config.baseToken : null;
66
+ }
67
+ async getGraduationThreshold() {
68
+ const config = await this.getPlatformConfig();
69
+ return config ? config.graduationThreshold : null;
70
+ }
71
+ async setGraduationThreshold(threshold) {
72
+ try {
73
+ const instruction = await this.setGraduationThresholdInstruction(threshold);
74
+ const tx = new web3_js_1.Transaction().add(instruction);
75
+ return await this.sendTransaction(tx, [], "confirmed");
76
+ }
77
+ catch (err) {
78
+ throw (0, errors_1.toSdkError)("setGraduationThreshold failed", err, this.idlErrors);
79
+ }
80
+ }
81
+ async setGraduationThresholdInstruction(threshold) {
82
+ const config = this.getConfigAddress();
83
+ try {
84
+ return await this.program.methods
85
+ .setGraduationThreshold((0, utils_1.toBN)(threshold))
86
+ .accounts({
87
+ authority: this.provider.wallet.publicKey,
88
+ config,
89
+ })
90
+ .instruction();
91
+ }
92
+ catch (err) {
93
+ throw (0, errors_1.toSdkError)("setGraduationThresholdInstruction failed", err, this.idlErrors);
94
+ }
95
+ }
96
+ async getFeeBeneficiaries(pairInput) {
97
+ const pair = await this.resolvePair(pairInput);
98
+ return pair.data.feeBeneficiaries.slice(0, pair.data.feeBeneficiaryCount);
99
+ }
100
+ async getFeeShare(pairInput, wallet) {
101
+ const beneficiaries = await this.getFeeBeneficiaries(pairInput);
102
+ const entry = beneficiaries.find((beneficiary) => beneficiary.wallet.equals(wallet));
103
+ return entry ? entry.shareBps : 0;
104
+ }
105
+ async getTotalCreatorFeeBps(pairInput) {
106
+ const beneficiaries = await this.getFeeBeneficiaries(pairInput);
107
+ return beneficiaries.reduce((acc, beneficiary) => acc + beneficiary.shareBps, 0);
108
+ }
109
+ async getPair(pairOrAddress) {
110
+ const resolved = await this.resolvePair(pairOrAddress);
111
+ return resolved;
112
+ }
113
+ async getPairByAddress(pair) {
114
+ try {
115
+ const data = (await this.program.account.pairState.fetch(pair));
116
+ return {
117
+ address: pair,
118
+ mintA: data.mintA,
119
+ mintB: data.mintB,
120
+ data,
121
+ };
122
+ }
123
+ catch (err) {
124
+ throw (0, errors_1.toSdkError)("getPairByAddress failed", err, this.idlErrors);
125
+ }
126
+ }
127
+ async getPairByMints(mintA, mintB) {
128
+ const pair = this.getPairAddress(mintA, mintB);
129
+ return this.getPairByAddress(pair);
130
+ }
131
+ async createPair(params, mintA, mintB, options = {}) {
132
+ try {
133
+ const { instructions, pair, vaultA, vaultB, signers } = await this.createPairInstructions(params, mintA, mintB, options);
134
+ const tx = new web3_js_1.Transaction();
135
+ instructions.forEach((instruction) => tx.add(instruction));
136
+ const signature = await this.sendTransaction(tx, signers ?? [], "confirmed");
137
+ return { pair, vaultA, vaultB, signature };
138
+ }
139
+ catch (err) {
140
+ throw (0, errors_1.toSdkError)("createPair failed", err, this.idlErrors);
141
+ }
142
+ }
143
+ async createPairInstructions(params, mintA, mintB, options = {}) {
144
+ const payer = options.payer ?? this.provider.wallet.publicKey;
145
+ const tokenWalletAuthority = options.tokenWalletAuthority ?? this.provider.wallet.publicKey;
146
+ try {
147
+ const tokenProgramA = await (0, utils_1.getTokenProgramForMint)(this.provider.connection, mintA);
148
+ const tokenProgramB = await (0, utils_1.getTokenProgramForMint)(this.provider.connection, mintB);
149
+ const pair = this.getPairAddress(mintA, mintB);
150
+ const vaultA = this.getVaultAddress(pair, mintA);
151
+ const vaultB = this.getVaultAddress(pair, mintB);
152
+ const config = this.getConfigAddress();
153
+ let tokenWalletB = options.tokenWalletB;
154
+ const instructions = [];
155
+ if (!tokenWalletB) {
156
+ const ataResult = await (0, utils_1.maybeCreateAtaInstruction)(this.provider.connection, payer, tokenWalletAuthority, mintB, tokenProgramB);
157
+ tokenWalletB = ataResult.ata;
158
+ if (ataResult.instruction) {
159
+ instructions.push(ataResult.instruction);
160
+ }
161
+ }
162
+ const createParams = {
163
+ shift: (0, utils_1.toBN)(params.shift),
164
+ initialTokenBReserves: (0, utils_1.toBN)(params.initialTokenBReserves),
165
+ curve: params.curve,
166
+ feeBeneficiaries: params.feeBeneficiaries ?? [],
167
+ };
168
+ const ix = await this.program.methods
169
+ .create(createParams)
170
+ .accounts({
171
+ payer,
172
+ tokenWalletAuthority,
173
+ mintA,
174
+ mintB,
175
+ tokenWalletB,
176
+ pair,
177
+ vaultA,
178
+ vaultB,
179
+ tokenProgramA,
180
+ tokenProgramB,
181
+ systemProgram: web3_js_1.SystemProgram.programId,
182
+ config,
183
+ })
184
+ .instruction();
185
+ instructions.push(ix);
186
+ return { instructions, pair, vaultA, vaultB, signers: options.signers };
187
+ }
188
+ catch (err) {
189
+ throw (0, errors_1.toSdkError)("createPairInstructions failed", err, this.idlErrors);
190
+ }
191
+ }
192
+ async createWithDevBuyInstructions(params, mintA, mintB, buyParams, options = {}) {
193
+ try {
194
+ const { instructions: createInstructions, pair, vaultA, vaultB, signers } = await this.createPairInstructions(params, mintA, mintB, options);
195
+ const swapParams = { amount: (0, utils_1.toBN)(buyParams.amount), limit: (0, utils_1.toBN)(buyParams.limit) };
196
+ const user = this.provider.wallet.publicKey;
197
+ const autoCreateAta = options.autoCreateAta ?? true;
198
+ const wrapSol = options.wrapSol ?? true;
199
+ const unwrapSol = options.unwrapSol ?? false;
200
+ const tokenProgramA = await (0, utils_1.getTokenProgramForMint)(this.provider.connection, mintA);
201
+ const tokenProgramB = await (0, utils_1.getTokenProgramForMint)(this.provider.connection, mintB);
202
+ const preInstructions = [];
203
+ const postInstructions = [];
204
+ let userTaA = options.userTokenAccountA;
205
+ let userTaB = options.userTokenAccountB;
206
+ let createdAtaA = false;
207
+ if (!userTaA) {
208
+ const ataResult = await (0, utils_1.maybeCreateAtaInstruction)(this.provider.connection, user, user, mintA, tokenProgramA);
209
+ userTaA = ataResult.ata;
210
+ if (ataResult.instruction && !autoCreateAta) {
211
+ throw new Error(`Missing ATA for mintA: ${mintA.toBase58()}`);
212
+ }
213
+ if (ataResult.instruction) {
214
+ createdAtaA = true;
215
+ preInstructions.push(ataResult.instruction);
216
+ }
217
+ }
218
+ if (!userTaB) {
219
+ const ataResult = await (0, utils_1.maybeCreateAtaInstruction)(this.provider.connection, user, user, mintB, tokenProgramB, true);
220
+ userTaB = ataResult.ata;
221
+ if (ataResult.instruction && !autoCreateAta) {
222
+ throw new Error(`Missing ATA for mintB: ${mintB.toBase58()}`);
223
+ }
224
+ if (ataResult.instruction) {
225
+ preInstructions.push(ataResult.instruction);
226
+ }
227
+ }
228
+ const isNativeA = mintA.equals(spl_token_1.NATIVE_MINT);
229
+ if (isNativeA && wrapSol) {
230
+ preInstructions.push((0, utils_1.transferSolInstruction)(user, userTaA, swapParams.amount));
231
+ preInstructions.push((0, spl_token_1.createSyncNativeInstruction)(userTaA));
232
+ }
233
+ if (isNativeA && unwrapSol && createdAtaA) {
234
+ postInstructions.push((0, spl_token_1.createCloseAccountInstruction)(userTaA, user, user));
235
+ }
236
+ const config = this.getConfigAddress();
237
+ const configState = (await this.program.account.platformConfig.fetch(config));
238
+ const feeBeneficiary = configState.feeBeneficiary;
239
+ let platformFeeTaA = options.platformFeeTokenAccount;
240
+ if (!platformFeeTaA) {
241
+ const ataResult = await (0, utils_1.maybeCreateAtaInstruction)(this.provider.connection, user, feeBeneficiary, mintA, tokenProgramA, true);
242
+ platformFeeTaA = ataResult.ata;
243
+ if (ataResult.instruction && !autoCreateAta) {
244
+ throw new Error(`Missing platform fee ATA for mintA: ${mintA.toBase58()}`);
245
+ }
246
+ if (ataResult.instruction) {
247
+ preInstructions.push(ataResult.instruction);
248
+ }
249
+ }
250
+ const beneficiaries = params.feeBeneficiaries ?? [];
251
+ if (options.beneficiaryTokenAccounts &&
252
+ options.beneficiaryTokenAccounts.length !== beneficiaries.length) {
253
+ throw new Error("beneficiaryTokenAccounts length mismatch");
254
+ }
255
+ const remainingAccounts = options.beneficiaryTokenAccounts ??
256
+ (await Promise.all(beneficiaries.map(async (beneficiary) => {
257
+ const ataResult = await (0, utils_1.maybeCreateAtaInstruction)(this.provider.connection, user, beneficiary.wallet, mintA, tokenProgramA, true);
258
+ if (ataResult.instruction && !autoCreateAta) {
259
+ throw new Error(`Missing beneficiary ATA for mintA: ${mintA.toBase58()}`);
260
+ }
261
+ if (ataResult.instruction) {
262
+ preInstructions.push(ataResult.instruction);
263
+ }
264
+ return ataResult.ata;
265
+ })));
266
+ const ammProgramId = options.ammProgramId ?? this.ammProgramId;
267
+ if (!ammProgramId) {
268
+ throw new Error("ammProgramId is required for VMM swaps");
269
+ }
270
+ const ammPool = options.ammPool ?? (0, utils_1.getPoolAddress)(ammProgramId, pair, mintA, mintB);
271
+ const ammVaultA = options.ammVaultA ?? (0, utils_1.getVaultAddress)(ammProgramId, ammPool, mintA);
272
+ const ammVaultB = options.ammVaultB ?? (0, utils_1.getVaultAddress)(ammProgramId, ammPool, mintB);
273
+ const ammConfig = options.ammConfig ?? (0, utils_1.getConfigAddress)(ammProgramId);
274
+ const ix = await this.program.methods.buy(swapParams)
275
+ .accounts({
276
+ pair,
277
+ user,
278
+ mintA,
279
+ mintB,
280
+ userTaA,
281
+ userTaB,
282
+ vaultA,
283
+ vaultB,
284
+ platformFeeTaA,
285
+ tokenProgramA,
286
+ tokenProgramB,
287
+ systemProgram: web3_js_1.SystemProgram.programId,
288
+ config,
289
+ ammProgram: ammProgramId,
290
+ ammPool,
291
+ ammVaultA,
292
+ ammVaultB,
293
+ ammConfig,
294
+ })
295
+ .remainingAccounts(remainingAccounts.map((pubkey) => ({
296
+ pubkey,
297
+ isWritable: true,
298
+ isSigner: false,
299
+ })))
300
+ .instruction();
301
+ return {
302
+ instructions: [...createInstructions, ...preInstructions, ix, ...postInstructions],
303
+ signers,
304
+ pair,
305
+ vaultA,
306
+ vaultB,
307
+ userTokenAccountA: userTaA,
308
+ userTokenAccountB: userTaB,
309
+ };
310
+ }
311
+ catch (err) {
312
+ console.error(err);
313
+ throw (0, errors_1.toSdkError)("createWithDevBuyInstructions failed", err, this.idlErrors);
314
+ }
315
+ }
316
+ async buy(pairInput, params, options = {}) {
317
+ try {
318
+ const { instructions, signers } = await this.buyInstructions(pairInput, params, options);
319
+ const tx = new web3_js_1.Transaction();
320
+ instructions.forEach((instruction) => tx.add(instruction));
321
+ return await this.sendTransaction(tx, signers ?? [], "confirmed");
322
+ }
323
+ catch (err) {
324
+ throw (0, errors_1.toSdkError)("buy failed", err, this.idlErrors);
325
+ }
326
+ }
327
+ async sell(pairInput, params, options = {}) {
328
+ try {
329
+ const { instructions, signers } = await this.sellInstructions(pairInput, params, options);
330
+ const tx = new web3_js_1.Transaction();
331
+ instructions.forEach((instruction) => tx.add(instruction));
332
+ return await this.sendTransaction(tx, signers ?? [], "confirmed");
333
+ }
334
+ catch (err) {
335
+ throw (0, errors_1.toSdkError)("sell failed", err, this.idlErrors);
336
+ }
337
+ }
338
+ async buyInstructions(pairInput, params, options = {}) {
339
+ try {
340
+ const pair = await this.resolvePair(pairInput);
341
+ return await this.swapInstructions("buy", pair, params, options);
342
+ }
343
+ catch (err) {
344
+ throw (0, errors_1.toSdkError)("buyInstructions failed", err, this.idlErrors);
345
+ }
346
+ }
347
+ async sellInstructions(pairInput, params, options = {}) {
348
+ try {
349
+ const pair = await this.resolvePair(pairInput);
350
+ return await this.swapInstructions("sell", pair, params, options);
351
+ }
352
+ catch (err) {
353
+ throw (0, errors_1.toSdkError)("sellInstructions failed", err, this.idlErrors);
354
+ }
355
+ }
356
+ async estimateBuy(pairInput, params) {
357
+ return this.estimate("quoteBuy", pairInput, params);
358
+ }
359
+ async estimateSell(pairInput, params) {
360
+ return this.estimate("quoteSell", pairInput, params);
361
+ }
362
+ async estimate(method, pairInput, params) {
363
+ try {
364
+ const pair = await this.resolvePair(pairInput);
365
+ const swapParams = { amount: (0, utils_1.toBN)(params.amount), limit: (0, utils_1.toBN)(params.limit) };
366
+ const config = this.getConfigAddress();
367
+ const result = await this.program.methods[method](swapParams)
368
+ .accounts({
369
+ pair: pair.address,
370
+ mintA: pair.mintA,
371
+ mintB: pair.mintB,
372
+ config,
373
+ })
374
+ .view();
375
+ return {
376
+ newReservesA: result.newReservesA,
377
+ newReservesB: result.newReservesB,
378
+ amountA: result.amountA,
379
+ amountB: result.amountB,
380
+ feeA: result.feeA,
381
+ };
382
+ }
383
+ catch (err) {
384
+ throw (0, errors_1.toSdkError)(`${method} failed`, err, this.idlErrors);
385
+ }
386
+ }
387
+ async resolvePair(pairInput) {
388
+ if (pairInput instanceof web3_js_1.PublicKey) {
389
+ return this.getPairByAddress(pairInput);
390
+ }
391
+ return this.getPairByAddress(pairInput.address);
392
+ }
393
+ async swapInstructions(action, pair, params, options) {
394
+ const swapParams = { amount: (0, utils_1.toBN)(params.amount), limit: (0, utils_1.toBN)(params.limit) };
395
+ const user = this.provider.wallet.publicKey;
396
+ const autoCreateAta = options.autoCreateAta ?? true;
397
+ const wrapSol = options.wrapSol ?? action === "buy";
398
+ const unwrapSol = options.unwrapSol ?? action === "sell";
399
+ const tokenProgramA = await (0, utils_1.getTokenProgramForMint)(this.provider.connection, pair.mintA);
400
+ const tokenProgramB = await (0, utils_1.getTokenProgramForMint)(this.provider.connection, pair.mintB);
401
+ const preInstructions = [];
402
+ const postInstructions = [];
403
+ let userTaA = options.userTokenAccountA;
404
+ let userTaB = options.userTokenAccountB;
405
+ let createdAtaA = false;
406
+ if (!userTaA) {
407
+ const ataResult = await (0, utils_1.maybeCreateAtaInstruction)(this.provider.connection, user, user, pair.mintA, tokenProgramA);
408
+ userTaA = ataResult.ata;
409
+ if (ataResult.instruction && !autoCreateAta) {
410
+ throw new Error(`Missing ATA for mintA: ${pair.mintA.toBase58()}`);
411
+ }
412
+ if (ataResult.instruction) {
413
+ createdAtaA = true;
414
+ preInstructions.push(ataResult.instruction);
415
+ }
416
+ }
417
+ if (!userTaB) {
418
+ const ataResult = await (0, utils_1.maybeCreateAtaInstruction)(this.provider.connection, user, user, pair.mintB, tokenProgramB);
419
+ userTaB = ataResult.ata;
420
+ if (ataResult.instruction && !autoCreateAta) {
421
+ throw new Error(`Missing ATA for mintB: ${pair.mintB.toBase58()}`);
422
+ }
423
+ if (ataResult.instruction) {
424
+ preInstructions.push(ataResult.instruction);
425
+ }
426
+ }
427
+ const isNativeA = pair.mintA.equals(spl_token_1.NATIVE_MINT);
428
+ if (isNativeA && wrapSol) {
429
+ preInstructions.push((0, utils_1.transferSolInstruction)(user, userTaA, swapParams.amount));
430
+ preInstructions.push((0, spl_token_1.createSyncNativeInstruction)(userTaA));
431
+ }
432
+ if (isNativeA && unwrapSol && createdAtaA) {
433
+ postInstructions.push((0, spl_token_1.createCloseAccountInstruction)(userTaA, user, user));
434
+ }
435
+ const vaultA = this.getVaultAddress(pair.address, pair.mintA);
436
+ const vaultB = this.getVaultAddress(pair.address, pair.mintB);
437
+ const config = this.getConfigAddress();
438
+ const configState = (await this.program.account.platformConfig.fetch(config));
439
+ const feeBeneficiary = configState.feeBeneficiary;
440
+ let platformFeeTaA = options.platformFeeTokenAccount;
441
+ if (!platformFeeTaA) {
442
+ const ataResult = await (0, utils_1.maybeCreateAtaInstruction)(this.provider.connection, user, feeBeneficiary, pair.mintA, tokenProgramA, true);
443
+ platformFeeTaA = ataResult.ata;
444
+ if (ataResult.instruction && !autoCreateAta) {
445
+ throw new Error(`Missing platform fee ATA for mintA: ${pair.mintA.toBase58()}`);
446
+ }
447
+ if (ataResult.instruction) {
448
+ preInstructions.push(ataResult.instruction);
449
+ }
450
+ }
451
+ const beneficiaries = pair.data.feeBeneficiaries.slice(0, pair.data.feeBeneficiaryCount);
452
+ if (options.beneficiaryTokenAccounts &&
453
+ options.beneficiaryTokenAccounts.length !== beneficiaries.length) {
454
+ throw new Error("beneficiaryTokenAccounts length mismatch");
455
+ }
456
+ const remainingAccounts = options.beneficiaryTokenAccounts ??
457
+ (await Promise.all(beneficiaries.map(async (beneficiary) => {
458
+ const ataResult = await (0, utils_1.maybeCreateAtaInstruction)(this.provider.connection, user, beneficiary.wallet, pair.mintA, tokenProgramA, true);
459
+ if (ataResult.instruction && !autoCreateAta) {
460
+ throw new Error(`Missing beneficiary ATA for mintA: ${pair.mintA.toBase58()}`);
461
+ }
462
+ if (ataResult.instruction) {
463
+ preInstructions.push(ataResult.instruction);
464
+ }
465
+ return ataResult.ata;
466
+ })));
467
+ const ammProgramId = options.ammProgramId ?? this.ammProgramId;
468
+ if (!ammProgramId) {
469
+ throw new Error("ammProgramId is required for VMM swaps");
470
+ }
471
+ const ammPool = options.ammPool ?? (0, utils_1.getPoolAddress)(ammProgramId, pair.address, pair.mintA, pair.mintB);
472
+ const ammVaultA = options.ammVaultA ?? (0, utils_1.getVaultAddress)(ammProgramId, ammPool, pair.mintA);
473
+ const ammVaultB = options.ammVaultB ?? (0, utils_1.getVaultAddress)(ammProgramId, ammPool, pair.mintB);
474
+ const ammConfig = options.ammConfig ?? (0, utils_1.getConfigAddress)(ammProgramId);
475
+ const ix = await this.program.methods[action](swapParams)
476
+ .accounts({
477
+ pair: pair.address,
478
+ user,
479
+ mintA: pair.mintA,
480
+ mintB: pair.mintB,
481
+ userTaA,
482
+ userTaB,
483
+ vaultA,
484
+ vaultB,
485
+ platformFeeTaA,
486
+ tokenProgramA,
487
+ tokenProgramB,
488
+ systemProgram: web3_js_1.SystemProgram.programId,
489
+ config,
490
+ ammProgram: ammProgramId,
491
+ ammPool,
492
+ ammVaultA,
493
+ ammVaultB,
494
+ ammConfig,
495
+ })
496
+ .remainingAccounts(remainingAccounts.map((pubkey) => ({
497
+ pubkey,
498
+ isWritable: true,
499
+ isSigner: false,
500
+ })))
501
+ .instruction();
502
+ const instructions = [...preInstructions, ix, ...postInstructions];
503
+ return {
504
+ instructions,
505
+ userTokenAccountA: userTaA,
506
+ userTokenAccountB: userTaB,
507
+ };
508
+ }
509
+ resolveAmmProgramId() {
510
+ if (!this.ammProgramId) {
511
+ throw new Error("ammProgramId is required for AMM derivations");
512
+ }
513
+ return this.ammProgramId;
514
+ }
515
+ async sendTransaction(tx, signers = [], commitment) {
516
+ try {
517
+ if (!this.hasWallet) {
518
+ throw new errors_1.ScaleSdkError("No wallet provided. Direct execution requires an Anchor wallet.", "transaction failed", null);
519
+ }
520
+ return await this.provider.sendAndConfirm(tx, signers, { commitment });
521
+ }
522
+ catch (err) {
523
+ throw (0, errors_1.toSdkError)("transaction failed", err, this.idlErrors);
524
+ }
525
+ }
526
+ }
527
+ exports.ScaleVmm = ScaleVmm;
@@ -0,0 +1,112 @@
1
+ import { BN } from "@coral-xyz/anchor";
2
+ import { PublicKey, Signer, TransactionInstruction } from "@solana/web3.js";
3
+ export type FeeBeneficiaryInput = {
4
+ wallet: PublicKey;
5
+ shareBps: number;
6
+ };
7
+ export type CurveTypeInput = {
8
+ constantProduct: {};
9
+ } | {
10
+ exponential: {};
11
+ };
12
+ export type CreatePoolParamsInput = {
13
+ shift: BN | number;
14
+ initialTokenBReserves: BN | number;
15
+ curve: CurveTypeInput;
16
+ feeBeneficiaries: FeeBeneficiaryInput[];
17
+ };
18
+ export type CreatePairParamsInput = {
19
+ shift: BN | number;
20
+ initialTokenBReserves: BN | number;
21
+ curve: CurveTypeInput;
22
+ feeBeneficiaries: FeeBeneficiaryInput[];
23
+ };
24
+ export type SwapParamsInput = {
25
+ amount: BN | number;
26
+ limit: BN | number;
27
+ };
28
+ export type PoolAddress = PublicKey;
29
+ export type PoolRef = {
30
+ address: PublicKey;
31
+ owner: PublicKey;
32
+ mintA: PublicKey;
33
+ mintB: PublicKey;
34
+ };
35
+ export type PairAddress = PublicKey;
36
+ export type PairRef = {
37
+ address: PublicKey;
38
+ mintA: PublicKey;
39
+ mintB: PublicKey;
40
+ };
41
+ export type LaunchParams = {
42
+ baseToken: PublicKey;
43
+ programId?: PublicKey;
44
+ };
45
+ export type CreatePoolOptions = {
46
+ payer?: PublicKey;
47
+ owner?: PublicKey;
48
+ tokenWalletB?: PublicKey;
49
+ tokenWalletAuthority?: PublicKey;
50
+ signers?: Signer[];
51
+ };
52
+ export type CreatePairOptions = {
53
+ payer?: PublicKey;
54
+ tokenWalletB?: PublicKey;
55
+ tokenWalletAuthority?: PublicKey;
56
+ signers?: Signer[];
57
+ };
58
+ export type SwapOptions = {
59
+ userTokenAccountA?: PublicKey;
60
+ userTokenAccountB?: PublicKey;
61
+ platformFeeTokenAccount?: PublicKey;
62
+ beneficiaryTokenAccounts?: PublicKey[];
63
+ wrapSol?: boolean;
64
+ unwrapSol?: boolean;
65
+ autoCreateAta?: boolean;
66
+ };
67
+ export type VmmSwapOptions = SwapOptions & {
68
+ ammProgramId?: PublicKey;
69
+ ammPool?: PublicKey;
70
+ ammVaultA?: PublicKey;
71
+ ammVaultB?: PublicKey;
72
+ ammConfig?: PublicKey;
73
+ };
74
+ export type InstructionBundle = {
75
+ instructions: TransactionInstruction[];
76
+ signers?: Signer[];
77
+ };
78
+ export type CreatePoolInstructionResult = InstructionBundle & {
79
+ pool: PublicKey;
80
+ vaultA: PublicKey;
81
+ vaultB: PublicKey;
82
+ };
83
+ export type CreatePairInstructionResult = InstructionBundle & {
84
+ pair: PublicKey;
85
+ vaultA: PublicKey;
86
+ vaultB: PublicKey;
87
+ };
88
+ export type SwapInstructionResult = InstructionBundle & {
89
+ userTokenAccountA: PublicKey;
90
+ userTokenAccountB: PublicKey;
91
+ };
92
+ export type CreatePoolWithDevBuyInstructionResult = InstructionBundle & {
93
+ pool: PublicKey;
94
+ vaultA: PublicKey;
95
+ vaultB: PublicKey;
96
+ userTokenAccountA: PublicKey;
97
+ userTokenAccountB: PublicKey;
98
+ };
99
+ export type CreatePairWithDevBuyInstructionResult = InstructionBundle & {
100
+ pair: PublicKey;
101
+ vaultA: PublicKey;
102
+ vaultB: PublicKey;
103
+ userTokenAccountA: PublicKey;
104
+ userTokenAccountB: PublicKey;
105
+ };
106
+ export type EstimateResult = {
107
+ newReservesA: BN;
108
+ newReservesB: BN;
109
+ amountA: BN;
110
+ amountB: BN;
111
+ feeA: BN;
112
+ };
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,20 @@
1
+ import { BN } from "@coral-xyz/anchor";
2
+ import { PublicKey, TransactionInstruction } from "@solana/web3.js";
3
+ export declare const BPF_LOADER_UPGRADEABLE_PROGRAM_ID: PublicKey;
4
+ export declare const getProgramDataAddress: (programId: PublicKey) => PublicKey;
5
+ export declare const getPoolAddress: (programId: PublicKey, owner: PublicKey, mintA: PublicKey, mintB: PublicKey) => PublicKey;
6
+ export declare const getVaultAddress: (programId: PublicKey, pool: PublicKey, mint: PublicKey) => PublicKey;
7
+ export declare const getPairAddress: (programId: PublicKey, mintA: PublicKey, mintB: PublicKey) => PublicKey;
8
+ export declare const getConfigAddress: (programId: PublicKey) => PublicKey;
9
+ export declare const toBN: (value: BN | number) => any;
10
+ export declare const getTokenProgramForMint: (connection: {
11
+ getAccountInfo: (pubkey: PublicKey) => Promise<any>;
12
+ }, mint: PublicKey) => Promise<PublicKey>;
13
+ export declare const getAta: (mint: PublicKey, owner: PublicKey, tokenProgramId: PublicKey, allowOffCurve?: boolean) => PublicKey;
14
+ export declare const maybeCreateAtaInstruction: (connection: {
15
+ getAccountInfo: (pubkey: PublicKey) => Promise<any>;
16
+ }, payer: PublicKey, owner: PublicKey, mint: PublicKey, tokenProgramId: PublicKey, allowOffCurve?: boolean) => Promise<{
17
+ ata: PublicKey;
18
+ instruction?: TransactionInstruction;
19
+ }>;
20
+ export declare const transferSolInstruction: (from: PublicKey, to: PublicKey, lamports: number | BN) => TransactionInstruction;