aftermath-ts-sdk 1.2.64 → 1.2.65
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/general/dynamicGas/dynamicGas.d.ts +34 -0
- package/dist/general/dynamicGas/dynamicGas.d.ts.map +1 -1
- package/dist/general/dynamicGas/dynamicGas.js +34 -0
- package/dist/general/dynamicGas/dynamicGasTypes.d.ts +25 -0
- package/dist/general/dynamicGas/dynamicGasTypes.d.ts.map +1 -1
- package/dist/general/prices/prices.d.ts +73 -0
- package/dist/general/prices/prices.d.ts.map +1 -1
- package/dist/general/prices/prices.js +73 -0
- package/dist/general/providers/aftermath.d.ts +106 -25
- package/dist/general/providers/aftermath.d.ts.map +1 -1
- package/dist/general/providers/aftermath.js +108 -27
- package/dist/general/providers/aftermathApi.d.ts +127 -8
- package/dist/general/providers/aftermathApi.d.ts.map +1 -1
- package/dist/general/providers/aftermathApi.js +132 -12
- package/dist/general/types/castingTypes.d.ts +0 -7
- package/dist/general/types/castingTypes.d.ts.map +1 -1
- package/dist/general/types/castingTypes.js +6 -0
- package/dist/general/types/generalTypes.d.ts +228 -7
- package/dist/general/types/generalTypes.d.ts.map +1 -1
- package/dist/general/types/suiTypes.d.ts +0 -16
- package/dist/general/types/suiTypes.d.ts.map +1 -1
- package/dist/general/types/suiTypes.js +3 -25
- package/dist/general/utils/casting.d.ts +159 -3
- package/dist/general/utils/casting.d.ts.map +1 -1
- package/dist/general/utils/casting.js +162 -16
- package/dist/general/utils/fixedUtils.d.ts +89 -2
- package/dist/general/utils/fixedUtils.d.ts.map +1 -1
- package/dist/general/utils/fixedUtils.js +81 -4
- package/dist/general/utils/helpers.d.ts +320 -9
- package/dist/general/utils/helpers.d.ts.map +1 -1
- package/dist/general/utils/helpers.js +350 -89
- package/dist/general/utils/iFixedUtils.d.ts +64 -0
- package/dist/general/utils/iFixedUtils.d.ts.map +1 -1
- package/dist/general/utils/iFixedUtils.js +64 -0
- package/dist/general/wallet/wallet.d.ts +75 -0
- package/dist/general/wallet/wallet.d.ts.map +1 -1
- package/dist/general/wallet/wallet.js +75 -5
- package/dist/packages/auth/auth.d.ts +113 -0
- package/dist/packages/auth/auth.d.ts.map +1 -1
- package/dist/packages/auth/auth.js +129 -15
- package/dist/packages/auth/authTypes.d.ts +66 -0
- package/dist/packages/auth/authTypes.d.ts.map +1 -1
- package/dist/packages/coin/coin.d.ts +250 -0
- package/dist/packages/coin/coin.d.ts.map +1 -1
- package/dist/packages/coin/coin.js +238 -14
- package/dist/packages/coin/coinTypes.d.ts +93 -0
- package/dist/packages/coin/coinTypes.d.ts.map +1 -1
- package/dist/packages/dca/dca.d.ts +124 -35
- package/dist/packages/dca/dca.d.ts.map +1 -1
- package/dist/packages/dca/dca.js +127 -37
- package/dist/packages/dca/dcaTypes.d.ts +213 -28
- package/dist/packages/dca/dcaTypes.d.ts.map +1 -1
- package/dist/packages/farms/farms.d.ts +184 -1
- package/dist/packages/farms/farms.d.ts.map +1 -1
- package/dist/packages/farms/farms.js +185 -11
- package/dist/packages/farms/farmsStakedPosition.d.ts +140 -6
- package/dist/packages/farms/farmsStakedPosition.d.ts.map +1 -1
- package/dist/packages/farms/farmsStakedPosition.js +189 -144
- package/dist/packages/farms/farmsStakingPool.d.ts +161 -1
- package/dist/packages/farms/farmsStakingPool.d.ts.map +1 -1
- package/dist/packages/farms/farmsStakingPool.js +176 -70
- package/dist/packages/farms/farmsTypes.d.ts +283 -2
- package/dist/packages/farms/farmsTypes.d.ts.map +1 -1
- package/dist/packages/farms/farmsTypes.js +18 -0
- package/dist/packages/limitOrders/limitOrders.d.ts +108 -21
- package/dist/packages/limitOrders/limitOrders.d.ts.map +1 -1
- package/dist/packages/limitOrders/limitOrders.js +110 -22
- package/dist/packages/limitOrders/limitOrdersTypes.d.ts +142 -0
- package/dist/packages/limitOrders/limitOrdersTypes.d.ts.map +1 -1
- package/dist/packages/multisig/multisig.d.ts +33 -4
- package/dist/packages/multisig/multisig.d.ts.map +1 -1
- package/dist/packages/multisig/multisig.js +33 -4
- package/dist/packages/multisig/multisigTypes.d.ts +17 -0
- package/dist/packages/multisig/multisigTypes.d.ts.map +1 -1
- package/dist/packages/perpetuals/api/perpetualsApi.d.ts.map +1 -1
- package/dist/packages/pools/pool.d.ts +327 -85
- package/dist/packages/pools/pool.d.ts.map +1 -1
- package/dist/packages/pools/pool.js +333 -91
- package/dist/packages/pools/pools.d.ts +299 -37
- package/dist/packages/pools/pools.d.ts.map +1 -1
- package/dist/packages/pools/pools.js +306 -52
- package/dist/packages/pools/poolsTypes.d.ts +267 -2
- package/dist/packages/pools/poolsTypes.d.ts.map +1 -1
- package/dist/packages/referralVault/referralVault.d.ts +37 -0
- package/dist/packages/referralVault/referralVault.d.ts.map +1 -1
- package/dist/packages/referralVault/referralVault.js +37 -0
- package/dist/packages/router/router.d.ts +213 -17
- package/dist/packages/router/router.d.ts.map +1 -1
- package/dist/packages/router/router.js +214 -18
- package/dist/packages/router/routerTypes.d.ts +198 -14
- package/dist/packages/router/routerTypes.d.ts.map +1 -1
- package/dist/packages/staking/staking.d.ts +301 -43
- package/dist/packages/staking/staking.d.ts.map +1 -1
- package/dist/packages/staking/staking.js +308 -51
- package/dist/packages/staking/stakingTypes.d.ts +426 -6
- package/dist/packages/staking/stakingTypes.d.ts.map +1 -1
- package/dist/packages/staking/stakingTypes.js +19 -0
- package/dist/packages/sui/sui.d.ts +39 -0
- package/dist/packages/sui/sui.d.ts.map +1 -1
- package/dist/packages/sui/sui.js +39 -0
- package/dist/packages/userData/userData.d.ts +66 -12
- package/dist/packages/userData/userData.d.ts.map +1 -1
- package/dist/packages/userData/userData.js +66 -12
- package/dist/packages/userData/userDataTypes.d.ts +19 -0
- package/dist/packages/userData/userDataTypes.d.ts.map +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +0 -1
- package/package.json +1 -1
- package/dist/general/historicalData/historicalData.d.ts +0 -15
- package/dist/general/historicalData/historicalData.d.ts.map +0 -1
- package/dist/general/historicalData/historicalData.js +0 -45
- package/dist/general/historicalData/historicalDataTypes.d.ts +0 -10
- package/dist/general/historicalData/historicalDataTypes.d.ts.map +0 -1
- package/dist/general/historicalData/historicalDataTypes.js +0 -2
|
@@ -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,36 +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
|
+
*/
|
|
36
53
|
this.version = () => {
|
|
37
54
|
return this.stakedPosition.version;
|
|
38
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
|
+
*/
|
|
39
62
|
this.isLocked = (inputs) => {
|
|
40
63
|
return !this.isUnlocked(inputs);
|
|
41
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
|
+
*/
|
|
42
70
|
this.isLockDuration = () => {
|
|
43
71
|
return this.stakedPosition.lockDurationMs > 0;
|
|
44
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
|
+
*/
|
|
45
78
|
this.unlockTimestamp = () => {
|
|
46
79
|
return (this.stakedPosition.lockStartTimestamp +
|
|
47
80
|
this.stakedPosition.lockDurationMs);
|
|
48
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
|
+
*/
|
|
49
89
|
this.rewardCoinsToClaimableBalance = (inputs) => {
|
|
50
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 })) })), {});
|
|
51
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
|
+
*/
|
|
52
97
|
this.rewardCoinTypes = () => {
|
|
53
98
|
return this.stakedPosition.rewardCoins.map((coin) => coin.coinType);
|
|
54
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
|
+
*/
|
|
55
106
|
this.nonZeroRewardCoinTypes = (inputs) => {
|
|
56
107
|
return Object.entries(this.rewardCoinsToClaimableBalance(inputs))
|
|
57
108
|
.filter(([, val]) => val > BigInt(0))
|
|
58
109
|
.map(([key]) => key);
|
|
59
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
|
+
*/
|
|
60
118
|
this.rewardCoin = (inputs) => {
|
|
61
119
|
const foundCoin = this.stakedPosition.rewardCoins.find((coin) => coin.coinType === inputs.coinType);
|
|
62
120
|
if (!foundCoin)
|
|
63
121
|
throw new Error("Invalid coin type");
|
|
64
122
|
return foundCoin;
|
|
65
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
|
+
*/
|
|
66
130
|
this.hasClaimableRewards = (inputs) => {
|
|
67
131
|
const { stakingPool } = inputs;
|
|
68
132
|
return (utils_1.Helpers.sumBigInt(this.rewardCoinTypes().map((coinType) => this.rewardsEarned({
|
|
@@ -73,6 +137,13 @@ class FarmsStakedPosition extends caller_1.Caller {
|
|
|
73
137
|
// =========================================================================
|
|
74
138
|
// Calculations
|
|
75
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
|
+
*/
|
|
76
147
|
this.rewardsEarned = (inputs) => {
|
|
77
148
|
if (inputs.stakingPool.rewardCoin(inputs).actualRewards === BigInt(0))
|
|
78
149
|
return BigInt(0);
|
|
@@ -80,30 +151,37 @@ class FarmsStakedPosition extends caller_1.Caller {
|
|
|
80
151
|
const rewardCoin = this.rewardCoin(inputs);
|
|
81
152
|
const totalRewards = rewardCoin.multiplierRewardsAccumulated +
|
|
82
153
|
rewardCoin.baseRewardsAccumulated;
|
|
83
|
-
|
|
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
|
-
: totalRewards
|
|
86
|
-
? // ? Helpers.minBigInt(
|
|
87
|
-
// inputs.stakingPool.rewardCoin(inputs).actualRewards,
|
|
88
|
-
// inputs.stakingPool.rewardCoin(inputs).emissionRate
|
|
89
|
-
// )
|
|
90
|
-
BigInt(0)
|
|
91
|
-
: totalRewards;
|
|
163
|
+
: totalRewards;
|
|
92
164
|
};
|
|
93
|
-
|
|
94
|
-
|
|
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
|
+
*/
|
|
95
173
|
this.updatePosition = (inputs) => {
|
|
96
174
|
const stakingPool = new farmsStakingPool_1.FarmsStakingPool(utils_1.Helpers.deepCopy(inputs.stakingPool.stakingPool), this.config);
|
|
97
|
-
// If the
|
|
98
|
-
//
|
|
99
|
-
// the vault's limits.
|
|
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.
|
|
100
177
|
if (this.stakedPosition.lockDurationMs <=
|
|
101
178
|
stakingPool.stakingPool.maxLockDurationMs &&
|
|
102
179
|
this.stakedPosition.lockMultiplier <=
|
|
103
180
|
stakingPool.stakingPool.maxLockMultiplier) {
|
|
181
|
+
// Lock multiplier is valid; do nothing special
|
|
104
182
|
}
|
|
105
183
|
else {
|
|
106
|
-
//
|
|
184
|
+
// The position's lock duration or multiplier exceeds the pool's max allowed -> clamp
|
|
107
185
|
stakingPool.stakingPool.stakedAmountWithMultiplier -=
|
|
108
186
|
this.stakedPosition.stakedAmountWithMultiplier;
|
|
109
187
|
// ii. Update the `lock_duration` and `lock_multiplier` related fields.
|
|
@@ -117,14 +195,14 @@ class FarmsStakedPosition extends caller_1.Caller {
|
|
|
117
195
|
fixedUtils_1.FixedUtils.fixedOneB)) /
|
|
118
196
|
fixedUtils_1.FixedUtils.fixedOneB;
|
|
119
197
|
this.stakedPosition.rewardCoins = [
|
|
120
|
-
...this.stakedPosition.rewardCoins.map((rewardCoin) =>
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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 *
|
|
125
203
|
currentDebtPerShare) /
|
|
126
|
-
fixedUtils_1.FixedUtils.fixedOneB);
|
|
127
|
-
|
|
204
|
+
fixedUtils_1.FixedUtils.fixedOneB });
|
|
205
|
+
}),
|
|
128
206
|
];
|
|
129
207
|
// iii. Increase the `Vault`'s `total_staked_amount_with_multiplier` to account for the
|
|
130
208
|
// positions new lock multiplier.
|
|
@@ -132,8 +210,9 @@ class FarmsStakedPosition extends caller_1.Caller {
|
|
|
132
210
|
this.stakedPosition.stakedAmountWithMultiplier;
|
|
133
211
|
}
|
|
134
212
|
const currentTimestamp = (0, dayjs_1.default)().valueOf();
|
|
135
|
-
//
|
|
213
|
+
// Accumulate any newly emitted rewards in the pool’s state
|
|
136
214
|
stakingPool.emitRewards();
|
|
215
|
+
// Update position’s base + multiplier rewards using the updated pool info
|
|
137
216
|
for (const [rewardCoinIndex, rewardCoin,] of stakingPool.stakingPool.rewardCoins.entries()) {
|
|
138
217
|
//******************************************************************************************//
|
|
139
218
|
// debt (i.e. total_rewards_from_time_t0_to_th-1) //
|
|
@@ -156,27 +235,12 @@ class FarmsStakedPosition extends caller_1.Caller {
|
|
|
156
235
|
});
|
|
157
236
|
}
|
|
158
237
|
const stakedPositionRewardCoin = this.stakedPosition.rewardCoins[rewardCoinIndex];
|
|
159
|
-
|
|
160
|
-
// -- from time t0 to the current time -- given the vault's current state when, in reality,
|
|
161
|
-
// we need to calculate just the rewards that have accumulated since the last time the
|
|
162
|
-
// position's `rewards_accumulated` was updated [labeled time th-1].
|
|
163
|
-
//
|
|
164
|
-
let [totalBaseRewardsFromTimeT0, totalMultiplierRewardsFromTimeT0] = this.calcTotalRewardsFromTimeT0({
|
|
238
|
+
const [totalBaseRewardsFromTimeT0, totalMultiplierRewardsFromTimeT0,] = this.calcTotalRewardsFromTimeT0({
|
|
165
239
|
rewardsAccumulatedPerShare: rewardCoin.rewardsAccumulatedPerShare,
|
|
166
240
|
multiplierRewardsDebt: stakedPositionRewardCoin.multiplierRewardsDebt,
|
|
167
241
|
emissionEndTimestamp: stakingPool.stakingPool.emissionEndTimestamp,
|
|
168
242
|
});
|
|
169
|
-
//
|
|
170
|
-
// rewards received from time t0 is taken and stored as the position's `debt` field. Here,
|
|
171
|
-
// the debt is subtracted from `total_rewards_from_time_t0` in order to calculate the amount
|
|
172
|
-
// of rewards that have been accumulated since the last time `rewards_accumulated` was
|
|
173
|
-
// updated.
|
|
174
|
-
//
|
|
175
|
-
// `pending_rewards_at_time_th_minus_1` is added to the resulting value to account for all
|
|
176
|
-
// rewards that had accumulated up until the last time `update_position` was updated.
|
|
177
|
-
//
|
|
178
|
-
// iia. Allocate rewards to this position that have been emitted since the last time this
|
|
179
|
-
// function was called.
|
|
243
|
+
// Add newly accrued rewards since the last update
|
|
180
244
|
this.stakedPosition.rewardCoins[rewardCoinIndex].baseRewardsAccumulated =
|
|
181
245
|
totalBaseRewardsFromTimeT0 -
|
|
182
246
|
stakedPositionRewardCoin.baseRewardsDebt +
|
|
@@ -185,22 +249,21 @@ class FarmsStakedPosition extends caller_1.Caller {
|
|
|
185
249
|
totalMultiplierRewardsFromTimeT0 -
|
|
186
250
|
stakedPositionRewardCoin.multiplierRewardsDebt +
|
|
187
251
|
stakedPositionRewardCoin.multiplierRewardsAccumulated;
|
|
188
|
-
//
|
|
189
|
-
// the vault's current state. This allows the next `update_position` call to disregard
|
|
190
|
-
// all rewards accumulated up until this point -- preventing double emission cases.
|
|
252
|
+
// Update debts to the new total from time t0
|
|
191
253
|
this.stakedPosition.rewardCoins[rewardCoinIndex].baseRewardsDebt =
|
|
192
254
|
totalBaseRewardsFromTimeT0;
|
|
193
255
|
this.stakedPosition.rewardCoins[rewardCoinIndex].multiplierRewardsDebt = totalMultiplierRewardsFromTimeT0;
|
|
194
256
|
}
|
|
195
|
-
//
|
|
196
|
-
// longer locked.
|
|
257
|
+
// Check if this position’s lock has expired
|
|
197
258
|
if (this.unlockTimestamp() < currentTimestamp) {
|
|
198
259
|
this.unlock();
|
|
199
260
|
}
|
|
200
261
|
this.stakedPosition.lastHarvestRewardsTimestamp = currentTimestamp;
|
|
201
262
|
};
|
|
202
|
-
|
|
203
|
-
|
|
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
|
+
*/
|
|
204
267
|
this.unlock = () => {
|
|
205
268
|
// ia. Remove position's `multiplier_staked_amount` from the pool.
|
|
206
269
|
// afterburner_vault::decrease_stake_with_multiplier(vault, self.multiplier_staked_amount);
|
|
@@ -209,29 +272,21 @@ class FarmsStakedPosition extends caller_1.Caller {
|
|
|
209
272
|
this.stakedPosition.lockDurationMs = 0;
|
|
210
273
|
this.stakedPosition.lockMultiplier = fixedUtils_1.FixedUtils.fixedOneB;
|
|
211
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
|
+
*/
|
|
212
279
|
this.isUnlocked = (inputs) => {
|
|
213
280
|
const { stakingPool } = inputs;
|
|
214
|
-
|
|
215
|
-
//
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
// let length = emitted_rewards.length;
|
|
219
|
-
// let index = 0;
|
|
220
|
-
// let no_rewards_are_remaining = true;
|
|
221
|
-
// while (index < length && no_rewards_are_remaining) {
|
|
222
|
-
// let emitted = emitted_rewards[index];
|
|
223
|
-
// let total = total_rewards[index];
|
|
224
|
-
// if (emitted < total) no_rewards_are_remaining = false;
|
|
225
|
-
// index = index + 1;
|
|
226
|
-
// }
|
|
227
|
-
return (this.unlockTimestamp() <= (0, dayjs_1.default)().valueOf() ||
|
|
228
|
-
stakingPool.stakingPool.emissionEndTimestamp <= (0, dayjs_1.default)().valueOf() ||
|
|
229
|
-
// 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 ||
|
|
230
285
|
stakingPool.stakingPool.isUnlocked);
|
|
231
286
|
};
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
287
|
+
/**
|
|
288
|
+
* Provides access to the `Farms` provider in the `AftermathApi`.
|
|
289
|
+
*/
|
|
235
290
|
this.useProvider = () => {
|
|
236
291
|
var _a;
|
|
237
292
|
const provider = (_a = this.Provider) === null || _a === void 0 ? void 0 : _a.Farms();
|
|
@@ -243,28 +298,18 @@ class FarmsStakedPosition extends caller_1.Caller {
|
|
|
243
298
|
this.trueLastHarvestRewardsTimestamp =
|
|
244
299
|
trueLastHarvestRewardsTimestamp !== null && trueLastHarvestRewardsTimestamp !== void 0 ? trueLastHarvestRewardsTimestamp : stakedPosition.lastHarvestRewardsTimestamp;
|
|
245
300
|
}
|
|
246
|
-
// public calcTotalApr = (inputs: {
|
|
247
|
-
// rewardsUsd: number;
|
|
248
|
-
// stakeUsd: number;
|
|
249
|
-
// }): Apr => {
|
|
250
|
-
// const { rewardsUsd, stakeUsd } = inputs;
|
|
251
|
-
// dayjs.extend(duration);
|
|
252
|
-
// const oneYearMs = dayjs.duration(1, "year").asMilliseconds();
|
|
253
|
-
// const timeSinceLastHarvestMs =
|
|
254
|
-
// dayjs().valueOf() - this.trueLastHarvestRewardsTimestamp;
|
|
255
|
-
// const rewardsUsdOneYear =
|
|
256
|
-
// timeSinceLastHarvestMs > 0
|
|
257
|
-
// ? rewardsUsd * (oneYearMs / timeSinceLastHarvestMs)
|
|
258
|
-
// : 0;
|
|
259
|
-
// const apr = stakeUsd > 0 ? rewardsUsdOneYear / stakeUsd : 0;
|
|
260
|
-
// return apr < 0 ? 0 : isNaN(apr) ? 0 : apr;
|
|
261
|
-
// };
|
|
262
301
|
// =========================================================================
|
|
263
302
|
// Transactions
|
|
264
303
|
// =========================================================================
|
|
265
304
|
// =========================================================================
|
|
266
305
|
// Staking Transactions
|
|
267
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
|
+
*/
|
|
268
313
|
getDepositPrincipalTransaction(inputs) {
|
|
269
314
|
return __awaiter(this, void 0, void 0, function* () {
|
|
270
315
|
const args = Object.assign(Object.assign({}, inputs), { stakedPositionId: this.stakedPosition.objectId, stakeCoinType: this.stakedPosition.stakeCoinType, stakingPoolId: this.stakedPosition.stakingPoolObjectId });
|
|
@@ -273,6 +318,12 @@ class FarmsStakedPosition extends caller_1.Caller {
|
|
|
273
318
|
: this.useProvider().fetchBuildDepositPrincipalTxV2(args);
|
|
274
319
|
});
|
|
275
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
|
+
*/
|
|
276
327
|
getUnstakeTransaction(inputs) {
|
|
277
328
|
return __awaiter(this, void 0, void 0, function* () {
|
|
278
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) });
|
|
@@ -284,6 +335,12 @@ class FarmsStakedPosition extends caller_1.Caller {
|
|
|
284
335
|
// =========================================================================
|
|
285
336
|
// Locking Transactions
|
|
286
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
|
+
*/
|
|
287
344
|
getLockTransaction(inputs) {
|
|
288
345
|
return __awaiter(this, void 0, void 0, function* () {
|
|
289
346
|
const args = Object.assign(Object.assign({}, inputs), { stakedPositionId: this.stakedPosition.objectId, stakeCoinType: this.stakedPosition.stakeCoinType, stakingPoolId: this.stakedPosition.stakingPoolObjectId });
|
|
@@ -292,6 +349,12 @@ class FarmsStakedPosition extends caller_1.Caller {
|
|
|
292
349
|
: this.useProvider().buildLockTxV2(args);
|
|
293
350
|
});
|
|
294
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
|
+
*/
|
|
295
358
|
getRenewLockTransaction(inputs) {
|
|
296
359
|
return __awaiter(this, void 0, void 0, function* () {
|
|
297
360
|
const args = Object.assign(Object.assign({}, inputs), { stakedPositionId: this.stakedPosition.objectId, stakeCoinType: this.stakedPosition.stakeCoinType, stakingPoolId: this.stakedPosition.stakingPoolObjectId });
|
|
@@ -300,6 +363,12 @@ class FarmsStakedPosition extends caller_1.Caller {
|
|
|
300
363
|
: this.useProvider().buildRenewLockTxV2(args);
|
|
301
364
|
});
|
|
302
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
|
+
*/
|
|
303
372
|
getUnlockTransaction(inputs) {
|
|
304
373
|
return __awaiter(this, void 0, void 0, function* () {
|
|
305
374
|
const args = Object.assign(Object.assign({}, inputs), { stakedPositionId: this.stakedPosition.objectId, stakeCoinType: this.stakedPosition.stakeCoinType, stakingPoolId: this.stakedPosition.stakingPoolObjectId });
|
|
@@ -311,6 +380,13 @@ class FarmsStakedPosition extends caller_1.Caller {
|
|
|
311
380
|
// =========================================================================
|
|
312
381
|
// Reward Harvesting Transactions
|
|
313
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
|
+
*/
|
|
314
390
|
getHarvestRewardsTransaction(inputs) {
|
|
315
391
|
return __awaiter(this, void 0, void 0, function* () {
|
|
316
392
|
const args = Object.assign(Object.assign({}, inputs), { stakedPositionIds: [this.stakedPosition.objectId], stakeCoinType: this.stakedPosition.stakeCoinType, stakingPoolId: this.stakedPosition.stakingPoolObjectId, rewardCoinTypes: this.nonZeroRewardCoinTypes(inputs) });
|
|
@@ -325,79 +401,48 @@ class FarmsStakedPosition extends caller_1.Caller {
|
|
|
325
401
|
// =========================================================================
|
|
326
402
|
// Calculations
|
|
327
403
|
// =========================================================================
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
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
|
+
*/
|
|
331
411
|
calcTotalRewardsFromTimeT0(inputs) {
|
|
332
412
|
const { rewardsAccumulatedPerShare, multiplierRewardsDebt, emissionEndTimestamp, } = inputs;
|
|
333
413
|
const currentTimestamp = (0, dayjs_1.default)().valueOf();
|
|
334
414
|
const lastRewardTimestamp = this.stakedPosition.lastHarvestRewardsTimestamp;
|
|
335
415
|
const lockEndTimestamp = this.unlockTimestamp();
|
|
336
416
|
const principalStakedAmount = this.stakedPosition.stakedAmount;
|
|
337
|
-
|
|
338
|
-
const rewardsAttributedToPrincipal = (principalStakedAmount * rewardsAccumulatedPerShare) /
|
|
417
|
+
const baseRewards = (principalStakedAmount * rewardsAccumulatedPerShare) /
|
|
339
418
|
fixedUtils_1.FixedUtils.fixedOneB;
|
|
340
|
-
const
|
|
419
|
+
const totalMultiplierRewards = (this.stakedPosition.stakedAmountWithMultiplier *
|
|
341
420
|
rewardsAccumulatedPerShare) /
|
|
342
421
|
fixedUtils_1.FixedUtils.fixedOneB;
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
? totalRewardsAttributedToLockMultiplier
|
|
366
|
-
: (() => {
|
|
367
|
-
//*********************************************************************************************//
|
|
368
|
-
// lock_end_timestamp_ms emission_end_timestamp_ms v //
|
|
369
|
-
// |----------------------------+-----------------------------+----------------------------| //
|
|
370
|
-
// last_reward_timestamp_ms current_timestamp_ms //
|
|
371
|
-
//*********************************************************************************************//
|
|
372
|
-
//
|
|
373
|
-
// NOTE: there is no enforced ordering of `emission_end_timestamp_ms` and `current_timestamp_ms`
|
|
374
|
-
// by the time we get to this branch.
|
|
375
|
-
// Multiplier staked amount receives (altered) rewards dependent on the total time the
|
|
376
|
-
// position was locked since the last harvest.
|
|
377
|
-
const timeSpentLockedSinceLastHarvestMs = lockEndTimestamp - lastRewardTimestamp;
|
|
378
|
-
const timeSinceLastHarvestMs = currentTimestamp - lastRewardTimestamp;
|
|
379
|
-
// ********************************************************************************************//
|
|
380
|
-
// / timeSpentLockedSinceLastHarvestMs \ //
|
|
381
|
-
// | ----------------------------------------- | x totalRewardsAttributedToLockMultiplier //
|
|
382
|
-
// \ timeSinceLastHarvest / //
|
|
383
|
-
// ********************************************************************************************//
|
|
384
|
-
// Only disperse the multiplied rewards that were received while this position was locked.
|
|
385
|
-
//
|
|
386
|
-
// We are assigning this value to the total_multiplier_rewards later and this number should
|
|
387
|
-
// never decrease, so we use max() here.
|
|
388
|
-
const possibleMultiplierRewardsDebt = (totalRewardsAttributedToLockMultiplier *
|
|
389
|
-
BigInt(Math.floor(timeSpentLockedSinceLastHarvestMs))) /
|
|
390
|
-
BigInt(Math.floor(timeSinceLastHarvestMs));
|
|
391
|
-
return possibleMultiplierRewardsDebt >
|
|
392
|
-
multiplierRewardsDebt
|
|
393
|
-
? possibleMultiplierRewardsDebt
|
|
394
|
-
: multiplierRewardsDebt;
|
|
395
|
-
})();
|
|
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;
|
|
396
444
|
})();
|
|
397
|
-
return [
|
|
398
|
-
rewardsAttributedToPrincipal,
|
|
399
|
-
rewardsAttributedToLockMultiplier,
|
|
400
|
-
];
|
|
445
|
+
return [baseRewards, multiplierRewards];
|
|
401
446
|
}
|
|
402
447
|
}
|
|
403
448
|
exports.FarmsStakedPosition = FarmsStakedPosition;
|