Skip to content

Commit

Permalink
adding API queries (#1082)
Browse files Browse the repository at this point in the history
* adding queries for chain and pools tvl

* updating query response

* upgrading go version to 1.22.7

* changing testnet upgrade height 2 days later (50,000 blocks later)

* changing testnet upgrade height 3 days later (75000 blocks later)
  • Loading branch information
avkr003 authored Dec 27, 2024
1 parent 6ed947a commit 24a97bd
Show file tree
Hide file tree
Showing 12 changed files with 4,302 additions and 714 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/create-release-and-testnet-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
# helper functions
extract_txhash() { awk -F 'txhash: ' '/txhash:/{print $2; exit}'; }
extract_proposal_id() { awk -F 'key: proposal_id|value: ' '/key: proposal_id/ { getline; gsub(/"/, "", $2); print $2; exit }'; }
extract_and_calc_upgrade_height() { awk -F'"latest_block_height":"' '{ split($2,a,"\""); print a[1]+450; exit }'; }
extract_and_calc_upgrade_height() { awk -F'"latest_block_height":"' '{ split($2,a,"\""); print a[1]+75000; exit }'; }
extract_checksum() { awk "/elysd-${ELYS_VERSION}-linux-amd64.tar.gz/ {print \$1; exit}"; }
# environment variables
Expand Down
3,337 changes: 2,782 additions & 555 deletions api/elys/masterchef/query.pulsar.go

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions api/elys/masterchef/query_grpc.pb.go

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

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/elys-network/elys

go 1.22.6
go 1.22.7

require (
cosmossdk.io/api v0.7.5
Expand Down
63 changes: 63 additions & 0 deletions proto/elys/masterchef/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,70 @@ service Query {
rpc PoolRewards(QueryPoolRewardsRequest) returns (QueryPoolRewardsResponse) {
option (google.api.http).get = "/elys-network/elys/masterchef/pool_rewards";
}

rpc AllLiquidityPoolTVL(QueryAllLiquidityPoolTVLRequest)
returns (QueryAllLiquidityPoolTVLResponse) {
option (google.api.http).get =
"/elys-network/elys/masterchef/all_liquidity_pool_tvl";
}

rpc ChainTVL(QueryChainTVLRequest) returns (QueryChainTVLResponse) {
option (google.api.http).get = "/elys-network/elys/masterchef/chain_tvl";
}
}

message QueryAllLiquidityPoolTVLRequest {}

message QueryAllLiquidityPoolTVLResponse {

string total = 1 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
string pools = 2 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
string usdc_staking = 3 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
}

message QueryChainTVLRequest {}

message QueryChainTVLResponse {

string total = 1 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
string pools = 2 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
string usdc_staking = 3 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
string staked_elys = 4 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
string staked_eden = 5 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
}

// QueryParamsRequest is request type for the Query/Params RPC method.
message QueryParamsRequest {}

Expand Down
2 changes: 2 additions & 0 deletions x/masterchef/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
}

cmd.AddCommand(CmdQueryParams())
cmd.AddCommand(CmdChainTVL())
cmd.AddCommand(CmdAllLiquidityPoolTVL())
cmd.AddCommand(CmdQueryExternalIncentive())
cmd.AddCommand(CmdQueryPoolInfo())
cmd.AddCommand(CmdQueryPoolRewardInfo())
Expand Down
48 changes: 48 additions & 0 deletions x/masterchef/client/cli/query_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,54 @@ func CmdQueryParams() *cobra.Command {
return cmd
}

func CmdChainTVL() *cobra.Command {
cmd := &cobra.Command{
Use: "chain_tvl",
Short: "show chain tvl",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.ChainTVL(context.Background(), &types.QueryChainTVLRequest{})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func CmdAllLiquidityPoolTVL() *cobra.Command {
cmd := &cobra.Command{
Use: "all_liquidity_pool_tvl",
Short: "show all pools tvl",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.AllLiquidityPoolTVL(context.Background(), &types.QueryAllLiquidityPoolTVLRequest{})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

func CmdQueryStableStakeApr() *cobra.Command {
cmd := &cobra.Command{
Use: "stable-stake-apr",
Expand Down
48 changes: 48 additions & 0 deletions x/masterchef/keeper/query_all_liquidity_pool_tvl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package keeper

import (
"context"
"cosmossdk.io/math"
ptypes "github.com/elys-network/elys/x/parameter/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/masterchef/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

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

ctx := sdk.UnwrapSDKContext(goCtx)

allPools := k.amm.GetAllPool(ctx)
poolsTVL := math.LegacyZeroDec()
totalTVL := math.ZeroInt()

for _, pool := range allPools {
tvl, err := pool.TVL(ctx, k.oracleKeeper, k.accountedPoolKeeper)
if err != nil {
return nil, err
}

poolsTVL = poolsTVL.Add(tvl)
}
totalTVL = totalTVL.Add(poolsTVL.TruncateInt())

entry, found := k.assetProfileKeeper.GetEntry(ctx, ptypes.BaseCurrency)
if !found {
return nil, status.Error(codes.NotFound, "asset profile not found")
}

stableStakeTVL := k.stableKeeper.TVL(ctx, k.oracleKeeper, entry.Denom)
totalTVL = totalTVL.Add(stableStakeTVL.TruncateInt())

return &types.QueryAllLiquidityPoolTVLResponse{
Total: totalTVL,
Pools: poolsTVL.TruncateInt(),
UsdcStaking: stableStakeTVL.TruncateInt(),
}, nil
}
62 changes: 62 additions & 0 deletions x/masterchef/keeper/query_chain_tvl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package keeper

import (
"context"
"cosmossdk.io/math"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
ptypes "github.com/elys-network/elys/x/parameter/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/masterchef/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

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

ctx := sdk.UnwrapSDKContext(goCtx)

allPools := k.amm.GetAllPool(ctx)
poolsTVL := math.LegacyZeroDec()
totalTVL := math.ZeroInt()

for _, pool := range allPools {
tvl, err := pool.TVL(ctx, k.oracleKeeper, k.accountedPoolKeeper)
if err != nil {
return nil, err
}
poolsTVL = poolsTVL.Add(tvl)
}
totalTVL = totalTVL.Add(poolsTVL.TruncateInt())

baseCurrencyEntry, found := k.assetProfileKeeper.GetEntry(ctx, ptypes.BaseCurrency)
if !found {
return nil, status.Error(codes.NotFound, "asset profile not found")
}

stableStakeTVL := k.stableKeeper.TVL(ctx, k.oracleKeeper, baseCurrencyEntry.Denom)
totalTVL = totalTVL.Add(stableStakeTVL.TruncateInt())

elysPrice := k.amm.GetTokenPrice(ctx, ptypes.Elys, baseCurrencyEntry.Denom)

stakedElys := k.bankKeeper.GetBalance(ctx, authtypes.NewModuleAddress(stakingtypes.BondedPoolName), ptypes.Elys).Amount
stakedElysValue := elysPrice.MulInt(stakedElys)
totalTVL = totalTVL.Add(stakedElysValue.TruncateInt())

commitmentParams := k.commitmentKeeper.GetParams(ctx)
stakedEden := commitmentParams.TotalCommitted.AmountOf(ptypes.Eden)
stakedEdenValue := elysPrice.MulInt(stakedEden)
totalTVL = totalTVL.Add(stakedEdenValue.TruncateInt())

return &types.QueryChainTVLResponse{
Total: totalTVL,
Pools: poolsTVL.TruncateInt(),
UsdcStaking: stableStakeTVL.TruncateInt(),
StakedElys: stakedElysValue.TruncateInt(),
StakedEden: stakedEdenValue.TruncateInt(),
}, nil
}
Loading

0 comments on commit 24a97bd

Please sign in to comment.