0

experimenting with JSON RPC API using bitcoinrpc.authproxy for python.

so far it works in all instances accept when i try to create a raw transaction. somewhere along the way im passing parameters in wrong format.

i created helper methods to create my inputs/outputs like this:

def getTransactionJsonInput(self, sourcetransaction) -> str: 
    return {"txid": sourcetransaction['txid'] ,"vout":sourcetransaction['vout']}

def getTransactionJsonOutput(self, new_address, amount, returnaddress, returnamount) -> str: 
    return {
        new_address: amount,
        returnaddress: returnamount
    }

and when i print my call to createrawtransaction it appears to be correct

    print('TRANSACTION inputs', [input], type([input]))
    print('TRANSACTION outputs', output, type(output))
    return svc.createrawtransaction(input, output)

TRANSACTION inputs [{'txid': 'sourcetransactionid', 'vout': 1}] <class 'list'>

TRANSACTION outputs {'destinationaddress': 0.012, 'changeaddress': 2.56559703} <class 'dict'>

error:

bitcoinrpc.authproxy.JSONRPCException: -3: Expected type array, got object

solution

thanks to @m1xolyd1an i was steered in the right direction. All that was missing in my implementation was adding [] around inputs. i added them in print statement but not in the actual call!

before

return svc.createrawtransaction(input, output)

after

return svc.createrawtransaction([input], output)

new result:

new transaction: 0200000001005134d493cb536a5000b491b0b6dbca6c5187eec67ffb8769fdedac9ec25db40100000000ffffffff02804f12000000000017a914190150e1a961a2509b1d48c4d6cf6b644454cd968757ca4a0f0000000017a91496711dceda7240a6c492b2fabc5d44ab5d4bd19e8700000000

2
  • I think you should use double quotes in input like [{"txid":"id", "vout":n}] Sep 1, 2018 at 16:53
  • @AbhishekSinha i tried that initially, but then it was '"xxxx"' also the error was the same
    – Sonic Soul
    Sep 1, 2018 at 16:54

1 Answer 1

2

You are providing it an Object in JSON, not an array just as the error states.

 return {"txid": sourcetransaction['txid'] ,"vout":sourcetransaction['vout']}

I would try creating it from a dictionary and then adding it to a list instead of building it from a string, something like:

>>> inputs = {}   #create the dictionary
>>> inputs["txid"]=sourcetransaction['txid']  #add the value to the dictionary key 'txid'
>>> inputs["vout"]=sourcetransaction['vout']  #add nout value to key 'vout'  

>>> inputs
{'txid': 'a9d4599e15b53f3eb531608ddb31f48c695c3d0b3538a6bda871e8b34f2f430c', 'vout': 0}

>>> inputsList=[inputs] #add dictionary input to list 
>>> inputsList
[{'txid': 'a9d4599e15b53f3eb531608ddb31f48c695c3d0b3538a6bda871e8b34f2f430c', 'vout': 0}]

Also noted in the example from the source:

def createrawtransaction(self, inputs, outputs):
    """
    Creates a raw transaction spending given inputs
    (a list of dictionaries, each containing a transaction id and an output number),
    sending to given address(es).
    Returns hex-encoded raw transaction.
    Example usage:
    >>> conn.createrawtransaction(
            [{"txid": "a9d4599e15b53f3eb531608ddb31f48c695c3d0b3538a6bda871e8b34f2f430c",
              "vout": 0}],
            {"mkZBYBiq6DNoQEKakpMJegyDbw2YiNQnHT":50})
    Arguments:
    - *inputs* -- A list of {"txid": txid, "vout": n} dictionaries.
    - *outputs* -- A dictionary mapping (public) addresses to the amount
                   they are to be paid.
    """
    return self.proxy.createrawtransaction(inputs, outputs)
2
  • thanks! yeah i figured im just still clueless how python typing works .. :) trying it this way
    – Sonic Soul
    Sep 1, 2018 at 19:39
  • thanks! looks like the string is still ok, i just forgot to include []'s around input when calling the actual api
    – Sonic Soul
    Sep 1, 2018 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.