@@ -5,21 +5,19 @@ import (
55 "crypto/ecdsa"
66 "fmt"
77 "math/big"
8- "net/http"
98 "path/filepath"
109 "reflect"
1110 "strings"
1211 "time"
1312
1413 "github.com/avast/retry-go"
1514 "github.com/ethereum/go-ethereum"
16- "github.com/ethereum/go-ethereum/rpc "
15+ "github.com/ethereum/go-ethereum/ethclient/simulated "
1716
1817 "github.com/ethereum/go-ethereum/accounts/abi"
1918 "github.com/ethereum/go-ethereum/accounts/abi/bind"
2019 "github.com/ethereum/go-ethereum/common"
2120 "github.com/ethereum/go-ethereum/core/types"
22- "github.com/ethereum/go-ethereum/ethclient"
2321 "github.com/pkg/errors"
2422 "github.com/rs/zerolog"
2523 "golang.org/x/sync/errgroup"
6462// Client is a vanilla go-ethereum client with enhanced debug logging
6563type Client struct {
6664 Cfg * Config
67- Client * ethclient .Client
65+ Client simulated .Client
6866 Addresses []common.Address
6967 PrivateKeys []* ecdsa.PrivateKey
7068 ChainID int64
@@ -175,53 +173,63 @@ func NewClientRaw(
175173 pkeys []* ecdsa.PrivateKey ,
176174 opts ... ClientOpt ,
177175) (* Client , error ) {
178- if len (cfg .Network .URLs ) == 0 {
179- return nil , errors .New ("no RPC URL provided" )
180- }
181- if len (cfg .Network .URLs ) > 1 {
182- L .Warn ().Msg ("Multiple RPC URLs provided, only the first one will be used" )
183- }
184-
185176 if cfg .ReadOnly && (len (addrs ) > 0 || len (pkeys ) > 0 ) {
186177 return nil , errors .New (ErrReadOnlyWithPrivateKeys )
187178 }
188179
189- ctx , cancel := context .WithTimeout (context .Background (), cfg .Network .DialTimeout .Duration ())
190- defer cancel ()
191- rpcClient , err := rpc .DialOptions (ctx ,
192- cfg .FirstNetworkURL (),
193- rpc .WithHeaders (cfg .RPCHeaders ),
194- rpc .WithHTTPClient (& http.Client {
195- Transport : NewLoggingTransport (),
196- }),
197- )
198- if err != nil {
199- return nil , fmt .Errorf ("failed to connect RPC client to '%s' due to: %w" , cfg .FirstNetworkURL (), err )
200- }
201- client := ethclient .NewClient (rpcClient )
180+ // TODO we should execute this only if we haven't passed an instance of Client
181+ // or... we should externalize client creation to a separate function
182+ // and by default create a new instance of Client using the URL from the config
183+ // althouth that would change the API a bit
184+
185+ // if len(cfg.Network.URLs) == 0 {
186+ // return nil, errors.New("no RPC URL provided")
187+ // }
188+
189+ // if len(cfg.Network.URLs) > 1 {
190+ // L.Warn().Msg("Multiple RPC URLs provided, only the first one will be used")
191+ // }
192+
193+ // ctx, cancel := context.WithTimeout(context.Background(), cfg.Network.DialTimeout.Duration())
194+ // defer cancel()
195+ // rpcClient, err := rpc.DialOptions(ctx,
196+ // cfg.FirstNetworkURL(),
197+ // rpc.WithHeaders(cfg.RPCHeaders),
198+ // rpc.WithHTTPClient(&http.Client{
199+ // Transport: NewLoggingTransport(),
200+ // }),
201+ // )
202+ // if err != nil {
203+ // return nil, fmt.Errorf("failed to connect RPC client to '%s' due to: %w", cfg.FirstNetworkURL(), err)
204+ // }
205+ // client := ethclient.NewClient(rpcClient)
202206
203- if cfg .Network .ChainID == 0 {
204- chainId , err := client .ChainID (context .Background ())
205- if err != nil {
206- return nil , errors .Wrap (err , "failed to get chain ID" )
207- }
208- cfg .Network .ChainID = chainId .Uint64 ()
209- }
210207 ctx , cancelFunc := context .WithCancel (context .Background ())
211208 c := & Client {
212209 Cfg : cfg ,
213- Client : client ,
214210 Addresses : addrs ,
215211 PrivateKeys : pkeys ,
216212 URL : cfg .FirstNetworkURL (),
217213 ChainID : mustSafeInt64 (cfg .Network .ChainID ),
218214 Context : ctx ,
219215 CancelFunc : cancelFunc ,
220216 }
217+
221218 for _ , o := range opts {
222219 o (c )
223220 }
224221
222+ if cfg .Network .ChainID == 0 {
223+ chainId , err := c .Client .ChainID (context .Background ())
224+ if err != nil {
225+ return nil , errors .Wrap (err , "failed to get chain ID" )
226+ }
227+ cfg .Network .ChainID = chainId .Uint64 ()
228+ c .ChainID = int64 (cfg .Network .ChainID )
229+ }
230+
231+ var err error
232+
225233 if c .ContractAddressToNameMap .addressMap == nil {
226234 c .ContractAddressToNameMap = NewEmptyContractMap ()
227235 if ! cfg .IsSimulatedNetwork () {
@@ -407,7 +415,7 @@ func (m *Client) TransferETHFromKey(ctx context.Context, fromKeyNum int, to stri
407415 ctx , chainCancel := context .WithTimeout (ctx , m .Cfg .Network .TxnTimeout .Duration ())
408416 defer chainCancel ()
409417
410- chainID , err := m .Client .NetworkID (ctx )
418+ chainID , err := m .Client .ChainID (ctx )
411419 if err != nil {
412420 return errors .Wrap (err , "failed to get network ID" )
413421 }
@@ -529,6 +537,12 @@ func WithTracer(t *Tracer) ClientOpt {
529537 }
530538}
531539
540+ func WithEthClient (ethClient simulated.Client ) ClientOpt {
541+ return func (c * Client ) {
542+ c .Client = ethClient
543+ }
544+ }
545+
532546/* CallOpts function options */
533547
534548// CallOpt is a functional option for bind.CallOpts
@@ -828,6 +842,8 @@ This issue is caused by one of two things:
828842 return & bind.TransactOpts {Context : ctx }, NonceStatus {}, GasEstimations {}
829843 }
830844
845+ fmt .Println ("sender" , opts .From .Hex ())
846+
831847 if ctx != nil {
832848 opts .Context = ctx
833849 }
0 commit comments