Skip to content

Commit

Permalink
update external liquidity struct
Browse files Browse the repository at this point in the history
  • Loading branch information
jelysn committed Sep 15, 2023
1 parent b65ca7d commit eb287ad
Show file tree
Hide file tree
Showing 3 changed files with 328 additions and 106 deletions.
19 changes: 13 additions & 6 deletions proto/elys/amm/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,22 @@ message MsgFeedMultipleExternalLiquidity {
}
message MsgFeedMultipleExternalLiquidityResponse {}

// ExternalLiquidity defines price, volume, and time information for an exchange rate.
message ExternalLiquidity {
uint64 poolId = 1;
repeated cosmos.base.v1beta1.DecCoin amounts = 2 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.DecCoins"
message AssetAmountDepth {
string asset = 1;
string amount = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string depth = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}

// ExternalLiquidity defines price, volume, and time information for an exchange rate.
message ExternalLiquidity {
uint64 poolId = 1;
repeated AssetAmountDepth amountDepthInfo = 2 [
(gogoproto.nullable) = false
];
}
21 changes: 9 additions & 12 deletions x/amm/keeper/msg_server_feed_multiple_external_liquidity.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ import (
oracletypes "github.com/elys-network/elys/x/oracle/types"
)

func AssetsValue(ctx sdk.Context, oracleKeeper types.OracleKeeper, elCoins sdk.DecCoins) (sdk.Dec, error) {
func AssetsValue(ctx sdk.Context, oracleKeeper types.OracleKeeper, amountDepthInfo []types.AssetAmountDepth) (sdk.Dec, sdk.Dec, error) {
totalValue := sdk.ZeroDec()
for _, asset := range elCoins {
price, found := oracleKeeper.GetAssetPrice(ctx, asset.Denom)
totalDepth := sdk.ZeroDec()
for _, asset := range amountDepthInfo {
price, found := oracleKeeper.GetAssetPrice(ctx, asset.Asset)
if !found {
return sdk.ZeroDec(), fmt.Errorf("asset price not set: %s", asset.Denom)
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)
}
return totalValue, nil
return totalValue, totalDepth, nil
}

func LiquidityRatioFromPriceDepth(depth sdk.Dec) sdk.Dec {
Expand Down Expand Up @@ -57,18 +59,13 @@ func (k msgServer) FeedMultipleExternalLiquidity(goCtx context.Context, msg *typ
return nil, err
}

elValue, err := AssetsValue(ctx, k.oracleKeeper, el.Amounts)
elValue, elDepth, err := AssetsValue(ctx, k.oracleKeeper, el.AmountDepthInfo)
if err != nil {
return nil, err
}

fmt.Println("tvl", tvl.String())
fmt.Println("elValue", elValue.String())

elRatio := elValue.Quo(tvl)
fmt.Println("elRatio1", elRatio.String(), el.Depth)
elRatio = elRatio.Quo(LiquidityRatioFromPriceDepth(el.Depth))
fmt.Println("elRatio2", elRatio.String())
elRatio = elRatio.Quo(LiquidityRatioFromPriceDepth(elDepth))
if elRatio.LT(sdk.OneDec()) {
elRatio = sdk.OneDec()
}
Expand Down
Loading

0 comments on commit eb287ad

Please sign in to comment.