-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: BigQuery AutoFormatter <test@example.com>
- Loading branch information
sam bacha
and
BigQuery AutoFormatter
authored
Jan 30, 2021
1 parent
b34f231
commit 2d9b2b3
Showing
20 changed files
with
879 additions
and
553 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
*.bash text eol=lf | ||
*.fish text eol=lf | ||
*.sh text eol=lf | ||
|
||
*.sql filter=lfs diff=lfs merge=lfs -text | ||
# | ||
#*.sql filter=lfs diff=lfs merge=lfs -text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: "Formatting queries" | ||
on: # rebuild any PRs and master branch changes | ||
pull_request: | ||
push: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
test: # make sure the action works on a clean machine without building | ||
name: Run auto formatter | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Check out Git repository | ||
uses: actions/checkout@v2 | ||
|
||
- name: Install zetasql | ||
run: | | ||
wget https://github.com/Matts966/zetasql-formatter/releases/latest/download/zetasql-formatter_linux_x86_64.zip | ||
sudo unzip zetasql-formatter_linux_x86_64.zip -d /usr/local/bin | ||
rm zetasql-formatter_linux_x86_64.zip | ||
- name: SQLAutoFormatter | ||
uses: yoheikikuta/sql-autoformat-action@v1.0.2 | ||
with: | ||
github_token: ${{ secrets.github_token }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,23 @@ | ||
CREATE OR REPLACE VIEW balancer.view_swaps AS | ||
SELECT | ||
block_time, | ||
token_a_symbol, | ||
token_b_symbol, | ||
token_a_amount, | ||
token_b_amount, | ||
trader_a, | ||
trader_b, | ||
token_a_amount_raw, | ||
token_b_amount_raw, | ||
usd_amount, | ||
token_a_address, | ||
token_b_address, | ||
exchange_contract_address AS contract_address, | ||
tx_hash, | ||
tx_from, | ||
trace_address, | ||
evt_index | ||
FROM dex.trades | ||
WHERE project = 'Balancer' | ||
block_time, | ||
token_a_symbol, | ||
token_b_symbol, | ||
token_a_amount, | ||
token_b_amount, | ||
trader_a, | ||
trader_b, | ||
token_a_amount_raw, | ||
token_b_amount_raw, | ||
usd_amount, | ||
token_a_address, | ||
token_b_address, | ||
exchange_contract_address AS contract_address, | ||
tx_hash, | ||
tx_from, | ||
trace_address, | ||
evt_index | ||
FROM | ||
dex.trades | ||
WHERE | ||
project = 'Balancer'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
#standardSQL | ||
-- MIT License | ||
-- Copyright (c) 2019 Yaz Khoury, yaz.khoury@gmail.com | ||
|
||
|
||
SELECT miner, | ||
DATE(timestamp) as date, | ||
COUNT(miner) as total_block_reward | ||
FROM `bigquery-public-data.crypto_ethereum_classic.blocks` | ||
GROUP BY miner, date | ||
HAVING COUNT(miner) > 1 | ||
SELECT | ||
miner, | ||
DATE(timestamp) AS date, | ||
COUNT(miner) AS total_block_reward | ||
FROM | ||
`bigquery-public-data.crypto_ethereum_classic.blocks` | ||
GROUP BY | ||
miner, | ||
date | ||
HAVING COUNT(miner) > 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,97 @@ | ||
#standardSQL | ||
-- MIT License | ||
-- Copyright (c) 2019 Yaz Khoury, yaz.khoury@gmail.com | ||
|
||
|
||
WITH total_reward_book AS ( | ||
SELECT miner, | ||
DATE(timestamp) as date, | ||
COUNT(miner) as total_block_reward | ||
FROM `bigquery-public-data.crypto_ethereum_classic.blocks` | ||
GROUP BY miner, date | ||
), | ||
total_reward_book_by_date AS ( | ||
SELECT date, | ||
miner AS address, | ||
SUM(total_block_reward / POWER(10,0)) AS value | ||
FROM total_reward_book | ||
GROUP BY miner, date | ||
), | ||
daily_rewards_with_gaps AS ( | ||
SELECT | ||
address, | ||
date, | ||
SUM(value) OVER (PARTITION BY ADDRESS ORDER BY date) AS block_rewards, | ||
LEAD(date, 1, CURRENT_DATE()) OVER (PARTITION BY ADDRESS ORDER BY date) AS next_date | ||
FROM total_reward_book_by_date | ||
), | ||
calendar AS ( | ||
SELECT date | ||
FROM UNNEST(GENERATE_DATE_ARRAY('2015-07-30', CURRENT_DATE())) AS date | ||
), | ||
daily_rewards AS ( | ||
SELECT address, | ||
calendar.date, | ||
block_rewards | ||
FROM daily_rewards_with_gaps | ||
JOIN calendar ON daily_rewards_with_gaps.date <= calendar.date | ||
AND calendar.date < daily_rewards_with_gaps.next_date | ||
), | ||
supply AS ( | ||
SELECT date, | ||
SUM(block_rewards) AS total_rewards | ||
FROM daily_rewards | ||
GROUP BY date | ||
), | ||
ranked_daily_rewards AS ( | ||
SELECT daily_rewards.date AS date, | ||
block_rewards, | ||
ROW_NUMBER() OVER (PARTITION BY daily_rewards.date ORDER BY block_rewards DESC) AS rank | ||
FROM daily_rewards | ||
JOIN supply ON daily_rewards.date = supply.date | ||
WHERE SAFE_DIVIDE(block_rewards, total_rewards) >= 0.01 | ||
ORDER BY block_rewards DESC | ||
), | ||
daily_gini AS ( | ||
SELECT date, | ||
-- (1 − 2B) https://en.wikipedia.org/wiki/Gini_coefficient | ||
1 - 2 * SUM((block_rewards * (rank - 1) + block_rewards / 2)) / COUNT(*) / SUM(block_rewards) AS gini | ||
FROM ranked_daily_rewards | ||
GROUP BY DATE | ||
) | ||
SELECT date, | ||
WITH | ||
total_reward_book AS ( | ||
SELECT | ||
miner, | ||
DATE(timestamp) AS date, | ||
COUNT(miner) AS total_block_reward | ||
FROM | ||
`bigquery-public-data.crypto_ethereum_classic.blocks` | ||
GROUP BY | ||
miner, | ||
date | ||
), | ||
total_reward_book_by_date AS ( | ||
SELECT | ||
date, | ||
miner AS address, | ||
SUM(total_block_reward / POWER(10, 0)) AS value | ||
FROM | ||
total_reward_book | ||
GROUP BY | ||
miner, | ||
date | ||
), | ||
daily_rewards_with_gaps AS ( | ||
SELECT | ||
address, | ||
date, | ||
SUM(value) OVER (PARTITION BY ADDRESS | ||
ORDER BY date) AS block_rewards, | ||
LEAD(date, 1, `CURRENT_DATE`()) OVER (PARTITION BY ADDRESS | ||
ORDER BY date) AS next_date | ||
FROM | ||
total_reward_book_by_date | ||
), | ||
calendar AS ( | ||
SELECT | ||
date | ||
FROM | ||
UNNEST(GENERATE_DATE_ARRAY('2015-07-30', `CURRENT_DATE`())) AS date | ||
), | ||
daily_rewards AS ( | ||
SELECT | ||
address, | ||
calendar.date, | ||
block_rewards | ||
FROM | ||
daily_rewards_with_gaps | ||
JOIN | ||
calendar | ||
ON daily_rewards_with_gaps.date <= calendar.date AND calendar.date < daily_rewards_with_gaps.next_date | ||
), | ||
supply AS ( | ||
SELECT | ||
date, | ||
SUM(block_rewards) AS total_rewards | ||
FROM | ||
daily_rewards | ||
GROUP BY | ||
date | ||
), | ||
ranked_daily_rewards AS ( | ||
SELECT | ||
daily_rewards.date AS date, | ||
block_rewards, | ||
ROW_NUMBER() OVER (PARTITION BY daily_rewards.date | ||
ORDER BY block_rewards DESC) AS rank | ||
FROM | ||
daily_rewards | ||
JOIN | ||
supply | ||
ON daily_rewards.date = supply.date | ||
WHERE | ||
SAFE_DIVIDE(block_rewards, total_rewards) >= 0.01 | ||
ORDER BY block_rewards DESC | ||
), | ||
daily_gini AS ( | ||
SELECT | ||
date, | ||
-- (1 − 2B) https://en.wikipedia.org/wiki/Gini_coefficient | ||
1 - 2 * SUM((block_rewards * (rank - 1) + block_rewards / 2)) / COUNT(*) / SUM(block_rewards) AS gini | ||
FROM | ||
ranked_daily_rewards | ||
GROUP BY | ||
DATE | ||
) | ||
SELECT | ||
date, | ||
gini, | ||
AVG(gini) OVER (ORDER BY date ASC ROWS 7 PRECEDING) AS gini_sma_7, | ||
AVG(gini) OVER (ORDER BY date ASC ROWS 30 PRECEDING) AS gini_sma_30 | ||
FROM daily_gini | ||
AVG(gini) OVER ( | ||
ORDER BY date ROWS 7 PRECEDING) AS gini_sma_7, | ||
AVG(gini) OVER ( | ||
ORDER BY date ROWS 30 PRECEDING) AS gini_sma_30 | ||
FROM | ||
daily_gini; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
#standardSQL | ||
-- MIT License | ||
-- Copyright (c) 2019 Yaz Khoury, yaz.khoury@gmail.com | ||
|
||
|
||
SELECT miner, | ||
DATE(timestamp) as date, | ||
COUNT(miner) as total_block_reward | ||
FROM `bigquery-public-data.crypto_ethereum_classic.blocks` | ||
GROUP BY miner, date | ||
SELECT | ||
miner, | ||
DATE(timestamp) AS date, | ||
COUNT(miner) AS total_block_reward | ||
FROM | ||
`bigquery-public-data.crypto_ethereum_classic.blocks` | ||
GROUP BY | ||
miner, | ||
date | ||
HAVING COUNT(miner) > 100 | ||
ORDER BY date, COUNT(miner) ASC | ||
ORDER BY date, COUNT(miner); |
Oops, something went wrong.