github.com/decred/dcrlnd@v0.7.6/chainntnfs/dcrwnotify/driver.go (about)

     1  package dcrwnotify
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"decred.org/dcrwallet/v4/wallet"
     8  	"github.com/decred/dcrd/chaincfg/v3"
     9  	"github.com/decred/dcrlnd/blockcache"
    10  	"github.com/decred/dcrlnd/chainntnfs"
    11  )
    12  
    13  const (
    14  	// notifierType uniquely identifies this concrete implementation of the
    15  	// ChainNotifier interface.
    16  	notifierType = "dcrw"
    17  )
    18  
    19  // createNewNotifier creates a new instance of the ChainNotifier interface
    20  // implemented by DcrdNotifier.
    21  func createNewNotifier(args ...interface{}) (chainntnfs.ChainNotifier, error) {
    22  	if len(args) != 5 {
    23  		return nil, fmt.Errorf("incorrect number of arguments to "+
    24  			".New(...), expected 5, instead passed %v", len(args))
    25  	}
    26  
    27  	w, ok := args[0].(*wallet.Wallet)
    28  	if !ok {
    29  		return nil, errors.New("first argument to dcrdnotifier.New " +
    30  			"is incorrect, expected a *rpcclient.ConnConfig")
    31  	}
    32  
    33  	chainParams, ok := args[1].(*chaincfg.Params)
    34  	if !ok {
    35  		return nil, errors.New("second argument to dcrdnotifier.New " +
    36  			"is incorrect, expected a *chaincfg.Params")
    37  	}
    38  
    39  	spendHintCache, ok := args[2].(chainntnfs.SpendHintCache)
    40  	if !ok {
    41  		return nil, errors.New("third argument to dcrdnotifier.New " +
    42  			"is incorrect, expected a chainntnfs.SpendHintCache")
    43  	}
    44  
    45  	confirmHintCache, ok := args[3].(chainntnfs.ConfirmHintCache)
    46  	if !ok {
    47  		return nil, errors.New("third argument to dcrdnotifier.New " +
    48  			"is incorrect, expected a chainntnfs.ConfirmHintCache")
    49  	}
    50  
    51  	blockCache, ok := args[4].(*blockcache.BlockCache)
    52  	if !ok {
    53  		return nil, errors.New("third argument to dcrdnotifier.New " +
    54  			"is incorrect, expected a *blockcache.BlockCache")
    55  	}
    56  
    57  	return New(w, chainParams, spendHintCache, confirmHintCache, blockCache)
    58  }
    59  
    60  // init registers a driver for the DcrdNotifier concrete implementation of the
    61  // chainntnfs.ChainNotifier interface.
    62  func init() {
    63  	// Register the driver.
    64  	notifier := &chainntnfs.NotifierDriver{
    65  		NotifierType: notifierType,
    66  		New:          createNewNotifier,
    67  	}
    68  
    69  	if err := chainntnfs.RegisterNotifier(notifier); err != nil {
    70  		panic(fmt.Sprintf("failed to register notifier driver '%s': %v",
    71  			notifierType, err))
    72  	}
    73  }