decred.org/dcrdex@v1.0.3/client/asset/eth/chaincfg.go (about)

     1  // This code is available on the terms of the project LICENSE.md file,
     2  // also available online at https://blueoakcouncil.org/license/1.0.0.
     3  
     4  package eth
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"os/user"
    10  	"path/filepath"
    11  	"strings"
    12  
    13  	"decred.org/dcrdex/dex"
    14  	dexeth "decred.org/dcrdex/dex/networks/eth"
    15  	"github.com/ethereum/go-ethereum/common"
    16  	ethcore "github.com/ethereum/go-ethereum/core"
    17  	"github.com/ethereum/go-ethereum/eth/ethconfig"
    18  	"github.com/ethereum/go-ethereum/params"
    19  )
    20  
    21  // CompatibilityData is some addresses and hashes used for validating an RPC
    22  // APIs compatibility with trading.
    23  type CompatibilityData struct {
    24  	Addr      common.Address
    25  	TokenAddr common.Address
    26  	TxHash    common.Hash
    27  	BlockHash common.Hash
    28  }
    29  
    30  var (
    31  	mainnetCompatibilityData = CompatibilityData{
    32  		// Vitalik's address from https://twitter.com/VitalikButerin/status/1050126908589887488
    33  		Addr:      common.HexToAddress("0xab5801a7d398351b8be11c439e05c5b3259aec9b"),
    34  		TokenAddr: common.HexToAddress("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"),
    35  		TxHash:    common.HexToHash("0xea1a717af9fad5702f189d6f760bb9a5d6861b4ee915976fe7732c0c95cd8a0e"),
    36  		BlockHash: common.HexToHash("0x44ebd6f66b4fd546bccdd700869f6a433ef9a47e296a594fa474228f86eeb353"),
    37  	}
    38  
    39  	testnetCompatibilityData = CompatibilityData{
    40  		Addr:      common.HexToAddress("0x1268ad189526ac0b386faf06effc46779c340ee6"),
    41  		TokenAddr: common.HexToAddress("0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238"), // USDC
    42  		TxHash:    common.HexToHash("0x854860598c69e9b9e259fe74ca88610752428541e747180e0d40a4409d036f02"),
    43  		BlockHash: common.HexToHash("0x7f3cd2786bc82872ffb55eface91a0afb927a9868adbb16a8d7939fef7385772"),
    44  	}
    45  )
    46  
    47  // NetworkCompatibilityData returns the CompatibilityData for the specified
    48  // network. If using simnet, make sure the simnet harness is running.
    49  func NetworkCompatibilityData(net dex.Network) (c CompatibilityData, err error) {
    50  	switch net {
    51  	case dex.Mainnet:
    52  		return mainnetCompatibilityData, nil
    53  	case dex.Testnet:
    54  		return testnetCompatibilityData, nil
    55  	case dex.Simnet:
    56  	default:
    57  		return c, fmt.Errorf("no compatibility data for network # %d", net)
    58  	}
    59  	// simnet
    60  	tDir, err := simnetDataDir()
    61  	if err != nil {
    62  		return
    63  	}
    64  
    65  	addr := common.HexToAddress("18d65fb8d60c1199bb1ad381be47aa692b482605")
    66  	var (
    67  		tTxHashFile    = filepath.Join(tDir, "test_tx_hash.txt")
    68  		tBlockHashFile = filepath.Join(tDir, "test_block10_hash.txt")
    69  		tContractFile  = filepath.Join(tDir, "test_usdc_contract_address.txt")
    70  	)
    71  	readIt := func(path string) string {
    72  		b, err := os.ReadFile(path)
    73  		if err != nil {
    74  			panic(fmt.Sprintf("Problem reading simnet testing file %q: %v", path, err))
    75  		}
    76  		return strings.TrimSpace(string(b)) // mainly the trailing "\r\n"
    77  	}
    78  	return CompatibilityData{
    79  		Addr:      addr,
    80  		TokenAddr: common.HexToAddress(readIt(tContractFile)),
    81  		TxHash:    common.HexToHash(readIt(tTxHashFile)),
    82  		BlockHash: common.HexToHash(readIt(tBlockHashFile)),
    83  	}, nil
    84  }
    85  
    86  // simnetDataDir returns the data directory for Ethereum simnet.
    87  func simnetDataDir() (string, error) {
    88  	u, err := user.Current()
    89  	if err != nil {
    90  		return "", fmt.Errorf("error getting current user: %w", err)
    91  	}
    92  
    93  	return filepath.Join(u.HomeDir, "dextest", "eth"), nil
    94  }
    95  
    96  // ETHConfig returns the ETH protocol configuration for the specified network.
    97  func ETHConfig(net dex.Network) (c ethconfig.Config, err error) {
    98  	c = ethconfig.Defaults
    99  	switch net {
   100  	// Ethereum
   101  	case dex.Mainnet:
   102  		c.Genesis = ethcore.DefaultGenesisBlock()
   103  	case dex.Testnet:
   104  		c.Genesis = ethcore.DefaultSepoliaGenesisBlock()
   105  	case dex.Simnet:
   106  		c.Genesis, err = readSimnetGenesisFile()
   107  		if err != nil {
   108  			return c, fmt.Errorf("readSimnetGenesisFile error: %w", err)
   109  		}
   110  	default:
   111  		return c, fmt.Errorf("unknown network %d", net)
   112  
   113  	}
   114  	c.NetworkId = c.Genesis.Config.ChainID.Uint64()
   115  	return
   116  }
   117  
   118  // ChainConfig returns the core configuration for the blockchain.
   119  func ChainConfig(net dex.Network) (c *params.ChainConfig, err error) {
   120  	cfg, err := ETHConfig(net)
   121  	if err != nil {
   122  		return nil, err
   123  	}
   124  	return cfg.Genesis.Config, nil
   125  }
   126  
   127  // readSimnetGenesisFile reads the simnet genesis file.
   128  func readSimnetGenesisFile() (*ethcore.Genesis, error) {
   129  	dataDir, err := simnetDataDir()
   130  	if err != nil {
   131  		return nil, err
   132  	}
   133  
   134  	genesisFile := filepath.Join(dataDir, "genesis.json")
   135  	genesisCfg, err := dexeth.LoadGenesisFile(genesisFile)
   136  	if err != nil {
   137  		return nil, fmt.Errorf("error reading genesis file: %v", err)
   138  	}
   139  
   140  	return genesisCfg, nil
   141  }