Skip to content

Commit

Permalink
Create a new endpoint on chain that return list of latest asset prices (
Browse files Browse the repository at this point in the history
#1071)

* add all prices query

* add cli

* fix

* fix
  • Loading branch information
amityadav0 authored Dec 19, 2024
1 parent 3c591ac commit bb7495f
Show file tree
Hide file tree
Showing 9 changed files with 2,998 additions and 281 deletions.
2,133 changes: 1,949 additions & 184 deletions api/elys/tier/query.pulsar.go

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions api/elys/tier/query_grpc.pb.go

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

29 changes: 29 additions & 0 deletions proto/elys/tier/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ service Query {
option (google.api.http).get =
"/elys-network/elys/tier/get_users_pool_data";
}

// Queries a list of GetConsolidatedPrice items.
rpc GetAllPrices(QueryGetAllPricesRequest)
returns (QueryGetAllPricesResponse) {
option (google.api.http).get = "/elys-network/elys/tier/get_all_prices";
}
}
// QueryParamsRequest is request type for the Query/Params RPC method.
message QueryParamsRequest {}
Expand Down Expand Up @@ -265,3 +271,26 @@ message QueryGetUsersPoolDataResponse {
repeated UserData users = 1;
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message Price {
string denom = 1;
string oracle_price = 2 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string amm_price = 3 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
}

message QueryGetAllPricesRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

message QueryGetAllPricesResponse {
repeated Price prices = 1;
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}
2 changes: 2 additions & 0 deletions x/tier/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func GetQueryCmd(queryRoute string) *cobra.Command {

cmd.AddCommand(CmdLockedOrder())

cmd.AddCommand(CmdGetAllPrices())

// this line is used by starport scaffolding # 1

return cmd
Expand Down
37 changes: 37 additions & 0 deletions x/tier/client/cli/query_get_all_prices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cli

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/elys-network/elys/x/tier/types"
"github.com/spf13/cobra"
)

func CmdGetAllPrices() *cobra.Command {
cmd := &cobra.Command{
Use: "get-all-prices",
Short: "Query get-all-prices",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryGetAllPricesRequest{}

res, err := queryClient.GetAllPrices(cmd.Context(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
34 changes: 34 additions & 0 deletions x/tier/keeper/query_get_consolidated_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package keeper

import (
"context"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
oracle "github.com/elys-network/elys/x/oracle/keeper"
ptypes "github.com/elys-network/elys/x/parameter/types"
"github.com/elys-network/elys/x/tier/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -24,3 +27,34 @@ func (k Keeper) GetConsolidatedPrice(goCtx context.Context, req *types.QueryGetC
OraclePriceDec: oracleDec,
}, nil
}

func (k Keeper) GetAllPrices(goCtx context.Context, req *types.QueryGetAllPricesRequest) (*types.QueryGetAllPricesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(goCtx)
var prices []*types.Price

assetentries := k.assetProfileKeeper.GetAllEntry(ctx)
for _, assetEntry := range assetentries {
if strings.HasPrefix(assetEntry.Denom, "amm/pool") || strings.HasPrefix(assetEntry.Denom, "stablestake") {
continue
}
denom := assetEntry.Denom
if assetEntry.Denom == ptypes.Eden {
denom = ptypes.Elys
}
tokenPriceOracle := k.oracleKeeper.GetAssetPriceFromDenom(ctx, denom).Mul(oracle.Pow10(assetEntry.Decimals))
tokenPriceAmm := k.amm.CalcAmmPrice(ctx, denom, assetEntry.Decimals).Mul(oracle.Pow10(assetEntry.Decimals))
prices = append(prices, &types.Price{
Denom: assetEntry.Denom,
OraclePrice: tokenPriceOracle,
AmmPrice: tokenPriceAmm,
})
}

return &types.QueryGetAllPricesResponse{
Prices: prices,
}, nil
}
1 change: 1 addition & 0 deletions x/tier/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type PerpetualKeeper interface {
// AssetProfileKeeper defines the expected interface needed to retrieve denom info
type AssetProfileKeeper interface {
GetEntry(ctx sdk.Context, baseDenom string) (val assetprofiletypes.Entry, found bool)
GetAllEntry(ctx sdk.Context) (list []assetprofiletypes.Entry)
// GetUsdcDenom returns USDC denom
GetUsdcDenom(ctx sdk.Context) (string, bool)
// GetEntryByDenom returns a entry from its denom value
Expand Down
Loading

0 comments on commit bb7495f

Please sign in to comment.