aftermath-ts-sdk 1.2.51 → 1.2.52-perps.3

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 (214) hide show
  1. package/README.md +62 -28
  2. package/dist/general/apiHelpers/eventsApiHelpers.d.ts +1 -1
  3. package/dist/general/apiHelpers/eventsApiHelpers.d.ts.map +1 -1
  4. package/dist/general/apiHelpers/eventsApiHelpers.js +4 -1
  5. package/dist/general/apiHelpers/transactionsApiHelpers.d.ts +6 -0
  6. package/dist/general/apiHelpers/transactionsApiHelpers.d.ts.map +1 -1
  7. package/dist/general/apiHelpers/transactionsApiHelpers.js +46 -0
  8. package/dist/general/dynamicGas/dynamicGas.d.ts +34 -0
  9. package/dist/general/dynamicGas/dynamicGas.d.ts.map +1 -1
  10. package/dist/general/dynamicGas/dynamicGas.js +34 -0
  11. package/dist/general/dynamicGas/dynamicGasTypes.d.ts +25 -0
  12. package/dist/general/dynamicGas/dynamicGasTypes.d.ts.map +1 -1
  13. package/dist/general/prices/coinGeckoTypes.d.ts +2 -0
  14. package/dist/general/prices/coinGeckoTypes.d.ts.map +1 -1
  15. package/dist/general/prices/prices.d.ts +73 -0
  16. package/dist/general/prices/prices.d.ts.map +1 -1
  17. package/dist/general/prices/prices.js +73 -0
  18. package/dist/general/providers/aftermath.d.ts +107 -27
  19. package/dist/general/providers/aftermath.d.ts.map +1 -1
  20. package/dist/general/providers/aftermath.js +110 -30
  21. package/dist/general/providers/aftermathApi.d.ts +128 -10
  22. package/dist/general/providers/aftermathApi.d.ts.map +1 -1
  23. package/dist/general/providers/aftermathApi.js +133 -14
  24. package/dist/general/types/castingTypes.d.ts +15 -7
  25. package/dist/general/types/castingTypes.d.ts.map +1 -1
  26. package/dist/general/types/castingTypes.js +6 -0
  27. package/dist/general/types/configTypes.d.ts +14 -8
  28. package/dist/general/types/configTypes.d.ts.map +1 -1
  29. package/dist/general/types/generalTypes.d.ts +229 -7
  30. package/dist/general/types/generalTypes.d.ts.map +1 -1
  31. package/dist/general/types/moveErrorsInterface.d.ts +1 -1
  32. package/dist/general/types/moveErrorsInterface.d.ts.map +1 -1
  33. package/dist/general/types/suiTypes.d.ts +0 -16
  34. package/dist/general/types/suiTypes.d.ts.map +1 -1
  35. package/dist/general/types/suiTypes.js +3 -25
  36. package/dist/general/utils/caller.d.ts +6 -3
  37. package/dist/general/utils/caller.d.ts.map +1 -1
  38. package/dist/general/utils/caller.js +22 -4
  39. package/dist/general/utils/casting.d.ts +156 -5
  40. package/dist/general/utils/casting.d.ts.map +1 -1
  41. package/dist/general/utils/casting.js +159 -18
  42. package/dist/general/utils/fixedUtils.d.ts +89 -2
  43. package/dist/general/utils/fixedUtils.d.ts.map +1 -1
  44. package/dist/general/utils/fixedUtils.js +81 -4
  45. package/dist/general/utils/helpers.d.ts +327 -10
  46. package/dist/general/utils/helpers.d.ts.map +1 -1
  47. package/dist/general/utils/helpers.js +409 -91
  48. package/dist/general/utils/iFixedUtils.d.ts +64 -0
  49. package/dist/general/utils/iFixedUtils.d.ts.map +1 -1
  50. package/dist/general/utils/iFixedUtils.js +64 -0
  51. package/dist/general/wallet/wallet.d.ts +76 -1
  52. package/dist/general/wallet/wallet.d.ts.map +1 -1
  53. package/dist/general/wallet/wallet.js +75 -5
  54. package/dist/packages/auth/auth.d.ts +113 -4
  55. package/dist/packages/auth/auth.d.ts.map +1 -1
  56. package/dist/packages/auth/auth.js +190 -59
  57. package/dist/packages/auth/authTypes.d.ts +66 -0
  58. package/dist/packages/auth/authTypes.d.ts.map +1 -1
  59. package/dist/packages/coin/api/coinApi.d.ts.map +1 -1
  60. package/dist/packages/coin/api/coinApi.js +29 -20
  61. package/dist/packages/coin/coin.d.ts +256 -1
  62. package/dist/packages/coin/coin.d.ts.map +1 -1
  63. package/dist/packages/coin/coin.js +246 -15
  64. package/dist/packages/coin/coinTypes.d.ts +95 -0
  65. package/dist/packages/coin/coinTypes.d.ts.map +1 -1
  66. package/dist/packages/dca/api/dcaApi.d.ts +2 -0
  67. package/dist/packages/dca/api/dcaApi.d.ts.map +1 -1
  68. package/dist/packages/dca/api/dcaApi.js +6 -3
  69. package/dist/packages/dca/dca.d.ts +125 -35
  70. package/dist/packages/dca/dca.d.ts.map +1 -1
  71. package/dist/packages/dca/dca.js +135 -43
  72. package/dist/packages/dca/dcaTypes.d.ts +217 -42
  73. package/dist/packages/dca/dcaTypes.d.ts.map +1 -1
  74. package/dist/packages/farms/api/farmsApi.d.ts +725 -58
  75. package/dist/packages/farms/api/farmsApi.d.ts.map +1 -1
  76. package/dist/packages/farms/api/farmsApi.js +1149 -153
  77. package/dist/packages/farms/api/farmsApiCasting.d.ts +34 -18
  78. package/dist/packages/farms/api/farmsApiCasting.d.ts.map +1 -1
  79. package/dist/packages/farms/api/farmsApiCasting.js +218 -17
  80. package/dist/packages/farms/api/farmsApiCastingTypes.d.ts +117 -21
  81. package/dist/packages/farms/api/farmsApiCastingTypes.d.ts.map +1 -1
  82. package/dist/packages/farms/farms.d.ts +200 -12
  83. package/dist/packages/farms/farms.d.ts.map +1 -1
  84. package/dist/packages/farms/farms.js +204 -50
  85. package/dist/packages/farms/farmsStakedPosition.d.ts +143 -8
  86. package/dist/packages/farms/farmsStakedPosition.d.ts.map +1 -1
  87. package/dist/packages/farms/farmsStakedPosition.js +243 -137
  88. package/dist/packages/farms/farmsStakingPool.d.ts +171 -3
  89. package/dist/packages/farms/farmsStakingPool.d.ts.map +1 -1
  90. package/dist/packages/farms/farmsStakingPool.js +233 -73
  91. package/dist/packages/farms/farmsTypes.d.ts +310 -2
  92. package/dist/packages/farms/farmsTypes.d.ts.map +1 -1
  93. package/dist/packages/farms/farmsTypes.js +47 -0
  94. package/dist/packages/faucet/api/faucetApi.d.ts +9 -17
  95. package/dist/packages/faucet/api/faucetApi.d.ts.map +1 -1
  96. package/dist/packages/faucet/api/faucetApi.js +44 -49
  97. package/dist/packages/faucet/api/faucetApiCasting.d.ts.map +1 -1
  98. package/dist/packages/faucet/api/faucetApiCasting.js +8 -5
  99. package/dist/packages/faucet/api/faucetApiCastingTypes.d.ts +1 -4
  100. package/dist/packages/faucet/api/faucetApiCastingTypes.d.ts.map +1 -1
  101. package/dist/packages/faucet/faucet.d.ts +1 -1
  102. package/dist/packages/faucet/faucet.d.ts.map +1 -1
  103. package/dist/packages/faucet/faucet.js +1 -1
  104. package/dist/packages/faucet/faucetTypes.d.ts +2 -3
  105. package/dist/packages/faucet/faucetTypes.d.ts.map +1 -1
  106. package/dist/packages/limitOrders/api/limitOrdersApi.d.ts +12 -0
  107. package/dist/packages/limitOrders/api/limitOrdersApi.d.ts.map +1 -0
  108. package/dist/packages/limitOrders/api/limitOrdersApi.js +25 -0
  109. package/dist/packages/limitOrders/index.d.ts +2 -0
  110. package/dist/packages/limitOrders/index.d.ts.map +1 -0
  111. package/dist/packages/limitOrders/index.js +17 -0
  112. package/dist/packages/limitOrders/limitOrders.d.ts +143 -0
  113. package/dist/packages/limitOrders/limitOrders.d.ts.map +1 -0
  114. package/dist/packages/limitOrders/limitOrders.js +190 -0
  115. package/dist/packages/limitOrders/limitOrdersTypes.d.ts +211 -0
  116. package/dist/packages/limitOrders/limitOrdersTypes.d.ts.map +1 -0
  117. package/dist/packages/multisig/multisig.d.ts +34 -5
  118. package/dist/packages/multisig/multisig.d.ts.map +1 -1
  119. package/dist/packages/multisig/multisig.js +33 -4
  120. package/dist/packages/multisig/multisigTypes.d.ts +17 -0
  121. package/dist/packages/multisig/multisigTypes.d.ts.map +1 -1
  122. package/dist/packages/nftAmm/nftAmm.d.ts +1 -1
  123. package/dist/packages/nftAmm/nftAmm.d.ts.map +1 -1
  124. package/dist/packages/perpetuals/api/perpetualsApi.d.ts +38 -23
  125. package/dist/packages/perpetuals/api/perpetualsApi.d.ts.map +1 -1
  126. package/dist/packages/perpetuals/api/perpetualsApi.js +740 -299
  127. package/dist/packages/perpetuals/api/perpetualsApiCasting.d.ts +10 -6
  128. package/dist/packages/perpetuals/api/perpetualsApiCasting.d.ts.map +1 -1
  129. package/dist/packages/perpetuals/api/perpetualsApiCasting.js +112 -42
  130. package/dist/packages/perpetuals/perpetuals.d.ts +33 -4
  131. package/dist/packages/perpetuals/perpetuals.d.ts.map +1 -1
  132. package/dist/packages/perpetuals/perpetuals.js +105 -18
  133. package/dist/packages/perpetuals/perpetualsAccount.d.ts +107 -33
  134. package/dist/packages/perpetuals/perpetualsAccount.d.ts.map +1 -1
  135. package/dist/packages/perpetuals/perpetualsAccount.js +569 -68
  136. package/dist/packages/perpetuals/perpetualsCastingTypes.d.ts +104 -25
  137. package/dist/packages/perpetuals/perpetualsCastingTypes.d.ts.map +1 -1
  138. package/dist/packages/perpetuals/perpetualsMarket.d.ts +31 -9
  139. package/dist/packages/perpetuals/perpetualsMarket.d.ts.map +1 -1
  140. package/dist/packages/perpetuals/perpetualsMarket.js +85 -77
  141. package/dist/packages/perpetuals/perpetualsTypes.d.ts +348 -380
  142. package/dist/packages/perpetuals/perpetualsTypes.d.ts.map +1 -1
  143. package/dist/packages/perpetuals/perpetualsTypes.js +43 -113
  144. package/dist/packages/pools/api/poolsApiCasting.d.ts.map +1 -1
  145. package/dist/packages/pools/api/poolsApiCasting.js +0 -1
  146. package/dist/packages/pools/pool.d.ts +333 -106
  147. package/dist/packages/pools/pool.d.ts.map +1 -1
  148. package/dist/packages/pools/pool.js +347 -119
  149. package/dist/packages/pools/pools.d.ts +307 -39
  150. package/dist/packages/pools/pools.d.ts.map +1 -1
  151. package/dist/packages/pools/pools.js +323 -54
  152. package/dist/packages/pools/poolsTypes.d.ts +267 -2
  153. package/dist/packages/pools/poolsTypes.d.ts.map +1 -1
  154. package/dist/packages/referralVault/referralVault.d.ts +38 -1
  155. package/dist/packages/referralVault/referralVault.d.ts.map +1 -1
  156. package/dist/packages/referralVault/referralVault.js +37 -0
  157. package/dist/packages/router/api/routerApi.d.ts +2 -2
  158. package/dist/packages/router/api/routerApi.d.ts.map +1 -1
  159. package/dist/packages/router/api/routerApi.js +19 -11
  160. package/dist/packages/router/router.d.ts +214 -18
  161. package/dist/packages/router/router.d.ts.map +1 -1
  162. package/dist/packages/router/router.js +216 -20
  163. package/dist/packages/router/routerTypes.d.ts +202 -17
  164. package/dist/packages/router/routerTypes.d.ts.map +1 -1
  165. package/dist/packages/staking/staking.d.ts +306 -45
  166. package/dist/packages/staking/staking.d.ts.map +1 -1
  167. package/dist/packages/staking/staking.js +313 -51
  168. package/dist/packages/staking/stakingTypes.d.ts +431 -6
  169. package/dist/packages/staking/stakingTypes.d.ts.map +1 -1
  170. package/dist/packages/staking/stakingTypes.js +19 -0
  171. package/dist/packages/sui/sui.d.ts +40 -1
  172. package/dist/packages/sui/sui.d.ts.map +1 -1
  173. package/dist/packages/sui/sui.js +39 -0
  174. package/dist/packages/suiFrens/stakedSuiFren.d.ts +1 -1
  175. package/dist/packages/suiFrens/stakedSuiFren.d.ts.map +1 -1
  176. package/dist/packages/suiFrens/suiFren.d.ts +1 -1
  177. package/dist/packages/suiFrens/suiFren.d.ts.map +1 -1
  178. package/dist/packages/suiFrens/suiFrens.d.ts +1 -1
  179. package/dist/packages/suiFrens/suiFrens.d.ts.map +1 -1
  180. package/dist/packages/userData/api/userDataApi.d.ts +6 -0
  181. package/dist/packages/userData/api/userDataApi.d.ts.map +1 -0
  182. package/dist/packages/userData/api/userDataApi.js +12 -0
  183. package/dist/packages/userData/index.d.ts +2 -0
  184. package/dist/packages/userData/index.d.ts.map +1 -0
  185. package/dist/packages/userData/index.js +17 -0
  186. package/dist/packages/userData/userData.d.ts +89 -0
  187. package/dist/packages/userData/userData.d.ts.map +1 -0
  188. package/dist/packages/userData/userData.js +119 -0
  189. package/dist/packages/userData/userDataTypes.d.ts +29 -0
  190. package/dist/packages/userData/userDataTypes.d.ts.map +1 -0
  191. package/dist/types.d.ts.map +1 -1
  192. package/dist/types.js +0 -1
  193. package/package.json +1 -1
  194. package/dist/general/historicalData/historicalData.d.ts +0 -15
  195. package/dist/general/historicalData/historicalData.d.ts.map +0 -1
  196. package/dist/general/historicalData/historicalData.js +0 -45
  197. package/dist/general/historicalData/historicalDataTypes.d.ts +0 -10
  198. package/dist/general/historicalData/historicalDataTypes.d.ts.map +0 -1
  199. package/dist/packages/dca/api/dcaApiCasting.d.ts +0 -11
  200. package/dist/packages/dca/api/dcaApiCasting.d.ts.map +0 -1
  201. package/dist/packages/dca/api/dcaApiCasting.js +0 -186
  202. package/dist/packages/dca/api/dcaApiCastingTypes.d.ts +0 -142
  203. package/dist/packages/dca/api/dcaApiCastingTypes.d.ts.map +0 -1
  204. package/dist/packages/oracle/api/oracleApi.d.ts +0 -13
  205. package/dist/packages/oracle/api/oracleApi.d.ts.map +0 -1
  206. package/dist/packages/oracle/api/oracleApi.js +0 -35
  207. package/dist/packages/oracle/oracle.d.ts +0 -15
  208. package/dist/packages/oracle/oracle.d.ts.map +0 -1
  209. package/dist/packages/oracle/oracle.js +0 -46
  210. package/dist/packages/oracle/oracleTypes.d.ts +0 -2
  211. package/dist/packages/oracle/oracleTypes.d.ts.map +0 -1
  212. package/dist/packages/oracle/oracleTypes.js +0 -2
  213. /package/dist/{general/historicalData/historicalDataTypes.js → packages/limitOrders/limitOrdersTypes.js} +0 -0
  214. /package/dist/packages/{dca/api/dcaApiCastingTypes.js → userData/userDataTypes.js} +0 -0
