mukan-sdk/x/distribution/keeper/hooks.go
Mukan Erkin Törük abb1ff956e
Some checks are pending
Build SimApp / build (amd64) (push) Waiting to run
Build SimApp / build (arm64) (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Build & Push / build (push) Waiting to run
Run Gosec / Gosec (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Checks dependencies and mocks generation / Check go mod tidy (push) Waiting to run
Checks dependencies and mocks generation / Check up to date mocks (push) Waiting to run
System Tests / setup (push) Waiting to run
System Tests / test-system (push) Blocked by required conditions
System Tests / test-system-legacy (push) Blocked by required conditions
Tests / Code Coverage / split-test-files (push) Waiting to run
Tests / Code Coverage / tests (00) (push) Blocked by required conditions
Tests / Code Coverage / tests (01) (push) Blocked by required conditions
Tests / Code Coverage / tests (02) (push) Blocked by required conditions
Tests / Code Coverage / tests (03) (push) Blocked by required conditions
Tests / Code Coverage / test-integration (push) Waiting to run
Tests / Code Coverage / test-e2e (push) Waiting to run
Tests / Code Coverage / repo-analysis (push) Blocked by required conditions
Tests / Code Coverage / test-sim-nondeterminism (push) Waiting to run
Tests / Code Coverage / test-clientv2 (push) Waiting to run
Tests / Code Coverage / test-core (push) Waiting to run
Tests / Code Coverage / test-depinject (push) Waiting to run
Tests / Code Coverage / test-errors (push) Waiting to run
Tests / Code Coverage / test-math (push) Waiting to run
Tests / Code Coverage / test-schema (push) Waiting to run
Tests / Code Coverage / test-collections (push) Waiting to run
Tests / Code Coverage / test-cosmovisor (push) Waiting to run
Tests / Code Coverage / test-confix (push) Waiting to run
Tests / Code Coverage / test-store (push) Waiting to run
Tests / Code Coverage / test-log (push) Waiting to run
Tests / Code Coverage / test-x-tx (push) Waiting to run
Tests / Code Coverage / test-x-nft (push) Waiting to run
Tests / Code Coverage / test-x-circuit (push) Waiting to run
Tests / Code Coverage / test-x-feegrant (push) Waiting to run
Tests / Code Coverage / test-x-evidence (push) Waiting to run
Tests / Code Coverage / test-x-upgrade (push) Waiting to run
Tests / Code Coverage / test-tools-benchmark (push) Waiting to run
refactor: complete sovereign stack cleanup — all github.com upstream refs purged
2026-05-11 03:46:06 +03:00

182 lines
4.5 KiB
Go

package keeper
import (
"context"
sdkmath "cosmossdk.io/math"
sdk "git.cw.tr/mukan-network/mukan-sdk/types"
"git.cw.tr/mukan-network/mukan-sdk/x/distribution/types"
stakingtypes "git.cw.tr/mukan-network/mukan-sdk/x/staking/types"
)
// Wrapper struct
type Hooks struct {
k Keeper
}
var _ stakingtypes.StakingHooks = Hooks{}
// Create new distribution hooks
func (k Keeper) Hooks() Hooks {
return Hooks{k}
}
// initialize validator distribution record
func (h Hooks) AfterValidatorCreated(ctx context.Context, valAddr sdk.ValAddress) error {
val, err := h.k.stakingKeeper.Validator(ctx, valAddr)
if err != nil {
return err
}
return h.k.initializeValidator(ctx, val)
}
// AfterValidatorRemoved performs clean up after a validator is removed
func (h Hooks) AfterValidatorRemoved(ctx context.Context, _ sdk.ConsAddress, valAddr sdk.ValAddress) error {
// fetch outstanding
outstanding, err := h.k.GetValidatorOutstandingRewardsCoins(ctx, valAddr)
if err != nil {
return err
}
// force-withdraw commission
valCommission, err := h.k.GetValidatorAccumulatedCommission(ctx, valAddr)
if err != nil {
return err
}
commission := valCommission.Commission
if !commission.IsZero() {
// subtract from outstanding
outstanding = outstanding.Sub(commission)
// split into integral & remainder
coins, remainder := commission.TruncateDecimal()
// remainder to community pool
feePool, err := h.k.FeePool.Get(ctx)
if err != nil {
return err
}
feePool.CommunityPool = feePool.CommunityPool.Add(remainder...)
err = h.k.FeePool.Set(ctx, feePool)
if err != nil {
return err
}
// add to validator account
if !coins.IsZero() {
accAddr := sdk.AccAddress(valAddr)
withdrawAddr, err := h.k.GetDelegatorWithdrawAddr(ctx, accAddr)
if err != nil {
return err
}
if err := h.k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, withdrawAddr, coins); err != nil {
return err
}
}
}
// Add outstanding to community pool
// The validator is removed only after it has no more delegations.
// This operation sends only the remaining dust to the community pool.
feePool, err := h.k.FeePool.Get(ctx)
if err != nil {
return err
}
feePool.CommunityPool = feePool.CommunityPool.Add(outstanding...)
err = h.k.FeePool.Set(ctx, feePool)
if err != nil {
return err
}
// delete outstanding
err = h.k.DeleteValidatorOutstandingRewards(ctx, valAddr)
if err != nil {
return err
}
// remove commission record
err = h.k.DeleteValidatorAccumulatedCommission(ctx, valAddr)
if err != nil {
return err
}
// clear slashes
h.k.DeleteValidatorSlashEvents(ctx, valAddr)
// clear historical rewards
h.k.DeleteValidatorHistoricalRewards(ctx, valAddr)
// clear current rewards
err = h.k.DeleteValidatorCurrentRewards(ctx, valAddr)
if err != nil {
return err
}
return nil
}
// increment period
func (h Hooks) BeforeDelegationCreated(ctx context.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error {
val, err := h.k.stakingKeeper.Validator(ctx, valAddr)
if err != nil {
return err
}
_, err = h.k.IncrementValidatorPeriod(ctx, val)
return err
}
// withdraw delegation rewards (which also increments period)
func (h Hooks) BeforeDelegationSharesModified(ctx context.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error {
val, err := h.k.stakingKeeper.Validator(ctx, valAddr)
if err != nil {
return err
}
del, err := h.k.stakingKeeper.Delegation(ctx, delAddr, valAddr)
if err != nil {
return err
}
if _, err := h.k.withdrawDelegationRewards(ctx, val, del); err != nil {
return err
}
return nil
}
// create new delegation period record
func (h Hooks) AfterDelegationModified(ctx context.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error {
return h.k.initializeDelegation(ctx, valAddr, delAddr)
}
// record the slash event
func (h Hooks) BeforeValidatorSlashed(ctx context.Context, valAddr sdk.ValAddress, fraction sdkmath.LegacyDec) error {
return h.k.updateValidatorSlashFraction(ctx, valAddr, fraction)
}
func (h Hooks) BeforeValidatorModified(_ context.Context, _ sdk.ValAddress) error {
return nil
}
func (h Hooks) AfterValidatorBonded(_ context.Context, _ sdk.ConsAddress, _ sdk.ValAddress) error {
return nil
}
func (h Hooks) AfterValidatorBeginUnbonding(_ context.Context, _ sdk.ConsAddress, _ sdk.ValAddress) error {
return nil
}
func (h Hooks) BeforeDelegationRemoved(_ context.Context, _ sdk.AccAddress, _ sdk.ValAddress) error {
return nil
}
func (h Hooks) AfterUnbondingInitiated(_ context.Context, _ uint64) error {
return nil
}