-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement feed multiple external liquidity endpoint
- Loading branch information
Showing
7 changed files
with
837 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
x/amm/keeper/msg_server_feed_multiple_external_liquidity.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/elys-network/elys/x/amm/types" | ||
oracletypes "github.com/elys-network/elys/x/oracle/types" | ||
) | ||
|
||
func AssetsValue(ctx sdk.Context, oracleKeeper types.OracleKeeper, elCoins sdk.DecCoins) (sdk.Dec, error) { | ||
totalValue := sdk.ZeroDec() | ||
for _, asset := range elCoins { | ||
tokenPrice := oracleKeeper.GetAssetPriceFromDenom(ctx, asset.Denom) | ||
if tokenPrice.IsZero() { | ||
return sdk.ZeroDec(), fmt.Errorf("token price not set: %s", asset.Denom) | ||
} else { | ||
v := tokenPrice.Mul(asset.Amount) | ||
totalValue = totalValue.Add(v) | ||
} | ||
} | ||
return totalValue, nil | ||
} | ||
|
||
func (k msgServer) FeedMultipleExternalLiquidity(goCtx context.Context, msg *types.MsgFeedMultipleExternalLiquidity) (*types.MsgFeedMultipleExternalLiquidityResponse, error) { | ||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
|
||
feeder, found := k.oracleKeeper.GetPriceFeeder(ctx, msg.Sender) | ||
if !found { | ||
return nil, oracletypes.ErrNotAPriceFeeder | ||
} | ||
|
||
if !feeder.IsActive { | ||
return nil, oracletypes.ErrPriceFeederNotActive | ||
} | ||
|
||
for _, el := range msg.Liquidity { | ||
pool, found := k.GetPool(ctx, el.PoolId) | ||
if !found { | ||
return nil, types.ErrInvalidPoolId | ||
} | ||
|
||
tvl, err := pool.TVL(ctx, k.oracleKeeper) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
elValue, err := AssetsValue(ctx, k.oracleKeeper, el.Amounts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
elRatio := elValue.Quo(tvl) | ||
if elRatio.LT(sdk.OneDec()) { | ||
elRatio = sdk.OneDec() | ||
} | ||
|
||
pool.PoolParams.ExternalLiquidityRatio = elRatio | ||
k.SetPool(ctx, pool) | ||
} | ||
|
||
return &types.MsgFeedMultipleExternalLiquidityResponse{}, nil | ||
} |
1 change: 1 addition & 0 deletions
1
x/amm/keeper/msg_server_feed_multiple_external_liquidity_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package keeper_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.