-
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 (#182)
* implement feed multiple external liquidity endpoint * Add LiquidityRatioFromPriceDepth function to be able to calculate total estimated liquidity from price depth & use it on external liquidity calculation * Add unit test on LiquidityRatioFromPriceDepth & resolve message codec registration issue * add changes on config.yml while testing price-feeder * - Update assets value calculation script on external liquidity feed function - Update config.yml to be able to feed external liquidity info locally * update external liquidity struct * update openapi yml * update to use average depth when calculating external liquidity ratio
- Loading branch information
Showing
13 changed files
with
1,233 additions
and
132 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
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
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
82 changes: 82 additions & 0 deletions
82
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,82 @@ | ||
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, amountDepthInfo []types.AssetAmountDepth) (sdk.Dec, sdk.Dec, error) { | ||
totalValue := sdk.ZeroDec() | ||
totalDepth := sdk.ZeroDec() | ||
if len(amountDepthInfo) == 0 { | ||
return sdk.ZeroDec(), sdk.ZeroDec(), nil | ||
} | ||
for _, asset := range amountDepthInfo { | ||
price, found := oracleKeeper.GetAssetPrice(ctx, asset.Asset) | ||
if !found { | ||
return sdk.ZeroDec(), sdk.ZeroDec(), fmt.Errorf("asset price not set: %s", asset.Asset) | ||
} else { | ||
v := price.Price.Mul(asset.Amount) | ||
totalValue = totalValue.Add(v) | ||
} | ||
totalDepth = totalDepth.Add(asset.Depth) | ||
} | ||
avgDepth := totalDepth.Quo(sdk.NewDec(int64(len(amountDepthInfo)))) | ||
return totalValue, avgDepth, nil | ||
} | ||
|
||
func LiquidityRatioFromPriceDepth(depth sdk.Dec) sdk.Dec { | ||
if depth == sdk.OneDec() { | ||
return sdk.OneDec() | ||
} | ||
sqrt, err := sdk.OneDec().Sub(depth).ApproxSqrt() | ||
if err != nil { | ||
panic(err) | ||
} | ||
return sdk.OneDec().Sub(sqrt) | ||
} | ||
|
||
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, elDepth, err := AssetsValue(ctx, k.oracleKeeper, el.AmountDepthInfo) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
elRatio := elValue.Quo(tvl) | ||
elRatio = elRatio.Quo(LiquidityRatioFromPriceDepth(elDepth)) | ||
if elRatio.LT(sdk.OneDec()) { | ||
elRatio = sdk.OneDec() | ||
} | ||
|
||
pool.PoolParams.ExternalLiquidityRatio = elRatio | ||
k.SetPool(ctx, pool) | ||
} | ||
|
||
return &types.MsgFeedMultipleExternalLiquidityResponse{}, nil | ||
} |
27 changes: 27 additions & 0 deletions
27
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,27 @@ | ||
package keeper_test | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/elys-network/elys/x/amm/keeper" | ||
) | ||
|
||
func (suite *KeeperTestSuite) TestLiquidityRatioFromPriceDepth() { | ||
depth := sdk.NewDecWithPrec(1, 2) // 1% | ||
suite.Require().Equal("0.005012562893380046", keeper.LiquidityRatioFromPriceDepth(depth).String()) | ||
depth = sdk.NewDecWithPrec(2, 2) // 2% | ||
suite.Require().Equal("0.010050506338833466", keeper.LiquidityRatioFromPriceDepth(depth).String()) | ||
depth = sdk.NewDecWithPrec(5, 2) // 5% | ||
suite.Require().Equal("0.025320565519103610", keeper.LiquidityRatioFromPriceDepth(depth).String()) | ||
depth = sdk.NewDecWithPrec(10, 2) // 10% | ||
suite.Require().Equal("0.051316701949486201", keeper.LiquidityRatioFromPriceDepth(depth).String()) | ||
depth = sdk.NewDecWithPrec(30, 2) // 30% | ||
suite.Require().Equal("0.163339973465924452", keeper.LiquidityRatioFromPriceDepth(depth).String()) | ||
depth = sdk.NewDecWithPrec(50, 2) // 50% | ||
suite.Require().Equal("0.292893218813452476", keeper.LiquidityRatioFromPriceDepth(depth).String()) | ||
depth = sdk.NewDecWithPrec(70, 2) // 70% | ||
suite.Require().Equal("0.452277442494833887", keeper.LiquidityRatioFromPriceDepth(depth).String()) | ||
depth = sdk.NewDecWithPrec(90, 2) // 90% | ||
suite.Require().Equal("0.683772233983162067", keeper.LiquidityRatioFromPriceDepth(depth).String()) | ||
depth = sdk.NewDecWithPrec(100, 2) // 100% | ||
suite.Require().Equal("1.000000000000000000", keeper.LiquidityRatioFromPriceDepth(depth).String()) | ||
} |
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
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,45 @@ | ||
package types | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
) | ||
|
||
const TypeMsgFeedMultipleExternalLiquidity = "feed_multiple_external_liquidity" | ||
|
||
var _ sdk.Msg = &MsgFeedMultipleExternalLiquidity{} | ||
|
||
func NewMsgFeedMultipleExternalLiquidity(sender string) *MsgFeedMultipleExternalLiquidity { | ||
return &MsgFeedMultipleExternalLiquidity{ | ||
Sender: sender, | ||
} | ||
} | ||
|
||
func (msg *MsgFeedMultipleExternalLiquidity) Route() string { | ||
return RouterKey | ||
} | ||
|
||
func (msg *MsgFeedMultipleExternalLiquidity) Type() string { | ||
return TypeMsgSwapExactAmountOut | ||
} | ||
|
||
func (msg *MsgFeedMultipleExternalLiquidity) GetSigners() []sdk.AccAddress { | ||
sender, err := sdk.AccAddressFromBech32(msg.Sender) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return []sdk.AccAddress{sender} | ||
} | ||
|
||
func (msg *MsgFeedMultipleExternalLiquidity) GetSignBytes() []byte { | ||
bz := ModuleCdc.MustMarshalJSON(msg) | ||
return sdk.MustSortJSON(bz) | ||
} | ||
|
||
func (msg *MsgFeedMultipleExternalLiquidity) ValidateBasic() error { | ||
_, err := sdk.AccAddressFromBech32(msg.Sender) | ||
if err != nil { | ||
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid sender address (%s)", err) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.