-
Notifications
You must be signed in to change notification settings - Fork 142
/
evmcall.go
44 lines (37 loc) · 1.3 KB
/
evmcall.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package chainutil
import (
"fmt"
"time"
"github.com/ethereum/go-ethereum"
"github.com/iotaledger/wasp/packages/chain"
"github.com/iotaledger/wasp/packages/isc"
"github.com/iotaledger/wasp/packages/parameters"
"github.com/iotaledger/wasp/packages/vm/core/evm"
"github.com/iotaledger/wasp/packages/vm/gas"
)
// EVMCall executes an EVM contract call and returns its output, discarding any state changes
func EVMCall(ch chain.ChainCore, aliasOutput *isc.AliasOutputWithID, call ethereum.CallMsg) ([]byte, error) {
info := getChainInfo(ch)
// 0 means view call
gasLimit := gas.EVMCallGasLimit(info.GasLimits, &info.GasFeePolicy.EVMGasRatio)
if call.Gas != 0 && call.Gas > gasLimit {
call.Gas = gasLimit
}
if call.GasPrice == nil {
call.GasPrice = info.GasFeePolicy.DefaultGasPriceFullDecimals(parameters.L1().BaseToken.Decimals)
}
iscReq := isc.NewEVMOffLedgerCallRequest(ch.ID(), call)
// TODO: setting EstimateGasMode = true feels wrong here
res, err := runISCRequest(ch, aliasOutput, time.Now(), iscReq, true)
if err != nil {
return nil, err
}
if res.Receipt.Error != nil {
vmerr, resolvingErr := ResolveError(ch, res.Receipt.Error)
if resolvingErr != nil {
panic(fmt.Errorf("error resolving vmerror %w", resolvingErr))
}
return nil, vmerr
}
return res.Return[evm.FieldResult], nil
}