aftermath-ts-sdk 1.1.97 → 1.2.0-refactor.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.
Files changed (136) hide show
  1. package/dist/general/historicalData/historicalData.d.ts +1 -1
  2. package/dist/general/historicalData/historicalData.d.ts.map +1 -1
  3. package/dist/general/nfts/nftsApi.d.ts +1 -5
  4. package/dist/general/nfts/nftsApi.d.ts.map +1 -1
  5. package/dist/general/nfts/nftsApi.js +15 -29
  6. package/dist/general/nfts/nftsApiCasting.d.ts +0 -1
  7. package/dist/general/nfts/nftsApiCasting.d.ts.map +1 -1
  8. package/dist/general/nfts/nftsApiCasting.js +0 -10
  9. package/dist/general/nfts/nftsTypes.d.ts +0 -1
  10. package/dist/general/nfts/nftsTypes.d.ts.map +1 -1
  11. package/dist/general/prices/{coingecko/coinGeckoTypes.d.ts → coinGeckoTypes.d.ts} +1 -1
  12. package/dist/general/prices/coinGeckoTypes.d.ts.map +1 -0
  13. package/dist/general/providers/aftermath.d.ts.map +1 -1
  14. package/dist/general/providers/aftermath.js +1 -2
  15. package/dist/general/providers/aftermathApi.d.ts +1 -34
  16. package/dist/general/providers/aftermathApi.d.ts.map +1 -1
  17. package/dist/general/providers/aftermathApi.js +2 -90
  18. package/dist/general/types/castingTypes.d.ts +0 -7
  19. package/dist/general/types/castingTypes.d.ts.map +1 -1
  20. package/dist/general/types/configTypes.d.ts +0 -6
  21. package/dist/general/types/configTypes.d.ts.map +1 -1
  22. package/dist/general/types/generalTypes.d.ts +0 -3
  23. package/dist/general/types/generalTypes.d.ts.map +1 -1
  24. package/dist/general/types/index.d.ts +1 -0
  25. package/dist/general/types/index.d.ts.map +1 -1
  26. package/dist/general/types/index.js +1 -0
  27. package/dist/general/utils/helpers.d.ts +1 -16
  28. package/dist/general/utils/helpers.d.ts.map +1 -1
  29. package/dist/general/utils/helpers.js +2 -32
  30. package/dist/general/utils/index.d.ts +0 -1
  31. package/dist/general/utils/index.d.ts.map +1 -1
  32. package/dist/general/utils/index.js +0 -1
  33. package/dist/packages/coin/api/coinApi.d.ts +1 -12
  34. package/dist/packages/coin/api/coinApi.d.ts.map +1 -1
  35. package/dist/packages/coin/api/coinApi.js +0 -123
  36. package/dist/packages/dca/api/dcaApi.d.ts +1 -28
  37. package/dist/packages/dca/api/dcaApi.d.ts.map +1 -1
  38. package/dist/packages/dca/api/dcaApi.js +0 -172
  39. package/dist/packages/farms/api/farmsApi.d.ts +1 -24
  40. package/dist/packages/farms/api/farmsApi.d.ts.map +1 -1
  41. package/dist/packages/farms/api/farmsApi.js +0 -69
  42. package/dist/packages/farms/api/farmsApiCasting.d.ts +2 -3
  43. package/dist/packages/farms/api/farmsApiCasting.d.ts.map +1 -1
  44. package/dist/packages/farms/api/farmsApiCasting.js +0 -45
  45. package/dist/packages/farms/api/farmsApiCastingTypes.d.ts +0 -9
  46. package/dist/packages/farms/api/farmsApiCastingTypes.d.ts.map +1 -1
  47. package/dist/packages/faucet/api/faucetApi.d.ts +0 -5
  48. package/dist/packages/faucet/api/faucetApi.d.ts.map +1 -1
  49. package/dist/packages/faucet/api/faucetApi.js +2 -29
  50. package/dist/packages/faucet/faucet.d.ts +3 -2
  51. package/dist/packages/faucet/faucet.d.ts.map +1 -1
  52. package/dist/packages/faucet/faucet.js +2 -2
  53. package/dist/packages/leveragedStaking/api/leveragedStakingApi.d.ts.map +1 -1
  54. package/dist/packages/leveragedStaking/api/leveragedStakingApi.js +383 -298
  55. package/dist/packages/nftAmm/nftAmmMarket.d.ts +9 -5
  56. package/dist/packages/nftAmm/nftAmmMarket.d.ts.map +1 -1
  57. package/dist/packages/nftAmm/nftAmmMarket.js +34 -6
  58. package/dist/packages/oracle/api/oracleApi.d.ts +1 -10
  59. package/dist/packages/oracle/api/oracleApi.d.ts.map +1 -1
  60. package/dist/packages/oracle/api/oracleApi.js +0 -38
  61. package/dist/packages/perpetuals/api/perpetualsApi.d.ts +2 -58
  62. package/dist/packages/perpetuals/api/perpetualsApi.d.ts.map +1 -1
  63. package/dist/packages/perpetuals/api/perpetualsApi.js +0 -735
  64. package/dist/packages/pools/api/poolsApi.d.ts +2 -217
  65. package/dist/packages/pools/api/poolsApi.d.ts.map +1 -1
  66. package/dist/packages/pools/api/poolsApi.js +0 -752
  67. package/dist/packages/pools/api/poolsApiCasting.d.ts +3 -15
  68. package/dist/packages/pools/api/poolsApiCasting.d.ts.map +1 -1
  69. package/dist/packages/pools/api/poolsApiCasting.js +0 -86
  70. package/dist/packages/pools/api/poolsApiCastingTypes.d.ts +1 -15
  71. package/dist/packages/pools/api/poolsApiCastingTypes.d.ts.map +1 -1
  72. package/dist/packages/pools/pools.d.ts +4 -1
  73. package/dist/packages/pools/pools.d.ts.map +1 -1
  74. package/dist/packages/pools/pools.js +5 -0
  75. package/dist/packages/router/api/routerApi.d.ts +2 -116
  76. package/dist/packages/router/api/routerApi.d.ts.map +1 -1
  77. package/dist/packages/router/api/routerApi.js +3 -345
  78. package/dist/packages/router/api/routerApiCasting.d.ts +2 -5
  79. package/dist/packages/router/api/routerApiCasting.d.ts.map +1 -1
  80. package/dist/packages/router/api/routerApiCasting.js +0 -104
  81. package/dist/packages/router/api/routerApiCastingTypes.d.ts +1 -13
  82. package/dist/packages/router/api/routerApiCastingTypes.d.ts.map +1 -1
  83. package/dist/packages/router/routerTypes.d.ts +3 -35
  84. package/dist/packages/router/routerTypes.d.ts.map +1 -1
  85. package/dist/packages/staking/api/stakingApi.d.ts +2 -50
  86. package/dist/packages/staking/api/stakingApi.d.ts.map +1 -1
  87. package/dist/packages/staking/api/stakingApi.js +0 -226
  88. package/dist/packages/staking/api/stakingApiCasting.d.ts +2 -4
  89. package/dist/packages/staking/api/stakingApiCasting.d.ts.map +1 -1
  90. package/dist/packages/staking/api/stakingApiCasting.js +0 -21
  91. package/dist/packages/staking/api/stakingApiCastingTypes.d.ts +1 -8
  92. package/dist/packages/staking/api/stakingApiCastingTypes.d.ts.map +1 -1
  93. package/dist/packages/staking/stakingTypes.d.ts +2 -0
  94. package/dist/packages/staking/stakingTypes.d.ts.map +1 -1
  95. package/dist/packages/sui/api/suiApi.d.ts +1 -2
  96. package/dist/packages/sui/api/suiApi.d.ts.map +1 -1
  97. package/dist/packages/sui/api/suiApi.js +0 -3
  98. package/package.json +1 -1
  99. package/dist/general/dynamicGas/dynamicGasApi.d.ts +0 -15
  100. package/dist/general/dynamicGas/dynamicGasApi.d.ts.map +0 -1
  101. package/dist/general/dynamicGas/dynamicGasApi.js +0 -87
  102. package/dist/general/historicalData/historicalDataApi.d.ts +0 -9
  103. package/dist/general/historicalData/historicalDataApi.d.ts.map +0 -1
  104. package/dist/general/historicalData/historicalDataApi.js +0 -13
  105. package/dist/general/historicalData/historicalDataApiInterface.d.ts +0 -12
  106. package/dist/general/historicalData/historicalDataApiInterface.d.ts.map +0 -1
  107. package/dist/general/historicalData/historicalDataApiInterface.js +0 -2
  108. package/dist/general/historicalData/placeholderHistoricalDataApi.d.ts +0 -13
  109. package/dist/general/historicalData/placeholderHistoricalDataApi.d.ts.map +0 -1
  110. package/dist/general/historicalData/placeholderHistoricalDataApi.js +0 -26
  111. package/dist/general/prices/coingecko/coinGeckoApiHelpers.d.ts +0 -22
  112. package/dist/general/prices/coingecko/coinGeckoApiHelpers.d.ts.map +0 -1
  113. package/dist/general/prices/coingecko/coinGeckoApiHelpers.js +0 -179
  114. package/dist/general/prices/coingecko/coinGeckoPricesApi.d.ts +0 -23
  115. package/dist/general/prices/coingecko/coinGeckoPricesApi.d.ts.map +0 -1
  116. package/dist/general/prices/coingecko/coinGeckoPricesApi.js +0 -113
  117. package/dist/general/prices/coingecko/coinGeckoTypes.d.ts.map +0 -1
  118. package/dist/general/prices/placeholder/placeholderPricesApi.d.ts +0 -20
  119. package/dist/general/prices/placeholder/placeholderPricesApi.d.ts.map +0 -1
  120. package/dist/general/prices/placeholder/placeholderPricesApi.js +0 -85
  121. package/dist/general/prices/pricesApiInterface.d.ts +0 -13
  122. package/dist/general/prices/pricesApiInterface.d.ts.map +0 -1
  123. package/dist/general/prices/pricesApiInterface.js +0 -2
  124. package/dist/general/prices/pyth/pythPricesApi.d.ts +0 -2
  125. package/dist/general/prices/pyth/pythPricesApi.d.ts.map +0 -1
  126. package/dist/general/prices/pyth/pythPricesApi.js +0 -52
  127. package/dist/general/prices/pyth/pythPricesApiHelpers.d.ts +0 -13
  128. package/dist/general/prices/pyth/pythPricesApiHelpers.d.ts.map +0 -1
  129. package/dist/general/prices/pyth/pythPricesApiHelpers.js +0 -77
  130. package/dist/general/prices/router/routerPricesApi.d.ts +0 -18
  131. package/dist/general/prices/router/routerPricesApi.d.ts.map +0 -1
  132. package/dist/general/prices/router/routerPricesApi.js +0 -126
  133. package/dist/general/utils/indexerCaller.d.ts +0 -14
  134. package/dist/general/utils/indexerCaller.d.ts.map +0 -1
  135. package/dist/general/utils/indexerCaller.js +0 -129
  136. /package/dist/general/prices/{coingecko/coinGeckoTypes.js → coinGeckoTypes.js} +0 -0
