Skip to content

Commit 4c45835

Browse files
authored
set address in From optionally for CallOpts (#2290)
* set address in From optionally for CallOpts * changeset
1 parent 17ca79b commit 4c45835

File tree

2 files changed

+15
-34
lines changed

2 files changed

+15
-34
lines changed

seth/.changeset/v1.51.4.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Set From field for bind.CallOpts optionally, warn that some view/pure funcs may work incorrectly without sender

seth/client.go

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
)
2828

2929
const (
30+
WrnEmptyFromInCallOpts = "you are running Seth without keys, key %d was not found. In case remote blockchain node is used (msg.sender) will be empty. Private functions which check msg.sender may require the correct key to be used"
31+
3032
ErrEmptyConfigPath = "toml config path is empty, set SETH_CONFIG_PATH"
3133
ErrCreateABIStore = "failed to create ABI store"
3234
ErrReadingKeys = "failed to read keys"
@@ -582,12 +584,13 @@ func WithBlockNumber(bn uint64) CallOpt {
582584

583585
// NewCallOpts returns a new sequential call options wrapper
584586
func (m *Client) NewCallOpts(o ...CallOpt) *bind.CallOpts {
585-
if errCallOpts := m.errCallOptsIfAddressCountTooLow(0); errCallOpts != nil {
586-
return errCallOpts
587-
}
588587
co := &bind.CallOpts{
589588
Pending: false,
590-
From: m.Addresses[0],
589+
}
590+
if len(m.Addresses) > 0 {
591+
co.From = m.Addresses[0]
592+
} else {
593+
L.Warn().Msgf(WrnEmptyFromInCallOpts, 0)
591594
}
592595
for _, f := range o {
593596
f(co)
@@ -597,43 +600,20 @@ func (m *Client) NewCallOpts(o ...CallOpt) *bind.CallOpts {
597600

598601
// NewCallKeyOpts returns a new sequential call options wrapper from the key N
599602
func (m *Client) NewCallKeyOpts(keyNum int, o ...CallOpt) *bind.CallOpts {
600-
if errCallOpts := m.errCallOptsIfAddressCountTooLow(keyNum); errCallOpts != nil {
601-
return errCallOpts
602-
}
603-
604603
co := &bind.CallOpts{
605604
Pending: false,
606-
From: m.Addresses[keyNum],
605+
}
606+
if len(m.Addresses) >= keyNum {
607+
co.From = m.Addresses[keyNum]
608+
} else {
609+
L.Warn().Msgf(WrnEmptyFromInCallOpts, keyNum)
607610
}
608611
for _, f := range o {
609612
f(co)
610613
}
611614
return co
612615
}
613616

614-
// errCallOptsIfAddressCountTooLow returns non-nil CallOpts with error in Context if keyNum is out of range
615-
func (m *Client) errCallOptsIfAddressCountTooLow(keyNum int) *bind.CallOpts {
616-
if err := m.validateAddressesKeyNum(keyNum); err != nil {
617-
errText := err.Error()
618-
if keyNum == TimeoutKeyNum {
619-
errText += " (this is a probably because we didn't manage to find any synced key before timeout)"
620-
}
621-
622-
err := errors.New(errText)
623-
m.Errors = append(m.Errors, err)
624-
opts := &bind.CallOpts{}
625-
626-
// can't return nil, otherwise RPC wrapper will panic and we might lose funds on testnets/mainnets, that's why
627-
// error is passed in Context here to avoid panic, whoever is using Seth should make sure that there is no error
628-
// present in Context before using *bind.TransactOpts
629-
opts.Context = context.WithValue(context.Background(), ContextErrorKey{}, err)
630-
631-
return opts
632-
}
633-
634-
return nil
635-
}
636-
637617
// errTxOptsIfPrivateKeysCountTooLow returns non-nil TransactOpts with error in Context if keyNum is out of range
638618
func (m *Client) errTxOptsIfPrivateKeysCountTooLow(keyNum int) *bind.TransactOpts {
639619
if err := m.validatePrivateKeysKeyNum(keyNum); err != nil {
@@ -913,14 +893,14 @@ func (m *Client) CalculateGasEstimations(request GasEstimationRequest) GasEstima
913893
ctx, cancel := context.WithTimeout(context.Background(), m.Cfg.Network.TxnTimeout.Duration())
914894
defer cancel()
915895

916-
var disableEstimationsIfNeeded = func(err error) {
896+
disableEstimationsIfNeeded := func(err error) {
917897
if strings.Contains(err.Error(), ZeroGasSuggestedErr) {
918898
L.Warn().Msg("Received incorrect gas estimations. Disabling them and reverting to hardcoded values. Remember to update your config!")
919899
m.Cfg.Network.GasPriceEstimationEnabled = false
920900
}
921901
}
922902

923-
var calculateLegacyFees = func() {
903+
calculateLegacyFees := func() {
924904
gasPrice, err := m.GetSuggestedLegacyFees(ctx, request.Priority)
925905
if err != nil {
926906
disableEstimationsIfNeeded(err)

0 commit comments

Comments
 (0)