github.com/0chain/gosdk@v1.17.11/zcnbridge/config.go (about)

     1  package zcnbridge
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"math/big"
     7  	"path"
     8  
     9  	"github.com/ethereum/go-ethereum/accounts/abi/bind"
    10  
    11  	"github.com/0chain/gosdk/zcnbridge/log"
    12  	"github.com/0chain/gosdk/zcnbridge/transaction"
    13  	"github.com/ethereum/go-ethereum/ethclient"
    14  
    15  	"github.com/spf13/viper"
    16  )
    17  
    18  const (
    19  	TenderlyProvider = iota
    20  	AlchemyProvider
    21  	UnknownProvider
    22  )
    23  
    24  const (
    25  	EthereumWalletStorageDir = "wallets"
    26  )
    27  
    28  const (
    29  	UniswapRouterAddress = "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D"
    30  	UsdcTokenAddress     = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
    31  	WethTokenAddress     = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"
    32  )
    33  
    34  // BridgeSDKConfig describes the configuration for the bridge SDK.
    35  type BridgeSDKConfig struct {
    36  	LogLevel        *string
    37  	LogPath         *string
    38  	ConfigChainFile *string
    39  	ConfigDir       *string
    40  	Development     *bool
    41  }
    42  
    43  // EthereumClient describes Ethereum JSON-RPC client generealized interface
    44  type EthereumClient interface {
    45  	bind.ContractBackend
    46  
    47  	ChainID(ctx context.Context) (*big.Int, error)
    48  }
    49  
    50  // BridgeClient is a wrapper, which exposes Ethereum KeyStore methods used by DEX bridge.
    51  type BridgeClient struct {
    52  	keyStore            KeyStore
    53  	transactionProvider transaction.TransactionProvider
    54  	ethereumClient      EthereumClient
    55  
    56  	BridgeAddress,
    57  	TokenAddress,
    58  	AuthorizersAddress,
    59  	UniswapAddress,
    60  	NFTConfigAddress,
    61  	EthereumAddress,
    62  	EthereumNodeURL,
    63  	Password string
    64  
    65  	BancorAPIURL string
    66  
    67  	ConsensusThreshold float64
    68  	GasLimit           uint64
    69  }
    70  
    71  // NewBridgeClient creates BridgeClient with the given parameters.
    72  //   - bridgeAddress is the address of the bridge smart contract on the Ethereum network.
    73  //   - tokenAddress is the address of the token smart contract on the Ethereum network.
    74  //   - authorizersAddress is the address of the authorizers smart contract on the Ethereum network.
    75  //   - authorizersAddress is the address of the authorizers smart contract on the Ethereum network.
    76  //   - uniswapAddress is the address of the user's ethereum wallet (on UniSwap).
    77  //   - ethereumAddress is the address of the user's ethereum wallet.
    78  //   - ethereumNodeURL is the URL of the Ethereum node.
    79  //   - password is the password for the user's ethereum wallet.
    80  //   - gasLimit is the gas limit for the transactions.
    81  //   - consensusThreshold is the consensus threshold, the minimum percentage of authorizers that need to agree on a transaction.
    82  //   - ethereumClient is the Ethereum JSON-RPC client.
    83  //   - transactionProvider provider interface for the transaction entity.
    84  //   - keyStore is the Ethereum KeyStore instance.
    85  func NewBridgeClient(
    86  	bridgeAddress,
    87  	tokenAddress,
    88  	authorizersAddress,
    89  	uniswapAddress,
    90  	ethereumAddress,
    91  	ethereumNodeURL,
    92  	password string,
    93  	gasLimit uint64,
    94  	consensusThreshold float64,
    95  	ethereumClient EthereumClient,
    96  	transactionProvider transaction.TransactionProvider,
    97  	keyStore KeyStore) *BridgeClient {
    98  	return &BridgeClient{
    99  		BridgeAddress:       bridgeAddress,
   100  		TokenAddress:        tokenAddress,
   101  		AuthorizersAddress:  authorizersAddress,
   102  		UniswapAddress:      uniswapAddress,
   103  		EthereumAddress:     ethereumAddress,
   104  		EthereumNodeURL:     ethereumNodeURL,
   105  		Password:            password,
   106  		GasLimit:            gasLimit,
   107  		ConsensusThreshold:  consensusThreshold,
   108  		ethereumClient:      ethereumClient,
   109  		transactionProvider: transactionProvider,
   110  		keyStore:            keyStore,
   111  	}
   112  }
   113  
   114  func initChainConfig(sdkConfig *BridgeSDKConfig) *viper.Viper {
   115  	cfg := readConfig(sdkConfig, func() string {
   116  		return *sdkConfig.ConfigChainFile
   117  	})
   118  
   119  	log.Logger.Info(fmt.Sprintf("Chain config has been initialized from %s", cfg.ConfigFileUsed()))
   120  
   121  	return cfg
   122  }
   123  
   124  func readConfig(sdkConfig *BridgeSDKConfig, getConfigName func() string) *viper.Viper {
   125  	cfg := viper.New()
   126  	cfg.AddConfigPath(*sdkConfig.ConfigDir)
   127  	cfg.SetConfigName(getConfigName())
   128  	cfg.SetConfigType("yaml")
   129  	err := cfg.ReadInConfig()
   130  	if err != nil {
   131  		log.Logger.Fatal(fmt.Errorf("%w: can't read config", err).Error())
   132  	}
   133  	return cfg
   134  }
   135  
   136  // SetupBridgeClientSDK initializes new bridge client.
   137  // Meant to be used from standalone application with 0chain SDK initialized.
   138  //   - cfg is the configuration for the bridge SDK.
   139  func SetupBridgeClientSDK(cfg *BridgeSDKConfig) *BridgeClient {
   140  	log.InitLogging(*cfg.Development, *cfg.LogPath, *cfg.LogLevel)
   141  
   142  	chainCfg := initChainConfig(cfg)
   143  
   144  	ethereumNodeURL := chainCfg.GetString("ethereum_node_url")
   145  
   146  	ethereumClient, err := ethclient.Dial(ethereumNodeURL)
   147  	if err != nil {
   148  		Logger.Error(err)
   149  	}
   150  
   151  	transactionProvider := transaction.NewTransactionProvider()
   152  
   153  	homedir := path.Dir(chainCfg.ConfigFileUsed())
   154  	if homedir == "" {
   155  		log.Logger.Fatal("err happened during home directory retrieval")
   156  	}
   157  
   158  	keyStore := NewKeyStore(path.Join(homedir, EthereumWalletStorageDir))
   159  
   160  	return NewBridgeClient(
   161  		chainCfg.GetString("bridge.bridge_address"),
   162  		chainCfg.GetString("bridge.token_address"),
   163  		chainCfg.GetString("bridge.authorizers_address"),
   164  		chainCfg.GetString("bridge.uniswap_address"),
   165  		chainCfg.GetString("bridge.ethereum_address"),
   166  		ethereumNodeURL,
   167  		chainCfg.GetString("bridge.password"),
   168  		chainCfg.GetUint64("bridge.gas_limit"),
   169  		chainCfg.GetFloat64("bridge.consensus_threshold"),
   170  		ethereumClient,
   171  		transactionProvider,
   172  		keyStore,
   173  	)
   174  }