stackswap-front-api-test-02 1.0.148 → 1.0.151
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/esm/stackswap/manager/swap.manager.d.ts +3 -0
- package/dist/esm/stackswap/manager/swap.manager.js +132 -46
- package/dist/esm/stackswap/manager/swap.manager.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/stackswap/manager/swap.manager.d.ts +3 -0
- package/dist/stackswap/manager/swap.manager.js +132 -46
- package/dist/stackswap/manager/swap.manager.js.map +1 -1
- package/package.json +1 -1
- package/src/stackswap/manager/swap.manager.ts +170 -58
|
@@ -211,7 +211,7 @@ export class SwapManager {
|
|
|
211
211
|
post_condition.push(await getPostConditionFromAsset(this.stackswap, this.stackswap.getSenderAddress(), token_bridge2.addr, bridge2_max.toString(), FungibleConditionCode.LessEqual));
|
|
212
212
|
post_condition.push(await getPostConditionFromAsset(this.stackswap, pair_to_addr, token_to.addr, to_min.toString(), FungibleConditionCode.GreaterEqual));
|
|
213
213
|
|
|
214
|
-
const options = getWriteOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_ROUTER_SWAP2(), '
|
|
214
|
+
const options = getWriteOptions(this.stackswap, this.stackswap.config.CONTRACT_NAME_STACKSWAP_ROUTER_SWAP2(), 'exchange-router2', [
|
|
215
215
|
parseAddressToCV(token_from_addr),
|
|
216
216
|
parseAddressToCV(token_bridge1_addr),
|
|
217
217
|
parseAddressToCV(token_bridge2_addr),
|
|
@@ -226,7 +226,7 @@ export class SwapManager {
|
|
|
226
226
|
uintCV(bridge1_min.toString()),
|
|
227
227
|
uintCV(bridge2_min.toString()),
|
|
228
228
|
uintCV(to_min.toString()),
|
|
229
|
-
this.stackswap.getSenderAddress(),
|
|
229
|
+
parseAddressToCV(this.stackswap.getSenderAddress()),
|
|
230
230
|
], post_condition, callback);
|
|
231
231
|
openContractCall(options);
|
|
232
232
|
}
|
|
@@ -397,40 +397,54 @@ export class SwapManager {
|
|
|
397
397
|
}
|
|
398
398
|
}
|
|
399
399
|
|
|
400
|
-
|
|
400
|
+
|
|
401
|
+
price_impact_router(router_from_mode: boolean, router_to_mode: boolean,
|
|
402
|
+
pair_from:LiquidityPool, pair_to:LiquidityPool , token_from_amount: string) {
|
|
401
403
|
// console.log('price impact');
|
|
402
404
|
// (dy (/ (* u997 balance-y dx) (+ (* u1000 balance-x) (* u997 dx)))) ;; overall fee is 30 bp, either all for the pool, or 25 bp for pool and 5 bp for operator
|
|
403
405
|
// dy = balancey * dx / balancex + dx
|
|
404
406
|
try {
|
|
405
|
-
const
|
|
406
|
-
const
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
const balance_y = router_to_mode ? pair_to.balance_y : pair_to.balance_x;
|
|
410
|
-
// console.log('balance_y', balance_y);
|
|
407
|
+
const from_input_balance = router_from_mode ? pair_from.balance_x : pair_from.balance_y;
|
|
408
|
+
const from_input_decimal = router_from_mode ? pair_from.token_x.decimal : pair_from.token_y.decimal;
|
|
409
|
+
const from_output_balance= router_from_mode ? pair_from.balance_y : pair_from.balance_y;
|
|
411
410
|
|
|
412
|
-
const
|
|
413
|
-
|
|
414
|
-
if (dx.toNumber() > new BigNumber(balance_x).toNumber()) {
|
|
415
|
-
// console.log('dx is bigger than dy');
|
|
411
|
+
const from_input_dx = new BigNumber(token_from_amount).multipliedBy(10 ** from_input_decimal);
|
|
412
|
+
if (from_input_dx.toNumber() > new BigNumber(from_input_balance).toNumber()) {
|
|
416
413
|
return 100;
|
|
417
414
|
}
|
|
418
|
-
const
|
|
419
|
-
|
|
420
|
-
const
|
|
421
|
-
|
|
422
|
-
const
|
|
423
|
-
|
|
424
|
-
const
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
const
|
|
429
|
-
|
|
430
|
-
|
|
415
|
+
const from_dy_1 = new BigNumber(from_output_balance).multipliedBy(from_input_dx);
|
|
416
|
+
const from_dy_2 = new BigNumber(from_input_balance).plus(from_input_dx);
|
|
417
|
+
const from_dy = (from_dy_1.toNumber()) / (from_dy_2.toNumber());
|
|
418
|
+
|
|
419
|
+
const from_input_price = new BigNumber(10 ** 10).multipliedBy(new BigNumber(from_input_balance)).dividedBy(new BigNumber(from_output_balance));
|
|
420
|
+
const from_output_price = new BigNumber(10 ** 10).multipliedBy(new BigNumber(from_input_balance)).plus(from_input_dx).dividedBy(new BigNumber(from_output_balance).minus(from_dy));
|
|
421
|
+
const from_price_impact = from_output_price.minus(from_input_price).multipliedBy(10000).dividedBy(from_input_price);
|
|
422
|
+
|
|
423
|
+
console.log("FROM : ", token_from_amount, from_dy, from_price_impact.toNumber())
|
|
424
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
425
|
+
const to_input_balance = router_to_mode ? pair_to.balance_x : pair_to.balance_y;
|
|
426
|
+
const to_input_decimal = router_to_mode ? pair_to.token_x.decimal : pair_to.token_y.decimal;
|
|
427
|
+
const to_output_balance= router_to_mode ? pair_to.balance_y : pair_to.balance_y;
|
|
428
|
+
|
|
429
|
+
const to_input_dx = new BigNumber(from_dy).multipliedBy(10 ** to_input_decimal);
|
|
430
|
+
if (to_input_dx.toNumber() > new BigNumber(to_input_balance).toNumber()) {
|
|
431
|
+
return 100;
|
|
432
|
+
}
|
|
433
|
+
const to_dy_1 = new BigNumber(to_output_balance).multipliedBy(to_input_dx);
|
|
434
|
+
const to_dy_2 = new BigNumber(to_input_balance).plus(to_input_dx);
|
|
435
|
+
const to_dy = (to_dy_1.toNumber()) / (to_dy_2.toNumber());
|
|
436
|
+
|
|
437
|
+
const to_input_price = new BigNumber(10 ** 10).multipliedBy(new BigNumber(to_input_balance)).dividedBy(new BigNumber(to_output_balance));
|
|
438
|
+
const to_output_price = new BigNumber(10 ** 10).multipliedBy(new BigNumber(to_input_balance)).plus(to_input_dx).dividedBy(new BigNumber(to_output_balance).minus(to_dy));
|
|
439
|
+
const to_price_impact = to_output_price.minus(to_input_price).multipliedBy(10000).dividedBy(to_input_price);
|
|
440
|
+
|
|
441
|
+
// console.log("TO : ", bridge_dy, to_dy, to_price_impact.toNumber())
|
|
442
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
443
|
+
let price_impact = from_price_impact.toNumber() * to_price_impact.toNumber();
|
|
431
444
|
if (price_impact >= 10000) {
|
|
432
445
|
price_impact = 9999;
|
|
433
446
|
}
|
|
447
|
+
console.log("TOTAL : ", price_impact)
|
|
434
448
|
return (price_impact / 100).toFixed(5);
|
|
435
449
|
} catch (e) {
|
|
436
450
|
// console.log(e);
|
|
@@ -438,6 +452,76 @@ export class SwapManager {
|
|
|
438
452
|
}
|
|
439
453
|
}
|
|
440
454
|
|
|
455
|
+
price_impact_router2(router_from_mode: boolean, router_bridge_mode: boolean, router_to_mode: boolean,
|
|
456
|
+
pair_from:LiquidityPool, pair_bridge:LiquidityPool, pair_to:LiquidityPool , token_from_amount:string) {
|
|
457
|
+
// console.log('price impact');
|
|
458
|
+
// (dy (/ (* u997 balance-y dx) (+ (* u1000 balance-x) (* u997 dx)))) ;; overall fee is 30 bp, either all for the pool, or 25 bp for pool and 5 bp for operator
|
|
459
|
+
// dy = balancey * dx / balancex + dx
|
|
460
|
+
try {
|
|
461
|
+
const from_input_balance = router_from_mode ? pair_from.balance_x : pair_from.balance_y;
|
|
462
|
+
const from_input_decimal = router_from_mode ? pair_from.token_x.decimal : pair_from.token_y.decimal;
|
|
463
|
+
const from_output_balance= router_from_mode ? pair_from.balance_y : pair_from.balance_y;
|
|
464
|
+
const from_input_dx = new BigNumber(token_from_amount).multipliedBy(10 ** from_input_decimal);
|
|
465
|
+
if (from_input_dx.toNumber() > new BigNumber(from_input_balance).toNumber()) {
|
|
466
|
+
return 100;
|
|
467
|
+
}
|
|
468
|
+
const from_dy_1 = new BigNumber(from_output_balance).multipliedBy(from_input_dx);
|
|
469
|
+
const from_dy_2 = new BigNumber(from_input_balance).plus(from_input_dx);
|
|
470
|
+
const from_dy = (from_dy_1.toNumber()) / (from_dy_2.toNumber());
|
|
471
|
+
|
|
472
|
+
const from_input_price = new BigNumber(10 ** 10).multipliedBy(new BigNumber(from_input_balance)).dividedBy(new BigNumber(from_output_balance));
|
|
473
|
+
const from_output_price = new BigNumber(10 ** 10).multipliedBy(new BigNumber(from_input_balance)).plus(from_input_dx).dividedBy(new BigNumber(from_output_balance).minus(from_dy));
|
|
474
|
+
const from_price_impact = from_output_price.minus(from_input_price).multipliedBy(10000).dividedBy(from_input_price);
|
|
475
|
+
|
|
476
|
+
console.log("FROM : ", token_from_amount, from_dy, from_price_impact.toNumber())
|
|
477
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
478
|
+
const bridge_input_balance = router_bridge_mode ? pair_bridge.balance_x : pair_bridge.balance_y;
|
|
479
|
+
const bridge_input_decimal = router_bridge_mode ? pair_bridge.token_x.decimal : pair_bridge.token_y.decimal;
|
|
480
|
+
const bridge_output_balance= router_bridge_mode ? pair_bridge.balance_y : pair_bridge.balance_y;
|
|
481
|
+
|
|
482
|
+
const bridge_input_dx = new BigNumber(from_dy).multipliedBy(10 ** bridge_input_decimal);
|
|
483
|
+
if (bridge_input_dx.toNumber() > new BigNumber(bridge_input_balance).toNumber()) {
|
|
484
|
+
return 100;
|
|
485
|
+
}
|
|
486
|
+
const bridge_dy_1 = new BigNumber(bridge_output_balance).multipliedBy(bridge_input_dx);
|
|
487
|
+
const bridge_dy_2 = new BigNumber(bridge_input_balance).plus(bridge_input_dx);
|
|
488
|
+
const bridge_dy = (bridge_dy_1.toNumber()) / (bridge_dy_2.toNumber());
|
|
489
|
+
|
|
490
|
+
const bridge_input_price = new BigNumber(10 ** 10).multipliedBy(new BigNumber(bridge_input_balance)).dividedBy(new BigNumber(bridge_output_balance));
|
|
491
|
+
const bridge_output_price = new BigNumber(10 ** 10).multipliedBy(new BigNumber(bridge_input_balance)).plus(bridge_input_dx).dividedBy(new BigNumber(bridge_output_balance).minus(bridge_dy));
|
|
492
|
+
const bridge_price_impact = bridge_output_price.minus(bridge_input_price).multipliedBy(10000).dividedBy(bridge_input_price);
|
|
493
|
+
|
|
494
|
+
console.log("BRIDGE : ", from_dy, bridge_dy, bridge_price_impact.toNumber())
|
|
495
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
496
|
+
const to_input_balance = router_to_mode ? pair_to.balance_x : pair_to.balance_y;
|
|
497
|
+
const to_input_decimal = router_to_mode ? pair_to.token_x.decimal : pair_to.token_y.decimal;
|
|
498
|
+
const to_output_balance= router_to_mode ? pair_to.balance_y : pair_to.balance_y;
|
|
499
|
+
|
|
500
|
+
const to_input_dx = new BigNumber(bridge_dy).multipliedBy(10 ** to_input_decimal);
|
|
501
|
+
if (to_input_dx.toNumber() > new BigNumber(to_input_balance).toNumber()) {
|
|
502
|
+
return 100;
|
|
503
|
+
}
|
|
504
|
+
const to_dy_1 = new BigNumber(to_output_balance).multipliedBy(to_input_dx);
|
|
505
|
+
const to_dy_2 = new BigNumber(to_input_balance).plus(to_input_dx);
|
|
506
|
+
const to_dy = (to_dy_1.toNumber()) / (to_dy_2.toNumber());
|
|
507
|
+
|
|
508
|
+
const to_input_price = new BigNumber(10 ** 10).multipliedBy(new BigNumber(to_input_balance)).dividedBy(new BigNumber(to_output_balance));
|
|
509
|
+
const to_output_price = new BigNumber(10 ** 10).multipliedBy(new BigNumber(to_input_balance)).plus(to_input_dx).dividedBy(new BigNumber(to_output_balance).minus(to_dy));
|
|
510
|
+
const to_price_impact = to_output_price.minus(to_input_price).multipliedBy(10000).dividedBy(to_input_price);
|
|
511
|
+
|
|
512
|
+
console.log("TO : ", bridge_dy, to_dy, to_price_impact.toNumber())
|
|
513
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
514
|
+
let price_impact = from_price_impact.toNumber() * bridge_price_impact.toNumber() * to_price_impact.toNumber();
|
|
515
|
+
if (price_impact >= 10000) {
|
|
516
|
+
price_impact = 9999;
|
|
517
|
+
}
|
|
518
|
+
console.log("TOTAL : ", price_impact)
|
|
519
|
+
return (price_impact / 100).toFixed(5);
|
|
520
|
+
} catch (e) {
|
|
521
|
+
// console.log(e);
|
|
522
|
+
return 0;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
441
525
|
getPriceString(pair : LiquidityPool | null, token_x : Token| null, token_y : Token| null, price : number, price_invert : boolean) {
|
|
442
526
|
try {
|
|
443
527
|
if (price < 0){
|
|
@@ -476,40 +560,22 @@ export class SwapManager {
|
|
|
476
560
|
}
|
|
477
561
|
|
|
478
562
|
case SwapType.ROUTER_STX:
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
let router_from_price = -1;
|
|
482
|
-
if ( from_res.output_price !== '0') {
|
|
483
|
-
router_from_price = (1 / parseFloat(from_res.output_price));
|
|
484
|
-
}
|
|
485
|
-
const router_bridge_amt = new BigNumber(new BigNumber(from_res.dy).toFixed(Number(bridge_token.decimal))).toString();
|
|
486
|
-
const to_res = swap_data.router_to_pair.getPoolPrice(!swap_data.router_to_mode, Number(router_bridge_amt) <= 0 ? '-1': router_bridge_amt);
|
|
487
|
-
let router_to_price = -1;
|
|
488
|
-
if ( to_res.output_price !== '0') {
|
|
489
|
-
router_to_price = (1 / parseFloat(to_res.output_price));
|
|
490
|
-
}
|
|
491
|
-
const token_y_amount2 = new BigNumber(to_res.dy).toFixed(Number(Number(token_x_amount) <= 0 || token_x_amount == '' ? 0 : swap_data.router_to_pair.getTokenY(swap_data.router_to_mode).decimal)).toString();
|
|
492
|
-
price = router_from_price * router_to_price;
|
|
493
|
-
const price_strings2 = this.getPriceString(swap_data.pair, swap_data.router_from_pair.getTokenX(swap_data.router_from_mode), swap_data.router_to_pair.getTokenY(swap_data.router_to_mode), price, price_invert);
|
|
494
|
-
return {price : price, dy: token_y_amount2, price_strings: price_strings2, bridge_amount : router_bridge_amt};
|
|
563
|
+
return this.getRouterDY_V1(swap_data, token_x_amount,
|
|
564
|
+
Token.getBaseTokens(this.stackswap, BaseToken.STX), price_invert);
|
|
495
565
|
|
|
496
566
|
case SwapType.ROUTER_STSW:
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
const token_y_amount3 = new BigNumber(to_res2.dy).toFixed(Number(Number(token_x_amount) <= 0 || token_x_amount == '' ? 0 : swap_data.router_to_pair.getTokenY(swap_data.router_to_mode).decimal)).toString();
|
|
510
|
-
price = router_from_price2 * router_to_price2;
|
|
511
|
-
const price_strings3 = this.getPriceString(swap_data.pair, swap_data.router_from_pair.getTokenX(swap_data.router_from_mode), swap_data.router_to_pair.getTokenY(swap_data.router_to_mode), price, price_invert);
|
|
512
|
-
return {price : price, dy: token_y_amount3, price_strings: price_strings3, bridge_amount : router_bridge_amt2};
|
|
567
|
+
return this.getRouterDY_V1(swap_data, token_x_amount,
|
|
568
|
+
Token.getBaseTokens(this.stackswap, BaseToken.STSW), price_invert);
|
|
569
|
+
|
|
570
|
+
case SwapType.ROUTER_STX_STSW:
|
|
571
|
+
return this.getRouterDY_V2(swap_data, token_x_amount,
|
|
572
|
+
Token.getBaseTokens(this.stackswap, BaseToken.STX),
|
|
573
|
+
Token.getBaseTokens(this.stackswap, BaseToken.STSW), price_invert);
|
|
574
|
+
|
|
575
|
+
case SwapType.ROUTER_STSW_STX:
|
|
576
|
+
return this.getRouterDY_V2(swap_data, token_x_amount,
|
|
577
|
+
Token.getBaseTokens(this.stackswap, BaseToken.STSW),
|
|
578
|
+
Token.getBaseTokens(this.stackswap, BaseToken.STX), price_invert);
|
|
513
579
|
|
|
514
580
|
default:
|
|
515
581
|
return {price : price, dy: '0', price_strings: ''};
|
|
@@ -517,5 +583,51 @@ export class SwapManager {
|
|
|
517
583
|
|
|
518
584
|
}
|
|
519
585
|
|
|
586
|
+
private getRouterDY_V1(swap_data: any, token_x_amount: string, bridge_token: Token, price_invert: boolean) :{price: number,dy: string, price_strings: string, bridge_amount: string} {
|
|
587
|
+
let price = -1;
|
|
588
|
+
const from_res = swap_data.router_from_pair.getPoolPrice(!swap_data.router_from_mode, Number(token_x_amount) <= 0 || token_x_amount == '' ? '1': token_x_amount);
|
|
589
|
+
let router_from_price = -1;
|
|
590
|
+
if ( from_res.output_price !== '0') {
|
|
591
|
+
router_from_price = (1 / parseFloat(from_res.output_price));
|
|
592
|
+
}
|
|
593
|
+
const router_bridge_amt = new BigNumber(new BigNumber(from_res.dy).toFixed(Number(bridge_token.decimal))).toString();
|
|
594
|
+
const to_res = swap_data.router_to_pair.getPoolPrice(!swap_data.router_to_mode, Number(router_bridge_amt) <= 0 ? '-1': router_bridge_amt);
|
|
595
|
+
let router_to_price = -1;
|
|
596
|
+
if ( to_res.output_price !== '0') {
|
|
597
|
+
router_to_price = (1 / parseFloat(to_res.output_price));
|
|
598
|
+
}
|
|
599
|
+
const token_y_amount = new BigNumber(to_res.dy).toFixed(Number(Number(token_x_amount) <= 0 || token_x_amount == '' ? 0 : swap_data.router_to_pair.getTokenY(swap_data.router_to_mode).decimal)).toString();
|
|
600
|
+
price = router_from_price * router_to_price;
|
|
601
|
+
const price_strings = this.getPriceString(swap_data.pair, swap_data.router_from_pair.getTokenX(swap_data.router_from_mode), swap_data.router_to_pair.getTokenY(swap_data.router_to_mode), price, price_invert);
|
|
602
|
+
return {price : price, dy: token_y_amount, price_strings: price_strings, bridge_amount : router_bridge_amt};
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
private getRouterDY_V2(swap_data: any, token_x_amount: string, bridge_token1: Token, bridge_token2: Token, price_invert: boolean) :{price: number,dy: string, price_strings: string, bridge_amount1: string, bridge_amount2: string} {
|
|
606
|
+
let price = -1;
|
|
607
|
+
const from_res = swap_data.router_from_pair.getPoolPrice(!swap_data.router_from_mode, Number(token_x_amount) <= 0 || token_x_amount == '' ? '1': token_x_amount);
|
|
608
|
+
let router_from_price = -1;
|
|
609
|
+
if ( from_res.output_price !== '0') {
|
|
610
|
+
router_from_price = (1 / parseFloat(from_res.output_price));
|
|
611
|
+
}
|
|
612
|
+
const router_bridge1_amt = new BigNumber(new BigNumber(from_res.dy).toFixed(Number(bridge_token1.decimal))).toString();
|
|
613
|
+
|
|
614
|
+
let router_bridge_price = -1;
|
|
615
|
+
const bridge_res = swap_data.router_bridge_pair.getPoolPrice(!swap_data.router_bridge_mode, Number(router_bridge1_amt) <= 0 || router_bridge1_amt == '' ? '1': router_bridge1_amt);
|
|
616
|
+
if ( bridge_res.output_price !== '0') {
|
|
617
|
+
router_bridge_price = (1 / parseFloat(bridge_res.output_price));
|
|
618
|
+
}
|
|
619
|
+
const router_bridge2_amt = new BigNumber(new BigNumber(bridge_res.dy).toFixed(Number(bridge_token2.decimal))).toString();
|
|
620
|
+
|
|
621
|
+
const to_res = swap_data.router_to_pair.getPoolPrice(!swap_data.router_to_mode, Number(router_bridge2_amt) <= 0 ? '-1': router_bridge2_amt);
|
|
622
|
+
let router_to_price = -1;
|
|
623
|
+
if ( to_res.output_price !== '0') {
|
|
624
|
+
router_to_price = (1 / parseFloat(to_res.output_price));
|
|
625
|
+
}
|
|
626
|
+
const token_y_amount = new BigNumber(to_res.dy).toFixed(Number(Number(token_x_amount) <= 0 || token_x_amount == '' ? 0 : swap_data.router_to_pair.getTokenY(swap_data.router_to_mode).decimal)).toString();
|
|
627
|
+
price = router_from_price * router_to_price * router_bridge_price;
|
|
628
|
+
const price_strings = this.getPriceString(swap_data.pair, swap_data.router_from_pair.getTokenX(swap_data.router_from_mode), swap_data.router_to_pair.getTokenY(swap_data.router_to_mode), price, price_invert);
|
|
629
|
+
return {price: price, dy: token_y_amount, price_strings: price_strings, bridge_amount1: router_bridge1_amt, bridge_amount2: router_bridge2_amt};
|
|
630
|
+
}
|
|
631
|
+
|
|
520
632
|
|
|
521
633
|
}
|