diff --git a/contracts/contracts.go b/contracts/contracts.go index 130eafc63..6f18acb94 100644 --- a/contracts/contracts.go +++ b/contracts/contracts.go @@ -23,6 +23,13 @@ type ContractMaker struct { RPCClient *rpc.Client } +func NewContractMaker(client *rpc.Client) (*ContractMaker, error) { + if client == nil { + return nil, errors.New("could not initialize ContractMaker with an rpc client") + } + return &ContractMaker{RPCClient: client}, nil +} + func (c *ContractMaker) NewRegistryWithAddress(chainID uint64, address common.Address) (*resolver.ENSRegistryWithFallback, error) { backend, err := c.RPCClient.EthClient(chainID) if err != nil { diff --git a/protocol/messenger.go b/protocol/messenger.go index 776aa59da..5c0f8d149 100644 --- a/protocol/messenger.go +++ b/protocol/messenger.go @@ -518,9 +518,6 @@ func NewMessenger( }{wait: make(chan struct{})}, browserDatabase: c.browserDatabase, httpServer: c.httpServer, - contractMaker: &contracts.ContractMaker{ - RPCClient: c.rpcClient, - }, shutdownTasks: []func() error{ ensVerifier.Stop, pushNotificationClient.Stop, @@ -547,6 +544,15 @@ func NewMessenger( logger: logger, savedAddressesManager: savedAddressesManager, } + + if c.rpcClient != nil { + contractMaker, err := contracts.NewContractMaker(c.rpcClient) + if err != nil { + return nil, err + } + messenger.contractMaker = contractMaker + } + messenger.mentionsManager = NewMentionManager(messenger) if c.walletService != nil { diff --git a/protocol/messenger_communities.go b/protocol/messenger_communities.go index ae0ff57c9..25e759da0 100644 --- a/protocol/messenger_communities.go +++ b/protocol/messenger_communities.go @@ -505,6 +505,11 @@ func (m *Messenger) startCuratedCommunitiesUpdateLoop() { } func (m *Messenger) CuratedCommunities() (*communities.KnownCommunitiesResponse, error) { + if m.contractMaker == nil { + m.logger.Warn("contract maker not initialized") + return nil, errors.New("contract maker not initialized") + } + // Revert code to https://github.com/status-im/status-go/blob/e6a3f63ec7f2fa691878ed35f921413dc8acfc66/protocol/messenger_communities.go#L211-L226 once the curated communities contract is deployed to mainnet chainID := uint64(420) // Optimism Goerli diff --git a/services/wallet/router.go b/services/wallet/router.go index 779e9ff82..9a5719773 100644 --- a/services/wallet/router.go +++ b/services/wallet/router.go @@ -373,7 +373,10 @@ func (r *Router) requireApproval(ctx context.Context, bridge bridge.Bridge, acco if token.IsNative() { return false, nil, 0, nil, nil } - contractMaker := &contracts.ContractMaker{RPCClient: r.rpcClient} + contractMaker, err := contracts.NewContractMaker(r.rpcClient) + if err != nil { + return false, nil, 0, nil, err + } bridgeAddress := bridge.GetContractAddress(network, token) if bridgeAddress == nil { diff --git a/services/wallet/token/token.go b/services/wallet/token/token.go index 32ea11e3f..078c81e54 100644 --- a/services/wallet/token/token.go +++ b/services/wallet/token/token.go @@ -568,7 +568,10 @@ func (tm *Manager) GetBalances(parent context.Context, clients map[uint64]*chain response[account][token] = &sumHex mu.Unlock() } - contractMaker := contracts.ContractMaker{RPCClient: tm.RPCClient} + contractMaker, err := contracts.NewContractMaker(tm.RPCClient) + if err != nil { + return nil, err + } for clientIdx := range clients { client := clients[clientIdx] @@ -703,7 +706,10 @@ func (tm *Manager) GetBalancesByChain(parent context.Context, clients map[uint64 mu.Unlock() } - contractMaker := contracts.ContractMaker{RPCClient: tm.RPCClient} + contractMaker, err := contracts.NewContractMaker(tm.RPCClient) + if err != nil { + return nil, err + } for clientIdx := range clients { client := clients[clientIdx] ethScanContract, err := contractMaker.NewEthScan(client.ChainID)