Skip to content

Commit

Permalink
Merge pull request #5516 from onflow/ramtin/flow-evm-improve-handler
Browse files Browse the repository at this point in the history
[Flow EVM] improve handler
  • Loading branch information
ramtinms authored Mar 6, 2024
2 parents bd1722d + 333b13d commit 19d31c7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
40 changes: 35 additions & 5 deletions fvm/evm/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func (h *ContractHandler) deployCOA(uuid uint64) (types.Address, error) {
if err != nil {
return types.Address{}, err
}
if res == nil || res.Failed() {
return types.Address{}, types.ErrDirectCallExecutionFailed
}

return res.DeployedContractAddress, nil
}

Expand Down Expand Up @@ -491,8 +495,17 @@ func (a *Account) deposit(v *types.FLOWTokenVault) error {
if err != nil {
return err
}
_, err = a.fch.executeAndHandleCall(ctx, call, v.Balance(), false)
return err

res, err := a.fch.executeAndHandleCall(ctx, call, v.Balance(), false)
if err != nil {
return err
}

if res == nil || res.Failed() {
return types.ErrDirectCallExecutionFailed
}

return nil
}

// Withdraw deducts the balance from the account and
Expand Down Expand Up @@ -521,11 +534,15 @@ func (a *Account) withdraw(b types.Balance) (*types.FLOWTokenVault, error) {
return nil, types.ErrWithdrawBalanceRounding
}

_, err = a.fch.executeAndHandleCall(ctx, call, b, true)
res, err := a.fch.executeAndHandleCall(ctx, call, b, true)
if err != nil {
return nil, err
}

if res == nil || res.Failed() {
return nil, types.ErrDirectCallExecutionFailed
}

return types.NewFlowTokenVault(b), nil
}

Expand All @@ -546,8 +563,16 @@ func (a *Account) transfer(to types.Address, balance types.Balance) error {
if err != nil {
return err
}
_, err = a.fch.executeAndHandleCall(ctx, call, nil, false)
return err
res, err := a.fch.executeAndHandleCall(ctx, call, nil, false)
if err != nil {
return err
}

if res == nil || res.Failed() {
return types.ErrDirectCallExecutionFailed
}

return nil
}

// Deploy deploys a contract to the EVM environment
Expand Down Expand Up @@ -576,6 +601,11 @@ func (a *Account) deploy(code types.Code, gaslimit types.GasLimit, balance types
if err != nil {
return types.Address{}, err
}

if res == nil || res.Failed() {
return types.Address{}, types.ErrDirectCallExecutionFailed
}

return types.Address(res.DeployedContractAddress), nil
}

Expand Down
3 changes: 3 additions & 0 deletions fvm/evm/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ var (
// yeild to rounding error, i.e. the balance contains fractions smaller than 10^8 Flow (smallest unit allowed to transfer).
ErrWithdrawBalanceRounding = NewEVMValidationError(errors.New("withdraw failed! the balance is susceptible to the rounding error"))

// ErrDirectCallExecutionFailed is returned when the direct call execution has failed.
ErrDirectCallExecutionFailed = NewEVMValidationError(errors.New("direct call execution failed"))

// ErrInsufficientTotalSupply is returned when flow token
// is withdraw request is there but not enough balance is on EVM vault
// this should never happen but its a saftey measure to protect Flow against EVM issues.
Expand Down

0 comments on commit 19d31c7

Please sign in to comment.