-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulate.go
62 lines (52 loc) · 1.27 KB
/
simulate.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package ethutils
import (
"context"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/lmittmann/w3/module/eth"
"github.com/lmittmann/w3/w3types"
)
// SimulateRevertedTx attempts to simulate a reverted tx and dump its revert reason
func (p *Provider) SimulateRevertedTx(ctx context.Context, txHash common.Hash, blockNumber *big.Int) (string, error) {
var (
tx *types.Transaction
output []byte
)
if err := p.Client.CallCtx(
ctx,
eth.Tx(txHash).Returns(&tx),
); err != nil {
return "", err
}
from, err := types.Sender(p.Signer, tx)
if err != nil {
return "", err
}
if err := p.Client.CallCtx(
ctx,
eth.Call(&w3types.Message{
From: from,
To: tx.To(),
Input: tx.Data(),
Gas: tx.Gas(),
GasPrice: tx.GasPrice(),
Value: tx.Value(),
}, blockNumber, nil).Returns(&output),
); err != nil {
revert, reason := parseError(err)
if revert {
return reason, nil
}
return "", err
}
return "", nil
}
func parseError(err error) (bool, string) {
executionRevertMsg := "w3: call failed: execution reverted: "
if strings.Contains(err.Error(), executionRevertMsg) {
return true, strings.TrimLeft(err.Error(), executionRevertMsg)
}
return false, ""
}