2022-12-09 21:24:35 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2024-02-06 22:54:10 +00:00
|
|
|
"github.com/cometbft/cometbft/libs/log"
|
2023-09-22 16:05:12 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
storetypes "github.com/cosmos/cosmos-sdk/store/types"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-12-09 21:24:35 +00:00
|
|
|
|
2024-08-03 09:51:56 +00:00
|
|
|
"github.com/0glabs/0g-chain/x/community/types"
|
2022-12-09 21:24:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Keeper of the community store
|
|
|
|
type Keeper struct {
|
2023-09-22 16:05:12 +00:00
|
|
|
key storetypes.StoreKey
|
|
|
|
cdc codec.Codec
|
|
|
|
|
2023-10-20 16:18:37 +00:00
|
|
|
accountKeeper types.AccountKeeper
|
2023-10-02 20:10:22 +00:00
|
|
|
bankKeeper types.BankKeeper
|
|
|
|
cdpKeeper types.CdpKeeper
|
|
|
|
distrKeeper types.DistributionKeeper
|
|
|
|
hardKeeper types.HardKeeper
|
|
|
|
moduleAddress sdk.AccAddress
|
|
|
|
mintKeeper types.MintKeeper
|
|
|
|
kavadistKeeper types.KavadistKeeper
|
2023-10-03 15:41:54 +00:00
|
|
|
stakingKeeper types.StakingKeeper
|
2022-12-19 21:56:46 +00:00
|
|
|
|
2023-10-11 17:22:25 +00:00
|
|
|
// the address capable of executing a MsgUpdateParams message. Typically, this
|
|
|
|
// should be the x/gov module account.
|
|
|
|
authority sdk.AccAddress
|
|
|
|
|
2022-12-19 21:56:46 +00:00
|
|
|
legacyCommunityPoolAddress sdk.AccAddress
|
2022-12-09 21:24:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewKeeper creates a new community Keeper instance
|
2023-09-22 16:05:12 +00:00
|
|
|
func NewKeeper(
|
|
|
|
cdc codec.Codec,
|
|
|
|
key storetypes.StoreKey,
|
|
|
|
ak types.AccountKeeper,
|
|
|
|
bk types.BankKeeper,
|
|
|
|
ck types.CdpKeeper,
|
|
|
|
dk types.DistributionKeeper,
|
|
|
|
hk types.HardKeeper,
|
2023-10-02 20:10:22 +00:00
|
|
|
mk types.MintKeeper,
|
|
|
|
kk types.KavadistKeeper,
|
2023-10-03 15:41:54 +00:00
|
|
|
sk types.StakingKeeper,
|
2023-10-11 17:22:25 +00:00
|
|
|
authority sdk.AccAddress,
|
2023-09-22 16:05:12 +00:00
|
|
|
) Keeper {
|
2022-12-09 21:24:35 +00:00
|
|
|
// ensure community module account is set
|
|
|
|
addr := ak.GetModuleAddress(types.ModuleAccountName)
|
|
|
|
if addr == nil {
|
|
|
|
panic(fmt.Sprintf("%s module account has not been set", types.ModuleAccountName))
|
|
|
|
}
|
2022-12-19 21:56:46 +00:00
|
|
|
legacyAddr := ak.GetModuleAddress(types.LegacyCommunityPoolModuleName)
|
|
|
|
if addr == nil {
|
|
|
|
panic("legacy community pool address not found")
|
|
|
|
}
|
2023-10-11 17:22:25 +00:00
|
|
|
if err := sdk.VerifyAddressFormat(authority); err != nil {
|
|
|
|
panic(fmt.Sprintf("invalid authority address: %s", err))
|
|
|
|
}
|
2022-12-09 21:24:35 +00:00
|
|
|
|
|
|
|
return Keeper{
|
2023-09-22 16:05:12 +00:00
|
|
|
key: key,
|
|
|
|
cdc: cdc,
|
|
|
|
|
2023-10-20 16:18:37 +00:00
|
|
|
accountKeeper: ak,
|
2023-10-02 20:10:22 +00:00
|
|
|
bankKeeper: bk,
|
|
|
|
cdpKeeper: ck,
|
|
|
|
distrKeeper: dk,
|
|
|
|
hardKeeper: hk,
|
|
|
|
mintKeeper: mk,
|
|
|
|
kavadistKeeper: kk,
|
2023-10-03 15:41:54 +00:00
|
|
|
stakingKeeper: sk,
|
2023-10-02 20:10:22 +00:00
|
|
|
moduleAddress: addr,
|
2022-12-19 21:56:46 +00:00
|
|
|
|
2023-10-11 17:22:25 +00:00
|
|
|
authority: authority,
|
2022-12-19 21:56:46 +00:00
|
|
|
legacyCommunityPoolAddress: legacyAddr,
|
2022-12-09 21:24:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-11 17:22:25 +00:00
|
|
|
// GetAuthority returns the x/community module's authority.
|
|
|
|
func (k Keeper) GetAuthority() sdk.AccAddress {
|
|
|
|
return k.authority
|
|
|
|
}
|
|
|
|
|
2022-12-09 21:24:35 +00:00
|
|
|
// Logger returns a module-specific logger.
|
|
|
|
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
|
|
|
|
return ctx.Logger().With("module", "x/"+types.ModuleName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetModuleAccountBalance returns all the coins held by the community module account
|
|
|
|
func (k Keeper) GetModuleAccountBalance(ctx sdk.Context) sdk.Coins {
|
|
|
|
return k.bankKeeper.GetAllBalances(ctx, k.moduleAddress)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FundCommunityPool transfers coins from the sender to the community module account.
|
|
|
|
func (k Keeper) FundCommunityPool(ctx sdk.Context, sender sdk.AccAddress, amount sdk.Coins) error {
|
|
|
|
return k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleAccountName, amount)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DistributeFromCommunityPool transfers coins from the community pool to recipient.
|
|
|
|
func (k Keeper) DistributeFromCommunityPool(ctx sdk.Context, recipient sdk.AccAddress, amount sdk.Coins) error {
|
|
|
|
return k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleAccountName, recipient, amount)
|
|
|
|
}
|
2023-10-03 15:41:54 +00:00
|
|
|
|
|
|
|
// GetStakingRewardsState returns the staking reward state or the default state if not set
|
|
|
|
func (k Keeper) GetStakingRewardsState(ctx sdk.Context) types.StakingRewardsState {
|
|
|
|
store := ctx.KVStore(k.key)
|
|
|
|
|
|
|
|
b := store.Get(types.StakingRewardsStateKey)
|
|
|
|
if b == nil {
|
|
|
|
return types.DefaultStakingRewardsState()
|
|
|
|
}
|
|
|
|
|
|
|
|
state := types.StakingRewardsState{}
|
|
|
|
k.cdc.MustUnmarshal(b, &state)
|
|
|
|
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetStakingRewardsState validates and sets the staking rewards state in the store
|
|
|
|
func (k Keeper) SetStakingRewardsState(ctx sdk.Context, state types.StakingRewardsState) {
|
|
|
|
if err := state.Validate(); err != nil {
|
|
|
|
panic(fmt.Sprintf("invalid state: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
store := ctx.KVStore(k.key)
|
|
|
|
b := k.cdc.MustMarshal(&state)
|
|
|
|
|
|
|
|
store.Set(types.StakingRewardsStateKey, b)
|
|
|
|
}
|