|
| 1 | +# Data collection |
| 2 | + |
| 3 | +Currently, the data for the analysis of the different ledgers is collected through |
| 4 | +[Google BigQuery](https://console.cloud.google.com/bigquery) . |
| 5 | + |
| 6 | + |
| 7 | +## Queries |
| 8 | + |
| 9 | +One can retrieve the data directly from BigQuery using the queries below: |
| 10 | + |
| 11 | +### Bitcoin |
| 12 | + |
| 13 | +``` |
| 14 | +WITH double_entry_book AS ( |
| 15 | + SELECT array_to_string(inputs.addresses, ",") as address, inputs.type, -inputs.value as value |
| 16 | + FROM `bigquery-public-data.crypto_bitcoin.inputs` as inputs |
| 17 | + WHERE block_timestamp < "{{timestamp}}" |
| 18 | + UNION ALL |
| 19 | + SELECT array_to_string(outputs.addresses, ",") as address, outputs.type, outputs.value as value |
| 20 | + FROM `bigquery-public-data.crypto_bitcoin.outputs` as outputs |
| 21 | + WHERE block_timestamp < "{{timestamp}}" |
| 22 | + ) |
| 23 | + SELECT address, type, sum(value) as balance |
| 24 | + FROM double_entry_book |
| 25 | + GROUP BY 1,2 |
| 26 | + HAVING balance > 0 |
| 27 | + ORDER BY balance DESC |
| 28 | +``` |
| 29 | + |
| 30 | +### Bitcoin Cash |
| 31 | + |
| 32 | +``` |
| 33 | +WITH double_entry_book AS ( |
| 34 | + SELECT array_to_string(inputs.addresses, ",") as address, inputs.type, -inputs.value as value |
| 35 | + FROM `bigquery-public-data.crypto_bitcoin_cash.inputs` as inputs |
| 36 | + WHERE block_timestamp < "{{timestamp}}" |
| 37 | + UNION ALL |
| 38 | + SELECT array_to_string(outputs.addresses, ",") as address, outputs.type, outputs.value as value |
| 39 | + FROM `bigquery-public-data.crypto_bitcoin_cash.outputs` as outputs |
| 40 | + WHERE block_timestamp < "{{timestamp}}" |
| 41 | + ) |
| 42 | + SELECT address, type, sum(value) as balance |
| 43 | + FROM double_entry_book |
| 44 | + GROUP BY 1,2 |
| 45 | + HAVING balance > 0 |
| 46 | + ORDER BY balance DESC |
| 47 | +``` |
| 48 | + |
| 49 | +### Cardano |
| 50 | + |
| 51 | +``` |
| 52 | +SELECT * |
| 53 | + FROM |
| 54 | + ( |
| 55 | + WITH blocks AS ( |
| 56 | + SELECT |
| 57 | + slot_no AS block_number, |
| 58 | + block_time |
| 59 | + FROM `iog-data-analytics.cardano_mainnet.block` |
| 60 | + WHERE block_time < "{{timestamp}}" |
| 61 | + ), |
| 62 | + OUTPUTS AS ( |
| 63 | + SELECT |
| 64 | + slot_no as output_slot_number, |
| 65 | + CAST(JSON_VALUE(a, '$.out_address') AS STRING) AS address, |
| 66 | + CAST(JSON_VALUE(a, '$.out_idx') AS INT64) as out_idx, |
| 67 | + CAST(JSON_VALUE(a, '$.out_value') AS INT64 ) AS value |
| 68 | + FROM `iog-data-analytics.cardano_mainnet.vw_tx_in_out_with_inputs_value` |
| 69 | + JOIN blocks ON block_number = slot_no |
| 70 | + JOIN UNNEST(JSON_QUERY_ARRAY(outputs)) AS a |
| 71 | + ), |
| 72 | + INPUTS AS ( |
| 73 | + SELECT |
| 74 | + address, |
| 75 | + CAST(JSON_VALUE(i, '$.out_value') AS INT64 ) AS value |
| 76 | + FROM `iog-data-analytics.cardano_mainnet.vw_tx_in_out_with_inputs_value` |
| 77 | + JOIN OUTPUTS ON slot_no = output_slot_number |
| 78 | + JOIN UNNEST(JSON_QUERY_ARRAY(inputs)) AS i ON CAST(JSON_VALUE(i, '$.in_idx') AS INT64) = OUTPUTS.out_idx |
| 79 | + ), |
| 80 | + INCOMING AS ( |
| 81 | + SELECT address, SUM(CAST(value AS numeric)) as sum_incoming |
| 82 | + FROM INPUTS |
| 83 | + GROUP BY address |
| 84 | + ), |
| 85 | + OUTGOING AS ( |
| 86 | + SELECT address, SUM(CAST(value AS numeric)) as sum_outgoing |
| 87 | + FROM OUTPUTS |
| 88 | + GROUP BY address |
| 89 | + ) |
| 90 | + SELECT i.address, i.sum_incoming - o.sum_outgoing AS balance |
| 91 | + FROM INCOMING AS i |
| 92 | + JOIN OUTGOING AS o ON i.address = o.address |
| 93 | + ) |
| 94 | + WHERE balance > 0 |
| 95 | + ORDER BY balance DESC |
| 96 | +``` |
| 97 | + |
| 98 | +### Dogecoin |
| 99 | + |
| 100 | +``` |
| 101 | +WITH double_entry_book AS ( |
| 102 | + SELECT array_to_string(inputs.addresses, ",") as address, inputs.type, -inputs.value as value |
| 103 | + FROM `bigquery-public-data.crypto_dogecoin.inputs` as inputs |
| 104 | + WHERE block_timestamp < "{{timestamp}}" |
| 105 | + UNION ALL |
| 106 | + SELECT array_to_string(outputs.addresses, ",") as address, outputs.type, outputs.value as value |
| 107 | + FROM `bigquery-public-data.crypto_dogecoin.outputs` as outputs |
| 108 | + WHERE block_timestamp < "{{timestamp}}" |
| 109 | + ) |
| 110 | + SELECT address, type, sum(value) as balance |
| 111 | + FROM double_entry_book |
| 112 | + GROUP BY 1,2 |
| 113 | + HAVING balance > 0 |
| 114 | + ORDER BY balance DESC |
| 115 | +``` |
| 116 | + |
| 117 | +### Ethereum |
| 118 | + |
| 119 | +``` |
| 120 | +WITH double_entry_book AS ( |
| 121 | + SELECT to_address as address, value AS value |
| 122 | + FROM `bigquery-public-data.crypto_ethereum.traces` |
| 123 | + WHERE to_address IS NOT null |
| 124 | + AND status = 1 |
| 125 | + AND (call_type NOT IN ('delegatecall', 'callcode', 'staticcall') OR call_type IS null) |
| 126 | + AND block_timestamp < "{{timestamp}}" |
| 127 | + UNION ALL |
| 128 | + SELECT from_address as address, -value AS value |
| 129 | + FROM `bigquery-public-data.crypto_ethereum.traces` |
| 130 | + WHERE from_address IS NOT null |
| 131 | + AND status = 1 |
| 132 | + AND (call_type NOT IN ('delegatecall', 'callcode', 'staticcall') OR call_type IS null) |
| 133 | + AND block_timestamp < "{{timestamp}}" |
| 134 | + UNION ALL |
| 135 | + SELECT miner AS address, sum(cast(receipt_gas_used as numeric) * cast(gas_price as numeric)) AS value |
| 136 | + FROM `bigquery-public-data.crypto_ethereum.transactions` AS transactions |
| 137 | + JOIN `bigquery-public-data.crypto_ethereum.blocks` AS blocks on blocks.number = transactions.block_number |
| 138 | + WHERE transactions.block_timestamp < "{{timestamp}}" |
| 139 | + GROUP BY blocks.miner |
| 140 | + UNION ALL |
| 141 | + SELECT from_address AS address, -(cast(receipt_gas_used as numeric) * cast(gas_price as numeric)) AS value |
| 142 | + FROM `bigquery-public-data.crypto_ethereum.transactions` |
| 143 | + WHERE block_timestamp < "{{timestamp}}" |
| 144 | + ) |
| 145 | + SELECT address, sum(value) AS balance |
| 146 | + FROM double_entry_book |
| 147 | + GROUP BY address |
| 148 | + HAVING balance > 0 |
| 149 | + ORDER BY balance DESC |
| 150 | +``` |
| 151 | + |
| 152 | +### Litecoin |
| 153 | + |
| 154 | +``` |
| 155 | +WITH double_entry_book AS ( |
| 156 | + SELECT array_to_string(inputs.addresses, ",") as address, inputs.type, -inputs.value as value |
| 157 | + FROM `bigquery-public-data.crypto_litecoin.inputs` as inputs |
| 158 | + WHERE block_timestamp < "{{timestamp}}" |
| 159 | + UNION ALL |
| 160 | + SELECT array_to_string(outputs.addresses, ",") as address, outputs.type, outputs.value as value |
| 161 | + FROM `bigquery-public-data.crypto_litecoin.outputs` as outputs |
| 162 | + WHERE block_timestamp < "{{timestamp}}" |
| 163 | + ) |
| 164 | + SELECT address, type, sum(value) as balance |
| 165 | + FROM double_entry_book |
| 166 | + GROUP BY 1,2 |
| 167 | + HAVING balance > 0 |
| 168 | + ORDER BY balance DESC |
| 169 | +``` |
| 170 | + |
| 171 | +### Tezos |
| 172 | + |
| 173 | +``` |
| 174 | +WITH double_entry_book as ( |
| 175 | + SELECT IF(kind = 'contract', contract, delegate) AS address, change AS value |
| 176 | + FROM `public-data-finance.crypto_tezos.balance_updates` |
| 177 | + WHERE (status IS NULL OR status = 'applied') AND (timestamp < "{{timestamp}}") |
| 178 | + UNION ALL |
| 179 | + SELECT address, balance_change |
| 180 | + FROM `public-data-finance.crypto_tezos.migrations` |
| 181 | + WHERE timestamp < "{{timestamp}}" |
| 182 | + ) |
| 183 | + SELECT address, SUM(value) AS balance |
| 184 | + FROM double_entry_book |
| 185 | + GROUP BY address |
| 186 | + HAVING balance > 0 |
| 187 | + ORDER BY balance DESC |
| 188 | +``` |
| 189 | + |
| 190 | +### Zcash |
| 191 | + |
| 192 | +``` |
| 193 | +WITH double_entry_book AS ( |
| 194 | + SELECT array_to_string(inputs.addresses, ",") as address, inputs.type, -inputs.value as value |
| 195 | + FROM `bigquery-public-data.crypto_zcash.inputs` as inputs |
| 196 | + WHERE block_timestamp < "{{timestamp}}" |
| 197 | + UNION ALL |
| 198 | + SELECT array_to_string(outputs.addresses, ",") as address, outputs.type, outputs.value as value |
| 199 | + FROM `bigquery-public-data.crypto_zcash.outputs` as outputs |
| 200 | + WHERE block_timestamp < "{{timestamp}}" |
| 201 | + ) |
| 202 | + SELECT address, type, sum(value) as balance |
| 203 | + FROM double_entry_book |
| 204 | + GROUP BY 1,2 |
| 205 | + HAVING balance > 0 |
| 206 | + ORDER BY balance DESC |
| 207 | +``` |
| 208 | + |
| 209 | +## Automating the data collection process |
| 210 | + |
| 211 | +Instead of executing each of these queries separately on the BigQuery console and saving the results manually, it is |
| 212 | +also possible to automate the process using a |
| 213 | +[script](https://github.com/Blockchain-Technology-Lab/tokenomics-decentralization/blob/main/data_collection_scripts/big_query_balance_data.py) |
| 214 | +and collect all relevant data in one go. Executing this script will run queries |
| 215 | +from [this file](https://github.com/Blockchain-Technology-Lab/tokenomics-decentralization/blob/main/data_collection_scripts/queries.yaml). |
| 216 | + |
| 217 | +IMPORTANT: the script uses service account credentials for authentication, therefore before running it, you need to |
| 218 | +generate the relevant credentials from Google, as described |
| 219 | +[here](https://developers.google.com/workspace/guides/create-credentials#service-account) and save your key in the |
| 220 | +`data_collections_scripts/` directory of the project under the name 'google-service-account-key-0.json'. Any additional |
| 221 | +keys should be named 'google-service-account-key-1.json', 'google-service-account-key-2.json', and so on. |
| 222 | +There is a |
| 223 | +[sample file](https://github.com/Blockchain-Technology-Lab/tokenomics-decentralization/blob/main/data_collection_scripts/google-service-account-key-SAMPLE.json) |
| 224 | +that you can consult, which shows what your credentials are supposed to look like (but note that this is for |
| 225 | +informational purposes only, this file is not used in the code). |
| 226 | + |
| 227 | +Once you have set up the credentials, you can just run the following command from the root |
| 228 | +directory to retrieve data for all supported blockchains: |
| 229 | + |
| 230 | +`python -m data_collection_scripts.big_query_balance_data` |
| 231 | + |
| 232 | +There are also three command line arguments that can be used to customize the data collection process: |
| 233 | + |
| 234 | +- `ledgers` accepts any number of the supported ledgers (case-insensitive). For example, adding `--ledgers bitcoin` |
| 235 | + results in collecting data only for Bitcoin, while `--ledgers Bitcoin Ethereum` would collect data for |
| 236 | + Bitcoin and Ethereum. If the `ledgers` argument is omitted, then the default value is used, which |
| 237 | + is taken from the |
| 238 | + [configuration file](https://github.com/Blockchain-Technology-Lab/tokenomics-decentralization/blob/main/config.yaml) |
| 239 | + and typically corresponds to all supported blockchains. |
| 240 | +- `snapshot_dates` accepts any number of dates formatted as YYYY-MM-DD, YYYY-MM, or YYYY. Then, data is collected for |
| 241 | + the specified date(s). Again, if this argument is omitted, the default value is taken from the |
| 242 | + [configuration file](https://github.com/Blockchain-Technology-Lab/tokenomics-decentralization/blob/main/config.yaml). |
| 243 | +- `--force-query` forces the collection of all raw data files, even if some or all of the files already |
| 244 | + exist. By default, this flag is set to False and the script only fetches data for some blockchain if the |
| 245 | + corresponding file does not already exist. |
0 commit comments