Skip to content

Commit

Permalink
Merge pull request #184 from decentrio/feat/transition-stake-remake
Browse files Browse the repository at this point in the history
feat: transition stake
  • Loading branch information
vuong177 authored Sep 5, 2024
2 parents 444ea0d + be24ae0 commit 9c98d29
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 8 deletions.
2 changes: 0 additions & 2 deletions tests/e2e/slashing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,6 @@ func TestSlasingImmediateUnbond(t *testing.T) {
base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(`{"validator": "%s"}`, myLocalValidatorAddr))))
providerCli.MustExecVault(execLocalStakingMsg)

assert.Equal(t, 100_000_000, providerCli.QueryVaultFreeBalance())

// Check slashable amount
require.Equal(t, 20_000_000, providerCli.QuerySlashableAmount())
// Check free collateral
Expand Down
Binary file modified tests/testdata/mesh_converter.wasm.gz
Binary file not shown.
Binary file modified tests/testdata/mesh_external_staking.wasm.gz
Binary file not shown.
Binary file modified tests/testdata/mesh_native_staking.wasm.gz
Binary file not shown.
Binary file modified tests/testdata/mesh_native_staking_proxy.wasm.gz
Binary file not shown.
Binary file modified tests/testdata/mesh_osmosis_price_provider.wasm.gz
Binary file not shown.
Binary file modified tests/testdata/mesh_remote_price_feed.wasm.gz
Binary file not shown.
Binary file modified tests/testdata/mesh_simple_price_feed.wasm.gz
Binary file not shown.
Binary file modified tests/testdata/mesh_vault.wasm.gz
Binary file not shown.
Binary file modified tests/testdata/mesh_virtual_staking.wasm.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/testdata/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
d9e27c854aaf3bb604d32f5fa8e6c3e42b686edf
8292d43b7f2bb59dfa884d3b0b1509ce659b0793
6 changes: 6 additions & 0 deletions x/meshsecurityprovider/contract/in_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type (
Bond *BondMsg `json:"bond,omitempty"`
Unbond *UnbondMsg `json:"unbond,omitempty"`
Unstake *UnstakeMsg `json:"unstake,omitempty"`
Restake *RestakeMsg `json:"restake,omitempty"`
}
BondMsg struct {
Amount wasmvmtypes.Coin `json:"amount"`
Expand All @@ -24,4 +25,9 @@ type (
Validator string `json:"validator"`
Delegator string `json:"delegator"`
}
RestakeMsg struct {
Amount wasmvmtypes.Coin `json:"amount"`
Delegator string `json:"delegator"`
Validator string `json:"validator"`
}
)
2 changes: 2 additions & 0 deletions x/meshsecurityprovider/keeper/handle_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func (h CustomMessenger) DispatchMsg(ctx sdk.Context, contractAddr sdk.AccAddres
return h.k.HandleUnbondMsg(ctx, contractAddr, customMsg.Provider.Unbond)
case customMsg.Provider.Unstake != nil:
return h.k.HandleUnstakeMsg(ctx, contractAddr, customMsg.Provider.Unstake)
case customMsg.Provider.Restake != nil:
return h.k.HandleRestakeMsg(ctx, contractAddr, customMsg.Provider.Restake)
}

return nil, nil, wasmtypes.ErrUnknownMsg
Expand Down
69 changes: 64 additions & 5 deletions x/meshsecurityprovider/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,60 @@ func (k Keeper) HandleUnstakeMsg(ctx sdk.Context, actor sdk.AccAddress, unstakeM
)}, nil, nil
}

