BitcoinTalk
Proposed change to sendtoaddress API call

View Satoshi only

External link

I'm proposing one small change to Bitcoin's JSON-RPC api:  return a transaction ID when Bitcoins are successfully sent.

Why?  Because I want to keep a complete audit trail for any coins going into or coming out of my application's wallet; I want to keep track of the particular transactions in the bitcoin network that correspond to actions my application takes.  The alternative is to call sendtoaddress and then call listtransactions, but that won't work properly if two similar transactions (same amount to same address) occur at about the same time.

So I propose doing the simplest thing possible: modify the JSON-RPC sendtoaddress call so it returns the string 'sent:' followed by the 256-bit-hexadecimal transactions id.

This could break any applications that look for exactly the string 'sent' (which is what sendtoaddress does now).  The fix would be to modify the apps to see if the string began with 'sent'.

Alternatives I thought about but think I don't like:
 + make it a new api call so old apps do not break (sendtoaddress2 ? yuck)
 + return just the transaction id on successful send instead of 'sent:...'
 + return an array with more information (maybe [ "tx_id": "...", "fee" : 0.0 ] )

Comments/criticisms?

100% agreed with your suggestion, about the need for txn_id association.

However, it seems better to avoid API breakage, because this API is so heavily used.  It is almost guaranteed that anyone automating bitcoins is using sendtoaddress.

Either create a "send2" RPC call, or add an "[extended-JSON=0]" parameter to the existing sendtoaddress RPC call, or something similar that preserves existing application data flows.


Just add a flag (like true/false) to the function with the default "false" so it works with older uses of the function call.

Like

sendtoaddress <bitcoinaddress> <amount> [comment] [comment-to] [return transaction ID, default false]

So
sendtoaddress 1FtDzyajiHKa9QbXiNxqztB 1.00 "Blah Comment" "Blah From" true
returns
sent XYZ....

But
sendtoaddress 1FtDzyajiHKa9QbXiNxqztB 1.00 "Blah Comment" "Blah From"
returns
sent
Also without having to break compatibility.

I think it would be a handy addition.
RE: adding a flag:  great idea!

If you set the flag, I don't see any reason to prepend 'sent' to the transaction ID; better to just return the transaction ID on successful send.

Patches:
Code:
diff --git a/rpc.cpp b/rpc.cpp
index 920fe90..8714b7e 100644
--- a/rpc.cpp
+++ b/rpc.cpp
@@ -342,10 +342,11 @@ Value getaddressesbylabel(const Array& params, bool fHelp)
 
 Value sendtoaddress(const Array& params, bool fHelp)
 {
-    if (fHelp || params.size() < 2 || params.size() > 4)
+    if (fHelp || params.size() < 2 || params.size() > 5)
         throw runtime_error(
-            "sendtoaddress <bitcoinaddress> <amount> [comment] [comment-to] "
-            "<amount> is a real and is rounded to the nearest 0.01");
+            "sendtoaddress <bitcoinaddress> <amount> [comment] [comment-to] [return-tx-id-flag] "
+            "<amount> is a real and is rounded to the nearest 0.01 "
+            "returns string 'sent' if return-tx-id-flag is false (default), otherwise returns transaction id.");
 
     string strAddress = params[0].get_str();
 
@@ -361,9 +362,15 @@ Value sendtoaddress(const Array& params, bool fHelp)
     if (params.size() > 3 && params[3].type() != null_type && !params[3].get_str().empty())
         wtx.mapValue["to"]      = params[3].get_str();
 
+    bool fReturnTxID = false;
+    if (params.size() > 4)
+        fReturnTxID = params[4].get_bool();
+
     string strError = SendMoneyToBitcoinAddress(strAddress, nAmount, wtx);
     if (strError != "")
         throw runtime_error(strError);
+    if (fReturnTxID)
+        return wtx.GetHash().ToString();
     return "sent";
 }
 
@@ -1103,6 +1110,7 @@ int CommandLineRPC(int argc, char *argv[])
         if (strMethod == "setgenerate"            && n > 0) ConvertTo<bool>(params[0]);
         if (strMethod == "setgenerate"            && n > 1) ConvertTo<boost::int64_t>(params[1]);
         if (strMethod == "sendtoaddress"          && n > 1) ConvertTo<double>(params[1]);
+        if (strMethod == "sendtoaddress"          && n > 4) ConvertTo<bool>(params[4]);
         if (strMethod == "listtransactions"       && n > 0) ConvertTo<boost::int64_t>(params[0]);
         if (strMethod == "listtransactions"       && n > 1) ConvertTo<bool>(params[1]);
         if (strMethod == "getamountreceived"      && n > 1) ConvertTo<boost::int64_t>(params[1]); // deprecated

What happens when we desire to return additional information, beyond tx-id?

For the sake of future compatibility, it seems like the flag should present a choice between returning (a) just the current 'sent', or (b) a JSON map containing tx-id, and perhaps other things.

What happens when we desire to return additional information, beyond tx-id?

For the sake of future compatibility, it seems like the flag should present a choice between returning (a) just the current 'sent', or (b) a JSON map containing tx-id, and perhaps other things.
A 'gettransaction tx_id' API call is on my short list.

What do other folks think; should sendtoaddress .... true   return just the tx_id, and you have to make another API call to get details if you need them?
Or should it return an Array?

Do you need 'gettransaction', given the existence of 'getblock'?

It's too soon to start junking up the API for backward compatibility at all costs.

Just return "<txid>".