github.com/cerberus-wallet/blockbook@v0.3.2/bchain/coins/qtum/qtumrpc.go (about)

     1  package qtum
     2  
     3  import (
     4  	"blockbook/bchain"
     5  	"blockbook/bchain/coins/btc"
     6  	"encoding/json"
     7  	"math/big"
     8  
     9  	"github.com/golang/glog"
    10  )
    11  
    12  // QtumRPC is an interface to JSON-RPC bitcoind service.
    13  type QtumRPC struct {
    14  	*btc.BitcoinRPC
    15  	minFeeRate *big.Int // satoshi per kb
    16  }
    17  
    18  // NewQtumRPC returns new QtumRPC instance.
    19  func NewQtumRPC(config json.RawMessage, pushHandler func(bchain.NotificationType)) (bchain.BlockChain, error) {
    20  	b, err := btc.NewBitcoinRPC(config, pushHandler)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  
    25  	s := &QtumRPC{
    26  		b.(*btc.BitcoinRPC),
    27  		big.NewInt(400000),
    28  	}
    29  	s.RPCMarshaler = btc.JSONMarshalerV1{}
    30  	s.ChainConfig.SupportsEstimateSmartFee = true
    31  
    32  	return s, nil
    33  }
    34  
    35  // Initialize initializes QtumRPC instance.
    36  func (b *QtumRPC) Initialize() error {
    37  	ci, err := b.GetChainInfo()
    38  	if err != nil {
    39  		return err
    40  	}
    41  	chainName := ci.Chain
    42  
    43  	params := GetChainParams(chainName)
    44  
    45  	// always create parser
    46  	b.Parser = NewQtumParser(params, b.ChainConfig)
    47  
    48  	// parameters for getInfo request
    49  	if params.Net == MainnetMagic {
    50  		b.Testnet = false
    51  		b.Network = "livenet"
    52  	} else {
    53  		b.Testnet = true
    54  		b.Network = "testnet"
    55  	}
    56  
    57  	glog.Info("rpc: block chain ", params.Name)
    58  
    59  	return nil
    60  }
    61  
    62  // GetTransactionForMempool returns a transaction by the transaction ID
    63  // It could be optimized for mempool, i.e. without block time and confirmations
    64  func (b *QtumRPC) GetTransactionForMempool(txid string) (*bchain.Tx, error) {
    65  	return b.GetTransaction(txid)
    66  }
    67  
    68  // EstimateSmartFee returns fee estimation
    69  func (b *QtumRPC) EstimateSmartFee(blocks int, conservative bool) (big.Int, error) {
    70  	feeRate, err := b.BitcoinRPC.EstimateSmartFee(blocks, conservative)
    71  	if err != nil {
    72  		if b.minFeeRate.Cmp(&feeRate) == 1 {
    73  			feeRate = *b.minFeeRate
    74  		}
    75  	}
    76  	return feeRate, err
    77  }