2020-09-21 21:08:43 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
supplyexported "github.com/cosmos/cosmos-sdk/x/supply/exported"
|
|
|
|
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
2020-12-21 17:18:55 +00:00
|
|
|
"github.com/kava-labs/kava/x/hard/types"
|
2020-09-21 21:08:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewQuerier is the module level router for state queries
|
|
|
|
func NewQuerier(k Keeper) sdk.Querier {
|
|
|
|
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err error) {
|
|
|
|
switch path[0] {
|
|
|
|
case types.QueryGetParams:
|
|
|
|
return queryGetParams(ctx, req, k)
|
|
|
|
case types.QueryGetModuleAccounts:
|
|
|
|
return queryGetModAccounts(ctx, req, k)
|
|
|
|
case types.QueryGetDeposits:
|
|
|
|
return queryGetDeposits(ctx, req, k)
|
|
|
|
case types.QueryGetClaims:
|
|
|
|
return queryGetClaims(ctx, req, k)
|
2020-10-30 09:59:47 +00:00
|
|
|
case types.QueryGetBorrows:
|
|
|
|
return queryGetBorrows(ctx, req, k)
|
2020-12-04 19:04:05 +00:00
|
|
|
case types.QueryGetBorrow:
|
|
|
|
return queryGetBorrow(ctx, req, k)
|
2020-11-12 15:50:54 +00:00
|
|
|
case types.QueryGetBorrowed:
|
|
|
|
return queryGetBorrowed(ctx, req, k)
|
2020-09-21 21:08:43 +00:00
|
|
|
default:
|
|
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint", types.ModuleName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 13:26:18 +00:00
|
|
|
func queryGetParams(ctx sdk.Context, _ abci.RequestQuery, k Keeper) ([]byte, error) {
|
2020-09-21 21:08:43 +00:00
|
|
|
// Get params
|
|
|
|
params := k.GetParams(ctx)
|
|
|
|
|
|
|
|
// Encode results
|
|
|
|
bz, err := codec.MarshalJSONIndent(k.cdc, params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryGetModAccounts(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
|
|
|
|
|
|
|
|
var params types.QueryAccountParams
|
|
|
|
err := types.ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
|
|
}
|
|
|
|
var accs []supplyexported.ModuleAccountI
|
|
|
|
if len(params.Name) > 0 {
|
2020-10-14 14:34:31 +00:00
|
|
|
acc := k.supplyKeeper.GetModuleAccount(ctx, params.Name)
|
2020-09-21 21:08:43 +00:00
|
|
|
accs = append(accs, acc)
|
|
|
|
} else {
|
|
|
|
acc := k.supplyKeeper.GetModuleAccount(ctx, types.ModuleAccountName)
|
|
|
|
accs = append(accs, acc)
|
|
|
|
acc = k.supplyKeeper.GetModuleAccount(ctx, types.LPAccount)
|
|
|
|
accs = append(accs, acc)
|
|
|
|
acc = k.supplyKeeper.GetModuleAccount(ctx, types.DelegatorAccount)
|
|
|
|
accs = append(accs, acc)
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := codec.MarshalJSONIndent(k.cdc, accs)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryGetDeposits(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
|
|
|
|
|
|
|
|
var params types.QueryDepositParams
|
|
|
|
err := types.ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
|
|
}
|
2020-10-12 13:26:18 +00:00
|
|
|
depositDenom := len(params.DepositDenom) > 0
|
|
|
|
owner := len(params.Owner) > 0
|
2020-09-21 21:08:43 +00:00
|
|
|
|
|
|
|
var deposits []types.Deposit
|
2020-10-12 13:26:18 +00:00
|
|
|
switch {
|
2020-11-12 16:43:28 +00:00
|
|
|
case depositDenom && owner:
|
2020-12-18 16:05:21 +00:00
|
|
|
deposit, found := k.GetDeposit(ctx, params.Owner)
|
2020-09-21 21:08:43 +00:00
|
|
|
if found {
|
2020-12-18 16:05:21 +00:00
|
|
|
for _, depCoin := range deposit.Amount {
|
|
|
|
if depCoin.Denom == params.DepositDenom {
|
|
|
|
deposits = append(deposits, deposit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
2020-11-12 16:43:28 +00:00
|
|
|
case depositDenom:
|
2020-12-18 16:05:21 +00:00
|
|
|
k.IterateDeposits(ctx, func(deposit types.Deposit) (stop bool) {
|
|
|
|
if deposit.Amount.AmountOf(params.DepositDenom).IsPositive() {
|
|
|
|
deposits = append(deposits, deposit)
|
|
|
|
}
|
2020-09-21 21:08:43 +00:00
|
|
|
return false
|
|
|
|
})
|
2020-11-12 16:43:28 +00:00
|
|
|
case owner:
|
2020-12-18 16:05:21 +00:00
|
|
|
deposit, found := k.GetDeposit(ctx, params.Owner)
|
|
|
|
if found {
|
|
|
|
deposits = append(deposits, deposit)
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
2020-10-12 13:26:18 +00:00
|
|
|
default:
|
2020-09-21 21:08:43 +00:00
|
|
|
k.IterateDeposits(ctx, func(deposit types.Deposit) (stop bool) {
|
|
|
|
deposits = append(deposits, deposit)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
start, end := client.Paginate(len(deposits), params.Page, params.Limit, 100)
|
|
|
|
if start < 0 || end < 0 {
|
|
|
|
deposits = []types.Deposit{}
|
|
|
|
} else {
|
|
|
|
deposits = deposits[start:end]
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := codec.MarshalJSONIndent(types.ModuleCdc, deposits)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryGetClaims(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
|
|
|
|
|
|
|
|
var params types.QueryClaimParams
|
|
|
|
err := types.ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
|
|
}
|
2020-10-12 13:26:18 +00:00
|
|
|
depositDenom := len(params.DepositDenom) > 0
|
|
|
|
owner := len(params.Owner) > 0
|
2020-11-12 16:43:28 +00:00
|
|
|
claimType := len(params.ClaimType) > 0
|
2020-09-21 21:08:43 +00:00
|
|
|
|
|
|
|
var claims []types.Claim
|
2020-10-12 13:26:18 +00:00
|
|
|
switch {
|
2020-11-12 16:43:28 +00:00
|
|
|
case depositDenom && owner && claimType:
|
|
|
|
claim, found := k.GetClaim(ctx, params.Owner, params.DepositDenom, params.ClaimType)
|
2020-09-21 21:08:43 +00:00
|
|
|
if found {
|
|
|
|
claims = append(claims, claim)
|
|
|
|
}
|
2020-10-12 13:26:18 +00:00
|
|
|
case depositDenom && owner:
|
2020-11-12 16:43:28 +00:00
|
|
|
for _, dt := range types.ClaimTypesClaimQuery {
|
2020-09-21 21:08:43 +00:00
|
|
|
claim, found := k.GetClaim(ctx, params.Owner, params.DepositDenom, dt)
|
|
|
|
if found {
|
|
|
|
claims = append(claims, claim)
|
|
|
|
}
|
|
|
|
}
|
2020-11-12 16:43:28 +00:00
|
|
|
case depositDenom && claimType:
|
|
|
|
k.IterateClaimsByTypeAndDenom(ctx, params.ClaimType, params.DepositDenom, func(claim types.Claim) (stop bool) {
|
2020-09-21 21:08:43 +00:00
|
|
|
claims = append(claims, claim)
|
|
|
|
return false
|
|
|
|
})
|
2020-11-12 16:43:28 +00:00
|
|
|
case owner && claimType:
|
2020-12-21 17:18:55 +00:00
|
|
|
hardParams := k.GetParams(ctx)
|
2020-10-13 14:37:46 +00:00
|
|
|
switch {
|
2020-11-12 16:43:28 +00:00
|
|
|
case params.ClaimType == types.LP:
|
2020-12-21 17:18:55 +00:00
|
|
|
for _, lps := range hardParams.LiquidityProviderSchedules {
|
2020-11-12 16:43:28 +00:00
|
|
|
claim, found := k.GetClaim(ctx, params.Owner, lps.DepositDenom, params.ClaimType)
|
2020-10-13 14:37:46 +00:00
|
|
|
if found {
|
|
|
|
claims = append(claims, claim)
|
|
|
|
}
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
2020-11-12 16:43:28 +00:00
|
|
|
case params.ClaimType == types.Stake:
|
2020-12-21 17:18:55 +00:00
|
|
|
for _, dss := range hardParams.DelegatorDistributionSchedules {
|
2020-11-12 16:43:28 +00:00
|
|
|
claim, found := k.GetClaim(ctx, params.Owner, dss.DistributionSchedule.DepositDenom, params.ClaimType)
|
2020-10-13 14:37:46 +00:00
|
|
|
if found {
|
|
|
|
claims = append(claims, claim)
|
|
|
|
}
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-12 13:26:18 +00:00
|
|
|
case depositDenom:
|
2020-11-12 16:43:28 +00:00
|
|
|
for _, dt := range types.ClaimTypesClaimQuery {
|
2020-09-21 21:08:43 +00:00
|
|
|
k.IterateClaimsByTypeAndDenom(ctx, dt, params.DepositDenom, func(claim types.Claim) (stop bool) {
|
|
|
|
claims = append(claims, claim)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
2020-10-12 13:26:18 +00:00
|
|
|
case owner:
|
2020-12-21 17:18:55 +00:00
|
|
|
hardParams := k.GetParams(ctx)
|
|
|
|
for _, lps := range hardParams.LiquidityProviderSchedules {
|
2020-10-09 13:28:18 +00:00
|
|
|
claim, found := k.GetClaim(ctx, params.Owner, lps.DepositDenom, types.LP)
|
|
|
|
if found {
|
|
|
|
claims = append(claims, claim)
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-21 17:18:55 +00:00
|
|
|
for _, dds := range hardParams.DelegatorDistributionSchedules {
|
2020-10-09 13:28:18 +00:00
|
|
|
claim, found := k.GetClaim(ctx, params.Owner, dds.DistributionSchedule.DepositDenom, types.Stake)
|
|
|
|
if found {
|
|
|
|
claims = append(claims, claim)
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-12 16:43:28 +00:00
|
|
|
case claimType:
|
2020-12-21 17:18:55 +00:00
|
|
|
hardParams := k.GetParams(ctx)
|
|
|
|
for _, lps := range hardParams.LiquidityProviderSchedules {
|
2020-11-12 16:43:28 +00:00
|
|
|
k.IterateClaimsByTypeAndDenom(ctx, params.ClaimType, lps.DepositDenom, func(claim types.Claim) (stop bool) {
|
2020-09-21 21:08:43 +00:00
|
|
|
claims = append(claims, claim)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
2020-12-21 17:18:55 +00:00
|
|
|
for _, dds := range hardParams.DelegatorDistributionSchedules {
|
2020-11-12 16:43:28 +00:00
|
|
|
k.IterateClaimsByTypeAndDenom(ctx, params.ClaimType, dds.DistributionSchedule.DepositDenom, func(claim types.Claim) (stop bool) {
|
2020-09-21 21:08:43 +00:00
|
|
|
claims = append(claims, claim)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
2020-10-12 13:26:18 +00:00
|
|
|
default:
|
2020-09-21 21:08:43 +00:00
|
|
|
k.IterateClaims(ctx, func(claim types.Claim) (stop bool) {
|
|
|
|
claims = append(claims, claim)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
start, end := client.Paginate(len(claims), params.Page, params.Limit, 100)
|
|
|
|
if start < 0 || end < 0 {
|
|
|
|
claims = []types.Claim{}
|
|
|
|
} else {
|
|
|
|
claims = claims[start:end]
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := codec.MarshalJSONIndent(types.ModuleCdc, claims)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
2020-10-30 09:59:47 +00:00
|
|
|
|
|
|
|
func queryGetBorrows(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
|
|
|
|
|
2020-12-22 16:08:27 +00:00
|
|
|
var params types.QueryBorrowsParams
|
2020-10-30 09:59:47 +00:00
|
|
|
err := types.ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: filter query results
|
|
|
|
// depositDenom := len(params.BorrowDenom) > 0
|
|
|
|
// owner := len(params.Owner) > 0
|
|
|
|
|
|
|
|
var borrows []types.Borrow
|
|
|
|
k.IterateBorrows(ctx, func(borrow types.Borrow) (stop bool) {
|
|
|
|
borrows = append(borrows, borrow)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
start, end := client.Paginate(len(borrows), params.Page, params.Limit, 100)
|
|
|
|
if start < 0 || end < 0 {
|
|
|
|
borrows = []types.Borrow{}
|
|
|
|
} else {
|
|
|
|
borrows = borrows[start:end]
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := codec.MarshalJSONIndent(types.ModuleCdc, borrows)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
2020-11-12 15:50:54 +00:00
|
|
|
|
|
|
|
func queryGetBorrowed(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
|
|
|
|
var params types.QueryBorrowedParams
|
|
|
|
err := types.ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
borrowedCoins, found := k.GetBorrowedCoins(ctx)
|
|
|
|
if !found {
|
|
|
|
return nil, types.ErrBorrowedCoinsNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// If user specified a denom only return coins of that denom type
|
|
|
|
if len(params.Denom) > 0 {
|
|
|
|
borrowedCoins = sdk.NewCoins(sdk.NewCoin(params.Denom, borrowedCoins.AmountOf(params.Denom)))
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := codec.MarshalJSONIndent(types.ModuleCdc, borrowedCoins)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
2020-12-04 19:04:05 +00:00
|
|
|
|
|
|
|
func queryGetBorrow(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
|
2020-12-22 16:08:27 +00:00
|
|
|
var params types.QueryBorrowsParams
|
2020-12-04 19:04:05 +00:00
|
|
|
err := types.ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var borrowBalance sdk.Coins
|
|
|
|
if len(params.Owner) > 0 {
|
|
|
|
borrowBalance = k.GetBorrowBalance(ctx, params.Owner)
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := codec.MarshalJSONIndent(types.ModuleCdc, borrowBalance)
|
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|