implemented ListReceivedByAccount

This commit is contained in:
Javed Khan 2014-10-13 18:59:47 +05:30 committed by Josh Rickmar
parent 9ab659cda3
commit 3f99ed233f

View file

@ -1348,6 +1348,7 @@ var rpcHandlers = map[string]requestHandler{
"keypoolrefill": KeypoolRefill, "keypoolrefill": KeypoolRefill,
"listaccounts": ListAccounts, "listaccounts": ListAccounts,
"listlockunspent": ListLockUnspent, "listlockunspent": ListLockUnspent,
"listreceivedbyaccount": ListReceivedByAccount,
"listreceivedbyaddress": ListReceivedByAddress, "listreceivedbyaddress": ListReceivedByAddress,
"listsinceblock": ListSinceBlock, "listsinceblock": ListSinceBlock,
"listtransactions": ListTransactions, "listtransactions": ListTransactions,
@ -1371,7 +1372,6 @@ var rpcHandlers = map[string]requestHandler{
"getwalletinfo": Unimplemented, "getwalletinfo": Unimplemented,
"importwallet": Unimplemented, "importwallet": Unimplemented,
"listaddressgroupings": Unimplemented, "listaddressgroupings": Unimplemented,
"listreceivedbyaccount": Unimplemented,
"move": Unimplemented, "move": Unimplemented,
"setaccount": Unimplemented, "setaccount": Unimplemented,
@ -2023,6 +2023,53 @@ func ListLockUnspent(w *Wallet, chainSvr *chain.Client, icmd btcjson.Cmd) (inter
return w.LockedOutpoints(), nil return w.LockedOutpoints(), nil
} }
// ListReceivedByAccount handles a listreceivedbyaccount request by returning
// a slice of objects, each one containing:
// "account": the receiving account;
// "amount": total amount received by the account;
// "confirmations": number of confirmations of the most recent transaction.
// It takes two parameters:
// "minconf": minimum number of confirmations to consider a transaction -
// default: one;
// "includeempty": whether or not to include addresses that have no transactions -
// default: false.
// Since btcwallet doesn't implement account support yet, only the default account ""
// will be returned
func ListReceivedByAccount(w *Wallet, chainSvr *chain.Client, icmd btcjson.Cmd) (interface{}, error) {
cmd := icmd.(*btcjson.ListReceivedByAccountCmd)
bs, err := w.SyncedChainTip()
if err != nil {
return nil, err
}
// Total amount received.
var amount btcutil.Amount
// Number of confirmations of the last transaction.
var confirmations int32
for _, record := range w.TxStore.Records() {
for _, credit := range record.Credits() {
if !credit.Confirmed(cmd.MinConf, bs.Height) {
// Not enough confirmations, skip the current block.
continue
}
amount += credit.Amount()
confirmations = credit.Confirmations(bs.Height)
}
}
ret := []btcjson.ListReceivedByAccountResult{
{
Account: "",
Amount: amount.ToUnit(btcutil.AmountBTC),
Confirmations: uint64(confirmations),
},
}
return ret, nil
}
// ListReceivedByAddress handles a listreceivedbyaddress request by returning // ListReceivedByAddress handles a listreceivedbyaddress request by returning
// a slice of objects, each one containing: // a slice of objects, each one containing:
// "account": the account of the receiving address; // "account": the account of the receiving address;