@@ -3,7 +3,6 @@ package pools
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "strings"
7
6
8
7
"cosmossdk.io/math"
9
8
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -29,10 +28,6 @@ type routableAlloyTransmuterPoolImpl struct {
29
28
SpreadFactor osmomath.Dec "json:\" spread_factor\" "
30
29
}
31
30
32
- const (
33
- alloyedLPShareDenomComponent = "all"
34
- )
35
-
36
31
// GetId implements domain.RoutablePool.
37
32
func (r * routableAlloyTransmuterPoolImpl ) GetId () uint64 {
38
33
return r .ChainPool .PoolId
@@ -187,7 +182,6 @@ func (r *routableAlloyTransmuterPoolImpl) CalcTokenOutAmt(tokenIn sdk.Coin, toke
187
182
}
188
183
189
184
// Check static upper rate limiter
190
- // We only need to check it for the token in coin since that is the only one that is increased by the current quote.
191
185
if err := r .checkStaticRateLimiter (tokenIn ); err != nil {
192
186
return osmomath.BigDec {}, err
193
187
}
@@ -202,9 +196,14 @@ func (r *routableAlloyTransmuterPoolImpl) CalcTokenOutAmt(tokenIn sdk.Coin, toke
202
196
return tokenOutAmount , nil
203
197
}
204
198
205
- // checkStaticRateLimiter checks the static rate limiter for the token in coin.
199
+ // checkStaticRateLimiter checks the static rate limiter.
200
+ // If token in denom is not alloyed, we only need to validate the token in balance.
201
+ // Since the token in balance is the only one that is increased by the current quote.
202
+ //
203
+ // If token in denom is alloyed, we need to validate all assets' balances except token out.
204
+ // Since the token out composition is decreasing, other assets' weights are increasing.
205
+ //
206
206
// Note: static rate limit only has an upper limit.
207
- // Therefore, we only need to validate the token in balance.
208
207
// No-op if the static rate limiter is not set.
209
208
// Returns error if the token in weight is greater than the upper limit.
210
209
// Returns nil if the token in weight is less than or equal to the upper limit.
@@ -214,12 +213,6 @@ func (r *routableAlloyTransmuterPoolImpl) checkStaticRateLimiter(tokenInCoin sdk
214
213
return nil
215
214
}
216
215
217
- // Check if the static rate limiter exists for the token in denom updated balance.
218
- tokeInStaticLimiter , ok := r .AlloyTransmuterData .RateLimiterConfig .GetStaticLimiter (tokenInCoin .Denom )
219
- if ! ok {
220
- return nil
221
- }
222
-
223
216
preComputedData := r .AlloyTransmuterData .PreComputedData
224
217
normalizationFactors := preComputedData .NormalizationScalingFactors
225
218
@@ -232,8 +225,8 @@ func (r *routableAlloyTransmuterPoolImpl) checkStaticRateLimiter(tokenInCoin sdk
232
225
assetConfig := r .AlloyTransmuterData .AssetConfigs [i ]
233
226
assetDenom := assetConfig .Denom
234
227
235
- // Skip if the asset is alloyed LP hsare
236
- if strings . Contains ( assetDenom , alloyedLPShareDenomComponent ) {
228
+ // Skip if the asset is alloyed LP share
229
+ if assetDenom == r . AlloyTransmuterData . AlloyedDenom {
237
230
continue
238
231
}
239
232
@@ -244,6 +237,11 @@ func (r *routableAlloyTransmuterPoolImpl) checkStaticRateLimiter(tokenInCoin sdk
244
237
assetBalance = assetBalance .Add (tokenInCoin .Amount )
245
238
}
246
239
240
+ // Subtract the token out balance from the asset balance
241
+ if assetDenom == r .TokenOutDenom {
242
+ assetBalance = assetBalance .Sub (tokenInCoin .Amount )
243
+ }
244
+
247
245
normalizationScalingFactor , ok := normalizationFactors [assetDenom ]
248
246
if ! ok {
249
247
return fmt .Errorf ("normalization scaling factor not found for asset %s, pool id %d" , assetDenom , r .GetId ())
@@ -259,34 +257,65 @@ func (r *routableAlloyTransmuterPoolImpl) checkStaticRateLimiter(tokenInCoin sdk
259
257
normalizeTotal = normalizeTotal .Add (normalizedBalance )
260
258
}
261
259
262
- // Calculate weights
263
- // Note: -1 for the alloyed LP share.
264
- weights := make (map [string ]osmomath.Dec , len (r .AlloyTransmuterData .AssetConfigs )- 1 )
265
- for i := 0 ; i < len (r .AlloyTransmuterData .AssetConfigs ); i ++ {
266
- assetConfig := r .AlloyTransmuterData .AssetConfigs [i ]
267
- assetDenom := assetConfig .Denom
268
-
269
- // Skip if the asset is alloyed LP hsare
270
- if strings .Contains (assetDenom , alloyedLPShareDenomComponent ) {
271
- continue
260
+ // If token in denom is alloyed, we need to validate limiters for all assets' balances except token out.
261
+ // Since the token out composition is decreasing, other assets' weights are increasing.
262
+ // else, we only need to validate the token in denom limiter.
263
+ if tokenInCoin .Denom == r .AlloyTransmuterData .AlloyedDenom {
264
+ for i := 0 ; i < len (r .AlloyTransmuterData .AssetConfigs ); i ++ {
265
+ assetConfig := r .AlloyTransmuterData .AssetConfigs [i ]
266
+ assetDenom := assetConfig .Denom
267
+
268
+ // Skip if the asset is alloyed LP share
269
+ if assetDenom == r .AlloyTransmuterData .AlloyedDenom {
270
+ continue
271
+ }
272
+
273
+ // skip if the asset is token out, since its weight is decreasing, no need to check limiter
274
+ if assetDenom == r .TokenOutDenom {
275
+ continue
276
+ }
277
+
278
+ // Check if the static rate limiter exists for the asset denom updated balance.
279
+ // If not, continue to the next asset
280
+ staticLimiter , ok := r .AlloyTransmuterData .RateLimiterConfig .GetStaticLimiter (assetDenom )
281
+ if ! ok {
282
+ continue
283
+ }
284
+
285
+ // Validate upper limit
286
+ upperLimitInt := osmomath .MustNewDecFromStr (staticLimiter .UpperLimit )
287
+
288
+ // Asset weight
289
+ assetWeight := normalizedBalances [assetDenom ].ToLegacyDec ().Quo (normalizeTotal .ToLegacyDec ())
290
+
291
+ // Check the upper limit
292
+ if assetWeight .GT (upperLimitInt ) {
293
+ return domain.StaticRateLimiterInvalidUpperLimitError {
294
+ UpperLimit : staticLimiter .UpperLimit ,
295
+ Weight : assetWeight .String (),
296
+ Denom : assetDenom ,
297
+ }
298
+ }
299
+ }
300
+ } else {
301
+ tokeInStaticLimiter , ok := r .AlloyTransmuterData .RateLimiterConfig .GetStaticLimiter (tokenInCoin .Denom )
302
+ if ! ok {
303
+ return nil
272
304
}
273
305
274
- // Calculate weight
275
- weights [assetDenom ] = normalizedBalances [assetDenom ].ToLegacyDec ().Quo (normalizeTotal .ToLegacyDec ())
276
- }
277
-
278
- // Validate upper limit
279
- upperLimitInt := osmomath .MustNewDecFromStr (tokeInStaticLimiter .UpperLimit )
306
+ // Validate upper limit
307
+ upperLimitInt := osmomath .MustNewDecFromStr (tokeInStaticLimiter .UpperLimit )
280
308
281
- // Token in weight
282
- tokenInWeight := weights [tokenInCoin .Denom ]
309
+ // Token in weight
310
+ tokenInWeight := normalizedBalances [tokenInCoin .Denom ]. ToLegacyDec (). Quo ( normalizeTotal . ToLegacyDec ())
283
311
284
- // Check the upper limit
285
- if tokenInWeight .GT (upperLimitInt ) {
286
- return domain.StaticRateLimiterInvalidUpperLimitError {
287
- UpperLimit : tokeInStaticLimiter .UpperLimit ,
288
- Weight : tokenInWeight .String (),
289
- Denom : tokenInCoin .Denom ,
312
+ // Check the upper limit
313
+ if tokenInWeight .GT (upperLimitInt ) {
314
+ return domain.StaticRateLimiterInvalidUpperLimitError {
315
+ UpperLimit : tokeInStaticLimiter .UpperLimit ,
316
+ Weight : tokenInWeight .String (),
317
+ Denom : tokenInCoin .Denom ,
318
+ }
290
319
}
291
320
}
292
321
0 commit comments