Skip to content

Commit

Permalink
buck's review
Browse files Browse the repository at this point in the history
  • Loading branch information
itswisdomagain committed Oct 25, 2021
1 parent df9116e commit 2a4f3be
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
18 changes: 8 additions & 10 deletions client/asset/dcr/dcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,7 @@ func (d *Driver) DecodeCoinID(coinID []byte) (string, error) {
return fmt.Sprintf("%v:%d", txid, vout), err
}

// Info returns basic information about the wallet and asset. WARNING: An
// ExchangeWallet instance may have different DefaultFeeRate set, so use
// (*ExchangeWallet).Info when possible.
// Info returns basic information about the wallet and asset.
func (d *Driver) Info() *asset.WalletInfo {
return WalletInfo
}
Expand Down Expand Up @@ -430,23 +428,23 @@ func NewWallet(cfg *asset.WalletConfig, logger dex.Logger, network dex.Network)
}

// Set dcr.wallet using either the default rpcWallet or a custom wallet.
if customWalletConstructor == nil {
dcr.wallet, err = newRPCWallet(walletCfg, chainParams, logger)
if makeCustomWallet, ok := customWalletConstructors[cfg.Type]; ok {
dcr.wallet, err = makeCustomWallet(cfg, chainParams, logger)
if err != nil {
return nil, err
return nil, fmt.Errorf("custom wallet setup error: %v", err)
}
} else {
dcr.wallet, err = customWalletConstructor(walletCfg, chainParams, logger)
dcr.wallet, err = newRPCWallet(walletCfg, chainParams, logger)
if err != nil {
return nil, fmt.Errorf("custom wallet setup error: %v", err)
return nil, err
}
}

return dcr, nil
}

// unconnectedWallet returns an ExchangeWallet without a node. The node should
// be set before use.
// unconnectedWallet returns an ExchangeWallet without a base wallet. The wallet
// should be set before use.
func unconnectedWallet(cfg *asset.WalletConfig, dcrCfg *Config, chainParams *chaincfg.Params, logger dex.Logger) (*ExchangeWallet, error) {
// If set in the user config, the fallback fee will be in units of DCR/kB.
// Convert to atoms/B.
Expand Down
8 changes: 4 additions & 4 deletions client/asset/dcr/rpcwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,15 @@ func (w *rpcWallet) Disconnected() bool {
return w.rpcConnector.Disconnected()
}

// Disconnected returns true if the rpc client is not connected.
// Network returns the network of the connected wallet.
// Part of the Wallet interface.
func (w *rpcWallet) Network(ctx context.Context) (wire.CurrencyNet, error) {
net, err := w.rpcClient.GetCurrentNet(ctx)
return net, translateRPCCancelErr(err)
}

// NotifyOnTipChange registers a callback function that the should be invoked
// when the wallet sees new mainchain blocks. The return value indicates if this
// NotifyOnTipChange registers a callback function that should be invoked when
// the wallet sees new mainchain blocks. The return value indicates if this
// notification can be provided.
// Part of the Wallet interface.
func (w *rpcWallet) NotifyOnTipChange(ctx context.Context, cb TipChangeCallback) bool {
Expand Down Expand Up @@ -525,7 +525,7 @@ func (w *rpcWallet) AddressPrivKey(ctx context.Context, address stdaddr.Address)
type anylist []interface{}

// rpcClientRawRequest is used to marshal parameters and send requests to the
// RPC server via rpcClient.RawRequest. If `thing` is non-nil, the resul will
// RPC server via rpcClient.RawRequest. If `thing` is non-nil, the result will
// be marshaled into `thing`.
func (w *rpcWallet) rpcClientRawRequest(ctx context.Context, method string, args anylist, thing interface{}) error {
params := make([]json.RawMessage, 0, len(args))
Expand Down
27 changes: 16 additions & 11 deletions client/asset/dcr/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,36 @@ import (

// WalletConstructor defines a function that can be invoked to create a custom
// implementation of the Wallet interface.
type WalletConstructor func(cfg *Config, chainParams *chaincfg.Params, logger dex.Logger) (Wallet, error)
type WalletConstructor func(cfg *asset.WalletConfig, chainParams *chaincfg.Params, logger dex.Logger) (Wallet, error)

// customWalletConstructor is a function that can be invoked when setting up
// the ExchangeWallet to set up a custom implementation of the Wallet interface
// which will be used instead of the default rpcWallet implementation.
var customWalletConstructor WalletConstructor
// customWalletConstructors are functions for setting up custom implementations
// of the Wallet interface that may be used by the ExchangeWallet instead of the
// default rpcWallet implementation.
var customWalletConstructors map[string]WalletConstructor

// RegisterCustomWallet specifies the function that should be used in creating
// a wallet implementation that the ExchangeWallet will use in place of the
// default rpcWallet implementation. External consumers can use this function
// to provide alternative Wallet implementations, and must do so before an
// ExchangeWallet instance is created.
// RegisterCustomWallet registers a function that should be used in creating a
// Wallet implementation that the ExchangeWallet of the specified type will use
// in place of the default rpcWallet implementation. External consumers can use
// this function to provide alternative Wallet implementations, and must do so
// before attempting to create an ExchangeWallet instance of this type.
func RegisterCustomWallet(constructor WalletConstructor, def *asset.WalletDefinition) error {
for _, availableWallets := range WalletInfo.AvailableWallets {
if def.Type == availableWallets.Type {
return fmt.Errorf("already support %q wallets", def.Type)
}
}
customWalletConstructor = constructor
customWalletConstructors[def.Type] = constructor
WalletInfo.AvailableWallets = append(WalletInfo.AvailableWallets, def)
return nil
}

type TipChangeCallback func(*chainhash.Hash, int64, error)

// Wallet defines methods that the ExchangeWallet uses for communicating with
// a Decred wallet and blockchain.
// TODO: Where possible, replace walletjson and chainjson return types with
// other types that define fewer fields e.g. *chainjson.TxRawResult with
// *wire.MsgTx.
type Wallet interface {
// Connect establishes a connection to the wallet.
Connect(ctx context.Context) error
Expand Down
2 changes: 0 additions & 2 deletions client/asset/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ func Assets() map[uint32]RegisteredAsset {
}

// Info returns the WalletInfo for the specified asset, if supported.
// WARNING: An ExchangeWallet instance may have different config values set,
// so use (*ExchangeWallet).Info when possible.
func Info(assetID uint32) (*WalletInfo, error) {
driversMtx.RLock()
drv, ok := drivers[assetID]
Expand Down

0 comments on commit 2a4f3be

Please sign in to comment.