@@ -17,10 +17,7 @@ const utils_1 = require("../../../general/utils");
17
17
  const eventsApiHelpers_1 = require("../../../general/apiHelpers/eventsApiHelpers");
18
18
  const coin_1 = require("../../coin");
19
19
  const sui_1 = require("../../sui");
20
- const __1 = require("..");
21
20
  const dayjs_1 = __importDefault(require("dayjs"));
22
- const duration_1 = __importDefault(require("dayjs/plugin/duration"));
23
- const __2 = require("../..");
24
21
  // import BigNumber from "bignumber.js";
25
22
  // import {
26
23
  // BalanceSheet,
@@ -414,111 +411,126 @@ class LeveragedStakingApi {
414
411
  });
415
412
  // TODO(Kevin): Documentation.
416
413
  this.fetchBuildLeveragedUnstakeTx = (inputs) => __awaiter(this, void 0, void 0, function* () {
417
- if (!this.ScallopProviders)
418
- throw new Error("Scallop providers not set");
419
- const { referrer, walletAddress, leveragedAfSuiPositionId, obligationId, unstakeAmount, desiredUnstakeCoinType, slippage, } = inputs;
420
- const scallopTx = this.ScallopProviders.Builder.createTxBlock();
421
- const tx = scallopTx.txBlock;
422
- tx.setSender(walletAddress);
423
- // i. Set the users referrer address.
424
- if (referrer)
425
- this.Provider.ReferralVault().updateReferrerTx({
426
- // @ts-ignore
427
- tx,
428
- referrer,
429
- });
430
- // ii. Obtain the user's `LeveragedAfSuiPosition`.
431
- const leveragedAfSuiPosition = yield this.fetchLeveragedAfSuiPosition({
432
- leveragedAfSuiPositionId,
433
- obligationId,
434
- });
435
- // iii. Initiate Unstake tx.
436
- const leveragedActionCapId = this.initiateLeverageUnstakeTx({
437
- // @ts-ignore
438
- tx,
439
- leveragedAfSuiPositionId,
440
- unstakeAmount,
441
- });
442
- const afSuiToSuiExchangeRate = yield this.Provider.Staking().fetchAfSuiToSuiExchangeRate();
443
- // iv. Calculate current leverage ratio.
444
- const currentLeverageRatio = __1.LeveragedStaking.calcLeverage({
445
- totalSuiDebt: leveragedAfSuiPosition.suiDebt,
446
- totalAfSuiCollateral: leveragedAfSuiPosition.afSuiCollateral,
447
- afSuiToSuiExchangeRate,
448
- });
449
- let unstakedCoinId;
450
- let unstakedCoinType;
451
- // [Edge Case] Position has no debt.
452
- if (leveragedAfSuiPosition.suiDebt === BigInt(0)) {
453
- // va. Withdraw `unstakeAmount` worth of afSUI collateral.
454
- const [unstakedAfSuiCollateral] = yield this.fetchBuildWithdrawAfSuiCollateralTx(Object.assign(Object.assign({}, inputs), { scallopTx,
455
- leveragedActionCapId, withdrawAmount: unstakeAmount }));
456
- unstakedCoinId = unstakedAfSuiCollateral;
457
- unstakedCoinType = "afsui";
458
- } /* (leveragedAfSuiPosition.suiDebt > BigInt(0)) */
459
- else {
460
- // vb. Decrease the leverage to the desired leverage ratio.
461
- const remainingSuiCoinId = yield this.fetchBuildDecreaseLeverageTx({
462
- scallopTx,
463
- leveragedActionCapId,
464
- leveragedAfSuiPositionId,
465
- obligationId,
466
- totalSuiDebt: leveragedAfSuiPosition.suiDebt,
467
- totalAfSuiCollateral: leveragedAfSuiPosition.afSuiCollateral,
468
- // REVIEW(Kevin): should we be subtracting from here too?
469
- //
470
- // totalAfSuiCollateral:
471
- // inputs.totalAfSuiCollateral - unstakeAmount,
472
- newLeverage: currentLeverageRatio,
473
- baseAfSuiCollateral: inputs.baseAfSuiCollateral - unstakeAmount,
474
- slippage,
475
- });
476
- unstakedCoinId = remainingSuiCoinId;
477
- unstakedCoinType = "sui";
478
- }
479
- // vi. Return the unstaked coin to the user in their desired coin (SUI or afSUI).
480
- if (unstakedCoinType === inputs.desiredUnstakeCoinType) {
481
- // via. Unstaked coin already in desired coin type; no extra work needed.
482
- tx.transferObjects([unstakedCoinId], walletAddress);
483
- }
484
- else if (desiredUnstakeCoinType === "sui") {
485
- // vib. Swap withdrawn afSUI into SUI and return to the user.
486
- const poolObject = yield this.Provider.Pools().fetchPool({
487
- objectId: this.addresses.leveragedStaking.objects.afSuiSuiPoolId,
488
- });
489
- const pool = new __2.Pool(poolObject);
490
- const swappedSuiCoinId = yield this.Provider.Pools().fetchAddTradeTx({
491
- // @ts-ignore
492
- tx,
493
- pool,
494
- coinInAmount: unstakeAmount,
495
- coinInId: unstakedCoinId,
496
- coinInType: this.Provider.Staking().coinTypes.afSui,
497
- coinOutType: coin_1.Coin.constants.suiCoinType,
498
- slippage,
499
- });
500
- tx.transferObjects([swappedSuiCoinId], walletAddress);
501
- } /* if (desiredUnstakeCoinType === "afsui") */
502
- else {
503
- // vic. Stake the withdrawn SUI for afSUI and return to the user.
504
- let [unstakedAfSuiCollateral] = this.Provider.Staking().stakeTx({
505
- // @ts-ignore
506
- tx,
507
- validatorAddress: this.addresses.staking.objects.aftermathValidator,
508
- suiCoin: unstakedCoinId,
509
- });
510
- tx.transferObjects([unstakedAfSuiCollateral], walletAddress);
511
- }
512
- // vii. Complete Unstake tx.
513
- this.completeActionTx({
514
- // @ts-ignore
515
- tx,
516
- leveragedActionCapId,
517
- leveragedAfSuiPositionId,
518
- obligationId,
519
- });
520
- // @ts-ignore
521
- return tx;
414
+ throw new Error("TODO");
415
+ // if (!this.ScallopProviders)
416
+ // throw new Error("Scallop providers not set");
417
+ // const {
418
+ // referrer,
419
+ // walletAddress,
420
+ // leveragedAfSuiPositionId,
421
+ // obligationId,
422
+ // unstakeAmount,
423
+ // desiredUnstakeCoinType,
424
+ // slippage,
425
+ // } = inputs;
426
+ // const scallopTx = this.ScallopProviders.Builder.createTxBlock();
427
+ // const tx = scallopTx.txBlock;
428
+ // tx.setSender(walletAddress);
429
+ // // i. Set the users referrer address.
430
+ // if (referrer)
431
+ // this.Provider.ReferralVault().updateReferrerTx({
432
+ // // @ts-ignore
433
+ // tx,
434
+ // referrer,
435
+ // });
436
+ // // ii. Obtain the user's `LeveragedAfSuiPosition`.
437
+ // const leveragedAfSuiPosition = await this.fetchLeveragedAfSuiPosition({
438
+ // leveragedAfSuiPositionId,
439
+ // obligationId,
440
+ // });
441
+ // // iii. Initiate Unstake tx.
442
+ // const leveragedActionCapId = this.initiateLeverageUnstakeTx({
443
+ // // @ts-ignore
444
+ // tx,
445
+ // leveragedAfSuiPositionId,
446
+ // unstakeAmount,
447
+ // });
448
+ // const afSuiToSuiExchangeRate =
449
+ // await this.Provider.Staking().fetchAfSuiToSuiExchangeRate();
450
+ // // iv. Calculate current leverage ratio.
451
+ // const currentLeverageRatio = LeveragedStaking.calcLeverage({
452
+ // totalSuiDebt: leveragedAfSuiPosition.suiDebt,
453
+ // totalAfSuiCollateral: leveragedAfSuiPosition.afSuiCollateral,
454
+ // afSuiToSuiExchangeRate,
455
+ // });
456
+ // let unstakedCoinId;
457
+ // let unstakedCoinType;
458
+ // // [Edge Case] Position has no debt.
459
+ // if (leveragedAfSuiPosition.suiDebt === BigInt(0)) {
460
+ // // va. Withdraw `unstakeAmount` worth of afSUI collateral.
461
+ // const [unstakedAfSuiCollateral] =
462
+ // await this.fetchBuildWithdrawAfSuiCollateralTx({
463
+ // ...inputs,
464
+ // scallopTx,
465
+ // leveragedActionCapId,
466
+ // withdrawAmount: unstakeAmount,
467
+ // });
468
+ // unstakedCoinId = unstakedAfSuiCollateral;
469
+ // unstakedCoinType = "afsui";
470
+ // } /* (leveragedAfSuiPosition.suiDebt > BigInt(0)) */ else {
471
+ // // vb. Decrease the leverage to the desired leverage ratio.
472
+ // const remainingSuiCoinId = await this.fetchBuildDecreaseLeverageTx({
473
+ // scallopTx,
474
+ // leveragedActionCapId,
475
+ // leveragedAfSuiPositionId,
476
+ // obligationId,
477
+ // totalSuiDebt: leveragedAfSuiPosition.suiDebt,
478
+ // totalAfSuiCollateral: leveragedAfSuiPosition.afSuiCollateral,
479
+ // // REVIEW(Kevin): should we be subtracting from here too?
480
+ // //
481
+ // // totalAfSuiCollateral:
482
+ // // inputs.totalAfSuiCollateral - unstakeAmount,
483
+ // newLeverage: currentLeverageRatio,
484
+ // baseAfSuiCollateral: inputs.baseAfSuiCollateral - unstakeAmount,
485
+ // slippage,
486
+ // });
487
+ // unstakedCoinId = remainingSuiCoinId;
488
+ // unstakedCoinType = "sui";
489
+ // }
490
+ // // vi. Return the unstaked coin to the user in their desired coin (SUI or afSUI).
491
+ // if (unstakedCoinType === inputs.desiredUnstakeCoinType) {
492
+ // // via. Unstaked coin already in desired coin type; no extra work needed.
493
+ // tx.transferObjects([unstakedCoinId], walletAddress);
494
+ // } else if (desiredUnstakeCoinType === "sui") {
495
+ // // vib. Swap withdrawn afSUI into SUI and return to the user.
496
+ // const poolObject = await this.Provider.Pools().fetchPool({
497
+ // objectId:
498
+ // this.addresses.leveragedStaking.objects.afSuiSuiPoolId,
499
+ // });
500
+ // const pool = new Pool(poolObject);
501
+ // const swappedSuiCoinId =
502
+ // await this.Provider.Pools().fetchAddTradeTx({
503
+ // // @ts-ignore
504
+ // tx,
505
+ // pool,
506
+ // coinInAmount: unstakeAmount,
507
+ // coinInId: unstakedCoinId,
508
+ // coinInType: this.Provider.Staking().coinTypes.afSui,
509
+ // coinOutType: Coin.constants.suiCoinType,
510
+ // slippage,
511
+ // });
512
+ // tx.transferObjects([swappedSuiCoinId], walletAddress);
513
+ // } /* if (desiredUnstakeCoinType === "afsui") */ else {
514
+ // // vic. Stake the withdrawn SUI for afSUI and return to the user.
515
+ // let [unstakedAfSuiCollateral] = this.Provider.Staking().stakeTx({
516
+ // // @ts-ignore
517
+ // tx,
518
+ // validatorAddress:
519
+ // this.addresses.staking.objects.aftermathValidator,
520
+ // suiCoin: unstakedCoinId,
521
+ // });
522
+ // tx.transferObjects([unstakedAfSuiCollateral], walletAddress);
523
+ // }
524
+ // // vii. Complete Unstake tx.
525
+ // this.completeActionTx({
526
+ // // @ts-ignore
527
+ // tx,
528
+ // leveragedActionCapId,
529
+ // leveragedAfSuiPositionId,
530
+ // obligationId,
531
+ // });
532
+ // // @ts-ignore
533
+ // return tx;
522
534
  });