@@ -19,46 +19,93 @@ const dayjs_1 = __importDefault(require("dayjs"));
19
19
  const duration_1 = __importDefault(require("dayjs/plugin/duration"));
20
20
  const fixedUtils_1 = require("../../general/utils/fixedUtils");
21
21
  const coin_1 = require("../coin/coin");
22
+ const farms_1 = require("./farms");
23
+ /**
24
+ * The `FarmsStakingPool` class represents a staking pool (also referred
25
+ * to as a "vault" in some contexts). It allows reading details about
26
+ * emission schedules, reward tokens, stake coin type, and lock durations,
27
+ * as well as constructing transactions to stake, harvest, and mutate the
28
+ * pool parameters if the user has the correct admin privileges.
29
+ */
22
30
  class FarmsStakingPool extends caller_1.Caller {
23
- // =========================================================================
24
- // Constructor
25
- // =========================================================================
31
+ /**
32
+ * Creates a `FarmsStakingPool` instance based on on-chain pool data.
33
+ *
34
+ * @param stakingPool - The on-chain data object describing the pool.
35
+ * @param config - An optional `CallerConfig` for network settings.
36
+ * @param Provider - An optional `AftermathApi` for transaction building.
37
+ */
26
38
  constructor(stakingPool, config, Provider) {
27
39
  super(config, "farms");
28
40
  this.stakingPool = stakingPool;
29
41
  this.Provider = Provider;
30
42
  // =========================================================================
31
- // Public
32
- // =========================================================================
33
- // =========================================================================
34
43
  // Getters
35
44
  // =========================================================================
45
+ /**
46
+ * Retrieves the version of this staking pool (1 or 2).
47
+ */
48
+ this.version = () => {
49
+ return this.stakingPool.version;
50
+ };
51
+ /**
52
+ * Lists all reward coin types offered by this staking pool.
53
+ *
54
+ * @returns An array of `CoinType` strings.
55
+ */
36
56
  this.rewardCoinTypes = () => {
37
57
  return this.stakingPool.rewardCoins.map((coin) => coin.coinType);
38
58
  };
59
+ /**
60
+ * Lists all reward coin types for which this pool currently has a non-zero actual reward balance.
61
+ *
62
+ * @returns An array of `CoinType` strings that have > 0 actual rewards.
63
+ */
39
64
  this.nonZeroRewardCoinTypes = () => {
40
65
  return this.stakingPool.rewardCoins
41
66
  .filter((coin) => coin.emissionRate <= coin.actualRewards &&
42
67
  coin.actualRewards > BigInt(0))
43
68
  .map((coin) => coin.coinType);
44
69
  };
70
+ /**
71
+ * Retrieves the on-chain record for a specific reward coin type in this pool.
72
+ *
73
+ * @param inputs - Contains the `coinType` to look up.
74
+ * @throws If the specified coinType is not found in `rewardCoins`.
75
+ * @returns A `FarmsStakingPoolRewardCoin` object.
76
+ */
45
77
  this.rewardCoin = (inputs) => {
46
78
  const foundCoin = this.stakingPool.rewardCoins.find((coin) => coin.coinType === inputs.coinType);
47
79
  if (!foundCoin)
48
80
  throw new Error("Invalid coin type");
49
81
  return foundCoin;
50
82
  };
83
+ /**
84
+ * Computes the maximum lock duration (in ms) that remains valid in this staking pool,
85
+ * factoring in the current time and the pool's emission end.
86
+ *
87
+ * @returns The maximum possible lock duration in milliseconds, or 0 if the pool is effectively closed.
88
+ */
51
89
  this.maxLockDurationMs = () => {
52
90
  return Math.max(Math.min(this.stakingPool.maxLockDurationMs, this.stakingPool.emissionEndTimestamp - (0, dayjs_1.default)().valueOf()), 0);
53
91
  };
54
92
  // =========================================================================
55
93
  // Calculations
56
94
  // =========================================================================
57
- // Calculates the amount of rewards that have emitted since the last time this function has been
58
- // called. Updates `rewards_accumulated_per_share`.
95
+ /**
96
+ * Calculates and applies newly emitted rewards for each reward coin in this pool,
97
+ * updating the `rewardsAccumulatedPerShare`. This simulates the on-chain
98
+ * `emitRewards` logic.
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * someFarmsPool.emitRewards();
103
+ * // The pool's rewardsAccumulatedPerShare fields are now updated.
104
+ * ```
105
+ */
59
106
  this.emitRewards = () => {
60
107
  const currentTimestamp = (0, dayjs_1.default)().valueOf();
61
- // ia. Check that the vault has deposits.
108
+ // If no staked amount, no distribution
62
109
  if (this.stakingPool.stakedAmount === BigInt(0))
63
110
  return;
64
111
  const rewardCoins = utils_1.Helpers.deepCopy(this.stakingPool.rewardCoins);
@@ -86,27 +133,45 @@ class FarmsStakingPool extends caller_1.Caller {
86
133
  numberOfEmissions * rewardCoin.emissionSchedulesMs;
87
134
  }
88
135
  };
136
+ /**
137
+ * Computes an approximate APR for a specific reward coin, based on the current
138
+ * emission rate, coin price, pool TVL, and the lock multiplier range. This assumes
139
+ * maximum lock multiplier for the final APR result.
140
+ *
141
+ * @param inputs - Includes the `coinType`, its `price` and `decimals`, plus the total `tvlUsd` in the pool.
142
+ * @returns A numeric APR (0.05 = 5%).
143
+ */
89
144
  this.calcApr = (inputs) => {
90
145
  const { coinType, price, decimals, tvlUsd } = inputs;
91
146
  if (price <= 0 || tvlUsd <= 0)
92
147
  return 0;
93
- // this.emitRewards();
94
148
  const rewardCoin = this.rewardCoin({ coinType });
95
149
  const currentTimestamp = (0, dayjs_1.default)().valueOf();
150
+ // If the current emission rate is below the actual supply, or if the pool hasn't started or is ended, yield 0
96
151
  if (rewardCoin.emissionRate > rewardCoin.actualRewards)
97
152
  return 0;
98
153
  if (rewardCoin.emissionStartTimestamp > currentTimestamp ||
99
- currentTimestamp > this.stakingPool.emissionEndTimestamp)
154
+ currentTimestamp > this.stakingPool.emissionEndTimestamp) {
100
155
  return 0;
101
- const emissionRateUsd = coin_1.Coin.balanceWithDecimals(rewardCoin.emissionRate, decimals) * price;
156
+ }
157
+ const emissionRateTokens = rewardCoin.emissionRate;
158
+ const emissionRateUsd = coin_1.Coin.balanceWithDecimals(emissionRateTokens, decimals) * price;
102
159
  dayjs_1.default.extend(duration_1.default);
103
160
  const oneYearMs = dayjs_1.default.duration(1, "year").asMilliseconds();
104
161
  const rewardsUsdOneYear = emissionRateUsd * (oneYearMs / rewardCoin.emissionSchedulesMs);
162
+ // The final APR is normalized by total staked value and the maximum lock multiplier
105
163
  const apr = rewardsUsdOneYear /
106
164
  tvlUsd /
107
165
  utils_1.Casting.bigIntToFixedNumber(this.stakingPool.maxLockMultiplier);
108
166
  return apr < 0 ? 0 : isNaN(apr) ? 0 : apr;
109
167
  };
168
+ /**
169
+ * Computes the total APR contributed by all reward coin types in this pool, summing
170
+ * up the individual APR for each coin type. This also assumes max lock multiplier.
171
+ *
172
+ * @param inputs - Contains price data (`coinsToPrice`), decimal data (`coinsToDecimals`), and the total TVL in USD.
173
+ * @returns The sum of all coin APRs (0.10 = 10%).
174
+ */
110
175
  this.calcTotalApr = (inputs) => {
111
176
  const { coinsToPrice, coinsToDecimals, tvlUsd } = inputs;
112
177
  const aprs = this.rewardCoinTypes().map((coinType) => this.calcApr({
@@ -117,6 +182,14 @@ class FarmsStakingPool extends caller_1.Caller {
117
182
  }));
118
183
  return utils_1.Helpers.sum(aprs);
119
184
  };
185
+ /**
186
+ * Given a lock duration in ms, calculates the lock multiplier to be used by staked positions.
187
+ * This function clamps the input duration between the pool's `minLockDurationMs` and
188
+ * `maxLockDurationMs`.
189
+ *
190
+ * @param inputs - An object containing the `lockDurationMs` for which to calculate a multiplier.
191
+ * @returns A `FarmsMultiplier` (bigint) representing the scaled factor (1.0 = 1e9 if using fixedOneB).
192
+ */
120
193
  this.calcMultiplier = (inputs) => {
121
194
  const lockDurationMs = inputs.lockDurationMs > this.stakingPool.maxLockDurationMs
122
195
  ? this.stakingPool.maxLockDurationMs
@@ -135,11 +208,14 @@ class FarmsStakingPool extends caller_1.Caller {
135
208
  const multiplier = utils_1.Casting.numberToFixedBigInt(newMultiplier);
136
209
  return multiplier < fixedUtils_1.FixedUtils.fixedOneB
137
210
  ? fixedUtils_1.FixedUtils.fixedOneB
138
- : multiplier;
211
+ : utils_1.Helpers.minBigInt(multiplier, this.stakingPool.maxLockMultiplier);
139
212
  };
140
213
  // =========================================================================
141
214
  // Helpers
142
215
  // =========================================================================
216
+ /**
217
+ * Provides access to the farm-specific provider methods for building transactions.
218
+ */
143
219
  this.useProvider = () => {
144
220
  var _a;
145
221
  const provider = (_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Farms();
@@ -148,83 +224,160 @@ class FarmsStakingPool extends caller_1.Caller {
148
224
  return provider;
149
225
  };
150
226
  this.stakingPool = stakingPool;
151
- // this.emitRewards();
152
227
  }
153
- // public calc_emitted_rewards(): bigint[] {
154
- // let emitted_rewards: bigint[] = [];
155
- // let length = this.stakingPool.rewardCoins.length;
156
- // let index = 0;
157
- // while (index < length) {
158
- // let emission_start_timestamp_ms =
159
- // this.stakingPool.rewardCoins[index].emissionStartTimestamp;
160
- // let last_reward_timestamp_ms =
161
- // this.stakingPool.rewardCoins[index].lastRewardTimestamp;
162
- // emitted_rewards.push(
163
- // this.calcRewardsEmittedFromTimeTmToTn({
164
- // timestampTm: emission_start_timestamp_ms,
165
- // timestampTn: last_reward_timestamp_ms,
166
- // rewardCoin: this.stakingPool.rewardCoins[index],
167
- // })
168
- // );
169
- // index = index + 1;
170
- // }
171
- // return emitted_rewards;
172
- // }
173
- // public calc_emitted_rewards_for(inputs: {
174
- // rewardCoinIndex: number;
175
- // }): bigint {
176
- // let reward_index = inputs.rewardCoinIndex;
177
- // let emission_start_timestamp_ms =
178
- // this.stakingPool.rewardCoins[reward_index].emissionStartTimestamp;
179
- // let last_reward_timestamp_ms =
180
- // this.stakingPool.rewardCoins[reward_index].lastRewardTimestamp;
181
- // return this.calcRewardsEmittedFromTimeTmToTn({
182
- // timestampTm: emission_start_timestamp_ms,
183
- // timestampTn: last_reward_timestamp_ms,
184
- // rewardCoin: this.stakingPool.rewardCoins[reward_index],
185
- // });
186
- // }
228
+ // =========================================================================
229
+ // Public
230
+ // =========================================================================
231
+ // =========================================================================
232
+ // Stats
233
+ // =========================================================================
234
+ /**
235
+ * Fetches the total value locked (TVL) for this staking pool alone.
236
+ *
237
+ * @returns A `number` representing this pool's TVL in USD (or another currency).
238
+ *
239
+ * @example
240
+ * ```typescript
241
+ * const poolTvl = await someFarmsPool.getTVL();
242
+ * console.log(poolTvl);
243
+ * ```
244
+ */
245
+ getTVL() {
246
+ return __awaiter(this, void 0, void 0, function* () {
247
+ return new farms_1.Farms(this.config, this.Provider).getTVL({
248
+ farmIds: [this.stakingPool.objectId],
249
+ });
250
+ });
251
+ }
252
+ /**
253
+ * Fetches the total value locked (TVL) of the reward coins in this specific staking pool.
254
+ *
255
+ * @returns A `number` representing this pool's reward TVL.
256
+ *
257
+ * @example
258
+ * ```typescript
259
+ * const rewardTvl = await someFarmsPool.getRewardsTVL();
260
+ * console.log(rewardTvl);
261
+ * ```
262
+ */
263
+ getRewardsTVL() {
264
+ return __awaiter(this, void 0, void 0, function* () {
265
+ return new farms_1.Farms(this.config, this.Provider).getRewardsTVL({
266
+ farmIds: [this.stakingPool.objectId],
267
+ });
268
+ });
269
+ }
187
270
  // =========================================================================
188
271
  // Transactions
189
272
  // =========================================================================
190
273
  // =========================================================================
191
274
  // Staking Transactions
192
275
  // =========================================================================
276
+ /**
277
+ * Builds a transaction to stake tokens into this pool, optionally locking them.
278
+ *
279
+ * @param inputs - Contains `stakeAmount`, `lockDurationMs`, `walletAddress`, and optional sponsorship.
280
+ * @returns A transaction object (or bytes) that can be signed and executed to create a staked position.
281
+ */
193
282
  getStakeTransaction(inputs) {
194
283
  return __awaiter(this, void 0, void 0, function* () {
195
- return this.useProvider().fetchBuildStakeTx(Object.assign(Object.assign({}, inputs), { stakeCoinType: this.stakingPool.stakeCoinType, stakingPoolId: this.stakingPool.objectId }));
284
+ const args = Object.assign(Object.assign({}, inputs), { stakeCoinType: this.stakingPool.stakeCoinType, stakingPoolId: this.stakingPool.objectId });
285
+ return this.version() === 1
286
+ ? this.useProvider().fetchBuildStakeTxV1(args)
287
+ : this.useProvider().fetchBuildStakeTxV2(Object.assign({}, args));
196
288
  });
197
289
  }
198
290
  // =========================================================================
199
291
  // Reward Harvesting Transactions
200
292
  // =========================================================================
293
+ /**
294
+ * Builds a transaction to harvest rewards from multiple staked positions in this pool.
295
+ *
296
+ * @param inputs - Contains `stakedPositionIds`, the `walletAddress`, and optionally any others.
297
+ * @returns A transaction that can be signed and executed to claim rewards from multiple positions.
298
+ */
201
299
  getHarvestRewardsTransaction(inputs) {
202
300
  return __awaiter(this, void 0, void 0, function* () {
203
- return this.useProvider().fetchBuildHarvestRewardsTx(Object.assign(Object.assign({}, inputs), { stakeCoinType: this.stakingPool.stakeCoinType, stakingPoolId: this.stakingPool.objectId, rewardCoinTypes: this.nonZeroRewardCoinTypes() }));
301
+ const args = Object.assign(Object.assign({}, inputs), { stakeCoinType: this.stakingPool.stakeCoinType, stakingPoolId: this.stakingPool.objectId, rewardCoinTypes: this.nonZeroRewardCoinTypes() });
302
+ return this.version() === 1
303
+ ? this.useProvider().fetchBuildHarvestRewardsTxV1(args)
304
+ : this.useProvider().fetchBuildHarvestRewardsTxV2(args);
204
305
  });
205
306
  }
206
307
  // =========================================================================
207
308
  // Mutation/Creation Transactions (Owner Only)
208
309
  // =========================================================================
310
+ /**
311
+ * Builds a transaction to increase the emission rate (or schedule) for specific reward coins.
312
+ *
313
+ * @param inputs - Contains the `ownerCapId` that authorizes changes, plus an array of `rewards` with new emission details.
314
+ * @returns A transaction to be signed and executed by the owner cap holder.
315
+ */
209
316
  getIncreaseRewardsEmissionsTransaction(inputs) {
210
317
  return __awaiter(this, void 0, void 0, function* () {
211
- return this.useProvider().fetchIncreaseStakingPoolRewardsEmissionsTx(Object.assign(Object.assign({}, inputs), { stakeCoinType: this.stakingPool.stakeCoinType, stakingPoolId: this.stakingPool.objectId }));
318
+ const args = Object.assign(Object.assign({}, inputs), { stakeCoinType: this.stakingPool.stakeCoinType, stakingPoolId: this.stakingPool.objectId });
319
+ return this.version() === 1
320
+ ? this.useProvider().fetchIncreaseStakingPoolRewardsEmissionsTxV1(args)
321
+ : this.useProvider().fetchIncreaseStakingPoolRewardsEmissionsTxV2(args);
212
322
  });
213
323
  }
324
+ /**
325
+ * Builds a transaction to update the pool's minimum stake amount, only authorized by the `ownerCapId`.
326
+ *
327
+ * @param inputs - Contains the new `minStakeAmount`, the `ownerCapId`, and the calling `walletAddress`.
328
+ * @returns A transaction that can be signed and executed to change the minimum stake requirement.
329
+ */
330
+ getUpdateMinStakeAmountTransaction(inputs) {
331
+ return __awaiter(this, void 0, void 0, function* () {
332
+ const args = Object.assign(Object.assign({}, inputs), { stakeCoinType: this.stakingPool.stakeCoinType, stakingPoolId: this.stakingPool.objectId });
333
+ return this.version() === 1
334
+ ? this.useProvider().buildSetStakingPoolMinStakeAmountTxV1(args)
335
+ : this.useProvider().buildSetStakingPoolMinStakeAmountTxV2(args);
336
+ });
337
+ }
338
+ /**
339
+ * Builds a transaction granting a one-time admin cap to another address, allowing them to perform specific
340
+ * one-time administrative actions (like initializing a reward).
341
+ *
342
+ * @param inputs - Body containing the `ownerCapId`, the `recipientAddress`, and the `rewardCoinType`.
343
+ * @returns A transaction to be executed by the current pool owner.
344
+ */
214
345
  getGrantOneTimeAdminCapTransaction(inputs) {
215
- return this.useProvider().buildGrantOneTimeAdminCapTx(inputs);
346
+ return this.version() === 1
347
+ ? this.useProvider().buildGrantOneTimeAdminCapTxV1(inputs)
348
+ : this.useProvider().buildGrantOneTimeAdminCapTxV2(inputs);
216
349
  }
217
350
  // =========================================================================
218
351
  // Mutation Transactions (Owner/Admin Only)
219
352
  // =========================================================================
353
+ /**
354
+ * Builds a transaction to initialize a new reward coin in this pool, specifying the amount, emission rate,
355
+ * and schedule parameters. This can be done by either the `ownerCapId` or a `oneTimeAdminCapId`.
356
+ *
357
+ * @param inputs - Contains emission info (rate, schedule) and which cap is used (`ownerCapId` or `oneTimeAdminCapId`).
358
+ * @returns A transaction object for the reward initialization.
359
+ */
220
360
  getInitializeRewardTransaction(inputs) {
221
361
  return __awaiter(this, void 0, void 0, function* () {
222
- return this.useProvider().fetchBuildInitializeStakingPoolRewardTx(Object.assign(Object.assign({}, inputs), { stakeCoinType: this.stakingPool.stakeCoinType, stakingPoolId: this.stakingPool.objectId }));
362
+ const args = Object.assign(Object.assign({}, inputs), { stakeCoinType: this.stakingPool.stakeCoinType, stakingPoolId: this.stakingPool.objectId });
363
+ return this.version() === 1
364
+ ? this.useProvider().fetchBuildInitializeStakingPoolRewardTxV1(args)
365
+ : this.useProvider().fetchBuildInitializeStakingPoolRewardTxV2(args);
223
366
  });
224
367
  }
368
+ /**
369
+ * Builds a transaction to add more reward coins (top-up) to an existing reward
370
+ * coin configuration, either as the owner or via a one-time admin cap.
371
+ *
372
+ * @param inputs - Contains an array of reward objects, each specifying amount and coin type.
373
+ * @returns A transaction that can be signed and executed to increase the reward distribution pool.
374
+ */
225
375
  getTopUpRewardsTransaction(inputs) {
226
376
  return __awaiter(this, void 0, void 0, function* () {
227
- return this.useProvider().fetchBuildTopUpStakingPoolRewardsTx(Object.assign(Object.assign({}, inputs), { stakeCoinType: this.stakingPool.stakeCoinType, stakingPoolId: this.stakingPool.objectId }));
377
+ const args = Object.assign(Object.assign({}, inputs), { stakeCoinType: this.stakingPool.stakeCoinType, stakingPoolId: this.stakingPool.objectId });
378
+ return this.version() === 1
379
+ ? this.useProvider().fetchBuildTopUpStakingPoolRewardsTxV1(args)
380
+ : this.useProvider().fetchBuildTopUpStakingPoolRewardsTxV2(args);
228
381
  });
229
382
  }
230
383
  // =========================================================================
@@ -233,27 +386,28 @@ class FarmsStakingPool extends caller_1.Caller {
233
386
  // =========================================================================
234
387
  // Calculations
235
388
  // =========================================================================
389
+ /**
390
+ * Updates `rewardsAccumulatedPerShare` by distributing `rewardsToEmit` among
391
+ * the total staked amount with multiplier. This mimics on-chain distribution logic.
392
+ *
393
+ * @param inputs - Contains the `rewardsToEmit` and which `rewardCoinIndex` to update.
394
+ */
236
395
  increaseRewardsAccumulatedPerShare(inputs) {
237
396
  const { rewardsToEmit, rewardCoinIndex } = inputs;
238
- // i. Calculate the pro-rata amount of rewards allocated to each staked amount.
397
+ const stakedWithMultiplier = this.stakingPool.stakedAmountWithMultiplier;
398
+ if (stakedWithMultiplier === BigInt(0))
399
+ return;
400
+ // Distribute proportionally
239
401
  const newRewardsAccumulatedPerShare = (rewardsToEmit * BigInt(1000000000000000000)) /
240
- this.stakingPool.stakedAmountWithMultiplier;
402
+ stakedWithMultiplier;
241
403
  if (newRewardsAccumulatedPerShare === BigInt(0))
242
404
  return;
243
- // ii. Increase the amount of rewards emitted per share.
244
405
  this.stakingPool.rewardCoins[rewardCoinIndex].rewardsAccumulatedPerShare += newRewardsAccumulatedPerShare;
245
406
  }
246
- rewardsRemaining(inputs) {
247
- const rewardCoin = this.stakingPool.rewardCoins[inputs.rewardCoinIndex];
248
- const currentTimestamp = (0, dayjs_1.default)().valueOf();
249
- const numberOfEmissions = BigInt(Math.floor((currentTimestamp - rewardCoin.emissionStartTimestamp) /
250
- rewardCoin.emissionSchedulesMs));
251
- const emittedRewards = rewardCoin.emissionRate * numberOfEmissions;
252
- const totalRewards = rewardCoin.rewards;
253
- if (totalRewards <= emittedRewards)
254
- return BigInt(0);
255
- return totalRewards - emittedRewards;
256
- }
407
+ /**
408
+ * Computes how many rewards to emit based on the time since `lastRewardTimestamp` and
409
+ * the pool's emission schedule, clamped by the total `rewardsRemaining`.
410
+ */
257
411
  calcRewardsToEmit(inputs) {
258
412
  const { rewardCoin } = inputs;
259
413
  const currentTimestamp = (0, dayjs_1.default)().valueOf();
@@ -263,18 +417,24 @@ class FarmsStakingPool extends caller_1.Caller {
263
417
  timestampTn: currentTimestamp,
264
418
  rewardCoin,
265
419
  });
266
- const rewardsRemaining = rewardCoin.rewardsRemaining;
420
+ const { rewardsRemaining } = rewardCoin;
267
421
  // IMPORTANT: Cap the amount of rewards to emit by the amount of remaining rewards.
268
- //
269
422
  return rewardsRemaining < rewardsToEmit
270
423
  ? rewardsRemaining
271
424
  : rewardsToEmit;
272
425
  }
426
+ /**
427
+ * Calculates how many tokens were emitted between two timestamps (Tm and Tn) for a given reward coin,
428
+ * based on the discrete `emissionRate` and `emissionSchedulesMs`.
429
+ *
430
+ * @param inputs - Contains `timestampTm`, `timestampTn`, and the relevant `rewardCoin`.
431
+ * @returns The total number of tokens emitted in that time window.
432
+ */
273
433
  calcRewardsEmittedFromTimeTmToTn(inputs) {
274
434
  const { timestampTm, timestampTn, rewardCoin } = inputs;
275
- const numberOfEmissionsFromTimeTmToTn = (timestampTn - timestampTm) /
276
- // -----------------------------------------------
277
- rewardCoin.emissionSchedulesMs;
435
+ const numberOfEmissionsFromTimeTmToTn = rewardCoin.emissionSchedulesMs === 0
436
+ ? 0
437
+ : (timestampTn - timestampTm) / rewardCoin.emissionSchedulesMs;
278
438
  return (BigInt(Math.floor(numberOfEmissionsFromTimeTmToTn)) *
279
439
  rewardCoin.emissionRate);
280
440
  }