Skip to content

Commit 6735a6b

Browse files
amityadav0avkr003
andauthored
Add leveraged Amount fields for query (#1179)
* add leverageamount * migration --------- Co-authored-by: Abhinav Kumar <57705190+avkr003@users.noreply.github.com>
1 parent 7dfdc37 commit 6735a6b

File tree

9 files changed

+1136
-83
lines changed

9 files changed

+1136
-83
lines changed

api/elys/leveragelp/pool.pulsar.go

Lines changed: 754 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/elys/leveragelp/pool.proto

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ message Pool {
2828
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
2929
(gogoproto.nullable) = false
3030
];
31+
repeated AssetLeverageAmount asset_leverage_amounts = 6;
3132
}
3233

3334
message LegacyPool {
@@ -48,3 +49,12 @@ message LegacyPool {
4849
(gogoproto.nullable) = false
4950
];
5051
}
52+
53+
message AssetLeverageAmount {
54+
string denom = 1;
55+
string leveraged_amount = 2 [
56+
(cosmos_proto.scalar) = "cosmos.Int",
57+
(gogoproto.customtype) = "cosmossdk.io/math.Int",
58+
(gogoproto.nullable) = false
59+
];
60+
}

x/leveragelp/keeper/msg_server_add_pool.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package keeper
22

33
import (
44
"context"
5-
sdkmath "cosmossdk.io/math"
65
"fmt"
76

7+
sdkmath "cosmossdk.io/math"
8+
89
errorsmod "cosmossdk.io/errors"
910
sdk "github.com/cosmos/cosmos-sdk/types"
1011
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
@@ -37,6 +38,12 @@ func (k msgServer) AddPool(goCtx context.Context, msg *types.MsgAddPool) (*types
3738
leverage := sdkmath.LegacyMinDec(msg.Pool.LeverageMax, maxLeverageAllowed)
3839

3940
newPool := types.NewPool(ammPool.PoolId, leverage)
41+
for _, asset := range ammPool.PoolAssets {
42+
newPool.AssetLeverageAmounts = append(newPool.AssetLeverageAmounts, &types.AssetLeverageAmount{
43+
Denom: asset.Token.Denom,
44+
LeveragedAmount: sdkmath.ZeroInt(),
45+
})
46+
}
4047
k.SetPool(ctx, newPool)
4148

4249
if k.hooks != nil {

x/leveragelp/keeper/pool.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package keeper
22

33
import (
4+
sdkmath "cosmossdk.io/math"
45
"cosmossdk.io/store/prefix"
56
storetypes "cosmossdk.io/store/types"
67
"github.com/cosmos/cosmos-sdk/runtime"
@@ -56,3 +57,34 @@ func (k Keeper) GetPool(ctx sdk.Context, poolId uint64) (val types.Pool, found b
5657
k.cdc.MustUnmarshal(b, &val)
5758
return val, true
5859
}
60+
61+
func (k Keeper) SetLeveragedAmount(ctx sdk.Context) {
62+
pools := k.GetAllPools(ctx)
63+
for _, pool := range pools {
64+
ammPool, found := k.amm.GetPool(ctx, pool.AmmPoolId)
65+
if !found {
66+
continue
67+
}
68+
for _, asset := range ammPool.PoolAssets {
69+
pool.AssetLeverageAmounts = append(pool.AssetLeverageAmounts, &types.AssetLeverageAmount{
70+
Denom: asset.Token.Denom,
71+
LeveragedAmount: sdkmath.ZeroInt(),
72+
})
73+
}
74+
k.SetPool(ctx, pool)
75+
}
76+
77+
iterator := k.GetPositionIterator(ctx)
78+
defer iterator.Close()
79+
80+
for ; iterator.Valid(); iterator.Next() {
81+
var position types.Position
82+
k.cdc.MustUnmarshal(iterator.Value(), &position)
83+
pool, found := k.GetPool(ctx, position.AmmPoolId)
84+
if !found {
85+
continue
86+
}
87+
pool.UpdateAssetLeveragedAmount(ctx, position.Collateral.Denom, position.LeveragedLpAmount, true)
88+
k.SetPool(ctx, pool)
89+
}
90+
}

x/leveragelp/keeper/position_close.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ func (k Keeper) CheckHealthStopLossThenRepayAndClose(ctx sdk.Context, position *
109109

110110
// Subtract amount that is being reduced from lp pool as it gets checked in CheckAmmUsdcBalance
111111
pool.LeveragedLpAmount = pool.LeveragedLpAmount.Sub(lpSharesForRepay)
112+
pool.UpdateAssetLeveragedAmount(ctx, position.Collateral.Denom, lpSharesForRepay, false)
112113
// pool is set here
113114
k.UpdatePoolHealth(ctx, pool)
114115

@@ -166,6 +167,7 @@ func (k Keeper) CheckHealthStopLossThenRepayAndClose(ctx sdk.Context, position *
166167
// Update the pool health.
167168
if sharesLeft.IsPositive() {
168169
pool.LeveragedLpAmount = pool.LeveragedLpAmount.Sub(sharesLeft)
170+
pool.UpdateAssetLeveragedAmount(ctx, position.Collateral.Denom, sharesLeft, false)
169171
}
170172

171173
var coinsForAmm sdk.Coins

x/leveragelp/keeper/position_open.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func (k Keeper) ProcessOpenLong(ctx sdk.Context, position *types.Position, poolI
107107

108108
// Update the pool health.
109109
pool.LeveragedLpAmount = pool.LeveragedLpAmount.Add(shares)
110+
pool.UpdateAssetLeveragedAmount(ctx, position.Collateral.Denom, shares, true)
110111
k.UpdatePoolHealth(ctx, &pool)
111112

112113
position.LeveragedLpAmount = position.LeveragedLpAmount.Add(shares)

x/leveragelp/migrations/v19_migration.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ import (
66

77
func (m Migrator) V19Migration(ctx sdk.Context) error {
88
m.keeper.SetAllPositions(ctx)
9+
m.keeper.SetLeveragedAmount(ctx)
910
return nil
1011
}

x/leveragelp/types/pool.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,18 @@ func (p *Pool) InitiatePool(ctx sdk.Context, ammPool *ammtypes.Pool) error {
2828
p.AmmPoolId = ammPool.PoolId
2929
return nil
3030
}
31+
32+
func (p *Pool) UpdateAssetLeveragedAmount(ctx sdk.Context, denom string, amount sdkmath.Int, isIncrease bool) {
33+
newAssetLevAmounts := make([]*AssetLeverageAmount, 0)
34+
for _, asset := range p.AssetLeverageAmounts {
35+
if asset.Denom == denom {
36+
if isIncrease {
37+
asset.LeveragedAmount = asset.LeveragedAmount.Add(amount)
38+
} else {
39+
asset.LeveragedAmount = asset.LeveragedAmount.Sub(amount)
40+
}
41+
}
42+
newAssetLevAmounts = append(newAssetLevAmounts, asset)
43+
}
44+
p.AssetLeverageAmounts = newAssetLevAmounts
45+
}

0 commit comments

Comments
 (0)