@liberfi.io/react-launchpad 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,797 @@
1
+ 'use strict';
2
+
3
+ var react = require('react');
4
+ var BigNumber3 = require('bignumber.js');
5
+
6
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
7
+
8
+ var BigNumber3__default = /*#__PURE__*/_interopDefault(BigNumber3);
9
+
10
+ // src/index.ts
11
+
12
+ // src/curve/BaseCurve.ts
13
+ function unimplemented(args) {
14
+ throw new Error("not implemented");
15
+ }
16
+ var BaseCurve = class {
17
+ static getPoolInitPriceByPool(args) {
18
+ return unimplemented();
19
+ }
20
+ static getPoolInitPriceByInit(args) {
21
+ return unimplemented();
22
+ }
23
+ static getPoolPrice(args) {
24
+ return unimplemented();
25
+ }
26
+ static getPoolEndPrice(args) {
27
+ return unimplemented();
28
+ }
29
+ static getPoolEndPriceReal(args) {
30
+ return unimplemented();
31
+ }
32
+ static getInitParam(args) {
33
+ return unimplemented();
34
+ }
35
+ static buyExactIn(args) {
36
+ return unimplemented();
37
+ }
38
+ static buyExactOut(args) {
39
+ return unimplemented();
40
+ }
41
+ static sellExactIn(args) {
42
+ return unimplemented();
43
+ }
44
+ static sellExactOut(args) {
45
+ return unimplemented();
46
+ }
47
+ };
48
+ var ConstantProductCurve = class extends BaseCurve {
49
+ static getPoolInitPriceByPool({
50
+ poolInfo,
51
+ decimalA,
52
+ decimalB
53
+ }) {
54
+ return new BigNumber3.BigNumber(poolInfo.virtualB).div(poolInfo.virtualA).times(10 ** (decimalA - decimalB));
55
+ }
56
+ static getPoolInitPriceByInit({
57
+ a,
58
+ b,
59
+ decimalA,
60
+ decimalB
61
+ }) {
62
+ return new BigNumber3.BigNumber(b).div(a).times(10 ** (decimalA - decimalB));
63
+ }
64
+ static getPoolPrice({
65
+ poolInfo,
66
+ decimalA,
67
+ decimalB
68
+ }) {
69
+ return new BigNumber3.BigNumber(new BigNumber3.BigNumber(poolInfo.virtualB).plus(poolInfo.realB)).div(new BigNumber3.BigNumber(poolInfo.virtualA).minus(poolInfo.realA)).times(10 ** (decimalA - decimalB));
70
+ }
71
+ static getPoolEndPrice({
72
+ supply,
73
+ totalSell,
74
+ totalLockedAmount,
75
+ totalFundRaising,
76
+ migrateFee,
77
+ decimalA,
78
+ decimalB
79
+ }) {
80
+ return new BigNumber3.BigNumber(new BigNumber3.BigNumber(totalFundRaising).minus(migrateFee)).div(new BigNumber3.BigNumber(supply).minus(totalSell).minus(totalLockedAmount)).times(10 ** (decimalA - decimalB));
81
+ }
82
+ static getPoolEndPriceReal({
83
+ poolInfo,
84
+ decimalA,
85
+ decimalB
86
+ }) {
87
+ const allSellToken = new BigNumber3.BigNumber(poolInfo.totalSellA).minus(poolInfo.realA);
88
+ const buyAllTokenUseB = new BigNumber3.BigNumber(poolInfo.totalFundRaisingB).minus(poolInfo.realB);
89
+ return new BigNumber3.BigNumber(
90
+ new BigNumber3.BigNumber(poolInfo.virtualB).plus(poolInfo.realB).plus(buyAllTokenUseB)
91
+ ).div(new BigNumber3.BigNumber(poolInfo.virtualA).minus(new BigNumber3.BigNumber(poolInfo.realA).plus(allSellToken))).times(10 ** (decimalA - decimalB));
92
+ }
93
+ static getInitParam({
94
+ supply,
95
+ totalFundRaising,
96
+ totalSell,
97
+ totalLockedAmount,
98
+ migrateFee
99
+ }) {
100
+ if (new BigNumber3.BigNumber(supply).lte(totalSell)) throw Error("supply need gt total sell");
101
+ const supplyMinusSellLocked = new BigNumber3.BigNumber(supply).minus(totalSell).minus(totalLockedAmount);
102
+ if (supplyMinusSellLocked.lte(new BigNumber3.BigNumber(0))) throw Error("supplyMinusSellLocked <= 0");
103
+ const tfMinusMf = new BigNumber3.BigNumber(totalFundRaising).minus(migrateFee);
104
+ if (tfMinusMf.lte(0)) throw Error("tfMinusMf <= 0");
105
+ const numerator = tfMinusMf.times(totalSell).times(totalSell).div(supplyMinusSellLocked);
106
+ const denominator = tfMinusMf.times(totalSell).div(supplyMinusSellLocked).minus(totalFundRaising);
107
+ if (denominator.lt(0)) throw Error("supply/totalSell/totalLockedAmount diff too high");
108
+ const x0 = numerator.div(denominator);
109
+ const y0 = new BigNumber3.BigNumber(totalFundRaising).times(totalFundRaising).div(denominator);
110
+ if (x0.lt(0) || y0.lt(0)) throw Error("invalid input 0");
111
+ return {
112
+ a: x0,
113
+ b: y0,
114
+ c: totalSell
115
+ };
116
+ }
117
+ static buyExactIn({
118
+ poolInfo,
119
+ amount
120
+ }) {
121
+ return this.getAmountOut({
122
+ amountIn: amount,
123
+ inputReserve: new BigNumber3.BigNumber(poolInfo.virtualB).plus(poolInfo.realB),
124
+ outputReserve: new BigNumber3.BigNumber(poolInfo.virtualA).minus(poolInfo.realA)
125
+ });
126
+ }
127
+ static buyExactOut({
128
+ poolInfo,
129
+ amount
130
+ }) {
131
+ return this.getAmountIn({
132
+ amountOut: amount,
133
+ inputReserve: new BigNumber3.BigNumber(poolInfo.virtualB).plus(poolInfo.realB),
134
+ outputReserve: new BigNumber3.BigNumber(poolInfo.virtualA).minus(poolInfo.realA)
135
+ });
136
+ }
137
+ static sellExactIn({
138
+ poolInfo,
139
+ amount
140
+ }) {
141
+ return this.getAmountOut({
142
+ amountIn: amount,
143
+ inputReserve: new BigNumber3.BigNumber(poolInfo.virtualA).minus(poolInfo.realA),
144
+ outputReserve: new BigNumber3.BigNumber(poolInfo.virtualB).plus(poolInfo.realB)
145
+ });
146
+ }
147
+ static sellExactOut({
148
+ poolInfo,
149
+ amount
150
+ }) {
151
+ return this.getAmountIn({
152
+ amountOut: amount,
153
+ inputReserve: new BigNumber3.BigNumber(poolInfo.virtualA).minus(poolInfo.realA),
154
+ outputReserve: new BigNumber3.BigNumber(poolInfo.virtualB).plus(poolInfo.realB)
155
+ });
156
+ }
157
+ static getAmountOut({
158
+ amountIn,
159
+ inputReserve,
160
+ outputReserve
161
+ }) {
162
+ const numerator = new BigNumber3.BigNumber(amountIn).times(outputReserve);
163
+ const denominator = new BigNumber3.BigNumber(inputReserve).plus(amountIn);
164
+ const amountOut = numerator.div(denominator);
165
+ return amountOut;
166
+ }
167
+ static getAmountIn({
168
+ amountOut,
169
+ inputReserve,
170
+ outputReserve
171
+ }) {
172
+ const numerator = new BigNumber3.BigNumber(inputReserve).times(amountOut);
173
+ const denominator = new BigNumber3.BigNumber(outputReserve).minus(amountOut);
174
+ const amountIn = numerator.div(denominator);
175
+ return amountIn;
176
+ }
177
+ };
178
+
179
+ // src/types.ts
180
+ var LaunchPadPlatform = /* @__PURE__ */ ((LaunchPadPlatform2) => {
181
+ LaunchPadPlatform2["PUMPFUN"] = "pumpfun";
182
+ LaunchPadPlatform2["RAYDIUM"] = "raydium";
183
+ return LaunchPadPlatform2;
184
+ })(LaunchPadPlatform || {});
185
+ var CurveType = /* @__PURE__ */ ((CurveType2) => {
186
+ CurveType2["CONSTANT_PRODUCT"] = "constant_product";
187
+ CurveType2["FIXED_PRICE"] = "fixed_price";
188
+ CurveType2["LINEAR_PRICE"] = "linear_price";
189
+ return CurveType2;
190
+ })(CurveType || {});
191
+ var FixedPriceCurve = class extends BaseCurve {
192
+ static getPoolInitPriceByPool({
193
+ poolInfo,
194
+ decimalA,
195
+ decimalB
196
+ }) {
197
+ return BigNumber3__default.default(poolInfo.virtualB).div(poolInfo.virtualA).times(10 ** (decimalA - decimalB));
198
+ }
199
+ static getPoolInitPriceByInit({
200
+ a,
201
+ b,
202
+ decimalA,
203
+ decimalB
204
+ }) {
205
+ return BigNumber3__default.default(b).div(a).times(10 ** (decimalA - decimalB));
206
+ }
207
+ static getPoolPrice({
208
+ poolInfo,
209
+ decimalA,
210
+ decimalB
211
+ }) {
212
+ return BigNumber3__default.default(poolInfo.virtualB).div(poolInfo.virtualA).times(10 ** (decimalA - decimalB));
213
+ }
214
+ static getPoolEndPrice({
215
+ supply,
216
+ totalSell,
217
+ totalLockedAmount,
218
+ totalFundRaising,
219
+ migrateFee,
220
+ decimalA,
221
+ decimalB
222
+ }) {
223
+ return BigNumber3__default.default(totalFundRaising).minus(migrateFee).div(BigNumber3__default.default(supply).minus(totalSell).minus(totalLockedAmount)).times(10 ** (decimalA - decimalB));
224
+ }
225
+ static getPoolEndPriceReal({
226
+ poolInfo,
227
+ decimalA,
228
+ decimalB
229
+ }) {
230
+ const allSellToken = BigNumber3__default.default(poolInfo.totalSellA).minus(poolInfo.realA);
231
+ const buyAllTokenUseB = BigNumber3__default.default(poolInfo.totalFundRaisingB).minus(poolInfo.realB);
232
+ return BigNumber3__default.default(poolInfo.virtualB).plus(poolInfo.realB).plus(buyAllTokenUseB).div(BigNumber3__default.default(poolInfo.virtualA).minus(poolInfo.realA).plus(allSellToken)).times(10 ** (decimalA - decimalB));
233
+ }
234
+ static getInitParam({
235
+ supply,
236
+ totalFundRaising,
237
+ totalLockedAmount,
238
+ migrateFee
239
+ }) {
240
+ const supplyMinusLocked = BigNumber3__default.default(supply).minus(totalLockedAmount);
241
+ if (supplyMinusLocked.lte(0)) throw Error("invalid input 1");
242
+ const denominator = BigNumber3__default.default(2).times(totalFundRaising).minus(migrateFee);
243
+ const numerator = BigNumber3__default.default(totalFundRaising).times(supplyMinusLocked);
244
+ const totalSellExpect = numerator.div(denominator);
245
+ return { a: totalSellExpect, b: totalFundRaising, c: totalSellExpect };
246
+ }
247
+ static buyExactIn({
248
+ poolInfo,
249
+ amount
250
+ }) {
251
+ return this.getAmountOut({
252
+ amountIn: amount,
253
+ initInput: poolInfo.virtualB,
254
+ initOutput: poolInfo.virtualA
255
+ });
256
+ }
257
+ static buyExactOut({
258
+ poolInfo,
259
+ amount
260
+ }) {
261
+ return this.getAmountIn({
262
+ amountOut: amount,
263
+ initInput: poolInfo.virtualB,
264
+ initOutput: poolInfo.virtualA
265
+ });
266
+ }
267
+ static sellExactIn({
268
+ poolInfo,
269
+ amount
270
+ }) {
271
+ return this.getAmountOut({
272
+ amountIn: amount,
273
+ initInput: poolInfo.virtualA,
274
+ initOutput: poolInfo.virtualB
275
+ });
276
+ }
277
+ static sellExactOut({
278
+ poolInfo,
279
+ amount
280
+ }) {
281
+ return this.getAmountIn({
282
+ amountOut: amount,
283
+ initInput: poolInfo.virtualA,
284
+ initOutput: poolInfo.virtualB
285
+ });
286
+ }
287
+ static getAmountOut({
288
+ amountIn,
289
+ initInput,
290
+ initOutput
291
+ }) {
292
+ const numerator = new BigNumber3__default.default(initOutput).times(amountIn);
293
+ const amountOut = numerator.div(initInput);
294
+ return amountOut;
295
+ }
296
+ static getAmountIn({
297
+ amountOut,
298
+ initInput,
299
+ initOutput
300
+ }) {
301
+ const numerator = new BigNumber3__default.default(initInput).times(amountOut);
302
+ const amountIn = numerator.div(initOutput);
303
+ return amountIn;
304
+ }
305
+ };
306
+ var LinearPriceCurve = class extends BaseCurve {
307
+ static getPoolInitPriceByPool(args) {
308
+ return 0;
309
+ }
310
+ static getPoolInitPriceByInit(args) {
311
+ return 0;
312
+ }
313
+ static getPoolPrice({
314
+ poolInfo,
315
+ decimalA,
316
+ decimalB
317
+ }) {
318
+ return new BigNumber3__default.default(poolInfo.virtualA).times(poolInfo.realA).div(new BigNumber3__default.default(1).shiftedBy(-64)).times(10 ** (decimalA - decimalB));
319
+ }
320
+ static getPoolEndPrice({
321
+ supply,
322
+ totalSell,
323
+ totalLockedAmount,
324
+ totalFundRaising,
325
+ migrateFee,
326
+ decimalA,
327
+ decimalB
328
+ }) {
329
+ return new BigNumber3__default.default(totalFundRaising).minus(migrateFee).div(new BigNumber3__default.default(supply).minus(totalSell).minus(totalLockedAmount)).times(10 ** (decimalA - decimalB));
330
+ }
331
+ static getPoolEndPriceReal({
332
+ poolInfo,
333
+ decimalA,
334
+ decimalB
335
+ }) {
336
+ const allSellToken = new BigNumber3__default.default(poolInfo.totalSellA).minus(poolInfo.realA);
337
+ const buyAllTokenUseB = new BigNumber3__default.default(poolInfo.totalFundRaisingB).minus(poolInfo.realB);
338
+ return new BigNumber3__default.default(poolInfo.virtualB).plus(poolInfo.realB).plus(buyAllTokenUseB).div(new BigNumber3__default.default(poolInfo.virtualA).minus(poolInfo.realA).minus(allSellToken)).times(10 ** (decimalA - decimalB));
339
+ }
340
+ static getInitParam({
341
+ supply,
342
+ totalFundRaising,
343
+ totalLockedAmount,
344
+ migrateFee
345
+ }) {
346
+ const supplyMinusLocked = new BigNumber3__default.default(supply).minus(totalLockedAmount);
347
+ if (supplyMinusLocked.lte(0)) throw Error("supplyMinusLocked need gt 0");
348
+ const denominator = new BigNumber3__default.default(totalFundRaising).times(3).minus(migrateFee);
349
+ const numerator = new BigNumber3__default.default(totalFundRaising).times(2).minus(supplyMinusLocked);
350
+ const totalSellExpect = numerator.div(denominator);
351
+ const totalSellSquared = totalSellExpect.times(totalSellExpect);
352
+ const a = new BigNumber3__default.default(totalFundRaising).times(2).times(new BigNumber3__default.default(1).shiftedBy(-64)).div(totalSellSquared);
353
+ if (!a.gt(0)) throw Error("a need gt 0");
354
+ const MaxU64 = new BigNumber3__default.default(1).shiftedBy(-64).minus(1);
355
+ if (!MaxU64.gt(a)) throw Error("a need lt u64 max");
356
+ return { a, b: 0, c: totalSellExpect };
357
+ }
358
+ static buyExactIn({
359
+ poolInfo,
360
+ amount
361
+ }) {
362
+ const newQuote = new BigNumber3__default.default(poolInfo.realB).plus(amount);
363
+ const termInsideSqrt = new BigNumber3__default.default(2).times(newQuote).times(new BigNumber3__default.default(1).shiftedBy(-64)).div(poolInfo.virtualA);
364
+ const sqrtTerm = new BigNumber3__default.default(new BigNumber3__default.default(termInsideSqrt).sqrt().toFixed(0));
365
+ const amountOut = sqrtTerm.minus(poolInfo.realA);
366
+ return amountOut;
367
+ }
368
+ static buyExactOut({
369
+ poolInfo,
370
+ amount
371
+ }) {
372
+ const newBase = new BigNumber3__default.default(poolInfo.realA).plus(amount);
373
+ const newBaseSquared = newBase.times(newBase);
374
+ const newQuote = new BigNumber3__default.default(poolInfo.virtualA).times(newBaseSquared).div(new BigNumber3__default.default(2).times(new BigNumber3__default.default(1).shiftedBy(-64)));
375
+ return newQuote.minus(poolInfo.realB);
376
+ }
377
+ static sellExactIn({
378
+ poolInfo,
379
+ amount
380
+ }) {
381
+ const newBase = new BigNumber3__default.default(poolInfo.realA).minus(amount);
382
+ const newBaseSquared = newBase.times(newBase);
383
+ const newQuote = new BigNumber3__default.default(poolInfo.virtualA).times(newBaseSquared).div(new BigNumber3__default.default(2).times(new BigNumber3__default.default(1).shiftedBy(-64)));
384
+ return new BigNumber3__default.default(poolInfo.realB).minus(newQuote);
385
+ }
386
+ static sellExactOut({
387
+ poolInfo,
388
+ amount
389
+ }) {
390
+ const newB = new BigNumber3__default.default(poolInfo.realB).minus(amount);
391
+ const termInsideSqrt = new BigNumber3__default.default(2).times(newB).times(new BigNumber3__default.default(1).shiftedBy(-64)).div(poolInfo.virtualA);
392
+ const sqrtTerm = new BigNumber3__default.default(new BigNumber3__default.default(termInsideSqrt).sqrt().toFixed(0));
393
+ const amountIn = new BigNumber3__default.default(poolInfo.realA).minus(sqrtTerm);
394
+ return amountIn;
395
+ }
396
+ };
397
+
398
+ // src/curve/Curve.ts
399
+ var Curve = class _Curve {
400
+ static getCurvePreviewPoints({
401
+ curveType,
402
+ pointCount,
403
+ poolInfo
404
+ }) {
405
+ return this.getPoolCurvePointByInit({
406
+ curveType,
407
+ pointCount,
408
+ supply: poolInfo.supply,
409
+ totalFundRaising: poolInfo.totalFundRaisingB,
410
+ totalSell: poolInfo.totalSellA,
411
+ totalLockedAmount: poolInfo.vestingSchedule.totalLockedAmount,
412
+ migrateFee: poolInfo.migrateFee,
413
+ decimalA: poolInfo.mintDecimalsA,
414
+ decimalB: poolInfo.mintDecimalsB
415
+ });
416
+ }
417
+ static getPoolCurvePointByInit({
418
+ curveType,
419
+ pointCount,
420
+ supply,
421
+ totalFundRaising,
422
+ totalSell,
423
+ totalLockedAmount,
424
+ migrateFee,
425
+ decimalA,
426
+ decimalB
427
+ }) {
428
+ if (pointCount < 3) throw Error("point count < 3");
429
+ const curve = this.getCurve(curveType);
430
+ const { a, b } = curve.getInitParam({
431
+ supply,
432
+ totalFundRaising,
433
+ totalSell,
434
+ totalLockedAmount,
435
+ migrateFee
436
+ });
437
+ const initPrice = curve.getPoolInitPriceByInit({ a, b, decimalA, decimalB });
438
+ const stepBuy = new BigNumber3__default.default(totalFundRaising).div(pointCount - 1);
439
+ const zero = new BigNumber3__default.default(0);
440
+ const returnPoints = [{ price: initPrice, totalSellSupply: 0 }];
441
+ let realA = zero;
442
+ let realB = zero;
443
+ for (let i = 1; i < pointCount; i++) {
444
+ const amountB = i !== pointCount - 1 ? stepBuy : new BigNumber3__default.default(totalFundRaising).minus(realB);
445
+ const itemBuy = this.buyExactIn({
446
+ poolInfo: {
447
+ virtualA: a,
448
+ virtualB: b,
449
+ realA,
450
+ realB,
451
+ totalFundRaisingB: totalFundRaising,
452
+ totalSellA: totalSell
453
+ },
454
+ amountB,
455
+ protocolFeeRate: zero,
456
+ platformFeeRate: zero,
457
+ curveType,
458
+ shareFeeRate: zero
459
+ });
460
+ realA = realA.plus(itemBuy.amountA);
461
+ realB = realB.plus(itemBuy.amountB);
462
+ const nowPoolPrice = this.getPrice({
463
+ poolInfo: { virtualA: a, virtualB: b, realA, realB },
464
+ decimalA,
465
+ decimalB,
466
+ curveType
467
+ });
468
+ returnPoints.push({
469
+ price: nowPoolPrice,
470
+ totalSellSupply: new BigNumber3__default.default(realA).div(10 ** decimalA).toNumber()
471
+ });
472
+ }
473
+ return returnPoints;
474
+ }
475
+ static getPoolInitPriceByPool({
476
+ poolInfo,
477
+ decimalA,
478
+ decimalB,
479
+ curveType
480
+ }) {
481
+ const curve = this.getCurve(curveType);
482
+ return curve.getPoolInitPriceByPool({ poolInfo, decimalA, decimalB });
483
+ }
484
+ static getPoolInitPriceByInit({
485
+ a,
486
+ b,
487
+ decimalA,
488
+ decimalB,
489
+ curveType
490
+ }) {
491
+ const curve = this.getCurve(curveType);
492
+ return curve.getPoolInitPriceByInit({ a, b, decimalA, decimalB });
493
+ }
494
+ static getPrice({
495
+ poolInfo,
496
+ curveType,
497
+ decimalA,
498
+ decimalB
499
+ }) {
500
+ const curve = this.getCurve(curveType);
501
+ return curve.getPoolPrice({ poolInfo, decimalA, decimalB });
502
+ }
503
+ static getEndPrice({
504
+ poolInfo,
505
+ curveType,
506
+ decimalA,
507
+ decimalB
508
+ }) {
509
+ const curve = this.getCurve(curveType);
510
+ return curve.getPoolPrice({ poolInfo, decimalA, decimalB });
511
+ }
512
+ static getPoolEndPriceReal({
513
+ poolInfo,
514
+ curveType,
515
+ decimalA,
516
+ decimalB
517
+ }) {
518
+ const curve = this.getCurve(curveType);
519
+ return curve.getPoolEndPriceReal({ poolInfo, decimalA, decimalB });
520
+ }
521
+ static checkParam({
522
+ supply,
523
+ totalFundRaising,
524
+ totalSell,
525
+ totalLockedAmount,
526
+ decimalsA,
527
+ decimalsB,
528
+ config,
529
+ migrateType
530
+ }) {
531
+ const supplyBn = new BigNumber3__default.default(supply);
532
+ if (decimalsA !== config.decimals) throw Error(`decimals should be ${config.decimals}`);
533
+ if (supplyBn.times(config.maxLockRate).lt(totalLockedAmount))
534
+ throw Error("totalLockedAmount exceeds max lock amount");
535
+ if (new BigNumber3__default.default(config.minSupplyA).shiftedBy(decimalsA).gte(supply))
536
+ throw Error("supply less than min supply");
537
+ if (supplyBn.times(config.minSellRateA).gte(totalSell))
538
+ throw Error("totalSell less than min sell");
539
+ if (new BigNumber3__default.default(config.minFundRaisingB).shiftedBy(decimalsB).gte(totalFundRaising))
540
+ throw Error("totalFundRaising less than min fund raising");
541
+ const migrateAmountBn = supplyBn.minus(totalSell).minus(totalLockedAmount);
542
+ if (migrateAmountBn.lt(supplyBn.times(config.minMigrateRateA)))
543
+ throw Error("migrateAmount less than min migrate amount");
544
+ const liquidity = migrateAmountBn.times(totalFundRaising).sqrt();
545
+ if (migrateType === "amm") {
546
+ if (liquidity.shiftedBy(-decimalsA).lte(1)) throw Error("insufficient liquidity");
547
+ } else if (migrateType === "cpmm") {
548
+ if (liquidity.lte(100)) throw Error("insufficient liquidity");
549
+ } else {
550
+ throw Error("invalid migrateType");
551
+ }
552
+ }
553
+ static buyExactIn({
554
+ poolInfo,
555
+ amountB,
556
+ protocolFeeRate,
557
+ platformFeeRate,
558
+ curveType,
559
+ shareFeeRate
560
+ }) {
561
+ const feeRate = new BigNumber3__default.default(protocolFeeRate).plus(shareFeeRate).plus(platformFeeRate);
562
+ const _totalFee = this.calculateFee({ amount: amountB, feeRate });
563
+ const amountLessFeeB = new BigNumber3__default.default(amountB).minus(_totalFee);
564
+ const curve = this.getCurve(curveType);
565
+ const _amountA = curve.buyExactIn({ poolInfo, amount: amountLessFeeB });
566
+ const remainingAmountA = new BigNumber3__default.default(poolInfo.totalSellA).minus(poolInfo.realA);
567
+ let amountA;
568
+ let realAmountB;
569
+ let totalFee;
570
+ if (remainingAmountA.lte(_amountA)) {
571
+ amountA = remainingAmountA;
572
+ const amountLessFeeB2 = curve.buyExactOut({
573
+ poolInfo,
574
+ amount: amountA
575
+ });
576
+ realAmountB = this.calculatePreFee({ postFeeAmount: amountLessFeeB2, feeRate });
577
+ totalFee = new BigNumber3__default.default(realAmountB).minus(amountLessFeeB2);
578
+ } else {
579
+ amountA = _amountA;
580
+ realAmountB = amountB;
581
+ totalFee = _totalFee;
582
+ }
583
+ const splitFee = this.splitFee({ totalFee, protocolFeeRate, platformFeeRate, shareFeeRate });
584
+ return { amountA, amountB: realAmountB, splitFee };
585
+ }
586
+ static buyExactOut({
587
+ poolInfo,
588
+ amountA,
589
+ protocolFeeRate,
590
+ platformFeeRate,
591
+ curveType,
592
+ shareFeeRate
593
+ }) {
594
+ const remainingAmountA = new BigNumber3__default.default(poolInfo.totalSellA).minus(poolInfo.realA);
595
+ let realAmountA = amountA;
596
+ if (remainingAmountA.lte(amountA)) {
597
+ realAmountA = remainingAmountA;
598
+ }
599
+ const curve = this.getCurve(curveType);
600
+ const amountInLessFeeB = curve.buyExactOut({ poolInfo, amount: amountA });
601
+ const totalFeeRate = new BigNumber3__default.default(protocolFeeRate).plus(shareFeeRate).plus(platformFeeRate);
602
+ const amountB = this.calculatePreFee({
603
+ postFeeAmount: amountInLessFeeB,
604
+ feeRate: totalFeeRate
605
+ });
606
+ const totalFee = new BigNumber3__default.default(amountB).minus(amountInLessFeeB);
607
+ const splitFee = this.splitFee({ totalFee, protocolFeeRate, platformFeeRate, shareFeeRate });
608
+ return { amountA: realAmountA, amountB, splitFee };
609
+ }
610
+ static sellExactIn({
611
+ poolInfo,
612
+ amountA,
613
+ protocolFeeRate,
614
+ platformFeeRate,
615
+ curveType,
616
+ shareFeeRate
617
+ }) {
618
+ const curve = this.getCurve(curveType);
619
+ const amountB = curve.sellExactIn({ poolInfo, amount: amountA });
620
+ const totalFee = this.calculateFee({
621
+ amount: amountB,
622
+ feeRate: new BigNumber3__default.default(protocolFeeRate).plus(shareFeeRate).plus(platformFeeRate)
623
+ });
624
+ const splitFee = this.splitFee({ totalFee, protocolFeeRate, platformFeeRate, shareFeeRate });
625
+ return { amountA, amountB: new BigNumber3__default.default(amountB).minus(totalFee), splitFee };
626
+ }
627
+ static sellExactOut({
628
+ poolInfo,
629
+ amountB,
630
+ protocolFeeRate,
631
+ platformFeeRate,
632
+ curveType,
633
+ shareFeeRate
634
+ }) {
635
+ const totalFeeRate = new BigNumber3__default.default(protocolFeeRate).plus(shareFeeRate).plus(platformFeeRate);
636
+ const amountOutWithFeeB = this.calculatePreFee({
637
+ postFeeAmount: amountB,
638
+ feeRate: totalFeeRate
639
+ });
640
+ if (new BigNumber3__default.default(poolInfo.realB).lt(amountOutWithFeeB)) throw Error("Insufficient liquidity");
641
+ const totalFee = new BigNumber3__default.default(amountOutWithFeeB).minus(amountB);
642
+ const curve = _Curve.getCurve(curveType);
643
+ const amountA = curve.sellExactOut({ poolInfo, amount: amountOutWithFeeB });
644
+ if (new BigNumber3__default.default(amountA).gt(poolInfo.realA)) throw Error("Insufficient liquidity");
645
+ const splitFee = this.splitFee({ totalFee, protocolFeeRate, platformFeeRate, shareFeeRate });
646
+ return { amountA, amountB, splitFee };
647
+ }
648
+ static splitFee({
649
+ totalFee,
650
+ protocolFeeRate,
651
+ platformFeeRate,
652
+ shareFeeRate
653
+ }) {
654
+ const totalFeeBn = new BigNumber3__default.default(totalFee);
655
+ const totalFeeRate = new BigNumber3__default.default(protocolFeeRate).plus(platformFeeRate).plus(shareFeeRate);
656
+ const platformFee = totalFeeRate.isZero() ? new BigNumber3__default.default(0) : totalFeeBn.times(platformFeeRate).div(totalFeeRate);
657
+ const shareFee = totalFeeRate.isZero() ? new BigNumber3__default.default(0) : totalFeeBn.times(shareFeeRate).div(totalFeeRate);
658
+ const protocolFee = totalFeeBn.minus(platformFee).minus(shareFee);
659
+ return { platformFee, shareFee, protocolFee };
660
+ }
661
+ static calculateFee({
662
+ amount,
663
+ feeRate
664
+ }) {
665
+ return new BigNumber3__default.default(amount).times(feeRate);
666
+ }
667
+ static calculatePreFee({
668
+ postFeeAmount,
669
+ feeRate
670
+ }) {
671
+ if (new BigNumber3__default.default(feeRate).isZero()) return postFeeAmount;
672
+ return new BigNumber3__default.default(postFeeAmount).div(new BigNumber3__default.default(feeRate).plus(1));
673
+ }
674
+ static getCurve(curveType) {
675
+ switch (curveType) {
676
+ case "constant_product" /* CONSTANT_PRODUCT */:
677
+ return ConstantProductCurve;
678
+ case "fixed_price" /* FIXED_PRICE */:
679
+ return FixedPriceCurve;
680
+ case "linear_price" /* LINEAR_PRICE */:
681
+ return LinearPriceCurve;
682
+ }
683
+ }
684
+ };
685
+
686
+ // src/create-token-runtime.ts
687
+ function validateCreateTokenIntent(intent) {
688
+ if (!intent.name.trim()) throw new Error("name required");
689
+ if (!intent.symbol.trim()) throw new Error("symbol required");
690
+ if (intent.symbol.length > 12) throw new Error("symbol too long");
691
+ }
692
+ function createLaunchpadCreateRuntime(options) {
693
+ let current = { status: "idle" };
694
+ const set = (next) => {
695
+ current = next;
696
+ options.onChange?.(next);
697
+ return next;
698
+ };
699
+ return {
700
+ snapshot() {
701
+ return current;
702
+ },
703
+ async submit(intent) {
704
+ set({ status: "validating", intent });
705
+ try {
706
+ validateCreateTokenIntent(intent);
707
+ } catch (error) {
708
+ const err = error instanceof Error ? error : new Error(String(error));
709
+ return set({ status: "failed", intent, error: err });
710
+ }
711
+ let imageUri = intent.imageUri ?? "";
712
+ if (options.upload) {
713
+ set({ status: "uploading", intent });
714
+ try {
715
+ imageUri = await options.upload.upload(intent);
716
+ } catch (error) {
717
+ const err = error instanceof Error ? error : new Error(String(error));
718
+ return set({ status: "failed", intent, error: err });
719
+ }
720
+ }
721
+ if (!options.signer) {
722
+ return set({
723
+ status: "rejected",
724
+ intent,
725
+ error: new Error("missing signer")
726
+ });
727
+ }
728
+ set({ status: "signing", intent: { ...intent, imageUri } });
729
+ let signed;
730
+ try {
731
+ signed = await options.signer.sign(imageUri || intent.symbol);
732
+ } catch (error) {
733
+ const err = error instanceof Error ? error : new Error(String(error));
734
+ return set({ status: "rejected", intent, error: err });
735
+ }
736
+ set({ status: "submitting", intent });
737
+ try {
738
+ const result = await options.execution.submit(signed, intent);
739
+ return set({ status: "succeeded", intent, txHash: result.txHash });
740
+ } catch (error) {
741
+ const err = error instanceof Error ? error : new Error(String(error));
742
+ return set({ status: "failed", intent, error: err });
743
+ }
744
+ }
745
+ };
746
+ }
747
+
748
+ // src/in-memory-launchpad-adapter.ts
749
+ function createInMemoryLaunchpadPorts(options = {}) {
750
+ return {
751
+ upload: {
752
+ async upload() {
753
+ if (options.uploadError) throw options.uploadError;
754
+ return "ipfs://memory";
755
+ }
756
+ },
757
+ signer: {
758
+ async sign(payload) {
759
+ if (options.signError) throw options.signError;
760
+ return `signed:${payload}`;
761
+ }
762
+ },
763
+ execution: {
764
+ async submit() {
765
+ if (options.timeout) throw new Error("submit timeout");
766
+ if (options.submitError) throw options.submitError;
767
+ return { txHash: "tx-memory" };
768
+ }
769
+ }
770
+ };
771
+ }
772
+
773
+ // src/index.ts
774
+ var PACKAGE_NAME = "@liberfi.io/react-launchpad";
775
+ var ReadyContext = react.createContext(true);
776
+ function LaunchpadProvider({ children }) {
777
+ return react.createElement(ReadyContext.Provider, { value: true }, children);
778
+ }
779
+ function useLaunchpadReady() {
780
+ return react.useContext(ReadyContext);
781
+ }
782
+
783
+ exports.BaseCurve = BaseCurve;
784
+ exports.ConstantProductCurve = ConstantProductCurve;
785
+ exports.Curve = Curve;
786
+ exports.CurveType = CurveType;
787
+ exports.FixedPriceCurve = FixedPriceCurve;
788
+ exports.LaunchPadPlatform = LaunchPadPlatform;
789
+ exports.LaunchpadProvider = LaunchpadProvider;
790
+ exports.LinearPriceCurve = LinearPriceCurve;
791
+ exports.PACKAGE_NAME = PACKAGE_NAME;
792
+ exports.createInMemoryLaunchpadPorts = createInMemoryLaunchpadPorts;
793
+ exports.createLaunchpadCreateRuntime = createLaunchpadCreateRuntime;
794
+ exports.useLaunchpadReady = useLaunchpadReady;
795
+ exports.validateCreateTokenIntent = validateCreateTokenIntent;
796
+ //# sourceMappingURL=index.js.map
797
+ //# sourceMappingURL=index.js.map