Skip to content

Commit

Permalink
finalize code for G4
Browse files Browse the repository at this point in the history
  • Loading branch information
gachouchani1999 committed Mar 26, 2024
1 parent 45bb0e7 commit 8a95f19
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 43 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ var (
maccPerms = map[string][]string{
authtypes.FeeCollectorName: {authtypes.Burner},
distrtypes.ModuleName: nil,
distrtypes.RewardsDripperName: nil,
minttypes.ModuleName: {authtypes.Minter},
stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
Expand Down
5 changes: 5 additions & 0 deletions x/distribution/keeper/alias_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ func (k Keeper) GetFeePoolCommunityCoins(ctx sdk.Context) sdk.DecCoins {
func (k Keeper) GetDistributionAccount(ctx sdk.Context) authtypes.ModuleAccountI {
return k.authKeeper.GetModuleAccount(ctx, types.ModuleName)
}

// GetRewardsDripperAccount returns the rewards dripper ModuleAccount
func (k Keeper) GetRewardsDripperAccount(ctx sdk.Context) authtypes.ModuleAccountI {
return k.authKeeper.GetModuleAccount(ctx, types.RewardsDripperName)
}
3 changes: 1 addition & 2 deletions x/distribution/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"cosmossdk.io/math"

"github.com/andromedaprotocol/andromedad/x/distribution/types"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
Expand Down Expand Up @@ -141,7 +140,7 @@ func (k Keeper) CalculateDelegationRewards(ctx sdk.Context, val stakingtypes.Val
func (k Keeper) withdrawDelegationRewards(ctx sdk.Context, val stakingtypes.ValidatorI, del stakingtypes.DelegationI) (sdk.Coins, error) {
// check existence of delegator starting info
if !k.HasDelegatorStartingInfo(ctx, del.GetValidatorAddr(), del.GetDelegatorAddr()) {
return nil, types.ErrEmptyDelegationDistInfo
return nil, fmt.Errorf("delegation rewards not found for delegator %s on validator %s")

Check failure on line 143 in x/distribution/keeper/delegation.go

View workflow job for this annotation

GitHub Actions / andromeda amd64 for darwin

fmt.Errorf format %s reads arg #1, but call has 0 args

Check failure on line 143 in x/distribution/keeper/delegation.go

View workflow job for this annotation

GitHub Actions / andromeda amd64 for linux

fmt.Errorf format %s reads arg #1, but call has 0 args

Check failure on line 143 in x/distribution/keeper/delegation.go

View workflow job for this annotation

GitHub Actions / andromeda arm64 for darwin

fmt.Errorf format %s reads arg #1, but call has 0 args

Check failure on line 143 in x/distribution/keeper/delegation.go

View workflow job for this annotation

GitHub Actions / andromeda arm64 for linux

fmt.Errorf format %s reads arg #1, but call has 0 args

Check failure on line 143 in x/distribution/keeper/delegation.go

View workflow job for this annotation

GitHub Actions / andromeda amd64 for windows

fmt.Errorf format %s reads arg #1, but call has 0 args
}

// end current period and calculate rewards
Expand Down
4 changes: 3 additions & 1 deletion x/distribution/keeper/fee_pool.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"fmt"

"github.com/andromedaprotocol/andromedad/x/distribution/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand All @@ -15,7 +17,7 @@ func (k Keeper) DistributeFromFeePool(ctx sdk.Context, amount sdk.Coins, receive
// must be reduced separately from the SendCoinsFromModuleToAccount call
newPool, negative := feePool.CommunityPool.SafeSub(sdk.NewDecCoinsFromCoins(amount...))
if negative {
return types.ErrBadDistribution
return fmt.Errorf("insufficient funds to distribute from the community pool")
}

feePool.CommunityPool = newPool
Expand Down
11 changes: 10 additions & 1 deletion x/distribution/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,20 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) {
if moduleAcc == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}

// check if rewards dripper Acconut exists
rewardsDripperAcc := k.GetRewardsDripperAccount(ctx)
if rewardsDripperAcc == nil {
panic(fmt.Sprintf("%s module account has not been set", types.RewardsDripperName))
}
balances := k.bankKeeper.GetAllBalances(ctx, moduleAcc.GetAddress())
if balances.IsZero() {
k.authKeeper.SetModuleAccount(ctx, moduleAcc)
}
// get all balances for rewards dripper account
rewardBalance := k.bankKeeper.GetAllBalances(ctx, rewardsDripperAcc.GetAddress())
if rewardBalance.IsZero() {
k.authKeeper.SetModuleAccount(ctx, rewardsDripperAcc)
}
if !balances.IsEqual(moduleHoldingsInt) {
panic(fmt.Sprintf("distribution module balance does not match the module holdings: %s <-> %s", balances, moduleHoldingsInt))
}
Expand Down
10 changes: 5 additions & 5 deletions x/distribution/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package keeper

import (
"context"
"fmt"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/andromedaprotocol/andromedad/x/distribution/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/query"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
Expand Down Expand Up @@ -53,14 +53,14 @@ func (k Querier) ValidatorDistributionInfo(c context.Context, req *types.QueryVa
// self-delegation rewards
val := k.stakingKeeper.Validator(ctx, valAdr)
if val == nil {
return nil, sdkerrors.Wrap(types.ErrNoValidatorExists, req.ValidatorAddress)
return nil, fmt.Errorf("validator not found for address %s", req.ValidatorAddress)
}

delAdr := sdk.AccAddress(valAdr)

del := k.stakingKeeper.Delegation(ctx, delAdr, valAdr)
if del == nil {
return nil, types.ErrNoDelegationExists
return nil, fmt.Errorf("delegation not found for delegator %s to validator %s", delAdr, valAdr)
}

endingPeriod := k.IncrementValidatorPeriod(ctx, val)
Expand Down Expand Up @@ -184,7 +184,7 @@ func (k Querier) DelegationRewards(c context.Context, req *types.QueryDelegation

val := k.stakingKeeper.Validator(ctx, valAdr)
if val == nil {
return nil, sdkerrors.Wrap(types.ErrNoValidatorExists, req.ValidatorAddress)
return nil, fmt.Errorf("validator not found for address %s", req.ValidatorAddress)
}

delAdr, err := sdk.AccAddressFromBech32(req.DelegatorAddress)
Expand All @@ -193,7 +193,7 @@ func (k Querier) DelegationRewards(c context.Context, req *types.QueryDelegation
}
del := k.stakingKeeper.Delegation(ctx, delAdr, valAdr)
if del == nil {
return nil, types.ErrNoDelegationExists
return nil, fmt.Errorf("delegation not found for delegator %s to validator %s", req.DelegatorAddress, req.ValidatorAddress)
}

endingPeriod := k.IncrementValidatorPeriod(ctx, val)
Expand Down
8 changes: 4 additions & 4 deletions x/distribution/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (k Keeper) SetWithdrawAddr(ctx sdk.Context, delegatorAddr sdk.AccAddress, w
}

if !k.GetWithdrawAddrEnabled(ctx) {
return types.ErrSetWithdrawAddrDisabled
return fmt.Errorf("withdraw address functionality is disabled")
}

ctx.EventManager().EmitEvent(
Expand All @@ -88,12 +88,12 @@ func (k Keeper) SetWithdrawAddr(ctx sdk.Context, delegatorAddr sdk.AccAddress, w
func (k Keeper) WithdrawDelegationRewards(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) (sdk.Coins, error) {
val := k.stakingKeeper.Validator(ctx, valAddr)
if val == nil {
return nil, types.ErrNoValidatorDistInfo
return nil, fmt.Errorf("validator %s not found", valAddr)
}

del := k.stakingKeeper.Delegation(ctx, delAddr, valAddr)
if del == nil {
return nil, types.ErrEmptyDelegationDistInfo
return nil, fmt.Errorf("delegation from %s to %s not found", delAddr, valAddr)
}

// withdraw rewards
Expand All @@ -112,7 +112,7 @@ func (k Keeper) WithdrawValidatorCommission(ctx sdk.Context, valAddr sdk.ValAddr
// fetch validator accumulated commission
accumCommission := k.GetValidatorAccumulatedCommission(ctx, valAddr)
if accumCommission.Commission.IsZero() {
return nil, types.ErrNoValidatorCommission
return nil, fmt.Errorf("no commission to withdraw")
}

commission, remainder := accumCommission.Commission.TruncateDecimal()
Expand Down
2 changes: 2 additions & 0 deletions x/distribution/testutil/app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ var AppConfig = appconfig.Compose(&appv1alpha1.Config{
ModuleAccountPermissions: []*authmodulev1.ModuleAccountPermission{
{Account: authtypes.FeeCollectorName},
{Account: distrtypes.ModuleName},
// set for reward dist
{Account: distrtypes.RewardsDripperName},
{Account: minttypes.ModuleName, Permissions: []string{authtypes.Minter}},
{Account: stakingtypes.BondedPoolName, Permissions: []string{authtypes.Burner, stakingtypes.ModuleName}},
{Account: stakingtypes.NotBondedPoolName, Permissions: []string{authtypes.Burner, stakingtypes.ModuleName}},
Expand Down
14 changes: 7 additions & 7 deletions x/distribution/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import (
// and concrete types on the provided LegacyAmino codec. These types are used
// for Amino JSON serialization.
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
legacy.RegisterAminoMsg(cdc, &MsgWithdrawDelegatorReward{}, "cosmos-sdk/MsgWithdrawDelegationReward")
legacy.RegisterAminoMsg(cdc, &MsgWithdrawValidatorCommission{}, "cosmos-sdk/MsgWithdrawValCommission")
legacy.RegisterAminoMsg(cdc, &MsgSetWithdrawAddress{}, "cosmos-sdk/MsgModifyWithdrawAddress")
legacy.RegisterAminoMsg(cdc, &MsgFundCommunityPool{}, "cosmos-sdk/MsgFundCommunityPool")
legacy.RegisterAminoMsg(cdc, &MsgUpdateParams{}, "cosmos-sdk/distribution/MsgUpdateParams")
legacy.RegisterAminoMsg(cdc, &MsgCommunityPoolSpend{}, "cosmos-sdk/distr/MsgCommunityPoolSpend")
legacy.RegisterAminoMsg(cdc, &MsgWithdrawDelegatorReward{}, "andromedad/MsgWithdrawDelegationReward")
legacy.RegisterAminoMsg(cdc, &MsgWithdrawValidatorCommission{}, "andromedad/MsgWithdrawValCommission")
legacy.RegisterAminoMsg(cdc, &MsgSetWithdrawAddress{}, "andromedad/MsgModifyWithdrawAddress")
legacy.RegisterAminoMsg(cdc, &MsgFundCommunityPool{}, "andromedad/MsgFundCommunityPool")
legacy.RegisterAminoMsg(cdc, &MsgUpdateParams{}, "andromedad/distribution/MsgUpdateParams")
legacy.RegisterAminoMsg(cdc, &MsgCommunityPoolSpend{}, "andromedad/distr/MsgCommunityPoolSpend")

cdc.RegisterConcrete(Params{}, "cosmos-sdk/x/distribution/Params", nil)
cdc.RegisterConcrete(Params{}, "andromedad/x/distribution/Params", nil)
}

func RegisterInterfaces(registry types.InterfaceRegistry) {
Expand Down
21 changes: 0 additions & 21 deletions x/distribution/types/errors.go

This file was deleted.

4 changes: 2 additions & 2 deletions x/distribution/types/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ func (csp *CommunityPoolSpendProposal) ValidateBasic() error {
return err
}
if !csp.Amount.IsValid() {
return ErrInvalidProposalAmount
return fmt.Errorf("invalid amount: %s", csp.Amount)
}
if csp.Recipient == "" {
return ErrEmptyProposalRecipient
return fmt.Errorf("proposal recipient cannot be empty")
}

return nil
Expand Down

0 comments on commit 8a95f19

Please sign in to comment.