2020-09-21 21:08:43 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2023-04-05 23:21:59 +00:00
|
|
|
errorsmod "cosmossdk.io/errors"
|
2020-09-21 21:08:43 +00:00
|
|
|
"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"
|
2022-01-08 00:39:27 +00:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2020-09-21 21:08:43 +00:00
|
|
|
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
2024-05-01 03:17:24 +00:00
|
|
|
"github.com/0glabs/0g-chain/x/hard/types"
|
2020-09-21 21:08:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewQuerier is the module level router for state queries
|
2022-01-08 00:39:27 +00:00
|
|
|
func NewQuerier(k Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
|
2020-09-21 21:08:43 +00:00
|
|
|
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err error) {
|
|
|
|
switch path[0] {
|
|
|
|
case types.QueryGetParams:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryGetParams(ctx, req, k, legacyQuerierCdc)
|
2020-09-21 21:08:43 +00:00
|
|
|
case types.QueryGetModuleAccounts:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryGetModAccounts(ctx, req, k, legacyQuerierCdc)
|
2020-09-21 21:08:43 +00:00
|
|
|
case types.QueryGetDeposits:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryGetDeposits(ctx, req, k, legacyQuerierCdc)
|
2021-03-22 17:57:07 +00:00
|
|
|
case types.QueryGetUnsyncedDeposits:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryGetUnsyncedDeposits(ctx, req, k, legacyQuerierCdc)
|
2021-01-13 18:14:58 +00:00
|
|
|
case types.QueryGetTotalDeposited:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryGetTotalDeposited(ctx, req, k, legacyQuerierCdc)
|
2020-10-30 09:59:47 +00:00
|
|
|
case types.QueryGetBorrows:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryGetBorrows(ctx, req, k, legacyQuerierCdc)
|
2021-03-22 17:57:07 +00:00
|
|
|
case types.QueryGetUnsyncedBorrows:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryGetUnsyncedBorrows(ctx, req, k, legacyQuerierCdc)
|
2021-01-13 18:14:58 +00:00
|
|
|
case types.QueryGetTotalBorrowed:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryGetTotalBorrowed(ctx, req, k, legacyQuerierCdc)
|
2021-02-10 17:53:53 +00:00
|
|
|
case types.QueryGetInterestRate:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryGetInterestRate(ctx, req, k, legacyQuerierCdc)
|
2021-02-22 20:48:52 +00:00
|
|
|
case types.QueryGetReserves:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryGetReserves(ctx, req, k, legacyQuerierCdc)
|
2021-03-22 17:57:07 +00:00
|
|
|
case types.QueryGetInterestFactors:
|
2022-01-08 00:39:27 +00:00
|
|
|
return queryGetInterestFactors(ctx, req, k, legacyQuerierCdc)
|
2020-09-21 21:08:43 +00:00
|
|
|
default:
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint", types.ModuleName)
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryGetParams(ctx sdk.Context, _ abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-09-21 21:08:43 +00:00
|
|
|
// Get params
|
|
|
|
params := k.GetParams(ctx)
|
|
|
|
|
|
|
|
// Encode results
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, params)
|
2020-09-21 21:08:43 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryGetModAccounts(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-09-21 21:08:43 +00:00
|
|
|
var params types.QueryAccountParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2020-09-21 21:08:43 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
var accs []authtypes.ModuleAccountI
|
2020-09-21 21:08:43 +00:00
|
|
|
if len(params.Name) > 0 {
|
2022-01-08 00:39:27 +00:00
|
|
|
acc := k.accountKeeper.GetModuleAccount(ctx, params.Name)
|
2020-09-21 21:08:43 +00:00
|
|
|
accs = append(accs, acc)
|
|
|
|
} else {
|
2022-01-08 00:39:27 +00:00
|
|
|
acc := k.accountKeeper.GetModuleAccount(ctx, types.ModuleAccountName)
|
2020-09-21 21:08:43 +00:00
|
|
|
accs = append(accs, acc)
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
// Include module account coins with its account to keep backwards compatibility with v39 account behavior
|
|
|
|
response := make([]types.ModAccountWithCoins, len(accs))
|
|
|
|
for i, acc := range accs {
|
|
|
|
coins := k.bankKeeper.GetAllBalances(ctx, acc.GetAddress())
|
|
|
|
response[i] = types.ModAccountWithCoins{
|
|
|
|
Account: acc,
|
|
|
|
Coins: coins,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, response)
|
2020-09-21 21:08:43 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryGetDeposits(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2021-01-07 16:22:00 +00:00
|
|
|
var params types.QueryDepositsParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2020-09-21 21:08:43 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
2021-01-13 18:14:58 +00:00
|
|
|
denom := len(params.Denom) > 0
|
2020-10-12 13:26:18 +00:00
|
|
|
owner := len(params.Owner) > 0
|
2020-09-21 21:08:43 +00:00
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
var deposits types.Deposits
|
2020-10-12 13:26:18 +00:00
|
|
|
switch {
|
2021-01-13 18:14:58 +00:00
|
|
|
case owner && denom:
|
|
|
|
deposit, found := k.GetSyncedDeposit(ctx, params.Owner)
|
2020-09-21 21:08:43 +00:00
|
|
|
if found {
|
2021-01-13 18:14:58 +00:00
|
|
|
for _, coin := range deposit.Amount {
|
|
|
|
if coin.Denom == params.Denom {
|
2020-12-18 16:05:21 +00:00
|
|
|
deposits = append(deposits, deposit)
|
|
|
|
}
|
|
|
|
}
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
2021-01-13 18:14:58 +00:00
|
|
|
case owner:
|
|
|
|
deposit, found := k.GetSyncedDeposit(ctx, params.Owner)
|
|
|
|
if found {
|
|
|
|
deposits = append(deposits, deposit)
|
|
|
|
}
|
|
|
|
case denom:
|
2020-12-18 16:05:21 +00:00
|
|
|
k.IterateDeposits(ctx, func(deposit types.Deposit) (stop bool) {
|
2021-01-13 18:14:58 +00:00
|
|
|
if deposit.Amount.AmountOf(params.Denom).IsPositive() {
|
2020-12-18 16:05:21 +00:00
|
|
|
deposits = append(deposits, deposit)
|
|
|
|
}
|
2020-09-21 21:08:43 +00:00
|
|
|
return false
|
|
|
|
})
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
var bz []byte
|
|
|
|
|
|
|
|
// If owner param was specified then deposits array already contains the user's synced deposit
|
|
|
|
if owner {
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err = codec.MarshalJSONIndent(legacyQuerierCdc, deposits)
|
2021-01-13 18:14:58 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2021-01-13 18:14:58 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise we need to simulate syncing of each deposit
|
|
|
|
var syncedDeposits types.Deposits
|
|
|
|
for _, deposit := range deposits {
|
|
|
|
syncedDeposit, _ := k.GetSyncedDeposit(ctx, deposit.Depositor)
|
|
|
|
syncedDeposits = append(syncedDeposits, syncedDeposit)
|
|
|
|
}
|
|
|
|
|
|
|
|
start, end := client.Paginate(len(syncedDeposits), params.Page, params.Limit, 100)
|
2020-09-21 21:08:43 +00:00
|
|
|
if start < 0 || end < 0 {
|
2021-01-13 18:14:58 +00:00
|
|
|
syncedDeposits = types.Deposits{}
|
2020-09-21 21:08:43 +00:00
|
|
|
} else {
|
2021-01-13 18:14:58 +00:00
|
|
|
syncedDeposits = syncedDeposits[start:end]
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err = codec.MarshalJSONIndent(legacyQuerierCdc, syncedDeposits)
|
2020-09-21 21:08:43 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryGetUnsyncedDeposits(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2021-03-22 17:57:07 +00:00
|
|
|
var params types.QueryUnsyncedDepositsParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2021-03-22 17:57:07 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2021-03-22 17:57:07 +00:00
|
|
|
}
|
|
|
|
denom := len(params.Denom) > 0
|
|
|
|
owner := len(params.Owner) > 0
|
|
|
|
|
|
|
|
var deposits types.Deposits
|
|
|
|
switch {
|
|
|
|
case owner && denom:
|
|
|
|
deposit, found := k.GetDeposit(ctx, params.Owner)
|
|
|
|
if found {
|
|
|
|
for _, coin := range deposit.Amount {
|
|
|
|
if coin.Denom == params.Denom {
|
|
|
|
deposits = append(deposits, deposit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case owner:
|
|
|
|
deposit, found := k.GetDeposit(ctx, params.Owner)
|
|
|
|
if found {
|
|
|
|
deposits = append(deposits, deposit)
|
|
|
|
}
|
|
|
|
case denom:
|
|
|
|
k.IterateDeposits(ctx, func(deposit types.Deposit) (stop bool) {
|
|
|
|
if deposit.Amount.AmountOf(params.Denom).IsPositive() {
|
|
|
|
deposits = append(deposits, deposit)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
k.IterateDeposits(ctx, func(deposit types.Deposit) (stop bool) {
|
|
|
|
deposits = append(deposits, deposit)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var bz []byte
|
|
|
|
|
|
|
|
start, end := client.Paginate(len(deposits), params.Page, params.Limit, 100)
|
|
|
|
if start < 0 || end < 0 {
|
|
|
|
deposits = types.Deposits{}
|
|
|
|
} else {
|
|
|
|
deposits = deposits[start:end]
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err = codec.MarshalJSONIndent(legacyQuerierCdc, deposits)
|
2021-03-22 17:57:07 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2021-03-22 17:57:07 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryGetBorrows(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2020-12-22 16:08:27 +00:00
|
|
|
var params types.QueryBorrowsParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2020-10-30 09:59:47 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-10-30 09:59:47 +00:00
|
|
|
}
|
2021-01-13 18:14:58 +00:00
|
|
|
denom := len(params.Denom) > 0
|
|
|
|
owner := len(params.Owner) > 0
|
2020-10-30 09:59:47 +00:00
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
var borrows types.Borrows
|
|
|
|
switch {
|
|
|
|
case owner && denom:
|
|
|
|
borrow, found := k.GetSyncedBorrow(ctx, params.Owner)
|
|
|
|
if found {
|
|
|
|
for _, coin := range borrow.Amount {
|
|
|
|
if coin.Denom == params.Denom {
|
|
|
|
borrows = append(borrows, borrow)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case owner:
|
|
|
|
borrow, found := k.GetSyncedBorrow(ctx, params.Owner)
|
|
|
|
if found {
|
|
|
|
borrows = append(borrows, borrow)
|
|
|
|
}
|
|
|
|
case denom:
|
|
|
|
k.IterateBorrows(ctx, func(borrow types.Borrow) (stop bool) {
|
|
|
|
if borrow.Amount.AmountOf(params.Denom).IsPositive() {
|
|
|
|
borrows = append(borrows, borrow)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
k.IterateBorrows(ctx, func(borrow types.Borrow) (stop bool) {
|
|
|
|
borrows = append(borrows, borrow)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var bz []byte
|
2020-10-30 09:59:47 +00:00
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
// If owner param was specified then borrows array already contains the user's synced borrow
|
|
|
|
if owner {
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err = codec.MarshalJSONIndent(legacyQuerierCdc, borrows)
|
2021-01-13 18:14:58 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2021-01-13 18:14:58 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise we need to simulate syncing of each borrow
|
|
|
|
var syncedBorrows types.Borrows
|
|
|
|
for _, borrow := range borrows {
|
|
|
|
syncedBorrow, _ := k.GetSyncedBorrow(ctx, borrow.Borrower)
|
|
|
|
syncedBorrows = append(syncedBorrows, syncedBorrow)
|
|
|
|
}
|
2020-10-30 09:59:47 +00:00
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
start, end := client.Paginate(len(syncedBorrows), params.Page, params.Limit, 100)
|
2020-10-30 09:59:47 +00:00
|
|
|
if start < 0 || end < 0 {
|
2021-01-13 18:14:58 +00:00
|
|
|
syncedBorrows = types.Borrows{}
|
2020-10-30 09:59:47 +00:00
|
|
|
} else {
|
2021-01-13 18:14:58 +00:00
|
|
|
syncedBorrows = syncedBorrows[start:end]
|
2020-10-30 09:59:47 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err = codec.MarshalJSONIndent(legacyQuerierCdc, syncedBorrows)
|
2020-10-30 09:59:47 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-10-30 09:59:47 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
2020-11-12 15:50:54 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryGetUnsyncedBorrows(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2021-03-22 17:57:07 +00:00
|
|
|
var params types.QueryUnsyncedBorrowsParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2021-03-22 17:57:07 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2021-03-22 17:57:07 +00:00
|
|
|
}
|
|
|
|
denom := len(params.Denom) > 0
|
|
|
|
owner := len(params.Owner) > 0
|
|
|
|
|
|
|
|
var borrows types.Borrows
|
|
|
|
switch {
|
|
|
|
case owner && denom:
|
|
|
|
borrow, found := k.GetBorrow(ctx, params.Owner)
|
|
|
|
if found {
|
|
|
|
for _, coin := range borrow.Amount {
|
|
|
|
if coin.Denom == params.Denom {
|
|
|
|
borrows = append(borrows, borrow)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case owner:
|
|
|
|
borrow, found := k.GetBorrow(ctx, params.Owner)
|
|
|
|
if found {
|
|
|
|
borrows = append(borrows, borrow)
|
|
|
|
}
|
|
|
|
case denom:
|
|
|
|
k.IterateBorrows(ctx, func(borrow types.Borrow) (stop bool) {
|
|
|
|
if borrow.Amount.AmountOf(params.Denom).IsPositive() {
|
|
|
|
borrows = append(borrows, borrow)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
k.IterateBorrows(ctx, func(borrow types.Borrow) (stop bool) {
|
|
|
|
borrows = append(borrows, borrow)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var bz []byte
|
|
|
|
|
|
|
|
start, end := client.Paginate(len(borrows), params.Page, params.Limit, 100)
|
|
|
|
if start < 0 || end < 0 {
|
|
|
|
borrows = types.Borrows{}
|
|
|
|
} else {
|
|
|
|
borrows = borrows[start:end]
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err = codec.MarshalJSONIndent(legacyQuerierCdc, borrows)
|
2021-03-22 17:57:07 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2021-03-22 17:57:07 +00:00
|
|
|
}
|
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryGetTotalBorrowed(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2021-01-13 18:14:58 +00:00
|
|
|
var params types.QueryTotalBorrowedParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2020-11-12 15:50:54 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-11-12 15:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
borrowedCoins, found := k.GetBorrowedCoins(ctx)
|
|
|
|
if !found {
|
2022-09-29 19:28:10 +00:00
|
|
|
borrowedCoins = sdk.NewCoins()
|
2020-11-12 15:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)))
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, borrowedCoins)
|
2020-11-12 15:50:54 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2020-11-12 15:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
2020-12-04 19:04:05 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryGetTotalDeposited(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2021-01-13 18:14:58 +00:00
|
|
|
var params types.QueryTotalDepositedParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2020-12-04 19:04:05 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2020-12-04 19:04:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
suppliedCoins, found := k.GetSuppliedCoins(ctx)
|
|
|
|
if !found {
|
2022-09-29 19:28:10 +00:00
|
|
|
suppliedCoins = sdk.NewCoins()
|
2021-01-07 16:22:00 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 18:14:58 +00:00
|
|
|
// If user specified a denom only return coins of that denom type
|
|
|
|
if len(params.Denom) > 0 {
|
|
|
|
suppliedCoins = sdk.NewCoins(sdk.NewCoin(params.Denom, suppliedCoins.AmountOf(params.Denom)))
|
2021-01-07 16:22:00 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, suppliedCoins)
|
2021-01-07 16:22:00 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2021-01-07 16:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
2021-02-10 17:53:53 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryGetInterestRate(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2021-02-10 17:53:53 +00:00
|
|
|
var params types.QueryInterestRateParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2021-02-10 17:53:53 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2021-02-10 17:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var moneyMarketInterestRates types.MoneyMarketInterestRates
|
|
|
|
var moneyMarkets types.MoneyMarkets
|
|
|
|
if len(params.Denom) > 0 {
|
|
|
|
moneyMarket, found := k.GetMoneyMarket(ctx, params.Denom)
|
|
|
|
if !found {
|
|
|
|
return nil, types.ErrMoneyMarketNotFound
|
|
|
|
}
|
|
|
|
moneyMarkets = append(moneyMarkets, moneyMarket)
|
|
|
|
} else {
|
|
|
|
moneyMarkets = k.GetAllMoneyMarkets(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the borrow and supply APY interest rates for each money market
|
|
|
|
for _, moneyMarket := range moneyMarkets {
|
|
|
|
denom := moneyMarket.Denom
|
2022-01-08 00:39:27 +00:00
|
|
|
macc := k.accountKeeper.GetModuleAccount(ctx, types.ModuleName)
|
|
|
|
cash := k.bankKeeper.GetBalance(ctx, macc.GetAddress(), denom).Amount
|
2021-02-10 17:53:53 +00:00
|
|
|
|
|
|
|
borrowed := sdk.NewCoin(denom, sdk.ZeroInt())
|
|
|
|
borrowedCoins, foundBorrowedCoins := k.GetBorrowedCoins(ctx)
|
|
|
|
if foundBorrowedCoins {
|
|
|
|
borrowed = sdk.NewCoin(denom, borrowedCoins.AmountOf(denom))
|
|
|
|
}
|
|
|
|
|
|
|
|
reserves, foundReserves := k.GetTotalReserves(ctx)
|
|
|
|
if !foundReserves {
|
|
|
|
reserves = sdk.NewCoins()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalculateBorrowRate calculates the current interest rate based on utilization (the fraction of supply that has been borrowed)
|
|
|
|
borrowAPY, err := CalculateBorrowRate(moneyMarket.InterestRateModel, sdk.NewDecFromInt(cash), sdk.NewDecFromInt(borrowed.Amount), sdk.NewDecFromInt(reserves.AmountOf(denom)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
utilRatio := CalculateUtilizationRatio(sdk.NewDecFromInt(cash), sdk.NewDecFromInt(borrowed.Amount), sdk.NewDecFromInt(reserves.AmountOf(denom)))
|
|
|
|
fullSupplyAPY := borrowAPY.Mul(utilRatio)
|
|
|
|
realSupplyAPY := fullSupplyAPY.Mul(sdk.OneDec().Sub(moneyMarket.ReserveFactor))
|
|
|
|
|
|
|
|
moneyMarketInterestRate := types.MoneyMarketInterestRate{
|
|
|
|
Denom: denom,
|
2022-01-08 00:39:27 +00:00
|
|
|
SupplyInterestRate: realSupplyAPY.String(),
|
|
|
|
BorrowInterestRate: borrowAPY.String(),
|
2021-02-10 17:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
moneyMarketInterestRates = append(moneyMarketInterestRates, moneyMarketInterestRate)
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, moneyMarketInterestRates)
|
2021-02-10 17:53:53 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2021-02-10 17:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
2021-02-22 20:48:52 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryGetReserves(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2021-02-22 20:48:52 +00:00
|
|
|
var params types.QueryReservesParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2021-02-22 20:48:52 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2021-02-22 20:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reserveCoins, found := k.GetTotalReserves(ctx)
|
|
|
|
if !found {
|
|
|
|
reserveCoins = sdk.Coins{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If user specified a denom only return coins of that denom type
|
|
|
|
if len(params.Denom) > 0 {
|
|
|
|
reserveCoins = sdk.NewCoins(sdk.NewCoin(params.Denom, reserveCoins.AmountOf(params.Denom)))
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, reserveCoins)
|
2021-02-22 20:48:52 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2021-02-22 20:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|
2021-03-22 17:57:07 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func queryGetInterestFactors(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
|
2021-03-22 17:57:07 +00:00
|
|
|
var params types.QueryInterestFactorsParams
|
2022-01-08 00:39:27 +00:00
|
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
2021-03-22 17:57:07 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
2021-03-22 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var interestFactors types.InterestFactors
|
|
|
|
if len(params.Denom) > 0 {
|
|
|
|
// Fetch supply/borrow interest factors for a single denom
|
|
|
|
interestFactor := types.InterestFactor{}
|
|
|
|
interestFactor.Denom = params.Denom
|
|
|
|
supplyInterestFactor, found := k.GetSupplyInterestFactor(ctx, params.Denom)
|
|
|
|
if found {
|
2022-01-08 00:39:27 +00:00
|
|
|
interestFactor.SupplyInterestFactor = supplyInterestFactor.String()
|
2021-03-22 17:57:07 +00:00
|
|
|
}
|
|
|
|
borrowInterestFactor, found := k.GetBorrowInterestFactor(ctx, params.Denom)
|
|
|
|
if found {
|
2022-01-08 00:39:27 +00:00
|
|
|
interestFactor.BorrowInterestFactor = borrowInterestFactor.String()
|
2021-03-22 17:57:07 +00:00
|
|
|
}
|
|
|
|
interestFactors = append(interestFactors, interestFactor)
|
|
|
|
} else {
|
|
|
|
interestFactorMap := make(map[string]types.InterestFactor)
|
|
|
|
// Populate mapping with supply interest factors
|
|
|
|
k.IterateSupplyInterestFactors(ctx, func(denom string, factor sdk.Dec) (stop bool) {
|
2022-01-08 00:39:27 +00:00
|
|
|
interestFactor := types.InterestFactor{Denom: denom, SupplyInterestFactor: factor.String()}
|
2021-03-22 17:57:07 +00:00
|
|
|
interestFactorMap[denom] = interestFactor
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
// Populate mapping with borrow interest factors
|
|
|
|
k.IterateBorrowInterestFactors(ctx, func(denom string, factor sdk.Dec) (stop bool) {
|
|
|
|
interestFactor, ok := interestFactorMap[denom]
|
|
|
|
if !ok {
|
2022-01-08 00:39:27 +00:00
|
|
|
newInterestFactor := types.InterestFactor{Denom: denom, BorrowInterestFactor: factor.String()}
|
2021-03-22 17:57:07 +00:00
|
|
|
interestFactorMap[denom] = newInterestFactor
|
|
|
|
} else {
|
2022-01-08 00:39:27 +00:00
|
|
|
interestFactor.BorrowInterestFactor = factor.String()
|
2021-03-22 17:57:07 +00:00
|
|
|
interestFactorMap[denom] = interestFactor
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
// Translate mapping to slice
|
|
|
|
for _, val := range interestFactorMap {
|
|
|
|
interestFactors = append(interestFactors, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, interestFactors)
|
2021-03-22 17:57:07 +00:00
|
|
|
if err != nil {
|
2023-04-05 23:21:59 +00:00
|
|
|
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2021-03-22 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|