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

fix: improve ExecuteEVM in VMCall #888

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion contract/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ var (

type Caller interface {
QueryContract(ctx context.Context, from, contract common.Address, abi abi.ABI, method string, res interface{}, args ...interface{}) error
ApplyContract(ctx context.Context, from, contract common.Address, value *big.Int, abi abi.ABI, method string, constructorData ...interface{}) (*evmtypes.MsgEthereumTxResponse, error)
ApplyContract(ctx context.Context, from, contract common.Address, value *big.Int, abi abi.ABI, method string, args ...interface{}) (*evmtypes.MsgEthereumTxResponse, error)
ExecuteEVM(ctx sdk.Context, from common.Address, contract *common.Address, value *big.Int, gasLimit uint64, data []byte) (*evmtypes.MsgEthereumTxResponse, error)
}

Expand Down
23 changes: 10 additions & 13 deletions precompiles/vm_caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,28 @@ func (v *VMCall) QueryContract(_ context.Context, from, contract common.Address,
return nil
}

func (v *VMCall) ApplyContract(_ context.Context, from, contract common.Address, value *big.Int, abi abi.ABI, method string, constructorData ...interface{}) (*evmtypes.MsgEthereumTxResponse, error) {
data, err := abi.Pack(method, constructorData...)
func (v *VMCall) ApplyContract(_ context.Context, from, contract common.Address, value *big.Int, abi abi.ABI, method string, args ...interface{}) (*evmtypes.MsgEthereumTxResponse, error) {
data, err := abi.Pack(method, args...)
if err != nil {
return nil, types.ErrABIPack.Wrap(err.Error())
}
ret, leftoverGas, vmErr := v.evm.Call(vm.AccountRef(from), contract, data, v.maxGas, value)
var vmError string
if vmErr != nil {
vmError = vmErr.Error()
}
return &evmtypes.MsgEthereumTxResponse{
GasUsed: v.maxGas - leftoverGas,
VmError: vmError,
Ret: ret,
}, nil
return v.ExecuteEVM(sdk.Context{}, from, &contract, value, v.maxGas, data)
}

func (v *VMCall) ExecuteEVM(_ sdk.Context, from common.Address, contract *common.Address, value *big.Int, gasLimit uint64, data []byte) (*evmtypes.MsgEthereumTxResponse, error) {
if value == nil {
value = big.NewInt(0)
}
if contract == nil {
contract = &common.Address{}
}
ret, leftoverGas, vmErr := v.evm.Call(vm.AccountRef(from), *contract, data, gasLimit, value)
var vmError string
if vmErr != nil {
vmError = vmErr.Error()
}
return &evmtypes.MsgEthereumTxResponse{
GasUsed: v.maxGas - leftoverGas,
GasUsed: gasLimit - leftoverGas,
VmError: vmError,
Ret: ret,
}, nil
Expand Down
6 changes: 3 additions & 3 deletions x/evm/keeper/contract_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,16 @@ func (k *Keeper) QueryContract(ctx context.Context, from, contract common.Addres
}

// ApplyContract apply contract with args
func (k *Keeper) ApplyContract(ctx context.Context, from, contract common.Address, value *big.Int, abi abi.ABI, method string, constructorData ...interface{}) (*evmtypes.MsgEthereumTxResponse, error) {
args, err := abi.Pack(method, constructorData...)
func (k *Keeper) ApplyContract(ctx context.Context, from, contract common.Address, value *big.Int, abi abi.ABI, method string, args ...interface{}) (*evmtypes.MsgEthereumTxResponse, error) {
data, err := abi.Pack(method, args...)
if err != nil {
return nil, types.ErrABIPack.Wrap(err.Error())
}
nonce, err := k.accountKeeper.GetSequence(ctx, from.Bytes())
if err != nil {
return nil, err
}
resp, err := k.callEvm(sdk.UnwrapSDKContext(ctx), from, &contract, value, nonce, args, true)
resp, err := k.callEvm(sdk.UnwrapSDKContext(ctx), from, &contract, value, nonce, data, true)
if err != nil {
return nil, err
}
Expand Down
Loading