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

feat: new iya query #685

Merged
merged 5 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 64 additions & 13 deletions proto/ununifi/yieldaggregator/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ service Query {
option (google.api.http).get = "/ununifi/yieldaggregator/vaults";
}

rpc VaultAllByShareHolder(QueryAllVaultByShareHolderRequest) returns (QueryAllVaultByShareHolderResponse) {
option (google.api.http).get = "/ununifi/yieldaggregator/vaults/share-holders/{share_holder}";
}

rpc Vault(QueryGetVaultRequest) returns (QueryGetVaultResponse) {
option (google.api.http).get = "/ununifi/yieldaggregator/vaults/{id}";
}
Expand Down Expand Up @@ -59,34 +63,60 @@ message QueryAllVaultRequest {
cosmos.base.query.v1beta1.PageRequest pagination = 1;
}

message VaultContainer {
Vault vault = 1 [(gogoproto.nullable) = false];
string total_bonded_amount = 2 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
string total_unbonding_amount = 3 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
string withdraw_reserve = 4 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
}

message QueryAllVaultResponse {
repeated Vault vaults = 1 [(gogoproto.nullable) = false];
repeated VaultContainer vaults = 1 [(gogoproto.nullable) = false];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

message QueryAllVaultByShareHolderRequest {
string share_holder = 1;
}

message QueryAllVaultByShareHolderResponse {
repeated VaultContainer vaults = 1 [(gogoproto.nullable) = false];
}

message QueryGetVaultRequest {
uint64 id = 1;
}

message QueryGetVaultResponse {
Vault vault = 1 [(gogoproto.nullable) = false];
repeated Strategy strategies = 2 [(gogoproto.nullable) = false];
string vault_address = 3;
string total_bonded_amount = 4 [
Vault vault = 1 [(gogoproto.nullable) = false];
string total_bonded_amount = 2 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
string total_unbonding_amount = 5 [
string total_unbonding_amount = 3 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
string total_withdrawal_balance = 6 [
string withdraw_reserve = 4 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
repeated Strategy strategies = 5 [(gogoproto.nullable) = false];
}

message QueryAllStrategyRequest {
Expand All @@ -95,7 +125,7 @@ message QueryAllStrategyRequest {
}

message QueryAllStrategyResponse {
repeated Strategy strategies = 1 [(gogoproto.nullable) = false];
repeated StrategyContainer strategies = 1 [(gogoproto.nullable) = false];
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

Expand All @@ -104,8 +134,27 @@ message QueryGetStrategyRequest {
uint64 id = 2;
}

message StrategyContainer {
Strategy strategy = 1 [(gogoproto.nullable) = false];
string deposit_fee_rate = 2 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string withdraw_fee_rate = 3 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string performance_fee_rate = 4 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
}

message QueryGetStrategyResponse {
Strategy strategy = 1 [(gogoproto.nullable) = false];
StrategyContainer strategy = 1 [(gogoproto.nullable) = false];
}

message QueryEstimateMintAmountRequest {
Expand All @@ -123,5 +172,7 @@ message QueryEstimateRedeemAmountRequest {
}

message QueryEstimateRedeemAmountResponse {
cosmos.base.v1beta1.Coin redeem_amount = 1 [(gogoproto.nullable) = false];
cosmos.base.v1beta1.Coin share_amount = 1 [(gogoproto.nullable) = false];
cosmos.base.v1beta1.Coin fee = 2 [(gogoproto.nullable) = false];
cosmos.base.v1beta1.Coin redeem_amount = 3 [(gogoproto.nullable) = false];
}
1 change: 1 addition & 0 deletions x/yieldaggregator/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func GetQueryCmd(queryRoute string) *cobra.Command {
CmdListStrategy(),
CmdShowStrategy(),
CmdShowVault(),
CmdVaultAllByShareHolder(),
)

return cmd
Expand Down
4 changes: 2 additions & 2 deletions x/yieldaggregator/client/cli/query_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ func CmdShowStrategy() *cobra.Command {
cmd := &cobra.Command{
Use: "show-strategy [vault-denom] [id]",
Short: "shows a strategy",
Args: cobra.ExactArgs(1),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

id, err := strconv.ParseUint(args[0], 10, 64)
id, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return err
}
Expand Down
28 changes: 28 additions & 0 deletions x/yieldaggregator/client/cli/query_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,31 @@ func CmdShowVault() *cobra.Command {

return cmd
}

func CmdVaultAllByShareHolder() *cobra.Command {
cmd := &cobra.Command{
Use: "list-vault-by-share-holder [holder]",
Short: "List vaults by share holder",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

params := &types.QueryAllVaultByShareHolderRequest{
ShareHolder: args[0],
}

res, err := queryClient.VaultAllByShareHolder(context.Background(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
63 changes: 60 additions & 3 deletions x/yieldaggregator/keeper/grpc_query_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package keeper

import (
"context"
"encoding/json"

math "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand All @@ -18,7 +20,7 @@ func (k Keeper) StrategyAll(c context.Context, req *types.QueryAllStrategyReques
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

var strategies []types.Strategy
var strategies []types.StrategyContainer
ctx := sdk.UnwrapSDKContext(c)

store := ctx.KVStore(k.storeKey)
Expand All @@ -30,7 +32,12 @@ func (k Keeper) StrategyAll(c context.Context, req *types.QueryAllStrategyReques
return err
}

strategies = append(strategies, strategy)
strategyContainer, err := k.GetStrategyContainer(ctx, strategy)
if err != nil {
return err
}

strategies = append(strategies, strategyContainer)
return nil
})

Expand All @@ -41,6 +48,49 @@ func (k Keeper) StrategyAll(c context.Context, req *types.QueryAllStrategyReques
return &types.QueryAllStrategyResponse{Strategies: strategies, Pagination: pageRes}, nil
}

func (k Keeper) GetStrategyContainer(ctx sdk.Context, strategy types.Strategy) (types.StrategyContainer, error) {
strategyAddr, err := sdk.AccAddressFromBech32(strategy.ContractAddress)
if err != nil {
return types.StrategyContainer{
Strategy: strategy,
DepositFeeRate: math.LegacyZeroDec(),
WithdrawFeeRate: math.LegacyZeroDec(),
PerformanceFeeRate: math.LegacyZeroDec(),
}, nil
}

wasmQuery := `{"fee":{}}`
result, err := k.wasmReader.QuerySmart(ctx, strategyAddr, []byte(wasmQuery))
if err != nil {
return types.StrategyContainer{}, err
}

jsonMap := make(map[string]string)
err = json.Unmarshal(result, &jsonMap)
if err != nil {
return types.StrategyContainer{}, err
}
depositFeeRate, err := math.LegacyNewDecFromStr(jsonMap["deposit_fee_rate"])
if err != nil {
return types.StrategyContainer{}, err
}
withdrawFeeRate, err := math.LegacyNewDecFromStr(jsonMap["withdraw_fee_rate"])
if err != nil {
return types.StrategyContainer{}, err
}
performanceFeeRate, err := math.LegacyNewDecFromStr(jsonMap["interest_fee_rate"])
if err != nil {
return types.StrategyContainer{}, err
}

return types.StrategyContainer{
Strategy: strategy,
DepositFeeRate: depositFeeRate,
WithdrawFeeRate: withdrawFeeRate,
PerformanceFeeRate: performanceFeeRate,
}, nil
}

func (k Keeper) Strategy(c context.Context, req *types.QueryGetStrategyRequest) (*types.QueryGetStrategyResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
Expand All @@ -52,5 +102,12 @@ func (k Keeper) Strategy(c context.Context, req *types.QueryGetStrategyRequest)
return nil, sdkerrors.ErrKeyNotFound
}

return &types.QueryGetStrategyResponse{Strategy: strategy}, nil
strategyContainer, err := k.GetStrategyContainer(ctx, strategy)
if err != nil {
return nil, err
}

return &types.QueryGetStrategyResponse{
Strategy: strategyContainer,
}, nil
}
21 changes: 15 additions & 6 deletions x/yieldaggregator/keeper/grpc_query_strategy_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
Expand All @@ -23,14 +24,22 @@ func (suite *KeeperTestSuite) TestStrategyQuerySingle() {
err error
}{
{
desc: "First",
request: &types.QueryGetStrategyRequest{Denom: vaultDenom, Id: msgs[0].Id},
response: &types.QueryGetStrategyResponse{Strategy: msgs[0]},
desc: "First",
request: &types.QueryGetStrategyRequest{Denom: vaultDenom, Id: msgs[0].Id},
response: &types.QueryGetStrategyResponse{
Strategy: types.StrategyContainer{
Strategy: msgs[0], DepositFeeRate: math.LegacyZeroDec(), WithdrawFeeRate: math.LegacyZeroDec(), PerformanceFeeRate: math.LegacyZeroDec(),
},
},
},
{
desc: "Second",
request: &types.QueryGetStrategyRequest{Denom: vaultDenom, Id: msgs[1].Id},
response: &types.QueryGetStrategyResponse{Strategy: msgs[1]},
desc: "Second",
request: &types.QueryGetStrategyRequest{Denom: vaultDenom, Id: msgs[1].Id},
response: &types.QueryGetStrategyResponse{
Strategy: types.StrategyContainer{
Strategy: msgs[1], DepositFeeRate: math.LegacyZeroDec(), WithdrawFeeRate: math.LegacyZeroDec(), PerformanceFeeRate: math.LegacyZeroDec(),
},
},
},
{
desc: "KeyNotFound",
Expand Down
Loading