Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(x/evmstaking): ubi auto withdraw #222

Merged
merged 8 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 8 additions & 53 deletions client/x/evmstaking/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

abci "github.com/cometbft/cometbft/abci/types"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/piplabs/story/client/x/evmstaking/types"
"github.com/piplabs/story/lib/errors"
Expand All @@ -20,73 +19,29 @@ func (k *Keeper) EndBlock(ctx context.Context) (abci.ValidatorUpdates, error) {
log.Debug(ctx, "EndBlock.evmstaking")
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker)

sdkCtx := sdk.UnwrapSDKContext(ctx)
blockHeight := sdkCtx.BlockHeader().Height

isSingularity, err := k.IsSingularity(ctx)
if err != nil {
return nil, err
}

if isSingularity {
return nil, nil
}

valUpdates, unbondedEntries, err := k.stakingKeeper.EndBlockerWithUnbondedEntries(ctx)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "process staking EndBlocker")
}

log.Debug(ctx, "Processing mature unbonding delegations", "count", len(unbondedEntries))

for _, entry := range unbondedEntries {
delegatorAddr, err := k.authKeeper.AddressCodec().StringToBytes(entry.DelegatorAddress)
if err != nil {
return nil, errors.Wrap(err, "delegator address from bech32")
}

log.Debug(ctx, "Adding undelegation to withdrawal queue",
"delegator", entry.DelegatorAddress,
"validator", entry.ValidatorAddress,
"amount", entry.Amount.String())

// Burn tokens from the delegator
_, coins := IPTokenToBondCoin(entry.Amount.BigInt())
err = k.bankKeeper.SendCoinsFromAccountToModule(ctx, delegatorAddr, types.ModuleName, coins)
if err != nil {
return nil, errors.Wrap(err, "send coins from account to module")
}
err = k.bankKeeper.BurnCoins(ctx, types.ModuleName, coins)
if err != nil {
return nil, errors.Wrap(err, "burn coins")
}

// This should not produce error, as all delegations are done via the evmstaking module via EL.
// However, we should gracefully handle in case Get fails.
delEvmAddr, err := k.DelegatorWithdrawAddress.Get(ctx, entry.DelegatorAddress)
if err != nil {
return nil, errors.Wrap(err, "map delegator pubkey to evm address")
}

// push the undelegation to the withdrawal queue
err = k.AddWithdrawalToQueue(ctx, types.NewWithdrawal(
uint64(blockHeight),
entry.DelegatorAddress,
entry.ValidatorAddress,
delEvmAddr,
entry.Amount.Uint64(),
))
if err != nil {
return nil, err
}
if err := k.ProcessUnbondingWithdrawals(ctx, unbondedEntries); err != nil {
return nil, errors.Wrap(err, "process unbonding withdrawals")
}

partialWithdrawals, err := k.ExpectedPartialWithdrawals(ctx)
if err != nil {
return nil, err
if err := k.ProcessRewardWithdrawals(ctx); err != nil {
return nil, errors.Wrap(err, "process reward withdrawals")
}
if err := k.EnqueueEligiblePartialWithdrawal(ctx, partialWithdrawals); err != nil {
return nil, err

if err := k.ProcessUbiWithdrawal(ctx); err != nil {
return nil, errors.Wrap(err, "process ubi withdrawal")
}

return valUpdates, nil
Expand Down
2 changes: 0 additions & 2 deletions client/x/evmstaking/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ func (k Keeper) GetWithdrawalQueue(ctx context.Context, request *types.QueryGetW
for _, w := range withdrawals {
ws = append(ws, &types.Withdrawal{
CreationHeight: w.CreationHeight,
DelegatorAddress: w.DelegatorAddress,
ValidatorAddress: w.ValidatorAddress,
ExecutionAddress: w.ExecutionAddress,
Amount: w.Amount,
})
Expand Down
6 changes: 3 additions & 3 deletions client/x/evmstaking/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ func (s *TestSuite) TestGetWithdrawalQueue() {
valAddr = "storyvaloper1hmjw3pvkjtndpg8wqppwdn8udd835qpaa6r6y0"
evmAddr = common.HexToAddress("0x131D25EDE18178BAc9275b312001a63C081722d2")
withdrawals := []types.Withdrawal{
types.NewWithdrawal(1, delAddr, valAddr, evmAddr.String(), 100),
types.NewWithdrawal(2, delAddr, valAddr, evmAddr.String(), 200),
types.NewWithdrawal(3, delAddr, valAddr, evmAddr.String(), 300),
types.NewWithdrawal(1, evmAddr.String(), 100),
types.NewWithdrawal(2, evmAddr.String(), 200),
types.NewWithdrawal(3, evmAddr.String(), 300),
}
require.Len(withdrawals, 3)
for _, w := range withdrawals {
Expand Down
2 changes: 0 additions & 2 deletions client/x/evmstaking/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ type TestSuite struct {
StakingKeeper *skeeper.Keeper
SlashingKeeper *estestutil.MockSlashingKeeper
EVMStakingKeeper *keeper.Keeper
msgServer types.MsgServiceServer
queryClient types.QueryClient

encCfg moduletestutil.TestEncodingConfig
Expand Down Expand Up @@ -176,7 +175,6 @@ func (s *TestSuite) SetupTest() {
)
s.Require().NoError(evmstakingKeeper.SetParams(s.Ctx, types.DefaultParams()))
s.EVMStakingKeeper = evmstakingKeeper
s.msgServer = keeper.NewMsgServerImpl(evmstakingKeeper)
queryHelper := baseapp.NewQueryServerTestHelper(s.Ctx, s.encCfg.InterfaceRegistry)
types.RegisterQueryServer(queryHelper, evmstakingKeeper)
s.queryClient = types.NewQueryClient(queryHelper)
Expand Down
93 changes: 0 additions & 93 deletions client/x/evmstaking/keeper/msg_server.go

This file was deleted.

136 changes: 0 additions & 136 deletions client/x/evmstaking/keeper/msg_server_test.go

This file was deleted.

Loading
Loading