523
535
  // TODO(Kevin): Documentation.
524
536
  this.fetchBuildChangeLeverageTx = (inputs) => __awaiter(this, void 0, void 0, function* () {
@@ -571,59 +583,83 @@ class LeveragedStakingApi {
571
583
  });
572
584
  // TODO(Kevin): Documentation.
573
585
  this.fetchBuildIncreaseLeverageTx = (inputs) => __awaiter(this, void 0, void 0, function* () {
574
- const { scallopTx } = inputs;
575
- const tx = scallopTx.txBlock;
576
- const afSuiToSuiExchangeRate = yield this.Provider.Staking().fetchAfSuiToSuiExchangeRate();
577
- const newTotalAfSuiCollateral = BigInt(Math.floor(Number(inputs.baseAfSuiCollateral) * inputs.newLeverage));
578
- // ia. Calculate the extra amount of afSUI collateral that must be deposited to reach the new
579
- // desired leverage.
580
- const increaseInTotalAfSuiCollateral = newTotalAfSuiCollateral - inputs.totalAfSuiCollateral;
581
- // ib. Calculate amount of SUI that must be flash loaned to account for
582
- // `increaseInAfSuiCollateral`.
583
- const flashLoanAmount = BigInt(Math.floor(Number(increaseInTotalAfSuiCollateral) * afSuiToSuiExchangeRate));
584
- // ii. Flash loan the required amount of SUI from Scallop to increase the position by
585
- // `increaseInAfSuiCollateral` afSUI.
586
- const [flashLoanedSuiCoinId, loan] = scallopTx.borrowFlashLoan(flashLoanAmount, "sui");
587
- const { afSuiCoinId } = yield this.swapOrStakeSuiToAfSui({
588
- // @ts-ignore
589
- tx,
590
- suiAmount: flashLoanAmount,
591
- suiCoinId: flashLoanedSuiCoinId,
592
- });
593
- // iv. Deposit the staked afSUI as collateral on Scallop.
594
- this.depositAfSuiCollateralTx(Object.assign(Object.assign({}, inputs), {
595
- // @ts-ignore
596
- tx,
597
- afSuiCoinId, obligationId: inputs.obligationId }));
598
- // REVIEW(Kevin): check if both assets need to be updated.
599
- //
600
- yield scallopTx.updateAssetPricesQuick(["sui", "afsui"]);
601
- // v. Borrow amount of SUI required to pay off flash loan.
602
- const [borrowedSuiCoinId] = this.borrowSuiTx(Object.assign(Object.assign({}, inputs), {
603
- // @ts-ignore
604
- tx, borrowAmount: flashLoanAmount + BigInt(/*0__0*/ 50000000) }));
605
- const repayLoanSuiCoinId = tx.splitCoins(borrowedSuiCoinId, [
606
- flashLoanAmount,
607
- ]);
608
- // vi. Repay flash loan on Scallop.
609
- scallopTx.repayFlashLoan(
610
- // flashLoanedSuiCoinId,
611
- repayLoanSuiCoinId,
612
- // borrowedSuiCoinId,
613
- loan, "sui");
614
- // Leftover SUI is used to repay SUI debt.
615
- this.repaySuiTx(Object.assign(Object.assign({}, inputs), {
616
- // @ts-ignore
617
- tx, suiCoinId: borrowedSuiCoinId }));
618
- // REVIEW(kevin): will there even be any leftover SUI to repay?
619
- //
620
- // // vii. [Potentially] Use remaining SUI to repay debt.
586
+ throw new Error("TODO");
587
+ // const { scallopTx } = inputs;
588
+ // const tx = scallopTx.txBlock;
589
+ // const afSuiToSuiExchangeRate =
590
+ // await this.Provider.Staking().fetchAfSuiToSuiExchangeRate();
591
+ // const newTotalAfSuiCollateral = BigInt(
592
+ // Math.floor(Number(inputs.baseAfSuiCollateral) * inputs.newLeverage)
593
+ // );
594
+ // // ia. Calculate the extra amount of afSUI collateral that must be deposited to reach the new
595
+ // // desired leverage.
596
+ // const increaseInTotalAfSuiCollateral =
597
+ // newTotalAfSuiCollateral - inputs.totalAfSuiCollateral;
598
+ // // ib. Calculate amount of SUI that must be flash loaned to account for
599
+ // // `increaseInAfSuiCollateral`.
600
+ // const flashLoanAmount: Balance = BigInt(
601
+ // Math.floor(
602
+ // Number(increaseInTotalAfSuiCollateral) * afSuiToSuiExchangeRate
603
+ // )
604
+ // );
605
+ // // ii. Flash loan the required amount of SUI from Scallop to increase the position by
606
+ // // `increaseInAfSuiCollateral` afSUI.
607
+ // const [flashLoanedSuiCoinId, loan] = scallopTx.borrowFlashLoan(
608
+ // flashLoanAmount,
609
+ // "sui"
610
+ // );
611
+ // const { afSuiCoinId } = await this.swapOrStakeSuiToAfSui({
612
+ // // @ts-ignore
613
+ // tx,
614
+ // suiAmount: flashLoanAmount,
615
+ // suiCoinId: flashLoanedSuiCoinId,
616
+ // });
617
+ // // iv. Deposit the staked afSUI as collateral on Scallop.
618
+ // this.depositAfSuiCollateralTx({
619
+ // ...inputs,
620
+ // // @ts-ignore
621
+ // tx,
622
+ // afSuiCoinId,
623
+ // obligationId: inputs.obligationId,
624
+ // });
625
+ // // REVIEW(Kevin): check if both assets need to be updated.
626
+ // //
627
+ // await scallopTx.updateAssetPricesQuick(["sui", "afsui"]);
628
+ // // v. Borrow amount of SUI required to pay off flash loan.
629
+ // const [borrowedSuiCoinId] = this.borrowSuiTx({
630
+ // ...inputs,
631
+ // // @ts-ignore
632
+ // tx,
633
+ // borrowAmount: flashLoanAmount + BigInt(/*0__0*/ 50_000_000),
634
+ // // borrowAmount: flashLoanAmount,
635
+ // });
636
+ // const repayLoanSuiCoinId = tx.splitCoins(borrowedSuiCoinId, [
637
+ // flashLoanAmount,
638
+ // ]);
639
+ // // vi. Repay flash loan on Scallop.
640
+ // scallopTx.repayFlashLoan(
641
+ // // flashLoanedSuiCoinId,
642
+ // repayLoanSuiCoinId,
643
+ // // borrowedSuiCoinId,
644
+ // loan,
645
+ // "sui"
646
+ // );
647
+ // // Leftover SUI is used to repay SUI debt.
621
648
  // this.repaySuiTx({
649
+ // ...inputs,
650
+ // // @ts-ignore
622
651
  // tx,
623
- // leveragedActionCapId,
624
- // obligationId,
625
652
  // suiCoinId: borrowedSuiCoinId,
626
653
  // });
654
+ // // REVIEW(kevin): will there even be any leftover SUI to repay?
655
+ // //
656
+ // // // vii. [Potentially] Use remaining SUI to repay debt.
657
+ // // this.repaySuiTx({
658
+ // // tx,
659
+ // // leveragedActionCapId,
660
+ // // obligationId,
661
+ // // suiCoinId: borrowedSuiCoinId,
662
+ // // });
627
663
  });
628
664
  // TODO(Kevin): Documentation.
629
665
  //
@@ -636,100 +672,134 @@ class LeveragedStakingApi {
636
672
  // 5. Convert afSUI to SUI.
637
673
  // 6. Repay flash loan.
638
674
  this.fetchBuildDecreaseLeverageTx = (inputs) => __awaiter(this, void 0, void 0, function* () {
639
- const { scallopTx, slippage } = inputs;
640
- const tx = scallopTx.txBlock;
641
- const afSuiToSuiExchangeRate = yield this.Provider.Staking().fetchAfSuiToSuiExchangeRate();
642
- let decreaseInAfSuiCollateral;
643
- let decreaseInSuiDebt;
644
- // [Edge Case] User wants to unstake their entire position.
645
- if (inputs.baseAfSuiCollateral === BigInt(0)) {
646
- // TODO: [edge case] handle closing of position.
647
- //
648
- decreaseInAfSuiCollateral = BigInt(inputs.totalAfSuiCollateral);
649
- // decreaseInAfSuiCollateral = inputs.totalAfSuiCollateral;
650
- decreaseInSuiDebt = inputs.totalSuiDebt;
651
- }
652
- else {
653
- // ia. Calculate the amount of afSUI collateral that must be withdrawn to reach
654
- // a leverage ratio of `newLeverage`.
655
- const newTotalAfSuiCollateral = BigInt(Math.floor(Number(inputs.baseAfSuiCollateral) * inputs.newLeverage));
656
- decreaseInAfSuiCollateral =
657
- inputs.totalAfSuiCollateral - newTotalAfSuiCollateral;
658
- // ib. Calculate the amount of SUI debt that must be repayed to allow withdrawing
659
- // `decreaseInAfSuiCollateral` worth of afSUI collateral.
660
- const newSuiDebt = BigInt(Math.floor(Number(newTotalAfSuiCollateral - inputs.baseAfSuiCollateral) * afSuiToSuiExchangeRate));
661
- decreaseInSuiDebt = inputs.totalSuiDebt - newSuiDebt;
662
- }
663
- // ii. Flash loan `decreaseInSuiDebt` worth of SUI from Scallop.
664
- const [flashLoanedSuiCoinId, loan] = scallopTx.borrowFlashLoan(decreaseInSuiDebt, "sui");
665
- // iii. Repay `decreaseInSuiDebt` of SUI debt.
666
- this.repaySuiTx(Object.assign(Object.assign({}, inputs), {
667
- // @ts-ignore
668
- tx, suiCoinId: flashLoanedSuiCoinId }));
669
- // iv. Withdraw `decreaseInCollateralAmount` worth of afSUI collateral.
670
- const [afSuiId] = yield this.fetchBuildWithdrawAfSuiCollateralTx(Object.assign(Object.assign({}, inputs), { withdrawAmount: decreaseInAfSuiCollateral }));
671
- // v. Convert `decreaseInCollateralAmount` of withdrawn collateral into SUI.
672
- const poolObject = yield this.Provider.Pools().fetchPool({
673
- objectId: this.addresses.leveragedStaking.objects.afSuiSuiPoolId,
674
- });
675
- const pool = new __2.Pool(poolObject);
676
- const swappedSuiCoinId = yield this.Provider.Pools().fetchAddTradeTx({
677
- // @ts-ignore
678
- tx,
679
- pool,
680
- coinInAmount: BigInt(Math.floor(Number(decreaseInSuiDebt) * afSuiToSuiExchangeRate)),
681
- coinInId: afSuiId,
682
- coinInType: this.Provider.Staking().coinTypes.afSui,
683
- coinOutType: coin_1.Coin.constants.suiCoinType,
684
- slippage,
685
- });
686
- const repayLoanSuiCoinId = tx.splitCoins(swappedSuiCoinId, [
687
- decreaseInSuiDebt,
688
- ]);
689
- // vi. Repay flash loan with converted SUI.
690
- scallopTx.repayFlashLoan(repayLoanSuiCoinId, loan, "sui");
691
- return swappedSuiCoinId;
675
+ throw new Error("TODO");
676
+ // const { scallopTx, slippage } = inputs;
677
+ // const tx = scallopTx.txBlock;
678
+ // const afSuiToSuiExchangeRate =
679
+ // await this.Provider.Staking().fetchAfSuiToSuiExchangeRate();
680
+ // let decreaseInAfSuiCollateral;
681
+ // let decreaseInSuiDebt;
682
+ // // [Edge Case] User wants to unstake their entire position.
683
+ // if (inputs.baseAfSuiCollateral === BigInt(0)) {
684
+ // // TODO: [edge case] handle closing of position.
685
+ // //
686
+ // decreaseInAfSuiCollateral = BigInt(inputs.totalAfSuiCollateral);
687
+ // // decreaseInAfSuiCollateral = inputs.totalAfSuiCollateral;
688
+ // decreaseInSuiDebt = inputs.totalSuiDebt;
689
+ // } else {
690
+ // // ia. Calculate the amount of afSUI collateral that must be withdrawn to reach
691
+ // // a leverage ratio of `newLeverage`.
692
+ // const newTotalAfSuiCollateral = BigInt(
693
+ // Math.floor(
694
+ // Number(inputs.baseAfSuiCollateral) * inputs.newLeverage
695
+ // )
696
+ // );
697
+ // decreaseInAfSuiCollateral =
698
+ // inputs.totalAfSuiCollateral - newTotalAfSuiCollateral;
699
+ // // ib. Calculate the amount of SUI debt that must be repayed to allow withdrawing
700
+ // // `decreaseInAfSuiCollateral` worth of afSUI collateral.
701
+ // const newSuiDebt = BigInt(
702
+ // Math.floor(
703
+ // Number(
704
+ // newTotalAfSuiCollateral - inputs.baseAfSuiCollateral
705
+ // ) * afSuiToSuiExchangeRate
706
+ // )
707
+ // );
708
+ // decreaseInSuiDebt = inputs.totalSuiDebt - newSuiDebt;
709
+ // }
710
+ // // ii. Flash loan `decreaseInSuiDebt` worth of SUI from Scallop.
711
+ // const [flashLoanedSuiCoinId, loan] = scallopTx.borrowFlashLoan(
712
+ // decreaseInSuiDebt,
713
+ // "sui"
714
+ // );
715
+ // // iii. Repay `decreaseInSuiDebt` of SUI debt.
716
+ // this.repaySuiTx({
717
+ // ...inputs,
718
+ // // @ts-ignore
719
+ // tx,
720
+ // suiCoinId: flashLoanedSuiCoinId,
721
+ // });
722
+ // // iv. Withdraw `decreaseInCollateralAmount` worth of afSUI collateral.
723
+ // const [afSuiId] = await this.fetchBuildWithdrawAfSuiCollateralTx({
724
+ // ...inputs,
725
+ // withdrawAmount: decreaseInAfSuiCollateral,
726
+ // });
727
+ // // v. Convert `decreaseInCollateralAmount` of withdrawn collateral into SUI.
728
+ // const poolObject = await this.Provider.Pools().fetchPool({
729
+ // objectId: this.addresses.leveragedStaking.objects.afSuiSuiPoolId,
730
+ // });
731
+ // const pool = new Pool(poolObject);
732
+ // const swappedSuiCoinId = await this.Provider.Pools().fetchAddTradeTx({
733
+ // // @ts-ignore
734
+ // tx,
735
+ // pool,
736
+ // coinInAmount: BigInt(
737
+ // Math.floor(Number(decreaseInSuiDebt) * afSuiToSuiExchangeRate)
738
+ // ),
739
+ // coinInId: afSuiId,
740
+ // coinInType: this.Provider.Staking().coinTypes.afSui,
741
+ // coinOutType: Coin.constants.suiCoinType,
742
+ // slippage,
743
+ // });
744
+ // const repayLoanSuiCoinId = tx.splitCoins(swappedSuiCoinId, [
745
+ // decreaseInSuiDebt,
746
+ // ]);
747
+ // // vi. Repay flash loan with converted SUI.
748
+ // scallopTx.repayFlashLoan(repayLoanSuiCoinId, loan, "sui");
749
+ // return swappedSuiCoinId;
692
750
  });
693
751
  // =========================================================================
694
752
  // Helpers
695
753
  // =========================================================================
696
754
  this.swapOrStakeSuiToAfSui = (inputs) => __awaiter(this, void 0, void 0, function* () {
697
- const { tx, suiAmount, suiCoinId } = inputs;
698
- const estimatedSlippageLowerBound = 0.0001; // 0.01%
699
- let afSuiCoinId;
700
- let minAmountOut;
701
- if (suiAmount >= __2.Staking.constants.bounds.minStake) {
702
- // Stake SUI into afSUI.
703
- afSuiCoinId = this.Provider.Staking().stakeTx({
704
- tx,
705
- validatorAddress: this.addresses.staking.objects.aftermathValidator,
706
- suiCoin: suiCoinId,
707
- });
708
- const afSuiToSuiExchangeRate = yield this.Provider.Staking().fetchAfSuiToSuiExchangeRate();
709
- minAmountOut = BigInt(Math.floor(Number(suiAmount) * afSuiToSuiExchangeRate));
710
- }
711
- else {
712
- const poolObject = yield this.Provider.Pools().fetchPool({
713
- objectId: this.addresses.leveragedStaking.objects.afSuiSuiPoolId,
714
- });
715
- const pool = new __2.Pool(poolObject);
716
- minAmountOut = BigInt(Math.floor(Number(pool.getTradeAmountOut({
717
- coinInAmount: suiAmount,
718
- coinInType: coin_1.Coin.constants.suiCoinType,
719
- coinOutType: this.Provider.Staking().coinTypes.afSui,
720
- })) *
721
- (1 - estimatedSlippageLowerBound)));
722
- afSuiCoinId = yield this.Provider.Pools().fetchAddTradeTx({
723
- tx,
724
- pool,
725
- slippage: 1,
726
- coinInAmount: suiAmount,
727
- coinInId: suiCoinId,
728
- coinInType: coin_1.Coin.constants.suiCoinType,
729
- coinOutType: this.Provider.Staking().coinTypes.afSui,
730
- });
731
- }
732
- return { afSuiCoinId, minAmountOut };
755
+ throw new Error("TODO");
756
+ // const { tx, suiAmount, suiCoinId } = inputs;
757
+ // const estimatedSlippageLowerBound = 0.0001; // 0.01%
758
+ // let afSuiCoinId: TransactionObjectArgument;
759
+ // let minAmountOut: Balance;
760
+ // if (suiAmount >= Staking.constants.bounds.minStake) {
761
+ // // Stake SUI into afSUI.
762
+ // afSuiCoinId = this.Provider.Staking().stakeTx({
763
+ // tx,
764
+ // validatorAddress:
765
+ // this.addresses.staking.objects.aftermathValidator,
766
+ // suiCoin: suiCoinId,
767
+ // });
768
+ // const afSuiToSuiExchangeRate =
769
+ // await this.Provider.Staking().fetchAfSuiToSuiExchangeRate();
770
+ // minAmountOut = BigInt(
771
+ // Math.floor(Number(suiAmount) * afSuiToSuiExchangeRate)
772
+ // );
773
+ // } else {
774
+ // const poolObject = await this.Provider.Pools().fetchPool({
775
+ // objectId:
776
+ // this.addresses.leveragedStaking.objects.afSuiSuiPoolId,
777
+ // });
778
+ // const pool = new Pool(poolObject);
779
+ // minAmountOut = BigInt(
780
+ // Math.floor(
781
+ // Number(
782
+ // pool.getTradeAmountOut({
783
+ // coinInAmount: suiAmount,
784
+ // coinInType: Coin.constants.suiCoinType,
785
+ // coinOutType:
786
+ // this.Provider.Staking().coinTypes.afSui,
787
+ // })
788
+ // ) *
789
+ // (1 - estimatedSlippageLowerBound)
790
+ // )
791
+ // );
792
+ // afSuiCoinId = await this.Provider.Pools().fetchAddTradeTx({
793
+ // tx,
794
+ // pool,
795
+ // slippage: 1, // 100%
796
+ // coinInAmount: suiAmount,
797
+ // coinInId: suiCoinId,
798
+ // coinInType: Coin.constants.suiCoinType,
799
+ // coinOutType: this.Provider.Staking().coinTypes.afSui,
800
+ // });
801
+ // }
802
+ // return { afSuiCoinId, minAmountOut };
733
803
  });
734
804
  // REVIEW: this is only needed if a user's Obligation can have its debt increased by another address.
735
805
  // If that isn't possible, then obligation's debt amount + borrow index will always be accurate after
@@ -793,18 +863,29 @@ class LeveragedStakingApi {
793
863
  */
794
864
  fetchEventsForUser(inputs) {
795
865
  return __awaiter(this, void 0, void 0, function* () {
796
- const { walletAddress, cursor, limit } = inputs;
797
- return this.Provider.indexerCaller.fetchIndexerEvents(`leveraged-staking/${walletAddress}/events`, {
798
- cursor,
799
- limit,
800
- }, (event) => {
801
- const eventType = event.type;
802
- return eventType.includes(this.eventTypes.leveragedStaked)
803
- ? utils_1.Casting.leveragedStaking.leveragedStakedEventFromOnChain(event)
804
- : eventType.includes(this.eventTypes.leveragedUnstaked)
805
- ? utils_1.Casting.leveragedStaking.leveragedUnstakedEventFromOnChain(event)
806
- : utils_1.Casting.leveragedStaking.leveragedStakeChangedEventFromOnChain(event);
807
- });
866
+ throw new Error("TODO");
867
+ // const { walletAddress, cursor, limit } = inputs;
868
+ // return this.Provider.indexerCaller.fetchIndexerEvents(
869
+ // `leveraged-staking/${walletAddress}/events`,
870
+ // {
871
+ // cursor,
872
+ // limit,
873
+ // },
874
+ // (event) => {
875
+ // const eventType = (event as EventOnChain<any>).type;
876
+ // return eventType.includes(this.eventTypes.leveragedStaked)
877
+ // ? Casting.leveragedStaking.leveragedStakedEventFromOnChain(
878
+ // event as LeveragedStakedEventOnChain
879
+ // )
880
+ // : eventType.includes(this.eventTypes.leveragedUnstaked)
881
+ // ? Casting.leveragedStaking.leveragedUnstakedEventFromOnChain(
882
+ // event as LeveragedUnstakedEventOnChain
883
+ // )
884
+ // : Casting.leveragedStaking.leveragedStakeChangedEventFromOnChain(
885
+ // event as LeveragedStakeChangedLeverageEventOnChain
886
+ // );
887
+ // }
888
+ // );
808
889
  });
809
890
  }
810
891
  // =========================================================================
@@ -817,46 +898,50 @@ class LeveragedStakingApi {
817
898
  */
818
899
  fetchPerformanceData(inputs) {
819
900
  return __awaiter(this, void 0, void 0, function* () {
820
- const { timeframe, borrowRate, maxLeverage } = inputs;
821
- dayjs_1.default.extend(duration_1.default);
822
- const limit = // days ~ epochs
823
- dayjs_1.default
824
- .duration(LeveragedStakingApi.dataTimeframesToDays[timeframe], "days")
825
- // + 2 to account for apy being calculated from events delta
826
- // (and possible initial 0 afsui supply)
827
- .asDays() + 2;
828
- // TODO: fetch borrow rate historically once scallop implements
829
- const [recentEpochChanges] = yield Promise.all([
830
- this.Provider.Staking().fetchEpochWasChangedEvents({
831
- limit,
832
- }),
833
- ]);
834
- if (recentEpochChanges.events.length <= 2)
835
- return [];
836
- const daysInYear = 365;
837
- const timeData = recentEpochChanges.events
838
- .slice(2)
839
- .map((event, index) => {
840
- var _a;
841
- const currentRate = Number(event.totalAfSuiSupply)
842
- ? Number(event.totalSuiAmount) /
843
- Number(event.totalAfSuiSupply)
844
- : 1;
845
- const pastEvent = recentEpochChanges.events[index + 1];
846
- const pastRate = Number(pastEvent.totalAfSuiSupply)
847
- ? Number(pastEvent.totalSuiAmount) /
848
- Number(pastEvent.totalAfSuiSupply)
849
- : 1;
850
- const afSuiApy = ((currentRate - pastRate) / pastRate) * daysInYear;
851
- return {
852
- time: (_a = event.timestamp) !== null && _a !== void 0 ? _a : 0,
853
- sui: 0,
854
- afSui: afSuiApy,
855
- leveragedAfSui: maxLeverage *
856
- (afSuiApy - borrowRate * (1 - 1 / maxLeverage)),
857
- };
858
- });
859
- return timeData;
901
+ // const { timeframe, borrowRate, maxLeverage } = inputs;
902
+ // dayjs.extend(duration);
903
+ // const limit = // days ~ epochs
904
+ // dayjs
905
+ // .duration(
906
+ // LeveragedStakingApi.dataTimeframesToDays[timeframe],
907
+ // "days"
908
+ // )
909
+ // // + 2 to account for apy being calculated from events delta
910
+ // // (and possible initial 0 afsui supply)
911
+ // .asDays() + 2;
912
+ // // TODO: fetch borrow rate historically once scallop implements
913
+ // const [recentEpochChanges] = await Promise.all([
914
+ // this.Provider.Staking().fetchEpochWasChangedEvents({
915
+ // limit,
916
+ // }),
917
+ // ]);
918
+ // if (recentEpochChanges.events.length <= 2) return [];
919
+ // const daysInYear = 365;
920
+ // const timeData = recentEpochChanges.events
921
+ // .slice(2)
922
+ // .map((event, index) => {
923
+ // const currentRate = Number(event.totalAfSuiSupply)
924
+ // ? Number(event.totalSuiAmount) /
925
+ // Number(event.totalAfSuiSupply)
926
+ // : 1;
927
+ // const pastEvent = recentEpochChanges.events[index + 1];
928
+ // const pastRate = Number(pastEvent.totalAfSuiSupply)
929
+ // ? Number(pastEvent.totalSuiAmount) /
930
+ // Number(pastEvent.totalAfSuiSupply)
931
+ // : 1;
932
+ // const afSuiApy =
933
+ // ((currentRate - pastRate) / pastRate) * daysInYear;
934
+ // return {
935
+ // time: event.timestamp ?? 0,
936
+ // sui: 0,
937
+ // afSui: afSuiApy,
938
+ // leveragedAfSui:
939
+ // maxLeverage *
940
+ // (afSuiApy - borrowRate * (1 - 1 / maxLeverage)),
941
+ // };
942
+ // });
943
+ // return timeData;
944
+ throw new Error("not implemented");
860
945
  });
861
946
  }
862
947
  // NOTE: ported from Scallop's SDK.