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

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 +349 -379
  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,10 +19,24 @@ const fixedUtils_1 = require("../../general/utils/fixedUtils");
19
19
  const utils_1 = require("../../general/utils");
20
20
  const dayjs_1 = __importDefault(require("dayjs"));
21
21
  const farms_1 = require("./farms");
22
+ /**
23
+ * The `FarmsStakedPosition` class represents a user's individual staked position
24
+ * in a particular staking pool. It provides methods to query position details,
25
+ * calculate potential rewards, lock/unlock stake, and build transactions
26
+ * for depositing, unstaking, or harvesting rewards.
27
+ */
22
28
  class FarmsStakedPosition extends caller_1.Caller {
23
29
  // =========================================================================
24
30
  // Constructor
25
31
  // =========================================================================
32
+ /**
33
+ * Creates a `FarmsStakedPosition` instance for a user's staked position in a farm.
34
+ *
35
+ * @param stakedPosition - The on-chain data object representing the user's staked position.
36
+ * @param trueLastHarvestRewardsTimestamp - Optionally overrides the last harvest time from the on-chain data.
37
+ * @param config - Optional configuration for the underlying `Caller`.
38
+ * @param Provider - Optional `AftermathApi` instance for transaction building.
39
+ */
26
40
  constructor(stakedPosition, trueLastHarvestRewardsTimestamp = undefined, config, Provider) {
27
41
  super(config, "farms");
28
42
  this.stakedPosition = stakedPosition;
@@ -33,33 +47,86 @@ class FarmsStakedPosition extends caller_1.Caller {
33
47
  // =========================================================================
34
48
  // Getters
35
49
  // =========================================================================
50
+ /**
51
+ * Returns the version of the farm system that this position belongs to (1 or 2).
52
+ */
53
+ this.version = () => {
54
+ return this.stakedPosition.version;
55
+ };
56
+ /**
57
+ * Checks whether the position is still locked, based on the current time and the lock parameters.
58
+ *
59
+ * @param inputs - Contains a `FarmsStakingPool` instance to check for system constraints.
60
+ * @returns `true` if the position is locked; otherwise, `false`.
61
+ */
36
62
  this.isLocked = (inputs) => {
37
63
  return !this.isUnlocked(inputs);
38
64
  };
65
+ /**
66
+ * Checks whether the position has a non-zero lock duration.
67
+ *
68
+ * @returns `true` if the position was created with a lock duration > 0.
69
+ */
39
70
  this.isLockDuration = () => {
40
71
  return this.stakedPosition.lockDurationMs > 0;
41
72
  };
73
+ /**
74
+ * Computes the timestamp (in ms) at which this position's lock will end.
75
+ *
76
+ * @returns The unlock timestamp (lock start + lock duration).
77
+ */
42
78
  this.unlockTimestamp = () => {
43
79
  return (this.stakedPosition.lockStartTimestamp +
44
80
  this.stakedPosition.lockDurationMs);
45
81
  };
82
+ /**
83
+ * Computes the user's accrued rewards for each reward coin in this position,
84
+ * returned as a `CoinsToBalance` object keyed by coin type.
85
+ *
86
+ * @param inputs - Contains a reference to the `FarmsStakingPool`.
87
+ * @returns A mapping from `coinType` to the amount of earned rewards.
88
+ */
46
89
  this.rewardCoinsToClaimableBalance = (inputs) => {
47
90
  return this.stakedPosition.rewardCoins.reduce((acc, coin) => (Object.assign(Object.assign({}, acc), { [coin.coinType]: this.rewardsEarned(Object.assign(Object.assign({}, inputs), { coinType: coin.coinType })) })), {});
48
91
  };
92
+ /**
93
+ * Lists all reward coin types associated with this position.
94
+ *
95
+ * @returns An array of `CoinType` strings representing the reward coins.
96
+ */
49
97
  this.rewardCoinTypes = () => {
50
98
  return this.stakedPosition.rewardCoins.map((coin) => coin.coinType);
51
99
  };
100
+ /**
101
+ * Returns only the reward coin types that currently have a non-zero claimable balance.
102
+ *
103
+ * @param inputs - Contains a reference to the `FarmsStakingPool`.
104
+ * @returns An array of `CoinType` strings that have pending rewards > 0.
105
+ */
52
106
  this.nonZeroRewardCoinTypes = (inputs) => {
53
107
  return Object.entries(this.rewardCoinsToClaimableBalance(inputs))
54
108
  .filter(([, val]) => val > BigInt(0))
55
109
  .map(([key]) => key);
56
110
  };
111
+ /**
112
+ * Retrieves the reward coin record for a specific coin type in this position.
113
+ *
114
+ * @param inputs - Must contain a `coinType` string to look up.
115
+ * @throws If the coin type is not found in this position.
116
+ * @returns The reward coin object from the position.
117
+ */
57
118
  this.rewardCoin = (inputs) => {
58
119
  const foundCoin = this.stakedPosition.rewardCoins.find((coin) => coin.coinType === inputs.coinType);
59
120
  if (!foundCoin)
60
121
  throw new Error("Invalid coin type");
61
122
  return foundCoin;
62
123
  };
124
+ /**
125
+ * Checks if this position has any claimable rewards across all reward coin types.
126
+ *
127
+ * @param inputs - Contains a reference to the `FarmsStakingPool`.
128
+ * @returns `true` if there are unclaimed rewards; otherwise, `false`.
129
+ */
63
130
  this.hasClaimableRewards = (inputs) => {
64
131
  const { stakingPool } = inputs;
65
132
  return (utils_1.Helpers.sumBigInt(this.rewardCoinTypes().map((coinType) => this.rewardsEarned({
@@ -70,27 +137,82 @@ class FarmsStakedPosition extends caller_1.Caller {
70
137
  // =========================================================================
71
138
  // Calculations
72
139
  // =========================================================================
140
+ /**
141
+ * Calculates the current amount of earned rewards for a specific coin type,
142
+ * factoring in any emission constraints and the pool's actual reward availability.
143
+ *
144
+ * @param inputs - Contains the `coinType` to check and a reference to the `FarmsStakingPool`.
145
+ * @returns The total `BigInt` amount of rewards earned for the specified coin type.
146
+ */
73
147
  this.rewardsEarned = (inputs) => {
74
- if (inputs.stakingPool.rewardCoin(inputs).actualRewards === BigInt(0) ||
75
- inputs.stakingPool.rewardCoin(inputs).actualRewards <
76
- inputs.stakingPool.rewardCoin(inputs).emissionRate)
148
+ if (inputs.stakingPool.rewardCoin(inputs).actualRewards === BigInt(0))
77
149
  return BigInt(0);
78
150
  this.updatePosition(inputs);
79
151
  const rewardCoin = this.rewardCoin(inputs);
80
152
  const totalRewards = rewardCoin.multiplierRewardsAccumulated +
81
153
  rewardCoin.baseRewardsAccumulated;
82
- return totalRewards < farms_1.Farms.constants.minimalRewardsToClaim ||
83
- totalRewards > inputs.stakingPool.rewardCoin(inputs).actualRewards
154
+ // If below the minimum threshold to claim, show 0. If the total rewards
155
+ // exceed what's actually in the pool, we clamp it to 0 or a logic fallback.
156
+ if (totalRewards < farms_1.Farms.constants.minRewardsToClaim) {
157
+ return BigInt(0);
158
+ }
159
+ // Additional clamp to handle overshoot beyond actual pool reserves
160
+ return totalRewards >
161
+ inputs.stakingPool.rewardCoin(inputs).actualRewards
84
162
  ? BigInt(0)
85
163
  : totalRewards;
86
164
  };
87
- // Updates the amount of rewards that can be harvested from `self` + the position's
88
- // debt. If the position's lock duration has elapsed, it will be unlocked.
165
+ /**
166
+ * Updates the position's reward calculations based on the pool's current
167
+ * emission state, effectively "syncing" the on-chain logic into this local
168
+ * representation. Also checks if the lock duration has elapsed.
169
+ *
170
+ * @param inputs - Contains a reference to the `FarmsStakingPool`.
171
+ * @remarks This method is typically called before computing `rewardsEarned()`.
172
+ */
89
173
  this.updatePosition = (inputs) => {
90
- const currentTimestamp = (0, dayjs_1.default)().valueOf();
91
- // i. Increase the vault's `rewardsAccumulatedPerShare` values.
92
174
  const stakingPool = new farmsStakingPool_1.FarmsStakingPool(utils_1.Helpers.deepCopy(inputs.stakingPool.stakingPool), this.config);
175
+ // If the lock multiplier is valid, proceed. If not, adjust the staked position
176
+ // to the pool's maximum allowed lock multiplier or duration.
177
+ if (this.stakedPosition.lockDurationMs <=
178
+ stakingPool.stakingPool.maxLockDurationMs &&
179
+ this.stakedPosition.lockMultiplier <=
180
+ stakingPool.stakingPool.maxLockMultiplier) {
181
+ // Lock multiplier is valid; do nothing special
182
+ }
183
+ else {
184
+ // The position's lock duration or multiplier exceeds the pool's max allowed -> clamp
185
+ stakingPool.stakingPool.stakedAmountWithMultiplier -=
186
+ this.stakedPosition.stakedAmountWithMultiplier;
187
+ // ii. Update the `lock_duration` and `lock_multiplier` related fields.
188
+ this.stakedPosition.lockDurationMs =
189
+ stakingPool.stakingPool.maxLockDurationMs;
190
+ this.stakedPosition.lockMultiplier =
191
+ stakingPool.stakingPool.maxLockMultiplier;
192
+ this.stakedPosition.stakedAmountWithMultiplier =
193
+ (this.stakedPosition.stakedAmount *
194
+ (this.stakedPosition.lockMultiplier -
195
+ fixedUtils_1.FixedUtils.fixedOneB)) /
196
+ fixedUtils_1.FixedUtils.fixedOneB;
197
+ this.stakedPosition.rewardCoins = [
198
+ ...this.stakedPosition.rewardCoins.map((rewardCoin) => {
199
+ const currentDebtPerShare = stakingPool.rewardCoin({
200
+ coinType: rewardCoin.coinType,
201
+ }).rewardsAccumulatedPerShare;
202
+ return Object.assign(Object.assign({}, rewardCoin), { multiplierRewardsDebt: (this.stakedPosition.stakedAmountWithMultiplier *
203
+ currentDebtPerShare) /
204
+ fixedUtils_1.FixedUtils.fixedOneB });
205
+ }),
206
+ ];
207
+ // iii. Increase the `Vault`'s `total_staked_amount_with_multiplier` to account for the
208
+ // positions new lock multiplier.
209
+ stakingPool.stakingPool.stakedAmountWithMultiplier +=
210
+ this.stakedPosition.stakedAmountWithMultiplier;
211
+ }
212
+ const currentTimestamp = (0, dayjs_1.default)().valueOf();
213
+ // Accumulate any newly emitted rewards in the pool’s state
93
214
  stakingPool.emitRewards();
215
+ // Update position’s base + multiplier rewards using the updated pool info
94
216
  for (const [rewardCoinIndex, rewardCoin,] of stakingPool.stakingPool.rewardCoins.entries()) {
95
217
  //******************************************************************************************//
96
218
  // debt (i.e. total_rewards_from_time_t0_to_th-1) //
@@ -113,27 +235,12 @@ class FarmsStakedPosition extends caller_1.Caller {
113
235
  });
114
236
  }
115
237
  const stakedPositionRewardCoin = this.stakedPosition.rewardCoins[rewardCoinIndex];
116
- // NOTE: `total_rewards_from_time_t0` contains the amount of rewards a position would receive
117
- // -- from time t0 to the current time -- given the vault's current state when, in reality,
118
- // we need to calculate just the rewards that have accumulated since the last time the
119
- // position's `rewards_accumulated` was updated [labeled time th-1].
120
- //
121
- let [totalBaseRewardsFromTimeT0, totalMultiplierRewardsFromTimeT0] = this.calcTotalRewardsFromTimeT0({
238
+ const [totalBaseRewardsFromTimeT0, totalMultiplierRewardsFromTimeT0,] = this.calcTotalRewardsFromTimeT0({
122
239
  rewardsAccumulatedPerShare: rewardCoin.rewardsAccumulatedPerShare,
123
240
  multiplierRewardsDebt: stakedPositionRewardCoin.multiplierRewardsDebt,
124
241
  emissionEndTimestamp: stakingPool.stakingPool.emissionEndTimestamp,
125
242
  });
126
- // NOTE: Every time a position's `rewards_accumulated` is updated, a snapshot of the total
127
- // rewards received from time t0 is taken and stored as the position's `debt` field. Here,
128
- // the debt is subtracted from `total_rewards_from_time_t0` in order to calculate the amount
129
- // of rewards that have been accumulated since the last time `rewards_accumulated` was
130
- // updated.
131
- //
132
- // `pending_rewards_at_time_th_minus_1` is added to the resulting value to account for all
133
- // rewards that had accumulated up until the last time `update_position` was updated.
134
- //
135
- // iia. Allocate rewards to this position that have been emitted since the last time this
136
- // function was called.
243
+ // Add newly accrued rewards since the last update
137
244
  this.stakedPosition.rewardCoins[rewardCoinIndex].baseRewardsAccumulated =
138
245
  totalBaseRewardsFromTimeT0 -
139
246
  stakedPositionRewardCoin.baseRewardsDebt +
@@ -142,22 +249,21 @@ class FarmsStakedPosition extends caller_1.Caller {
142
249
  totalMultiplierRewardsFromTimeT0 -
143
250
  stakedPositionRewardCoin.multiplierRewardsDebt +
144
251
  stakedPositionRewardCoin.multiplierRewardsAccumulated;
145
- // iib. Set the position's current debt to the total rewards attributed to this position given
146
- // the vault's current state. This allows the next `update_position` call to disregard
147
- // all rewards accumulated up until this point -- preventing double emission cases.
252
+ // Update debts to the new total from time t0
148
253
  this.stakedPosition.rewardCoins[rewardCoinIndex].baseRewardsDebt =
149
254
  totalBaseRewardsFromTimeT0;
150
255
  this.stakedPosition.rewardCoins[rewardCoinIndex].multiplierRewardsDebt = totalMultiplierRewardsFromTimeT0;
151
256
  }
152
- // iii. Remove the position's lock multiplier + bonus staked amount if the position is no
153
- // longer locked.
257
+ // Check if this positions lock has expired
154
258
  if (this.unlockTimestamp() < currentTimestamp) {
155
259
  this.unlock();
156
260
  }
157
261
  this.stakedPosition.lastHarvestRewardsTimestamp = currentTimestamp;
158
262
  };
159
- // Removes a positions `lock_duration_ms` and `lock_multiplier`. Updates the vault's
160
- // `staked_amount_with_multiplier` to account for the lost `lock_multiplier`.
263
+ /**
264
+ * Removes the lock multiplier from this position if the current time is beyond the lock duration,
265
+ * reverting `lockMultiplier` to 1.0 (fixedOneB).
266
+ */
161
267
  this.unlock = () => {
162
268
  // ia. Remove position's `multiplier_staked_amount` from the pool.
163
269
  // afterburner_vault::decrease_stake_with_multiplier(vault, self.multiplier_staked_amount);
@@ -166,29 +272,21 @@ class FarmsStakedPosition extends caller_1.Caller {
166
272
  this.stakedPosition.lockDurationMs = 0;
167
273
  this.stakedPosition.lockMultiplier = fixedUtils_1.FixedUtils.fixedOneB;
168
274
  };
275
+ /**
276
+ * Determines if this position is unlocked based on the lock end timestamp, the emission end timestamp,
277
+ * or a forced unlock condition in the pool.
278
+ */
169
279
  this.isUnlocked = (inputs) => {
170
280
  const { stakingPool } = inputs;
171
- // let emitted_rewards = stakingPool.calc_emitted_rewards();
172
- // let total_rewards = stakingPool.stakingPool.rewardCoins.map(
173
- // (coin) => coin.rewards
174
- // );
175
- // let length = emitted_rewards.length;
176
- // let index = 0;
177
- // let no_rewards_are_remaining = true;
178
- // while (index < length && no_rewards_are_remaining) {
179
- // let emitted = emitted_rewards[index];
180
- // let total = total_rewards[index];
181
- // if (emitted < total) no_rewards_are_remaining = false;
182
- // index = index + 1;
183
- // }
184
- return (this.unlockTimestamp() <= (0, dayjs_1.default)().valueOf() ||
185
- stakingPool.stakingPool.emissionEndTimestamp <= (0, dayjs_1.default)().valueOf() ||
186
- // no_rewards_are_remaining
281
+ const currentTime = (0, dayjs_1.default)().valueOf();
282
+ // If lock has expired, the emission has ended, or the pool is forcibly unlocked, then it is unlocked
283
+ return (this.unlockTimestamp() <= currentTime ||
284
+ stakingPool.stakingPool.emissionEndTimestamp <= currentTime ||
187
285
  stakingPool.stakingPool.isUnlocked);
188
286
  };
189
- // =========================================================================
190
- // Private Helpers
191
- // =========================================================================
287
+ /**
288
+ * Provides access to the `Farms` provider in the `AftermathApi`.
289
+ */
192
290
  this.useProvider = () => {
193
291
  var _a;
194
292
  const provider = (_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Farms();
@@ -200,62 +298,101 @@ class FarmsStakedPosition extends caller_1.Caller {
200
298
  this.trueLastHarvestRewardsTimestamp =
201
299
  trueLastHarvestRewardsTimestamp !== null && trueLastHarvestRewardsTimestamp !== void 0 ? trueLastHarvestRewardsTimestamp : stakedPosition.lastHarvestRewardsTimestamp;
202
300
  }
203
- // public calcTotalApr = (inputs: {
204
- // rewardsUsd: number;
205
- // stakeUsd: number;
206
- // }): Apr => {
207
- // const { rewardsUsd, stakeUsd } = inputs;
208
- // dayjs.extend(duration);
209
- // const oneYearMs = dayjs.duration(1, "year").asMilliseconds();
210
- // const timeSinceLastHarvestMs =
211
- // dayjs().valueOf() - this.trueLastHarvestRewardsTimestamp;
212
- // const rewardsUsdOneYear =
213
- // timeSinceLastHarvestMs > 0
214
- // ? rewardsUsd * (oneYearMs / timeSinceLastHarvestMs)
215
- // : 0;
216
- // const apr = stakeUsd > 0 ? rewardsUsdOneYear / stakeUsd : 0;
217
- // return apr < 0 ? 0 : isNaN(apr) ? 0 : apr;
218
- // };
219
301
  // =========================================================================
220
302
  // Transactions
221
303
  // =========================================================================
222
304
  // =========================================================================
223
305
  // Staking Transactions
224
306
  // =========================================================================
307
+ /**
308
+ * Builds a transaction to deposit additional principal into this staked position.
309
+ *
310
+ * @param inputs - Contains `depositAmount`, the `walletAddress` performing the deposit, and optional sponsorship.
311
+ * @returns A transaction object (or bytes) that can be signed and executed to increase stake.
312
+ */
225
313
  getDepositPrincipalTransaction(inputs) {
226
314
  return __awaiter(this, void 0, void 0, function* () {
227
- return this.useProvider().fetchBuildDepositPrincipalTx(Object.assign(Object.assign({}, inputs), { stakedPositionId: this.stakedPosition.objectId, stakeCoinType: this.stakedPosition.stakeCoinType, stakingPoolId: this.stakedPosition.stakingPoolObjectId }));
315
+ const args = Object.assign(Object.assign({}, inputs), { stakedPositionId: this.stakedPosition.objectId, stakeCoinType: this.stakedPosition.stakeCoinType, stakingPoolId: this.stakedPosition.stakingPoolObjectId });
316
+ return this.version() === 1
317
+ ? this.useProvider().fetchBuildDepositPrincipalTxV1(args)
318
+ : this.useProvider().fetchBuildDepositPrincipalTxV2(args);
228
319
  });
229
320
  }
321
+ /**
322
+ * Builds a transaction to unstake this entire position, optionally claiming SUI as afSUI.
323
+ *
324
+ * @param inputs - Contains `walletAddress`, the `FarmsStakingPool` reference, and optional `claimSuiAsAfSui`.
325
+ * @returns A transaction that can be signed and executed to fully withdraw principal and possibly rewards.
326
+ */
230
327
  getUnstakeTransaction(inputs) {
231
328
  return __awaiter(this, void 0, void 0, function* () {
232
- return this.useProvider().fetchBuildUnstakeTx(Object.assign(Object.assign({}, inputs), { stakedPositionId: this.stakedPosition.objectId, stakeCoinType: this.stakedPosition.stakeCoinType, stakingPoolId: this.stakedPosition.stakingPoolObjectId, withdrawAmount: this.stakedPosition.stakedAmount, rewardCoinTypes: this.nonZeroRewardCoinTypes(inputs) }));
329
+ const args = Object.assign(Object.assign({}, inputs), { stakedPositionId: this.stakedPosition.objectId, stakeCoinType: this.stakedPosition.stakeCoinType, stakingPoolId: this.stakedPosition.stakingPoolObjectId, withdrawAmount: this.stakedPosition.stakedAmount, rewardCoinTypes: this.nonZeroRewardCoinTypes(inputs) });
330
+ return this.version() === 1
331
+ ? this.useProvider().fetchBuildUnstakeTxV1(args)
332
+ : this.useProvider().fetchBuildUnstakeTxV2(args);
233
333
  });
234
334
  }
235
335
  // =========================================================================
236
336
  // Locking Transactions
237
337
  // =========================================================================
338
+ /**
339
+ * Builds a transaction to lock this position for a specified duration, increasing its lock multiplier (if any).
340
+ *
341
+ * @param inputs - Contains the `lockDurationMs` and the `walletAddress`.
342
+ * @returns A transaction that can be signed and executed to lock the position.
343
+ */
238
344
  getLockTransaction(inputs) {
239
345
  return __awaiter(this, void 0, void 0, function* () {
240
- return this.useProvider().buildLockTx(Object.assign(Object.assign({}, inputs), { stakedPositionId: this.stakedPosition.objectId, stakeCoinType: this.stakedPosition.stakeCoinType, stakingPoolId: this.stakedPosition.stakingPoolObjectId }));
346
+ const args = Object.assign(Object.assign({}, inputs), { stakedPositionId: this.stakedPosition.objectId, stakeCoinType: this.stakedPosition.stakeCoinType, stakingPoolId: this.stakedPosition.stakingPoolObjectId });
347
+ return this.version() === 1
348
+ ? this.useProvider().buildLockTxV1(args)
349
+ : this.useProvider().buildLockTxV2(args);
241
350
  });
242
351
  }
352
+ /**
353
+ * Builds a transaction to re-lock this position (renew lock duration) at the current multiplier.
354
+ *
355
+ * @param inputs - Contains the `walletAddress`.
356
+ * @returns A transaction that can be signed and executed to extend or refresh the lock.
357
+ */
243
358
  getRenewLockTransaction(inputs) {
244
359
  return __awaiter(this, void 0, void 0, function* () {
245
- return this.useProvider().buildRenewLockTx(Object.assign(Object.assign({}, inputs), { stakedPositionId: this.stakedPosition.objectId, stakeCoinType: this.stakedPosition.stakeCoinType, stakingPoolId: this.stakedPosition.stakingPoolObjectId }));
360
+ const args = Object.assign(Object.assign({}, inputs), { stakedPositionId: this.stakedPosition.objectId, stakeCoinType: this.stakedPosition.stakeCoinType, stakingPoolId: this.stakedPosition.stakingPoolObjectId });
361
+ return this.version() === 1
362
+ ? this.useProvider().buildRenewLockTxV1(args)
363
+ : this.useProvider().buildRenewLockTxV2(args);
246
364
  });
247
365
  }
366
+ /**
367
+ * Builds a transaction to unlock this position, removing any lock-based multiplier.
368
+ *
369
+ * @param inputs - Contains the `walletAddress`.
370
+ * @returns A transaction that can be signed and executed to unlock the position immediately.
371
+ */
248
372
  getUnlockTransaction(inputs) {
249
373
  return __awaiter(this, void 0, void 0, function* () {
250
- return this.useProvider().buildUnlockTx(Object.assign(Object.assign({}, inputs), { stakedPositionId: this.stakedPosition.objectId, stakeCoinType: this.stakedPosition.stakeCoinType, stakingPoolId: this.stakedPosition.stakingPoolObjectId }));
374
+ const args = Object.assign(Object.assign({}, inputs), { stakedPositionId: this.stakedPosition.objectId, stakeCoinType: this.stakedPosition.stakeCoinType, stakingPoolId: this.stakedPosition.stakingPoolObjectId });
375
+ return this.version() === 1
376
+ ? this.useProvider().buildUnlockTxV1(args)
377
+ : this.useProvider().buildUnlockTxV2(args);
251
378
  });
252
379
  }
253
380
  // =========================================================================
254
381
  // Reward Harvesting Transactions
255
382
  // =========================================================================
383
+ /**
384
+ * Builds a transaction to harvest (claim) the rewards from this position,
385
+ * optionally receiving SUI as afSUI.
386
+ *
387
+ * @param inputs - Includes the `walletAddress`, the `FarmsStakingPool`, and optional `claimSuiAsAfSui`.
388
+ * @returns A transaction that can be signed and executed to claim accrued rewards.
389
+ */
256
390
  getHarvestRewardsTransaction(inputs) {
257
391
  return __awaiter(this, void 0, void 0, function* () {
258
- return this.useProvider().fetchBuildHarvestRewardsTx(Object.assign(Object.assign({}, inputs), { stakedPositionIds: [this.stakedPosition.objectId], stakeCoinType: this.stakedPosition.stakeCoinType, stakingPoolId: this.stakedPosition.stakingPoolObjectId, rewardCoinTypes: this.nonZeroRewardCoinTypes(inputs) }));
392
+ const args = Object.assign(Object.assign({}, inputs), { stakedPositionIds: [this.stakedPosition.objectId], stakeCoinType: this.stakedPosition.stakeCoinType, stakingPoolId: this.stakedPosition.stakingPoolObjectId, rewardCoinTypes: this.nonZeroRewardCoinTypes(inputs) });
393
+ return this.version() === 1
394
+ ? this.useProvider().fetchBuildHarvestRewardsTxV1(args)
395
+ : this.useProvider().fetchBuildHarvestRewardsTxV2(args);
259
396
  });
260
397
  }
261
398
  // =========================================================================
@@ -264,79 +401,48 @@ class FarmsStakedPosition extends caller_1.Caller {
264
401
  // =========================================================================
265
402
  // Calculations
266
403
  // =========================================================================
267
- // Calculates a position's accrued rewards [from time t0] given a vault's
268
- // `rewardsAccumulatedPerShare`. If the position is beyond its lock duration, we need to only
269
- // apply the lock multiplier rewards to the time spent locked.
404
+ /**
405
+ * Calculates the total base + multiplier rewards from time t0 for this position,
406
+ * ensuring that multiplier rewards only apply during the locked period.
407
+ *
408
+ * @param inputs - Contains updated `rewardsAccumulatedPerShare`, the position’s `multiplierRewardsDebt`, and the pool’s `emissionEndTimestamp`.
409
+ * @returns A tuple `[baseRewards, multiplierRewards]`.
410
+ */
270
411
  calcTotalRewardsFromTimeT0(inputs) {
271
412
  const { rewardsAccumulatedPerShare, multiplierRewardsDebt, emissionEndTimestamp, } = inputs;
272
413
  const currentTimestamp = (0, dayjs_1.default)().valueOf();
273
414
  const lastRewardTimestamp = this.stakedPosition.lastHarvestRewardsTimestamp;
274
415
  const lockEndTimestamp = this.unlockTimestamp();
275
416
  const principalStakedAmount = this.stakedPosition.stakedAmount;
276
- // Base [e.g. principal] staked amount receives full (unaltered) rewards.
277
- const rewardsAttributedToPrincipal = (principalStakedAmount * rewardsAccumulatedPerShare) /
417
+ const baseRewards = (principalStakedAmount * rewardsAccumulatedPerShare) /
278
418
  fixedUtils_1.FixedUtils.fixedOneB;
279
- const totalRewardsAttributedToLockMultiplier = (this.stakedPosition.stakedAmountWithMultiplier *
419
+ const totalMultiplierRewards = (this.stakedPosition.stakedAmountWithMultiplier *
280
420
  rewardsAccumulatedPerShare) /
281
421
  fixedUtils_1.FixedUtils.fixedOneB;
282
- // The position should only receive multiplied rewards for the time that was spent locked since
283
- // the last harvest. This case occurs when the user calls `pending_rewards` after the position's
284
- // lock duration has expired.
285
- const rewardsAttributedToLockMultiplier = (() => {
286
- return currentTimestamp <= lockEndTimestamp
287
- ? //*********************************************************************************************//
288
- // v //
289
- // |-------------------------------------------+-------------------------------------------| //
290
- // last_reward_timestamp_ms current_timestamp_ms lock_end_timestamp_ms //
291
- //*********************************************************************************************//
292
- totalRewardsAttributedToLockMultiplier
293
- : lockEndTimestamp <= lastRewardTimestamp
294
- ? //*********************************************************************************************//
295
- // emission_end_timestamp_ms lock_end_timestamp_ms v //
296
- // |----------------------------+-----------------------------+----------------------------| //
297
- // last_reward_timestamp_ms current_timestamp_ms //
298
- //*********************************************************************************************//
299
- //
300
- // NOTE: if the lock period was longer than the rewards emission, the position receives the full
301
- // multiplier rewards
302
- multiplierRewardsDebt
303
- : emissionEndTimestamp <= lockEndTimestamp
304
- ? totalRewardsAttributedToLockMultiplier
305
- : (() => {
306
- //*********************************************************************************************//
307
- // lock_end_timestamp_ms emission_end_timestamp_ms v //
308
- // |----------------------------+-----------------------------+----------------------------| //
309
- // last_reward_timestamp_ms current_timestamp_ms //
310
- //*********************************************************************************************//
311
- //
312
- // NOTE: there is no enforced ordering of `emission_end_timestamp_ms` and `current_timestamp_ms`
313
- // by the time we get to this branch.
314
- // Multiplier staked amount receives (altered) rewards dependent on the total time the
315
- // position was locked since the last harvest.
316
- const timeSpentLockedSinceLastHarvestMs = lockEndTimestamp - lastRewardTimestamp;
317
- const timeSinceLastHarvestMs = currentTimestamp - lastRewardTimestamp;
318
- // ********************************************************************************************//
319
- // / timeSpentLockedSinceLastHarvestMs \ //
320
- // | ----------------------------------------- | x totalRewardsAttributedToLockMultiplier //
321
- // \ timeSinceLastHarvest / //
322
- // ********************************************************************************************//
323
- // Only disperse the multiplied rewards that were received while this position was locked.
324
- //
325
- // We are assigning this value to the total_multiplier_rewards later and this number should
326
- // never decrease, so we use max() here.
327
- const possibleMultiplierRewardsDebt = (totalRewardsAttributedToLockMultiplier *
328
- BigInt(Math.floor(timeSpentLockedSinceLastHarvestMs))) /
329
- BigInt(Math.floor(timeSinceLastHarvestMs));
330
- return possibleMultiplierRewardsDebt >
331
- multiplierRewardsDebt
332
- ? possibleMultiplierRewardsDebt
333
- : multiplierRewardsDebt;
334
- })();
422
+ const multiplierRewards = (() => {
423
+ // If position is fully locked throughout the last harvest period
424
+ if (currentTimestamp <= lockEndTimestamp) {
425
+ return totalMultiplierRewards;
426
+ }
427
+ // If lock ended before or at the last harvest, fallback to previously calculated debt
428
+ if (lockEndTimestamp <= lastRewardTimestamp) {
429
+ return multiplierRewardsDebt;
430
+ }
431
+ // If emission ended while still locked, or fully locked within emission window
432
+ if (emissionEndTimestamp <= lockEndTimestamp) {
433
+ return totalMultiplierRewards;
434
+ }
435
+ // Otherwise, partial locking scenario
436
+ const timeSpentLockedSinceLastHarvestMs = lockEndTimestamp - lastRewardTimestamp;
437
+ const timeSinceLastHarvestMs = currentTimestamp - lastRewardTimestamp;
438
+ const possibleMultiplierRewardsDebt = (totalMultiplierRewards *
439
+ BigInt(Math.floor(timeSpentLockedSinceLastHarvestMs))) /
440
+ BigInt(Math.floor(timeSinceLastHarvestMs));
441
+ return possibleMultiplierRewardsDebt > multiplierRewardsDebt
442
+ ? possibleMultiplierRewardsDebt
443
+ : multiplierRewardsDebt;
335
444
  })();
336
- return [
337
- rewardsAttributedToPrincipal,
338
- rewardsAttributedToLockMultiplier,
339
- ];
445
+ return [baseRewards, multiplierRewards];
340
446
  }
341
447
  }
342
448
  exports.FarmsStakedPosition = FarmsStakedPosition;