Skip to content

Commit

Permalink
implement feed multiple external liquidity endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jelysn committed Sep 5, 2023
1 parent ed993e6 commit 576f55a
Show file tree
Hide file tree
Showing 7 changed files with 837 additions and 79 deletions.
51 changes: 39 additions & 12 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77123,13 +77123,50 @@ definitions:
title: |-
QuerySmartContractStateResponse is the response type for the
Query/SmartContractState RPC method
cosmos.base.v1beta1.DecCoin:
type: object
properties:
denom:
type: string
amount:
type: string
description: |-
DecCoin defines a token with a denomination and a decimal amount.

NOTE: The amount field is an Dec which implements the custom method
signatures required by gogoproto.
elys.amm.DenomLiquidity:
type: object
properties:
denom:
type: string
liquidity:
type: string
elys.amm.ExternalLiquidity:
type: object
properties:
poolId:
type: string
format: uint64
amounts:
type: array
items:
type: object
properties:
denom:
type: string
amount:
type: string
description: |-
DecCoin defines a token with a denomination and a decimal amount.

NOTE: The amount field is an Dec which implements the custom method
signatures required by gogoproto.
depth:
type: string
description: >-
ExternalLiquidity defines price, volume, and time information for an
exchange rate.
elys.amm.MsgCreatePoolResponse:
type: object
properties:
Expand All @@ -77153,6 +77190,8 @@ definitions:

NOTE: The amount field is an Int which implements the custom method
signatures required by gogoproto.
elys.amm.MsgFeedMultipleExternalLiquidityResponse:
type: object
elys.amm.MsgJoinPoolResponse:
type: object
properties:
Expand Down Expand Up @@ -78120,18 +78159,6 @@ definitions:
description: |-
QueryEpochsInfoResponse is the response type for the Query/EpochInfos RPC
method.
cosmos.base.v1beta1.DecCoin:
type: object
properties:
denom:
type: string
amount:
type: string
description: |-
DecCoin defines a token with a denomination and a decimal amount.

NOTE: The amount field is an Dec which implements the custom method
signatures required by gogoproto.
elys.incentive.IncentiveInfo:
type: object
properties:
Expand Down
19 changes: 19 additions & 0 deletions proto/elys/amm/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ service Msg {
rpc ExitPool (MsgExitPool ) returns (MsgExitPoolResponse );
rpc SwapExactAmountIn (MsgSwapExactAmountIn ) returns (MsgSwapExactAmountInResponse );
rpc SwapExactAmountOut (MsgSwapExactAmountOut) returns (MsgSwapExactAmountOutResponse);
rpc FeedMultipleExternalLiquidity(MsgFeedMultipleExternalLiquidity) returns (MsgFeedMultipleExternalLiquidityResponse);
}
message MsgCreatePool {
string sender = 1;
Expand Down Expand Up @@ -74,3 +75,21 @@ message MsgSwapExactAmountOutResponse {
string tokenInAmount = 1 [(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
}

message MsgFeedMultipleExternalLiquidity {
string sender = 1;
repeated ExternalLiquidity liquidity = 2 [(gogoproto.nullable) = false];
}
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"
];
string depth = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}
64 changes: 64 additions & 0 deletions x/amm/keeper/msg_server_feed_multiple_external_liquidity.go
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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package keeper_test
2 changes: 2 additions & 0 deletions x/amm/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
atypes "github.com/elys-network/elys/x/assetprofile/types"
oracletypes "github.com/elys-network/elys/x/oracle/types"
)

// AccountKeeper defines the expected account keeper used for simulations (noalias)
Expand All @@ -31,6 +32,7 @@ type BankKeeper interface {
// OracleKeeper defines the expected interface needed to retrieve price info
type OracleKeeper interface {
GetAssetPriceFromDenom(ctx sdk.Context, denom string) sdk.Dec
GetPriceFeeder(ctx sdk.Context, feeder string) (val oracletypes.PriceFeeder, found bool)
}

// AssetProfileKeeper defines the expected interfaces
Expand Down
19 changes: 1 addition & 18 deletions x/amm/types/query.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 576f55a

Please sign in to comment.