decred.org/dcrdex@v1.0.5/server/asset/dgb/dgb.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 dgb
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"decred.org/dcrdex/dex"
    10  	dexbtc "decred.org/dcrdex/dex/networks/btc"
    11  	dexdgb "decred.org/dcrdex/dex/networks/dgb"
    12  	"decred.org/dcrdex/server/asset"
    13  	"decred.org/dcrdex/server/asset/btc"
    14  	"github.com/btcsuite/btcd/chaincfg"
    15  )
    16  
    17  var maxFeeBlocks = 16
    18  
    19  // Driver implements asset.Driver.
    20  type Driver struct{}
    21  
    22  // Setup creates the DGB backend. Start the backend with its Run method.
    23  func (d *Driver) Setup(cfg *asset.BackendConfig) (asset.Backend, error) {
    24  	return NewBackend(cfg)
    25  }
    26  
    27  // DecodeCoinID creates a human-readable representation of a coin ID for
    28  // DigiByte.
    29  func (d *Driver) DecodeCoinID(coinID []byte) (string, error) {
    30  	// Digibyte and Bitcoin have the same tx hash and output format.
    31  	return (&btc.Driver{}).DecodeCoinID(coinID)
    32  }
    33  
    34  // Version returns the Backend implementation's version number.
    35  func (d *Driver) Version() uint32 {
    36  	return version
    37  }
    38  
    39  // UnitInfo returns the dex.UnitInfo for the asset.
    40  func (d *Driver) UnitInfo() dex.UnitInfo {
    41  	return dexdgb.UnitInfo
    42  }
    43  
    44  // MinBondSize calculates the minimum bond size for a given fee rate that avoids
    45  // dust outputs on the bond and refund txs, assuming the maxFeeRate doesn't
    46  // change.
    47  func (d *Driver) MinBondSize(maxFeeRate uint64) uint64 {
    48  	return dexbtc.MinBondSize(maxFeeRate, false)
    49  }
    50  
    51  // MinLotSize calculates the minimum bond size for a given fee rate that avoids
    52  // dust outputs on the swap and refund txs, assuming the maxFeeRate doesn't
    53  // change.
    54  func (d *Driver) MinLotSize(maxFeeRate uint64) uint64 {
    55  	return dexbtc.MinLotSize(maxFeeRate, false)
    56  }
    57  
    58  // Name is the asset's name.
    59  func (d *Driver) Name() string {
    60  	return "DigiByte"
    61  }
    62  
    63  func init() {
    64  	asset.Register(BipID, &Driver{})
    65  }
    66  
    67  const (
    68  	version   = 0
    69  	BipID     = 20
    70  	assetName = "dgb"
    71  	feeConfs  = 3
    72  )
    73  
    74  // NewBackend generates the network parameters and creates a dgb backend as a
    75  // btc clone using an asset/btc helper function.
    76  func NewBackend(cfg *asset.BackendConfig) (asset.Backend, error) {
    77  	var params *chaincfg.Params
    78  	switch cfg.Net {
    79  	case dex.Mainnet:
    80  		params = dexdgb.MainNetParams
    81  	case dex.Testnet:
    82  		params = dexdgb.TestNetParams
    83  	case dex.Regtest:
    84  		params = dexdgb.RegressionNetParams
    85  	default:
    86  		return nil, fmt.Errorf("unknown network ID %v", cfg.Net)
    87  	}
    88  
    89  	// Designate the clone ports. These will be overwritten by any explicit
    90  	// settings in the configuration file.
    91  	ports := dexbtc.NetPorts{
    92  		Mainnet: "14022",
    93  		Testnet: "14023",
    94  		Simnet:  "18443",
    95  	}
    96  
    97  	configPath := cfg.ConfigPath
    98  	if configPath == "" {
    99  		configPath = dexbtc.SystemConfigPath("digibyte")
   100  	}
   101  
   102  	return btc.NewBTCClone(&btc.BackendCloneConfig{
   103  		Name:                 assetName,
   104  		Segwit:               true,
   105  		ConfigPath:           configPath,
   106  		Logger:               cfg.Logger,
   107  		Net:                  cfg.Net,
   108  		ChainParams:          params,
   109  		Ports:                ports,
   110  		FeeConfs:             feeConfs,
   111  		NoCompetitionFeeRate: 210, // 0.0021 DGB/kB
   112  		MaxFeeBlocks:         maxFeeBlocks,
   113  		RelayAddr:            cfg.RelayAddr,
   114  	})
   115  }