Skip to main content

SQL Friday – April 14th

· 2 min read
Nick Vernij

It’s friday! Every week we take a look at another this query.

mint.fun dropped their !funpoints pass this week, so let’s write a query to help you and your users find new and rising mints.

A mint is a transfer coming from the null address. We can easily query these on our ERC721 transfers table, and count mints by contract address.

To calculate the change over two periods, we write two separate queries in CTEs and subtract the results. We order by absolute mints, and also show the percentage change.

WITH period1_mints AS (
SELECT
contract_address,
COUNT(*) AS mints
FROM erc721_transfer
WHERE "from" = '\x0000000000000000000000000000000000000000'
AND block_number BETWEEN {{block}} - 4000 AND {{block}} - 2000
GROUP BY contract_address
),
period2_mints AS (
SELECT
contract_address,
COUNT(*) AS mints
FROM erc721_transfer
WHERE "from" = '\x0000000000000000000000000000000000000000'
AND block_number BETWEEN {{block}} - 2000 AND {{block}}
GROUP BY contract_address
)
SELECT
p1.contract_address,
p1.mints AS period1_mints,
p2.mints AS period2_mints,
p2.mints - p1.mints AS absolute_change,
(p2.mints::numeric / p1.mints - 1) * 100 AS relative_change_percentage
FROM period1_mints p1
JOIN period2_mints p2 ON p1.contract_address = p2.contract_address
ORDER BY relative_change_percentage DESC

This query runs super fast, and can easily be integrated in apps such as wallets and explorers.

Snapshot taken at block 17045352

absolute_changecontract_addressperiod1_mintsperiod2_mintsrelative_change_percentage
9440x85c4209f7965d36742291f9222bd2cdb104df75c1791123527.37
5650x0000000000664ceffed39244a8312bd8954708033867443214.61
4750x873b0ba7d721edb1cdef6b5a48efe1b6a2b925a0240715197.92
4340x5f04d47d698f79d76f85e835930170ff4c4ebdb710744111784.04
4080x14a89e4eb4716916f21e8bc6551bf34c78d1b2d660468680.00
1890x61a073585e637aacde3135284054a47d42e19b5019538496.92
590x45704edabe7d2e038c35876dd3b6789511e452cf2180280.95
520x87182ac8551425245326295ede5ece47694ec3061466371.43
480x6761bc096d2537b47673476b483ec1da54c8088d6711571.64
430x57f1887a8bf19b14fc0df6fd9b2acc9af147ea8512216535.25

➡️ Create a Basement account to write and deploy queries yourself https://dashboard.basement.dev/auth/signup

See you next Friday folks ✌️