Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: Ayakashi on November 21, 2013, 10:09:35 PM



Title: JSON RPC named parameters?
Post by: Ayakashi on November 21, 2013, 10:09:35 PM
I'm doing some work building yet another language-specific wrapper for BitcoinD's JSON-RPC requests.  I'm working from the wiki documentation at https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_calls_list (https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_calls_list).

I'm trying to understand the proper and best way to provide both required and optional parameters.

For example, this request body is valid and produces the response that I expect:
{"jsonrpc":"2.0","id":"1","method":"gettransaction","params":["315a1e40465591c5cbbbeab3318c57fb1b2cfa21e9f8ad297dcf1bc58d560dcd"]}

However, the parameter in this case is not named.  In this specific method, it's not such of a big deal since there is only one required parameter. But if you look at some of the more complex calls that have multiple parameters such as listreceivedbyaccount or move, sendmany, etc. it would seem that naming parameters would be more important.

By naming parameters, I'm talking about supplying both the parameter name and it's value to the server. 

Something like this, although this *does not work* (so don't use it):
{"jsonrpc":"2.0","id":"1","method":"gettransaction","params":[{"txid":"315a1e40465591c5cbbbeab3318c57fb1b2cfa21e9f8ad297dcf1bc58d560dcd"}]}

Have I just got the expected format wrong?  Is there any way to provide the specific parameter name and value to the server to be very specific? Or is it just positional order that is important?

thanks


Title: Re: JSON RPC named parameters?
Post by: wumpus on November 22, 2013, 01:34:49 PM
The positional order is important, you cannot use named parameters.


Title: Re: JSON RPC named parameters?
Post by: Ayakashi on November 22, 2013, 06:24:51 PM
Thanks John.

This introduces some questions when optional parameters are not supplied.  I'm sure I'm not fully grasping yet how to deal with these and I'd appreciate any feedback.

Consider the following example:

Suppose I want to get the balance for all accounts in a wallet.  This request works as expected, returning the combined balance from all accounts with a confirmation count of more than 1 (the default):

{"jsonrpc":"2.0","id":"1","method":"getbalance","params":[]}

But, suppose I wanted to get a combined balance for the wallet for all accounts with a confirmation count of more than 2?

{"jsonrpc":"2.0","id":"1","method":"getbalance","params":[null, 2]} - this fails
{"jsonrpc":"2.0","id":"1","method":"getbalance","params":[,2]} - this fails
{"jsonrpc":"2.0","id":"1","method":"getbalance","params":[2]} - this fails
{"jsonrpc":"2.0","id":"1","method":"getbalance","params":["", 2]} - this succeeds, but it doesn't deliver what we want, because it's really returning the default "" account and not a combined total for all accounts.

I can't figure out any other combinations to supply.  Any pointers appreciated.


Title: Re: JSON RPC named parameters?
Post by: kjj on November 23, 2013, 01:30:19 AM
*


Title: Re: JSON RPC named parameters?
Post by: wumpus on November 23, 2013, 10:31:42 AM
Suppose I want to get the balance for all accounts in a wallet.  This request works as expected, returning the combined balance from all accounts with a confirmation count of more than 1 (the default):

{"jsonrpc":"2.0","id":"1","method":"getbalance","params":[]}

But, suppose I wanted to get a combined balance for the wallet for all accounts with a confirmation count of more than 2?
Optional arguments can only be left out at the end. This is similar to the C++ convention.

You need to provide the default value for former arguments if you want to change latter arguments from their default.
(sometimes this is "", sometimes "*", sometimes null, this depends on the specific command)

But it's not very consistent.

For example in the case of getbalance, the zero-argument case takes a shortcut that calls pwalletMain->GetBalance() directly, which doesn't take a minimum amount of confirmations.

https://github.com/bitcoin/bitcoin/blob/master/src/rpcwallet.cpp#L705

You can however provide "*" as account name, which will do a GetBalance-like computation over all acccounts.



Title: Re: JSON RPC named parameters?
Post by: Ayakashi on November 23, 2013, 10:18:55 PM
Thanks John, that clears it up nicely.  Also appreciate the link to the right source page... very helpful.