Query Chainweb transfer events within a block range for a given chain.
Usage example:
CHAIN=1 MINHEIGHT=1420000 MAXHEIGHT=14201000 bash ./tranfers.sh
Pact events were introduced at block height 1138000.
# ############################################################################ # | |
# Base64 | |
def base64dUrl: . | |
| gsub("(?<x>[-_])"; if .x == "-" then "+" else "/" end) | |
| "\(.)===" | |
| @base64d | |
; | |
def fromjson64: . | |
| base64dUrl | |
| fromjson | |
; | |
# ############################################################################ # | |
# Chainweb Debugging tools | |
# Parse Payload With Outputs | |
def pWo: . | |
| .minerData = (.minerData | fromjson64) | |
| .coinbase = (.coinbase | fromjson64) | |
| .transactions = | |
[ .transactions[] | |
| | |
{ tx: (.[0] | fromjson64 | .cmd = (.cmd | fromjson)) | |
, result: (.[1] | fromjson64) | |
} | |
] | |
; | |
# Extra Transfer Events | |
# (Note that Pact events are avaiable only for block heights >= 1138000) | |
def transfers: . | |
| .transactions[] | |
| .tx.cmd.meta.creationTime as $creationTime | |
| .tx.cmd.meta.chainId as $chainId | |
| .tx.hash as $requestKey | |
| .result.events[]? | |
| select(.name == "TRANSFER") | |
| | |
{ sender: .params[0] | |
, receiver: .params[1] | |
, amount: .params[2] | |
, module: .module | |
, creationTime: $creationTime | |
, requestKey: $requestKey | |
, chainId: $chainId | |
} | |
; |
#!/bin/bash | |
# ############################################################################ # | |
# Collect all transfers within a given block height range. | |
# NOTE that Pact events are avaiable only for block heights >= 1138000. | |
CHAIN=${CHAIN:-0} | |
URL="https://api.chainweb.com/chainweb/0.0/mainnet01" | |
# ############################################################################ # | |
# Get current latest block | |
CUR=$(curl -sL "$URL/cut" | jq '.hashes."'"$CHAIN"'"') | |
CUR_HEIGHT=$(echo "$CUR" | jq -r '.height') | |
CUR_HASH=$(echo "$CUR" | jq -r '.hash') | |
# ############################################################################ # | |
# Setup MINHEIGHT and MAXHEIGHT parameters | |
# Block Height Range | |
# Note that chainweb API calls have different maximum page size limits | |
[[ -n "$MAXHEIGHT" ]] || MAXHEIGHT=$(( CUR_HEIGHT )) | |
[[ -n "$MINHEIGHT" ]] || MINHEIGHT=$(( MAXHEIGHT - 100 )) | |
{ | |
echo "CUR_HEIGHT: $CUR_HEIGHT" | |
echo "CUR_HASH: $CUR_HASH" | |
echo "MINHEIGHT: $MINHEIGHT" | |
echo "MAXHEIGHT: $MAXHEIGHT" | |
} 1>&2 | |
[[ $MINHEIGHT -lt $MAXHEIGHT ]] || { | |
echo "MINHEIGHT $MINHEIGHT is not smaller than MAXHEIGHT $MAXHEIGHT" 1>&2 | |
exit 1 | |
} | |
[[ $MINHEIGHT -ge 1138000 ]] || { | |
echo "Pact events are supported only since block height 1138000 but MINHEIGHT is $MINHEIGHT" 1>&2 | |
exit 1 | |
} | |
# ############################################################################ # | |
# Run Queries | |
# Query Block Hashes | |
PAYLOAD_HASHES=$(curl -sL "$URL/chain/$CHAIN/header/branch?minheight=$MINHEIGHT&maxheight=$MAXHEIGHT" \ | |
-H 'accept: application/json;blockheader-encoding=object' \ | |
-H 'content-type: application/json' \ | |
-XPOST -d '{"upper":["'"$CUR_HASH"'"], "lower":[]}' | | |
jq '[.items[].payloadHash]') | |
# Query Output Events | |
curl -sL "$URL/chain/$CHAIN/payload/outputs/batch" \ | |
-H 'content-type: application/json' \ | |
-XPOST -d "$PAYLOAD_HASHES" | | |
jq 'include "./tools"; .[] | pWo | transfers' |