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