func (k Keeper) HandleRestakeMsg(ctx sdk.Context, actor sdk.AccAddress, restakeMsg *contract.RestakeMsg) ([]sdk.Event, [][]byte, error) {
if actor.String() != k.VaultAddress(ctx) {
return nil, nil, sdkerrors.ErrUnauthorized.Wrapf("contract has no permission for mesh security operations")
}

coin, err := wasmkeeper.ConvertWasmCoinToSdkCoin(restakeMsg.Amount)
if err != nil {
return nil, nil, err
}
bondDenom := k.stakingKeeper.BondDenom(ctx)
if coin.Denom != bondDenom {
return nil, nil, sdkerrors.ErrInvalidRequest.Wrapf("invalid coin denomination: got %s, expected %s", coin.Denom, bondDenom)
}

delAddr, err := sdk.AccAddressFromBech32(restakeMsg.Delegator)
if err != nil {
return nil, nil, err
}
valAddr, err := sdk.ValAddressFromBech32(restakeMsg.Validator)
if err != nil {
return nil, nil, err
}

validatorInfo, found := k.stakingKeeper.GetValidator(ctx, valAddr)
if !found {
return nil, nil, sdkerrors.ErrNotFound.Wrapf("can not found validator with address: %s", restakeMsg.Validator)
}

shares, err := k.stakingKeeper.ValidateUnbondAmount(ctx, actor, valAddr, coin.Amount)
if err == stakingtypes.ErrNoDelegation {
return nil, nil, sdkerrors.ErrNotFound.Wrapf("can not found delegation with address: %s", restakeMsg.Delegator)
} else if err != nil {
return nil, nil, err
}
unbondAmt, err := k.InstantUndelegate(ctx, delAddr, validatorInfo, shares)
if err != nil {
return nil, nil, err
}
if !unbondAmt.Equal(coin) {
return nil, nil, sdkerrors.ErrInvalidRequest.Wrapf("Delegation has been slashed")
}
err = k.bankKeeper.DelegateCoins(ctx, delAddr, actor, sdk.NewCoins(unbondAmt))
if err != nil {
return nil, nil, err
}

return []sdk.Event{sdk.NewEvent(
types.EventTypeUnbond,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(sdk.AttributeKeyAmount, coin.String()),
sdk.NewAttribute(types.AttributeKeyDelegator, delAddr.String()),
)}, nil, nil
}

func (k Keeper) unstake(ctx sdk.Context, proxyContract sdk.AccAddress, validator sdk.ValAddress, coin sdk.Coin) error {
if coin.Amount.IsNil() || coin.Amount.IsZero() || coin.Amount.IsNegative() {
return sdkerrors.ErrInvalidRequest.Wrap("amount")
Expand Down Expand Up @@ -236,19 +290,24 @@ func (k Keeper) unstake(ctx sdk.Context, proxyContract sdk.AccAddress, validator
return nil
}

return k.InstantUndelegate(ctx, proxyContract, validator, shares)
_, err = k.InstantUndelegate(ctx, proxyContract, validatorInfo, shares)
return err
}

func (k Keeper) InstantUndelegate(ctx sdk.Context, delAddr sdk.AccAddress, validator sdk.ValAddress, sharesAmount sdk.Dec) error {
returnAmount, err := k.stakingKeeper.Unbond(ctx, delAddr, validator, sharesAmount)
func (k Keeper) InstantUndelegate(ctx sdk.Context, delAddr sdk.AccAddress, validator stakingtypes.Validator, sharesAmount sdk.Dec) (sdk.Coin, error) {
returnAmount, err := k.stakingKeeper.Unbond(ctx, delAddr, validator.GetOperator(), sharesAmount)
if err != nil {
return err
return sdk.Coin{}, err
}

bondDenom := k.stakingKeeper.BondDenom(ctx)

amt := sdk.NewCoin(bondDenom, returnAmount)
res := sdk.NewCoins(amt)

return k.bankKeeper.UndelegateCoinsFromModuleToAccount(ctx, stakingtypes.NotBondedPoolName, delAddr, res)
err = k.bankKeeper.UndelegateCoinsFromModuleToAccount(ctx, stakingtypes.NotBondedPoolName, delAddr, res)
if err != nil {
return sdk.Coin{}, err
}
return amt, nil
}

0 comments on commit 9c98d29

Please sign in to comment.