add SendPairs helper function to wallet package
This commit is contained in:
parent
d714bf3310
commit
a883c96aa5
2 changed files with 57 additions and 43 deletions
57
rpcserver.go
57
rpcserver.go
|
@ -2582,53 +2582,30 @@ func LockUnspent(w *wallet.Wallet, chainSvr *chain.Client, icmd interface{}) (in
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// sendPairs is a helper routine to reduce duplicated code when creating and
|
// sendPairs creates and sends payment transactions.
|
||||||
// sending payment transactions. It returns the transaction hash in string
|
// It returns the transaction hash in string format upon success
|
||||||
// format upon success.
|
// All errors are returned in btcjson.RPCError format
|
||||||
func sendPairs(w *wallet.Wallet, chainSvr *chain.Client, cmd interface{},
|
func sendPairs(w *wallet.Wallet, amounts map[string]btcutil.Amount,
|
||||||
amounts map[string]btcutil.Amount, account uint32, minconf int32) (string, error) {
|
account uint32, minconf int32) (string, error) {
|
||||||
|
txSha, err := w.SendPairs(amounts, account, minconf)
|
||||||
// Create transaction, replying with an error if the creation
|
|
||||||
// was not successful.
|
|
||||||
createdTx, err := w.CreateSimpleTx(account, amounts, minconf)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
switch {
|
if err == wallet.ErrNonPositiveAmount {
|
||||||
case err == wallet.ErrNonPositiveAmount:
|
|
||||||
return "", ErrNeedPositiveAmount
|
return "", ErrNeedPositiveAmount
|
||||||
case isManagerLockedError(err):
|
}
|
||||||
|
if isManagerLockedError(err) {
|
||||||
return "", &ErrWalletUnlockNeeded
|
return "", &ErrWalletUnlockNeeded
|
||||||
}
|
}
|
||||||
|
switch err.(type) {
|
||||||
|
case btcjson.RPCError:
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create transaction record and insert into the db.
|
return "", &btcjson.RPCError{
|
||||||
rec, err := wtxmgr.NewTxRecordFromMsgTx(createdTx.MsgTx, time.Now())
|
Code: btcjson.ErrRPCInternal.Code,
|
||||||
if err != nil {
|
Message: err.Error(),
|
||||||
log.Errorf("Cannot create record for created transaction: %v", err)
|
|
||||||
return "", btcjson.ErrRPCInternal
|
|
||||||
}
|
|
||||||
err = w.TxStore.InsertTx(rec, nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("Error adding sent tx history: %v", err)
|
|
||||||
return "", btcjson.ErrRPCInternal
|
|
||||||
}
|
|
||||||
|
|
||||||
if createdTx.ChangeIndex >= 0 {
|
|
||||||
err = w.TxStore.AddCredit(rec, nil, uint32(createdTx.ChangeIndex), true)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("Error adding change address for sent "+
|
|
||||||
"tx: %v", err)
|
|
||||||
return "", btcjson.ErrRPCInternal
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: The record already has the serialized tx, so no need to
|
|
||||||
// serialize it again.
|
|
||||||
txSha, err := chainSvr.SendRawTransaction(&rec.MsgTx, false)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
txShaStr := txSha.String()
|
txShaStr := txSha.String()
|
||||||
log.Infof("Successfully sent transaction %v", txShaStr)
|
log.Infof("Successfully sent transaction %v", txShaStr)
|
||||||
return txShaStr, nil
|
return txShaStr, nil
|
||||||
|
@ -2673,7 +2650,7 @@ func SendFrom(w *wallet.Wallet, chainSvr *chain.Client, icmd interface{}) (inter
|
||||||
cmd.ToAddress: amt,
|
cmd.ToAddress: amt,
|
||||||
}
|
}
|
||||||
|
|
||||||
return sendPairs(w, chainSvr, cmd, pairs, account, minConf)
|
return sendPairs(w, pairs, account, minConf)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendMany handles a sendmany RPC request by creating a new transaction
|
// SendMany handles a sendmany RPC request by creating a new transaction
|
||||||
|
@ -2714,7 +2691,7 @@ func SendMany(w *wallet.Wallet, chainSvr *chain.Client, icmd interface{}) (inter
|
||||||
pairs[k] = amt
|
pairs[k] = amt
|
||||||
}
|
}
|
||||||
|
|
||||||
return sendPairs(w, chainSvr, cmd, pairs, account, minConf)
|
return sendPairs(w, pairs, account, minConf)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendToAddress handles a sendtoaddress RPC request by creating a new
|
// SendToAddress handles a sendtoaddress RPC request by creating a new
|
||||||
|
@ -2750,7 +2727,7 @@ func SendToAddress(w *wallet.Wallet, chainSvr *chain.Client, icmd interface{}) (
|
||||||
}
|
}
|
||||||
|
|
||||||
// sendtoaddress always spends from the default account, this matches bitcoind
|
// sendtoaddress always spends from the default account, this matches bitcoind
|
||||||
return sendPairs(w, chainSvr, cmd, pairs, waddrmgr.DefaultAccountNum, 1)
|
return sendPairs(w, pairs, waddrmgr.DefaultAccountNum, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetTxFee sets the transaction fee per kilobyte added to transactions.
|
// SetTxFee sets the transaction fee per kilobyte added to transactions.
|
||||||
|
|
|
@ -1591,13 +1591,50 @@ func (w *Wallet) TotalReceivedForAddr(addr btcutil.Address, minConf int32) (btcu
|
||||||
return amount, err
|
return amount, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SendPairs creates and sends payment transactions. It returns the transaction
|
||||||
|
// hash upon success
|
||||||
|
func (w *Wallet) SendPairs(amounts map[string]btcutil.Amount, account uint32,
|
||||||
|
minconf int32) (*wire.ShaHash, error) {
|
||||||
|
|
||||||
|
// Create transaction, replying with an error if the creation
|
||||||
|
// was not successful.
|
||||||
|
createdTx, err := w.CreateSimpleTx(account, amounts, minconf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create transaction record and insert into the db.
|
||||||
|
rec, err := wtxmgr.NewTxRecordFromMsgTx(createdTx.MsgTx, time.Now())
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Cannot create record for created transaction: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = w.TxStore.InsertTx(rec, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Error adding sent tx history: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if createdTx.ChangeIndex >= 0 {
|
||||||
|
err = w.TxStore.AddCredit(rec, nil, uint32(createdTx.ChangeIndex), true)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Error adding change address for sent "+
|
||||||
|
"tx: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: The record already has the serialized tx, so no need to
|
||||||
|
// serialize it again.
|
||||||
|
return w.chainSvr.SendRawTransaction(&rec.MsgTx, false)
|
||||||
|
}
|
||||||
|
|
||||||
// Open loads an already-created wallet from the passed database and namespaces.
|
// Open loads an already-created wallet from the passed database and namespaces.
|
||||||
func Open(pubPass []byte, params *chaincfg.Params, db walletdb.DB, waddrmgrNS, wtxmgrNS walletdb.Namespace, cbs *waddrmgr.OpenCallbacks) (*Wallet, error) {
|
func Open(pubPass []byte, params *chaincfg.Params, db walletdb.DB, waddrmgrNS, wtxmgrNS walletdb.Namespace, cbs *waddrmgr.OpenCallbacks) (*Wallet, error) {
|
||||||
addrMgr, err := waddrmgr.Open(waddrmgrNS, pubPass, params, cbs)
|
addrMgr, err := waddrmgr.Open(waddrmgrNS, pubPass, params, cbs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
txMgr, err := wtxmgr.Open(wtxmgrNS)
|
txMgr, err := wtxmgr.Open(wtxmgrNS)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !wtxmgr.IsNoExists(err) {
|
if !wtxmgr.IsNoExists(err) {
|
||||||
|
|
Loading…
Reference in a new issue