decred.org/dcrdex@v1.0.5/client/asset/polygon/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 polygon
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"os/user"
    10  	"path/filepath"
    11  	"strings"
    12  
    13  	"decred.org/dcrdex/client/asset/eth"
    14  	"decred.org/dcrdex/dex"
    15  	dexeth "decred.org/dcrdex/dex/networks/eth"
    16  	dexpolygon "decred.org/dcrdex/dex/networks/polygon"
    17  	"github.com/ethereum/go-ethereum/common"
    18  	ethcore "github.com/ethereum/go-ethereum/core"
    19  	"github.com/ethereum/go-ethereum/params"
    20  )
    21  
    22  var (
    23  	mainnetCompatibilityData = eth.CompatibilityData{
    24  		Addr:      common.HexToAddress("0x5973918275C01F50555d44e92c9d9b353CaDAD54"),
    25  		TokenAddr: common.HexToAddress("0x2791bca1f2de4661ed88a30c99a7a9449aa84174"), // usdc
    26  		TxHash:    common.HexToHash("0xc388210f83679f9841e34fb3cdee0294f885846de3e01211e50f77d508a0d6ec"),
    27  		BlockHash: common.HexToHash("0xa603d7354686269521e8d561d6ffa4aa92aad80e01f3b4cc9745fdb54342f85b"),
    28  	}
    29  
    30  	testnetCompatibilityData = eth.CompatibilityData{
    31  		Addr:      common.HexToAddress("0x248528f5A2C3731fb598E8cc1dc5dB5f997E74BC"),
    32  		TokenAddr: common.HexToAddress("0x41E94Eb019C0762f9Bfcf9Fb1E58725BfB0e7582"), // usdc
    33  		TxHash:    common.HexToHash("0x4097fc20d8566702f40323f19e351d5e2feb70dc2c99b90ababdb660351b5247"),
    34  		BlockHash: common.HexToHash("0x813a2898f25530a634fdaff7c3ceda786c934f476e6cd5556c32025ac103a90f"),
    35  	}
    36  )
    37  
    38  // NetworkCompatibilityData returns the CompatibilityData for the specified
    39  // network. If using simnet, make sure the simnet harness is running.
    40  func NetworkCompatibilityData(net dex.Network) (c eth.CompatibilityData, err error) {
    41  	switch net {
    42  	case dex.Mainnet:
    43  		return mainnetCompatibilityData, nil
    44  	case dex.Testnet:
    45  		return testnetCompatibilityData, nil
    46  	case dex.Simnet:
    47  	default:
    48  		return c, fmt.Errorf("No compatibility data for network # %d", net)
    49  	}
    50  	// simnet
    51  	tDir, err := simnetDataDir()
    52  	if err != nil {
    53  		return
    54  	}
    55  
    56  	addr := common.HexToAddress("18d65fb8d60c1199bb1ad381be47aa692b482605")
    57  	var (
    58  		tTxHashFile    = filepath.Join(tDir, "test_tx_hash.txt")
    59  		tBlockHashFile = filepath.Join(tDir, "test_block10_hash.txt")
    60  		tContractFile  = filepath.Join(tDir, "test_usdc_contract_address.txt")
    61  	)
    62  	readIt := func(path string) string {
    63  		b, err := os.ReadFile(path)
    64  		if err != nil {
    65  			panic(fmt.Sprintf("Problem reading simnet testing file %q: %v", path, err))
    66  		}
    67  		return strings.TrimSpace(string(b)) // mainly the trailing "\r\n"
    68  	}
    69  	return eth.CompatibilityData{
    70  		Addr:      addr,
    71  		TokenAddr: common.HexToAddress(readIt(tContractFile)),
    72  		TxHash:    common.HexToHash(readIt(tTxHashFile)),
    73  		BlockHash: common.HexToHash(readIt(tBlockHashFile)),
    74  	}, nil
    75  }
    76  
    77  // simnetDataDir returns the data directory for Ethereum simnet.
    78  func simnetDataDir() (string, error) {
    79  	u, err := user.Current()
    80  	if err != nil {
    81  		return "", fmt.Errorf("error getting current user: %w", err)
    82  	}
    83  
    84  	return filepath.Join(u.HomeDir, "dextest", "polygon"), nil
    85  }
    86  
    87  // ChainConfig returns the core configuration for the blockchain.
    88  func ChainConfig(net dex.Network) (c *params.ChainConfig, err error) {
    89  	switch net {
    90  	case dex.Mainnet:
    91  		return dexpolygon.BorMainnetChainConfig, nil
    92  	case dex.Testnet:
    93  		return dexpolygon.AmoyChainConfig, nil
    94  	case dex.Simnet:
    95  	default:
    96  		return c, fmt.Errorf("unknown network %d", net)
    97  	}
    98  	// simnet
    99  	g, err := readSimnetGenesisFile()
   100  	if err != nil {
   101  		return c, fmt.Errorf("readSimnetGenesisFile error: %w", err)
   102  	}
   103  	return g.Config, nil
   104  }
   105  
   106  // readSimnetGenesisFile reads the simnet genesis file.
   107  func readSimnetGenesisFile() (*ethcore.Genesis, error) {
   108  	dataDir, err := simnetDataDir()
   109  	if err != nil {
   110  		return nil, err
   111  	}
   112  
   113  	genesisFile := filepath.Join(dataDir, "genesis.json")
   114  	genesisCfg, err := dexeth.LoadGenesisFile(genesisFile)
   115  	if err != nil {
   116  		return nil, fmt.Errorf("error reading genesis file: %v", err)
   117  	}
   118  
   119  	return genesisCfg, nil
   120  }