Step 3. Create Raw Transaction
Here’s where the fun part happens.
Explanation
The transaction output we found has 0.050 BTC we can spend. We want to send 0.030 BTC to another wallet address.
Remember that we have to spend the whole output from a transaction and we will get the change back to our wallet (the wallet from which the value originated).
Finally, we also have to pay a transaction fee. In this case, we’ll pay 0.0005 BTC in fees. This fee will be reduced from our change (0.020 BTC - 0.0005 BTC = 0.0195 BTC).
To summarize:
Sum(Inputs) - Sum(Outputs) = Transaction Fee
(0.050 BTC) - (0.030 BTC + 0.0195 BTC) = 0.0005 BTC
In order for change to return back to us, we will need to create two outputs in our transaction, 0.30 and 0.0195 BTC, which will go in the"address":amount
parameters. The remainder, 0.0005 BTC, will be the transaction fee.
Command
We will use thecreaterawtransaction command:
Command Parameters:
createrawtransaction [{"txid":"id","vout":n},...] {"address":amount,"data":"hex",...} ( locktime ) ( replaceable )
Arguments:
1. "inputs" (array, required) A json array of json objects
[
{
"txid":"id", (string, required) The transaction id
"vout":n, (numeric, required) The output number
"sequence":n (numeric, optional) The sequence number
}
,...
]
2. "outputs" (object, required) a json object with outputs
{
"address": x.xxx, (numeric or string, required) The key is the bitcoin address, the numeric value (can be string) is the BTC amount
"data": "hex" (string, required) The key is "data", the value is hex encoded data
,...
}
3. locktime (numeric, optional, default=0) Raw locktime. Non-0 value also locktime-activates inputs
4. replaceable (boolean, optional, default=false) Marks this transaction as BIP125 replaceable.
Allows this transaction to be replaced by a transaction with higher fees. If provided, it is an error if explicit sequence numbers are incompatible.
Result:
"transaction" (string) hex string of the transaction
### Build Out Command
Let’s build out the command. To help us better visualize it, I’ll gather the parameter values and build out the command in a separate text editor Visual Studio:
// CREATE RAW TRANSACTION
TXID = Get this from listunspent command
VOUT = Get this from listunspent command
ADDRESS = Where will this amount be sent
AMOUNT = How much BTC will be sent
// Use the values you’ve collected above to fill in the createrawtransaction command
createrawtransaction '[{"txid":"TXID","vout": VOUT}]''{"ADDRESS":AMOUNT}'
Execute Command
createrawtransaction '[{"txid": "e787a27bda32c8b54ee501be46d2cfcd47c1566c8ef6ee339bdb7cd5c82b701c", "vout":0}]' '{"2NFK8YHKT6hPPTDKTPP3c5bx7oPGrYhzj2y":0.030, "2Mzxx8wGAmQQyCCrb2vXP4yxaYY9s9nepfy":0.0195}'
Note: As mentioned above, this example has two outputs. The first is to send 0.030 btc to the receiving address. The second is to send 0.0195 btc as change back to the original wallet address.
Result
02000000011c702bc8d57cdb9b33eef68e6c56c147cdcfd246be01e54eb5c832da7ba287e70000000000ffffffff02c0c62d000000000017a914f20fe211102535e3c37bb0e659099387ddc035b58730c11d000000000017a91454ad1e8953876c90d3fc15798c687835ab3d3aee8700000000
The output of createrawtransaction was a raw hex string of the transaction.