decred.org/dcrdex@v1.0.3/client/asset/dash/dash.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 dash
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"decred.org/dcrdex/client/asset"
    10  	"decred.org/dcrdex/client/asset/btc"
    11  	"decred.org/dcrdex/dex"
    12  	dexbtc "decred.org/dcrdex/dex/networks/btc"
    13  	dexdash "decred.org/dcrdex/dex/networks/dash"
    14  
    15  	"github.com/btcsuite/btcd/chaincfg"
    16  )
    17  
    18  const (
    19  	version                 = 0
    20  	BipID                   = 5
    21  	minNetworkVersion       = 200101 // Dash v20.1.1
    22  	walletTypeRPC           = "dashdRPC"
    23  	defaultRedeemConfTarget = 2
    24  )
    25  
    26  var (
    27  	configOpts = append(btc.RPCConfigOpts("Dash", "9998"), []*asset.ConfigOption{
    28  		{
    29  			Key:          "fallbackfee",
    30  			DisplayName:  "Fallback fee rate",
    31  			Description:  "Dash's 'fallbackfee' rate. Units: DASH/kB",
    32  			DefaultValue: dexdash.DefaultFee * 1000 / 1e8,
    33  		},
    34  		{
    35  			Key:         "feeratelimit",
    36  			DisplayName: "Highest acceptable fee rate",
    37  			Description: "This is the highest network fee rate you are willing to " +
    38  				"pay on swap transactions. If feeratelimit is lower than a market's " +
    39  				"maxfeerate, you will not be able to trade on that market with this " +
    40  				"wallet.  Units: DASH/kB",
    41  			DefaultValue: dexdash.DefaultFeeRateLimit * 1000 / 1e8,
    42  		},
    43  		{
    44  			Key:         "redeemconftarget",
    45  			DisplayName: "Redeem confirmation target",
    46  			Description: "The target number of blocks for the redeem transaction " +
    47  				"to be mined. Used to set the transaction's fee rate. " +
    48  				"(default: 2 blocks)",
    49  			DefaultValue: defaultRedeemConfTarget,
    50  		},
    51  		{
    52  			Key:         "txsplit",
    53  			DisplayName: "Pre-split funding inputs",
    54  			Description: "When placing an order, create a \"split\" transaction to fund the order without locking more of the wallet balance than " +
    55  				"necessary. Otherwise, excess funds may be reserved to fund the order until the first swap contract is broadcast " +
    56  				"during match settlement, or the order is canceled. This an extra transaction for which network mining fees are paid. " +
    57  				"Used only for standing-type orders, e.g. limit orders without immediate time-in-force.",
    58  			IsBoolean:    true,
    59  			DefaultValue: true,
    60  		},
    61  	}...)
    62  
    63  	// WalletInfo defines some general information about a Dash wallet.
    64  	WalletInfo = &asset.WalletInfo{
    65  		Name:              "Dash",
    66  		SupportedVersions: []uint32{version},
    67  		UnitInfo:          dexdash.UnitInfo,
    68  		AvailableWallets: []*asset.WalletDefinition{
    69  			{
    70  				Type:              walletTypeRPC,
    71  				Tab:               "Dash Core (external)",
    72  				Description:       "Connect to dashd",
    73  				DefaultConfigPath: dexbtc.SystemConfigPath("dash"),
    74  				ConfigOpts:        configOpts,
    75  			},
    76  		},
    77  	}
    78  )
    79  
    80  func init() {
    81  	asset.Register(BipID, &Driver{})
    82  }
    83  
    84  // Driver implements asset.Driver.
    85  type Driver struct{}
    86  
    87  // Open creates the Dash exchange wallet. Start the wallet with its Run method.
    88  func (d *Driver) Open(cfg *asset.WalletConfig, logger dex.Logger, network dex.Network) (asset.Wallet, error) {
    89  	return newWallet(cfg, logger, network)
    90  }
    91  
    92  // DecodeCoinID creates a human-readable representation of a coin ID for Dash
    93  func (d *Driver) DecodeCoinID(coinID []byte) (string, error) {
    94  	// Dash and Bitcoin have the same tx hash and output format.
    95  	return (&btc.Driver{}).DecodeCoinID(coinID)
    96  }
    97  
    98  // Info returns basic information about the wallet and asset.
    99  func (d *Driver) Info() *asset.WalletInfo {
   100  	return WalletInfo
   101  }
   102  
   103  // MinLotSize calculates the minimum bond size for a given fee rate that avoids
   104  // dust outputs on the swap and refund txs, assuming the maxFeeRate doesn't
   105  // change.
   106  func (d *Driver) MinLotSize(maxFeeRate uint64) uint64 {
   107  	return dexbtc.MinLotSize(maxFeeRate, false)
   108  }
   109  
   110  // newWallet constructs a new client wallet for Dash based on the WalletDefinition.Type
   111  func newWallet(cfg *asset.WalletConfig, logger dex.Logger, network dex.Network) (asset.Wallet, error) {
   112  	var params *chaincfg.Params
   113  	switch network {
   114  	case dex.Mainnet:
   115  		params = dexdash.MainNetParams
   116  	case dex.Testnet:
   117  		params = dexdash.TestNetParams
   118  	case dex.Regtest:
   119  		params = dexdash.RegressionNetParams
   120  	default:
   121  		return nil, fmt.Errorf("unknown network ID %v", network)
   122  	}
   123  
   124  	// Designate the clone ports.
   125  	ports := dexbtc.NetPorts{
   126  		Mainnet: "9998",
   127  		Testnet: "19998",
   128  		Simnet:  "19898",
   129  	}
   130  
   131  	cloneCFG := &btc.BTCCloneCFG{
   132  		WalletCFG:           cfg,
   133  		MinNetworkVersion:   minNetworkVersion,
   134  		WalletInfo:          WalletInfo,
   135  		Symbol:              "dash",
   136  		Logger:              logger,
   137  		Network:             network,
   138  		ChainParams:         params,
   139  		Ports:               ports,
   140  		DefaultFallbackFee:  dexdash.DefaultFee,
   141  		DefaultFeeRateLimit: dexdash.DefaultFeeRateLimit,
   142  		LegacyBalance:       false,
   143  		Segwit:              false,
   144  		// Dash v19.1.0 has a breaking change from the true/false 'allowhighfees'
   145  		// to 'maxfeerate' in DASH/kB, the same as btc.
   146  		LegacyRawFeeLimit:        false,
   147  		InitTxSize:               dexbtc.InitTxSize,
   148  		InitTxSizeBase:           dexbtc.InitTxSizeBase,
   149  		PrivKeyFunc:              nil,
   150  		AddressDecoder:           nil,
   151  		AddressStringer:          nil,
   152  		BlockDeserializer:        nil,
   153  		ArglessChangeAddrRPC:     true, // getrawchangeaddress has No address-type arg
   154  		NonSegwitSigner:          nil,
   155  		FeeEstimator:             nil, // estimatesmartfee + getblockstats
   156  		ExternalFeeEstimator:     nil,
   157  		OmitAddressType:          true,  // getnewaddress has No address-type arg
   158  		LegacySignTxRPC:          false, // Has signrawtransactionwithwallet RPC
   159  		BooleanGetBlockRPC:       false, // Use 0/1 for verbose param
   160  		LegacyValidateAddressRPC: false, // getaddressinfo
   161  		SingularWallet:           false, // wallet can have "" as a path but also a name like "gamma"
   162  		UnlockSpends:             false,
   163  		ConstantDustLimit:        0,
   164  		AssetID:                  BipID,
   165  	}
   166  
   167  	return btc.BTCCloneWallet(cloneCFG)
   168  }