Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create recurringdonors.sql #37

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions flipside/recurringdonors.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
-- forked from brian-terra / Donation Receivers - sputnik-dao only @ https://flipsidecrypto.xyz/brian-terra/q/vID6_6Kl35D-/donation-receivers---sputnik-dao-only

--recurring donor's first and last donations, as well as their amounts
with txns as
(select distinct tx_hash, transaction_fee as tx_fee
from near.core.fact_transactions b
where (tx_receiver = 'registry.potlock.near'
or tx_signer = 'registry.potlock.near')
and tx_succeeded = TRUE)
,
qmain as (
select block_timestamp,
b.tx_hash,
signer_id,
--args,
deposit / 1e24 as deposit
from near.core.fact_actions_events_function_call b, txns
where b.tx_hash = txns.tx_hash
and method_name = 'register'
),
txns_donate as
(select distinct a.tx_hash, transaction_fee as tx_fee
from near.core.fact_actions_events_function_call a, near.core.fact_transactions b
where receiver_id = 'donate.potlock.near'
and method_name = 'donate'
and a.tx_hash = b.tx_hash
and tx_succeeded = TRUE
),
qmain_donate as (
select block_timestamp,
signer_id,
receiver_id,
try_parse_json(b.action_data):"deposit"::float / 1e24 as deposit,
txns_donate.tx_fee::float / 1e24 as tx_fee
from near.core.fact_actions_events b, txns_donate
where b.tx_hash = txns_donate.tx_hash
and b.action_name = 'Transfer'
and b.receiver_id <> b.signer_id
and receiver_id <> 'impact.sputnik-dao.near'
),
first_donation as (
select distinct signer_id,
first_value(block_timestamp) over (partition by signer_id, date(block_timestamp) order by block_timestamp) as first_donation_date,
deposit as first_amount_donated
from qmain_donate
),
last_donation as (
select distinct signer_id,
last_value(block_timestamp) over (partition by signer_id, date(block_timestamp) order by block_timestamp) as last_donation_date,
deposit as last_amount_donated
from qmain_donate
)

select signer_id,
first_donation_date,
first_amount_donated,
last_donation_date,
last_amount_donated,
case
when last_donation_date >= current_date - interval '14 days' then 'Yes'
else 'No'
end as actively_donating
from (
select signer_id,
first_donation_date,
first_amount_donated,
last_donation_date,
last_amount_donated,
row_number() over (partition by signer_id order by last_donation_date desc) as rn
from (
select fd.signer_id,
fd.first_donation_date,
fd.first_amount_donated,
ld.last_donation_date,
ld.last_amount_donated
from first_donation fd
join last_donation ld on fd.signer_id = ld.signer_id;
) sub
) sub2
where rn = 1;
104 changes: 104 additions & 0 deletions recurringdonorfilteredbypot
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
-- forked from Lordking / POT donation - Total ✔️ @ https://flipsidecrypto.xyz/Lordking/q/OkIw_EjhUDY7/pot-donation---total
---------------------------------------------------------------------
-- L1 fail_receipts 👉 cd1b6803-297b-4bcb-9a88-b92f2a083e75

WITH raw_pot AS (
SELECT livequery.live.udf_api(
'GET',
'https://api.flipsidecrypto.com/api/v2/queries/cd1b6803-297b-4bcb-9a88-b92f2a083e75/data/latest',
{'accept': 'application/json'}, {}) AS response
),
raw_data_pot AS (
SELECT VALUE:"TX_HASH" AS "TX_HASH"
FROM raw_pot, LATERAL FLATTEN (input => response:data)
),
raw_donor AS (
SELECT livequery.live.udf_api(
'GET',
'https://api.flipsidecrypto.com/api/v2/queries/6ad607ae-b012-4d30-814e-42f53dafe273/data/latest',
{'accept': 'application/json'}, {}) AS response
),
raw_donor_build AS (
SELECT VALUE:"Donor" AS "Donor"
FROM raw_donor, LATERAL FLATTEN (input => response:data)
),
pot_donation AS (
SELECT DISTINCT
call.SIGNER_ID AS "Donor",
call.DEPOSIT/1e24 AS "Amount (near)",
call.ARGS:project_id AS "Project",
call.BLOCK_TIMESTAMP AS "Time",
call.RECEIVER_ID AS "POT",
call.TX_HASH AS "Transaction",
call.ARGS:bypass_protocol_fee AS "bypass_protocol_fee",
call.ARGS:message AS "message",
ROUND(call.DEPOSIT/POW(10,24) * (
SELECT AVG(PRICE_USD)
FROM near.price.fact_prices
WHERE DATE_TRUNC('minute', TIMESTAMP) = (
SELECT MAX(DATE_TRUNC('minute', TIMESTAMP))
FROM near.price.fact_prices
)
AND SYMBOL = 'wNEAR'
)) AS "current_usd"
FROM near.core.fact_actions_events_function_call call
WHERE RECEIPT_SUCCEEDED = 'TRUE'
AND ACTION_NAME = 'FunctionCall'
AND METHOD_NAME = 'donate'
AND call.RECEIVER_ID ILIKE '%potfactory.potlock.near%'
AND "Project" IS NOT NULL
AND call.TX_HASH NOT IN (SELECT DISTINCT TX_HASH FROM raw_data_pot)
AND SIGNER_ID IN (SELECT DISTINCT "Donor" FROM raw_donor_build)
),
pot AS (
SELECT
"Donor",
"Time",
SUM("Amount (near)") AS "Donated (near)",
COUNT(DISTINCT "Project") AS "Projects",
SPLIT(MIN("Time"),' ')[0] AS "First donation (Public round)",
SPLIT(MIN("Time"),' ')[0] AS "Last donation (Public round)",
COUNT(DISTINCT "POT") AS "POTs",
COUNT(DISTINCT "Transaction") AS "Donation (transactions)",
COUNT("message") AS "Message",
SUM("current_usd") AS "Current USD"
FROM pot_donation
GROUP BY 1, 2
),
first_donation AS (
SELECT DISTINCT
"Donor",
FIRST_VALUE("Time") OVER (PARTITION BY "Donor", DATE("Time") ORDER BY "Time") AS first_donation_date,
"Donated (near)" AS first_amount_donated
FROM pot
),
last_donation AS (
SELECT DISTINCT
"Donor",
LAST_VALUE("Time") OVER (PARTITION BY "Donor", DATE("Time") ORDER BY "Time") AS last_donation_date,
"Donated (near)" AS last_amount_donated
FROM pot
)
SELECT
"Donor",
first_donation_date,
first_amount_donated,
last_donation_date,
last_amount_donated,
CASE
WHEN last_donation_date >= CURRENT_DATE - INTERVAL '14 days' THEN 'Yes'
ELSE 'No'
END AS actively_donating
FROM (
SELECT
fd."Donor",
fd.first_donation_date,
fd.first_amount_donated,
ld.last_donation_date,
ld.last_amount_donated,
ROW_NUMBER() OVER (PARTITION BY fd."Donor" ORDER BY ld.last_donation_date DESC) AS rn
FROM first_donation fd
JOIN last_donation ld ON fd."Donor" = ld."Donor"
) sub
WHERE rn = 1
AND first_donation_date != last_donation_date;