Skip to content

Commit

Permalink
chore: refactor x/query
Browse files Browse the repository at this point in the history
  • Loading branch information
shifty11 committed Oct 3, 2023
1 parent 8a7fea3 commit 0c0cec8
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 95 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ func NewKYVEApp(
app.GlobalKeeper,
*app.GovKeeper,
app.TeamKeeper,
app.FundersKeeper,
)
// this line is used by starport scaffolding # stargate/app/keeperDefinition

Expand Down
38 changes: 20 additions & 18 deletions x/query/keeper/grpc_account_funded.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,29 @@ func (k Keeper) AccountFundedList(goCtx context.Context, req *types.QueryAccount

pools := k.poolKeeper.GetAllPools(ctx)

for i := range pools {
pool := pools[i]
// TODO(rapha): fix this
//amount := pool.GetFunderAmount(req.Address)
amount := uint64(0)
for _, pool := range pools {
funding, found := k.fundersKeeper.GetFunding(ctx, req.Address, pool.Id)
if !found {
return nil, status.Error(codes.Internal, "funding not found")
}
fundingState, found := k.fundersKeeper.GetFundingState(ctx, pool.Id)
if !found {
return nil, status.Error(codes.Internal, "funding state not found")
}

if amount > 0 {
if funding.Amount > 0 {
funded = append(funded, types.Funded{
Amount: amount,
Amount: funding.Amount,
Pool: &types.BasicPool{
Id: pool.Id,
Name: pool.Name,
Runtime: pool.Runtime,
Logo: pool.Logo,
OperatingCost: pool.OperatingCost,
UploadInterval: pool.UploadInterval,
// TODO(rapha): fix this
TotalFunds: 0,
//TotalFunds: pool.TotalFunds,
TotalDelegation: k.delegationKeeper.GetDelegationOfPool(ctx, pool.Id),
Status: k.GetPoolStatus(ctx, &pool),
Id: pool.Id,
Name: pool.Name,
Runtime: pool.Runtime,
Logo: pool.Logo,
InflationShareWeight: pool.InflationShareWeight,
UploadInterval: pool.UploadInterval,
TotalFunds: fundingState.TotalAmount,
TotalDelegation: k.delegationKeeper.GetDelegationOfPool(ctx, pool.Id),
Status: k.GetPoolStatus(ctx, &pool, &fundingState),
},
})
}
Expand Down
2 changes: 1 addition & 1 deletion x/query/keeper/grpc_query_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (k Keeper) parsePoolResponse(ctx sdk.Context, pool *poolTypes.Pool) types.P
Stakers: stakers,
TotalSelfDelegation: totalSelfDelegation,
TotalDelegation: totalDelegation,
Status: k.GetPoolStatus(ctx, pool),
Status: k.GetPoolStatus(ctx, pool, nil),
Account: poolAccount.String(),
AccountBalance: poolBalance,
}
Expand Down
36 changes: 21 additions & 15 deletions x/query/keeper/helper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
funderstypes "github.com/KYVENetwork/chain/x/funders/types"
globalTypes "github.com/KYVENetwork/chain/x/global/types"
pooltypes "github.com/KYVENetwork/chain/x/pool/types"
"github.com/KYVENetwork/chain/x/query/types"
Expand Down Expand Up @@ -37,23 +38,23 @@ func (k Keeper) GetFullStaker(ctx sdk.Context, stakerAddress string) *types.Full
for _, valaccount := range k.stakerKeeper.GetValaccountsFromStaker(ctx, staker.Address) {

pool, _ := k.poolKeeper.GetPool(ctx, valaccount.PoolId)
fundingState, _ := k.fundersKeeper.GetFundingState(ctx, valaccount.PoolId)

accountValaddress, _ := sdk.AccAddressFromBech32(valaccount.Valaddress)
balanceValaccount := k.bankKeeper.GetBalance(ctx, accountValaddress, globalTypes.Denom).Amount.Uint64()

poolMemberships = append(
poolMemberships, &types.PoolMembership{
Pool: &types.BasicPool{
Id: pool.Id,
Name: pool.Name,
Runtime: pool.Runtime,
Logo: pool.Logo,
OperatingCost: pool.OperatingCost,
UploadInterval: pool.UploadInterval,
// TODO(rapha): fix this
//TotalFunds: pool.TotalFunds,
TotalDelegation: k.delegationKeeper.GetDelegationOfPool(ctx, pool.Id),
Status: k.GetPoolStatus(ctx, &pool),
Id: pool.Id,
Name: pool.Name,
Runtime: pool.Runtime,
Logo: pool.Logo,
InflationShareWeight: pool.InflationShareWeight,
UploadInterval: pool.UploadInterval,
TotalFunds: fundingState.TotalAmount,
TotalDelegation: k.delegationKeeper.GetDelegationOfPool(ctx, pool.Id),
Status: k.GetPoolStatus(ctx, &pool, &fundingState),
},
Points: valaccount.Points,
IsLeaving: valaccount.IsLeaving,
Expand Down Expand Up @@ -82,22 +83,27 @@ func (k Keeper) GetFullStaker(ctx sdk.Context, stakerAddress string) *types.Full
}
}

func (k Keeper) GetPoolStatus(ctx sdk.Context, pool *pooltypes.Pool) pooltypes.PoolStatus {
func (k Keeper) GetPoolStatus(ctx sdk.Context, pool *pooltypes.Pool, fundingState *funderstypes.FundingState) pooltypes.PoolStatus {
totalDelegation := k.delegationKeeper.GetDelegationOfPool(ctx, pool.Id)

var poolStatus pooltypes.PoolStatus

poolStatus = pooltypes.POOL_STATUS_ACTIVE
if pool.UpgradePlan.ScheduledAt > 0 && uint64(ctx.BlockTime().Unix()) >= pool.UpgradePlan.ScheduledAt {
poolStatus = pooltypes.POOL_STATUS_UPGRADING
} else if pool.Disabled {
poolStatus = pooltypes.POOL_STATUS_DISABLED
} else if totalDelegation < pool.MinDelegation {
poolStatus = pooltypes.POOL_STATUS_NOT_ENOUGH_DELEGATION
// TODO(rapha): fix this
//} else if pool.TotalFunds == 0 {
// poolStatus = pooltypes.POOL_STATUS_NO_FUNDS
} else {
poolStatus = pooltypes.POOL_STATUS_ACTIVE
// Get funding state if not provided
if fundingState == nil {
fs, _ := k.fundersKeeper.GetFundingState(ctx, pool.Id)
fundingState = &fs
}
if fundingState.TotalAmount == 0 {
poolStatus = pooltypes.POOL_STATUS_NO_FUNDS
}
}

return poolStatus
Expand Down
4 changes: 4 additions & 0 deletions x/query/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"fmt"
fundersKeeper "github.com/KYVENetwork/chain/x/funders/keeper"

globalKeeper "github.com/KYVENetwork/chain/x/global/keeper"
teamKeeper "github.com/KYVENetwork/chain/x/team/keeper"
Expand Down Expand Up @@ -41,6 +42,7 @@ type (
globalKeeper globalKeeper.Keeper
govKeeper govkeeper.Keeper
teamKeeper teamKeeper.Keeper
fundersKeeper fundersKeeper.Keeper
}
)

Expand All @@ -60,6 +62,7 @@ func NewKeeper(
globalKeeper globalKeeper.Keeper,
govKeeper govkeeper.Keeper,
teamKeeper teamKeeper.Keeper,
fundersKeeper fundersKeeper.Keeper,
) *Keeper {
return &Keeper{
cdc: cdc,
Expand All @@ -77,6 +80,7 @@ func NewKeeper(
globalKeeper: globalKeeper,
govKeeper: govKeeper,
teamKeeper: teamKeeper,
fundersKeeper: fundersKeeper,
}
}

Expand Down
123 changes: 62 additions & 61 deletions x/query/types/query.pb.go

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

0 comments on commit 0c0cec8

Please sign in to comment.