0g-chain/precompiles/staking/tx.go

172 lines
4.2 KiB
Go
Raw Normal View History

2024-10-24 13:23:45 +00:00
package staking
import (
"fmt"
2024-11-04 11:01:53 +00:00
"math/big"
2024-10-24 13:23:45 +00:00
2024-12-12 12:15:37 +00:00
precompiles_common "github.com/0glabs/0g-chain/precompiles/common"
2024-10-24 13:23:45 +00:00
sdk "github.com/cosmos/cosmos-sdk/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/evmos/ethermint/x/evm/statedb"
)
func (s *StakingPrecompile) CreateValidator(
ctx sdk.Context,
evm *vm.EVM,
stateDB *statedb.StateDB,
contract *vm.Contract,
method *abi.Method,
args []interface{},
) ([]byte, error) {
2024-12-12 12:15:37 +00:00
msg, err := NewMsgCreateValidator(args, contract.CallerAddress, s.stakingKeeper.BondDenom(ctx))
2024-10-24 13:23:45 +00:00
if err != nil {
return nil, err
}
// validation
if contract.CallerAddress != evm.Origin {
2024-12-12 12:15:37 +00:00
return nil, fmt.Errorf(precompiles_common.ErrSenderNotOrigin)
2024-10-24 13:23:45 +00:00
}
// execute
_, err = stakingkeeper.NewMsgServerImpl(s.stakingKeeper).CreateValidator(ctx, msg)
if err != nil {
return nil, err
}
// emit events
return method.Outputs.Pack()
}
func (s *StakingPrecompile) EditValidator(
ctx sdk.Context,
evm *vm.EVM,
stateDB *statedb.StateDB,
contract *vm.Contract,
method *abi.Method,
args []interface{},
) ([]byte, error) {
2024-12-12 12:15:37 +00:00
msg, err := NewMsgEditValidator(args, contract.CallerAddress)
2024-10-24 13:23:45 +00:00
if err != nil {
return nil, err
}
// validation
if contract.CallerAddress != evm.Origin {
2024-12-12 12:15:37 +00:00
return nil, fmt.Errorf(precompiles_common.ErrSenderNotOrigin)
2024-10-24 13:23:45 +00:00
}
// execute
_, err = stakingkeeper.NewMsgServerImpl(s.stakingKeeper).EditValidator(ctx, msg)
if err != nil {
return nil, err
}
// emit events
return method.Outputs.Pack()
}
func (s *StakingPrecompile) Delegate(
ctx sdk.Context,
evm *vm.EVM,
stateDB *statedb.StateDB,
contract *vm.Contract,
method *abi.Method,
args []interface{},
) ([]byte, error) {
2024-12-12 12:15:37 +00:00
msg, err := NewMsgDelegate(args, contract.CallerAddress, s.stakingKeeper.BondDenom(ctx))
2024-10-24 13:23:45 +00:00
if err != nil {
return nil, err
}
// validation
2024-12-12 12:15:37 +00:00
/*
if contract.CallerAddress != evm.Origin {
return nil, fmt.Errorf(precompiles_common.ErrSenderNotOrigin)
}
*/
2024-10-24 13:23:45 +00:00
// execute
_, err = stakingkeeper.NewMsgServerImpl(s.stakingKeeper).Delegate(ctx, msg)
if err != nil {
return nil, err
}
// emit events
return method.Outputs.Pack()
}
func (s *StakingPrecompile) BeginRedelegate(
ctx sdk.Context,
evm *vm.EVM,
stateDB *statedb.StateDB,
contract *vm.Contract,
method *abi.Method,
args []interface{},
) ([]byte, error) {
2024-12-12 12:15:37 +00:00
msg, err := NewMsgBeginRedelegate(args, contract.CallerAddress, s.stakingKeeper.BondDenom(ctx))
2024-10-24 13:23:45 +00:00
if err != nil {
return nil, err
}
// validation
2024-12-12 12:15:37 +00:00
/*
if contract.CallerAddress != evm.Origin {
return nil, fmt.Errorf(precompiles_common.ErrSenderNotOrigin)
}
*/
2024-10-24 13:23:45 +00:00
// execute
2024-11-04 11:01:53 +00:00
response, err := stakingkeeper.NewMsgServerImpl(s.stakingKeeper).BeginRedelegate(ctx, msg)
2024-10-24 13:23:45 +00:00
if err != nil {
return nil, err
}
// emit events
2024-11-04 11:01:53 +00:00
return method.Outputs.Pack(big.NewInt(response.CompletionTime.UTC().Unix()))
2024-10-24 13:23:45 +00:00
}
func (s *StakingPrecompile) Undelegate(
ctx sdk.Context,
evm *vm.EVM,
stateDB *statedb.StateDB,
contract *vm.Contract,
method *abi.Method,
args []interface{},
) ([]byte, error) {
2024-12-12 12:15:37 +00:00
msg, err := NewMsgUndelegate(args, contract.CallerAddress, s.stakingKeeper.BondDenom(ctx))
2024-10-24 13:23:45 +00:00
if err != nil {
return nil, err
}
// validation
2024-12-12 12:15:37 +00:00
/*
if contract.CallerAddress != evm.Origin {
return nil, fmt.Errorf(precompiles_common.ErrSenderNotOrigin)
}
*/
2024-10-24 13:23:45 +00:00
// execute
2024-11-04 11:01:53 +00:00
response, err := stakingkeeper.NewMsgServerImpl(s.stakingKeeper).Undelegate(ctx, msg)
2024-10-24 13:23:45 +00:00
if err != nil {
return nil, err
}
// emit events
2024-11-04 11:01:53 +00:00
return method.Outputs.Pack(big.NewInt(response.CompletionTime.UTC().Unix()))
2024-10-24 13:23:45 +00:00
}
func (s *StakingPrecompile) CancelUnbondingDelegation(
ctx sdk.Context,
evm *vm.EVM,
stateDB *statedb.StateDB,
contract *vm.Contract,
method *abi.Method,
args []interface{},
) ([]byte, error) {
2024-12-12 12:15:37 +00:00
msg, err := NewMsgCancelUnbondingDelegation(args, contract.CallerAddress, s.stakingKeeper.BondDenom(ctx))
2024-10-24 13:23:45 +00:00
if err != nil {
return nil, err
}
// validation
2024-12-12 12:15:37 +00:00
/*
if contract.CallerAddress != evm.Origin {
return nil, fmt.Errorf(precompiles_common.ErrSenderNotOrigin)
}
*/
2024-10-24 13:23:45 +00:00
// execute
_, err = stakingkeeper.NewMsgServerImpl(s.stakingKeeper).CancelUnbondingDelegation(ctx, msg)
if err != nil {
return nil, err
}
// emit events
return method.Outputs.Pack()
}