github.com/status-im/status-go@v1.1.0/rpc/verif_proxy.go (about)

     1  //go:build nimbus_light_client
     2  // +build nimbus_light_client
     3  
     4  package rpc
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  
    10  	"github.com/ethereum/go-ethereum/common"
    11  	"github.com/ethereum/go-ethereum/common/hexutil"
    12  	"github.com/ethereum/go-ethereum/log"
    13  
    14  	gethrpc "github.com/ethereum/go-ethereum/rpc"
    15  
    16  	proxy "github.com/siphiuel/lc-proxy-wrapper"
    17  
    18  	"github.com/status-im/status-go/params"
    19  )
    20  
    21  type VerifProxy struct {
    22  	config *proxy.Config
    23  	client *gethrpc.Client
    24  	log    log.Logger
    25  }
    26  
    27  func init() {
    28  	verifProxyInitFn = func(c *Client) {
    29  		ctx := context.Background()
    30  		var testConfig = proxy.Config{
    31  			Eth2Network:      "mainnet",
    32  			TrustedBlockRoot: "0xc5182cdb750fe088138b0d475683cda26a96befc24de16fb17bcf49d9cadf2f7",
    33  			Web3Url:          c.upstreamURL,
    34  			RpcAddress:       "127.0.0.1",
    35  			RpcPort:          8545,
    36  			LogLevel:         "INFO",
    37  		}
    38  		proxy.StartLightClient(ctx, &testConfig)
    39  		verifProxy, err := newVerifProxy(&testConfig, c.log)
    40  		if err != nil {
    41  			c.RegisterHandler(
    42  				params.BalanceMethodName,
    43  				func(ctx context.Context, v uint64, params ...interface{}) (interface{}, error) {
    44  					addr := params[0].(common.Address)
    45  					return verifProxy.GetBalance(ctx, addr)
    46  				},
    47  			)
    48  		}
    49  	}
    50  }
    51  
    52  func newVerifProxy(cfg *proxy.Config, log log.Logger) (*VerifProxy, error) {
    53  	endpoint := "http://" + cfg.RpcAddress + ":" + fmt.Sprint(cfg.RpcPort)
    54  	client, err := gethrpc.DialHTTP(endpoint)
    55  	if err != nil {
    56  		log.Error("Error when creating VerifProxy client", err)
    57  		return nil, err
    58  	}
    59  	proxy := &VerifProxy{cfg, client, log}
    60  	return proxy, nil
    61  }
    62  
    63  func (p *VerifProxy) GetBalance(ctx context.Context, address common.Address) (interface{}, error) {
    64  	var result hexutil.Big
    65  	err := p.client.CallContext(ctx, &result, "eth_getBalance", address, "latest")
    66  	if err != nil {
    67  		p.log.Error("Error when invoking GetBalance", err)
    68  		return nil, err
    69  	}
    70  	return result, nil
    71  
    72  }