Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding API queries #1082

Merged
merged 11 commits into from
Dec 27, 2024
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 @@
}

cmd.AddCommand(CmdQueryParams())
cmd.AddCommand(CmdChainTVL())
cmd.AddCommand(CmdAllLiquidityPoolTVL())

Check warning on line 24 in x/masterchef/client/cli/query.go

View check run for this annotation

Codecov / codecov/patch

x/masterchef/client/cli/query.go#L23-L24

Added lines #L23 - L24 were not covered by tests
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 @@
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
}

Check warning on line 51 in x/masterchef/client/cli/query_cmds.go

View check run for this annotation

Codecov / codecov/patch

x/masterchef/client/cli/query_cmds.go#L38-L51

Added lines #L38 - L51 were not covered by tests

return clientCtx.PrintProto(res)

Check warning on line 53 in x/masterchef/client/cli/query_cmds.go

View check run for this annotation

Codecov / codecov/patch

x/masterchef/client/cli/query_cmds.go#L53

Added line #L53 was not covered by tests
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd

Check warning on line 59 in x/masterchef/client/cli/query_cmds.go

View check run for this annotation

Codecov / codecov/patch

x/masterchef/client/cli/query_cmds.go#L57-L59

Added lines #L57 - L59 were not covered by tests
}

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
}

Check warning on line 75 in x/masterchef/client/cli/query_cmds.go

View check run for this annotation

Codecov / codecov/patch

x/masterchef/client/cli/query_cmds.go#L62-L75

Added lines #L62 - L75 were not covered by tests

return clientCtx.PrintProto(res)

Check warning on line 77 in x/masterchef/client/cli/query_cmds.go

View check run for this annotation

Codecov / codecov/patch

x/masterchef/client/cli/query_cmds.go#L77

Added line #L77 was not covered by tests
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd

Check warning on line 83 in x/masterchef/client/cli/query_cmds.go

View check run for this annotation

Codecov / codecov/patch

x/masterchef/client/cli/query_cmds.go#L81-L83

Added lines #L81 - L83 were not covered by tests
}

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")
}

Check warning on line 17 in x/masterchef/keeper/query_all_liquidity_pool_tvl.go

View check run for this annotation

Codecov / codecov/patch

x/masterchef/keeper/query_all_liquidity_pool_tvl.go#L14-L17

Added lines #L14 - L17 were not covered by tests

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
}

Check warning on line 29 in x/masterchef/keeper/query_all_liquidity_pool_tvl.go

View check run for this annotation

Codecov / codecov/patch

x/masterchef/keeper/query_all_liquidity_pool_tvl.go#L19-L29

Added lines #L19 - L29 were not covered by tests

poolsTVL = poolsTVL.Add(tvl)

Check warning on line 31 in x/masterchef/keeper/query_all_liquidity_pool_tvl.go

View check run for this annotation

Codecov / codecov/patch

x/masterchef/keeper/query_all_liquidity_pool_tvl.go#L31

Added line #L31 was not covered by tests
}
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")
}

Check warning on line 38 in x/masterchef/keeper/query_all_liquidity_pool_tvl.go

View check run for this annotation

Codecov / codecov/patch

x/masterchef/keeper/query_all_liquidity_pool_tvl.go#L33-L38

Added lines #L33 - L38 were not covered by tests

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

Check warning on line 47 in x/masterchef/keeper/query_all_liquidity_pool_tvl.go

View check run for this annotation

Codecov / codecov/patch

x/masterchef/keeper/query_all_liquidity_pool_tvl.go#L40-L47

Added lines #L40 - L47 were not covered by tests
}
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")
}

Check warning on line 19 in x/masterchef/keeper/query_chain_tvl.go

View check run for this annotation

Codecov / codecov/patch

x/masterchef/keeper/query_chain_tvl.go#L16-L19

Added lines #L16 - L19 were not covered by tests

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)

Check warning on line 32 in x/masterchef/keeper/query_chain_tvl.go

View check run for this annotation

Codecov / codecov/patch

x/masterchef/keeper/query_chain_tvl.go#L21-L32

Added lines #L21 - L32 were not covered by tests
}
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")
}

Check warning on line 39 in x/masterchef/keeper/query_chain_tvl.go

View check run for this annotation

Codecov / codecov/patch

x/masterchef/keeper/query_chain_tvl.go#L34-L39

Added lines #L34 - L39 were not covered by tests

stableStakeTVL := k.stableKeeper.TVL(ctx, k.oracleKeeper, baseCurrencyEntry.Denom)
totalTVL = totalTVL.Add(stableStakeTVL.TruncateInt())
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved

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(),
amityadav0 marked this conversation as resolved.
Show resolved Hide resolved
}, nil

Check warning on line 61 in x/masterchef/keeper/query_chain_tvl.go

View check run for this annotation

Codecov / codecov/patch

x/masterchef/keeper/query_chain_tvl.go#L41-L61

Added lines #L41 - L61 were not covered by tests
}
Loading
Loading