linked source code

This commit is contained in:
Shannon Appelcline 2020-10-20 13:08:04 -10:00 committed by GitHub
parent 14c0cb9990
commit fb90defb37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -383,36 +383,13 @@ Any time you're looking through a large mass of information in a JSON object out
```
alias btcunspent="bitcoin-cli listunspent | jq -r '.[] | { txid: .txid, vout: .vout, amount: .amount }'"
```
## Summary: Using JQ
JQ makes it easy to extract information from JSON arrays and objects. It can also be used in shell scripts for fairly complex calculations that will make your life easier.
## Run The Transaction Fee Script
## What's Next?
Continue "Sending Bitcoin Transactions" with [§4.3 Creating a Raw Transaction with Named Arguments](04_3_Creating_a_Raw_Transaction_with_Named_Arguments.md).
## Appendix: The Transaction Fee Script
The following script runs the "Fee Calculation" from the above example.
The [Fee Calculation Script](src/04_2_i_txfee-calc.sh) is available in src-code directory. You can download it and save it as `txfee-calc.sh`.
> :warning: **WARNING:** This script has not been robustly checked. If you are going to use it to verify real transaction fees you should only do it as a triple-check after you've already done all the math yourself.
```
file: txfee-calc.sh
#!/bin/bash
if [ -z $1 ];
then
echo "You must include the raw transaction hex as an argument.";
exit;
fi
usedtxid=($(bitcoin-cli decoderawtransaction $1 | jq -r '.vin | .[] | .txid'))
usedvout=($(bitcoin-cli decoderawtransaction $1 | jq -r '.vin | .[] | .vout'))
btcin=$(for ((i=0; i<${#usedtxid[*]}; i++)); do txid=${usedtxid[i]}; vout=${usedvout[i]}; bitcoin-cli listunspent | jq -r '.[] | select (.txid | contains("'${txid}'")) | select(.vout | contains('$vout')) | .amount'; done | awk '{s+=$1} END {print s}')
btcout=$(bitcoin-cli decoderawtransaction $1 | jq -r '.vout [] | .value' | awk '{s+=$1} END {print s}')
echo "$btcin-$btcout"| /usr/bin/bc
```
Be sure the permissions on the script are right:
```
$ chmod 755 txfee-calc.sh
@ -426,3 +403,12 @@ You may also want to create an alias:
```
alias btctxfee="~/txfee-calc.sh"
```
## Summary: Using JQ
JQ makes it easy to extract information from JSON arrays and objects. It can also be used in shell scripts for fairly complex calculations that will make your life easier.
## What's Next?
Continue "Sending Bitcoin Transactions" with [§4.3 Creating a Raw Transaction with Named Arguments](04_3_Creating_a_Raw_Transaction_with_Named_Arguments.md).