Skip to content

Creating Transactions

Chris Priest edited this page Nov 7, 2017 · 1 revision

There is a wrapper class that helps you make transactions. Here is how to use it:

>>> from moneywagon.tx import Transaction
>>> tx = Transaction('btc')
>>> tx.add_inputs(private_key='KxDwaDis...')
>>> tx.add_output('1Fs3...', 1.42, unit='btc')
>>> tx.fee(4000, unit='satoshi') #defaut is 10000
>>> tx.get_hex() # call this method to see the tx in hex format
'00100137876876...
>>> tx.push()

You can pass in a paranoid parameter to the Transaction constructor that will make all external service calls cross checked. By default, all service calls are only performed once. You can increase this value to get more assurance that your blockchain source has not been compromised.

>>> tx = Transaction('btc', paranoid=2)

Or if you want more fine control over which inputs go in:

>>> my_inputs = get_unpent_outputs('1PZ3Ps9Rv...')[:2] # just the first two
>>> tx.add_raw_inputs(my_inputs, 'KdEr5D1a...')
>>> more_inputs = [x for x in get_unpent_outputs('1HWpyFJ7N...') if x['amount'] < 10000]]
>>> tx.add_raw_inputs(more_inputs, 'KxDwaDis...')
>>> tx.add_output('1Fd3...', 1.42, unit='btc')
>>> tx.push()

The last input that is added (either through add_raw_inputs or add_inputs) will be used as the change address. You can manually specify a change address by modifying the value of tx.change_address before calling tx.push().

>>> tx.add_inputs(address='1HWpyFJ7N...', private_key='KxDwaDis...')
>>> tx.add_output('1Fd3...', 1.42, unit='btc')
>>> tx.change_address = '1PZ3Ps9Rv...' # replace change address from 1HWpyFJ... -> 1PZ3Ps9Rv...
>>> tx.push()

The private key argument should be a string in hex format. You can also specify the amount argument to add_output with a unit argument:

>>> tx.add_output(address, amount=1423, unit='bits')
>>> tx.add_output(address2, amount=1.3, units="usd")

All exchange rates are taken from the get_current_price function defined above.

Currently there is no way to decode transactions using moneywagon. One day this feature will get added.

You can also make unsigned transactions by passing in just the address to the add_inputs function. You must also pass in signed=False to the get_hex function. This hex can't be pushed to the network until it has been signed.

>>> tx.add_inputs(address='1HWpyFJ7N...')
>>> tx.add_output('1Fd3...', 1.42, unit='btc')
>>> tx.get_hex(signed=False)
'00100137876876...

Push Transaction

If you have a raw transaction that you would like to push to the bitcoin network, you can use moneywagon to do that:

>>> from moneywagon import PushTx
>>> PushTx().push('btc', '0100000001d992c7a88...')

If the transaction went through successfully, the push method will return nothing. This functionality works much like the others. If one service is down, it falls back to another service.

Fee Estimation

Moneywagon can be used to get the optimal fee to use for a transaction based on the current state of the network.

>>> from moneywagon import get_optimal_fee
>>> get_optimal_fee('btc', tx_bytes=213)
10650

In the above example, a transaction that is 213 bytes that is to be confirmed in the next block, will need a fee of 10650 satoshis.

Currently, btc is the only currency supported for fee estimation.

Clone this wiki locally