mirror of
				https://github.com/ChristopherA/Learning-Bitcoin-from-the-Command-Line.git
				synced 2025-10-31 10:27:29 +00:00 
			
		
		
		
	linked source code
This commit is contained in:
		
							parent
							
								
									14c0cb9990
								
							
						
					
					
						commit
						fb90defb37
					
				| @ -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 }'" | 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? | 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`. | ||||||
| 
 |  | ||||||
| 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. |  | ||||||
| 
 | 
 | ||||||
| > :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. | > :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: | Be sure the permissions on the script are right: | ||||||
| ``` | ``` | ||||||
| $ chmod 755 txfee-calc.sh | $ chmod 755 txfee-calc.sh | ||||||
| @ -426,3 +403,12 @@ You may also want to create an alias: | |||||||
| ``` | ``` | ||||||
| alias btctxfee="~/txfee-calc.sh" | 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). | ||||||
|  | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user