Skip to content

Commit

Permalink
fix: improve ExecuteEVM in VMCall (#888)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code authored Jan 10, 2025
1 parent 3f57f1f commit 5a87334
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
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

0 comments on commit 5a87334

Please sign in to comment.