Skip to main content

· 2 min read
Nick Vernij

Time for another fun SQL query!

While it's $pepe szn, and gas is at an ATH, you wanna be the first to catch new contracts that are all the hype.

We're here to help, and created a SQL query that shows you recently deployed contracts, guzzling a lot of gas.

WITH deployed_contracts AS (
SELECT
contract_address
FROM
transaction
WHERE
block_number >= {{block_number}} AND contract_address IS NOT NULL
)
SELECT
dc.contract_address, SUM(t.gas_used) AS total_gas_used
FROM
transaction as t
JOIN
deployed_contracts as dc ON t.to = dc.contract_address
WHERE
block_number >= {{block_number}}
GROUP BY
dc.contract_address
ORDER BY
total_gas_used DESC
LIMIT 50

The query takes a list of newly deployed contracts after the given block_number. It then sums up all the gas of transactions going to these contracts. We took a snapshot of the past month, and at the top of the guzzlers, we find $pepe!

The snapshot below is taken with a block number 17010000. At the top we can find $pepe!

contract_addresstotal_gas_used
0x6982508145454ce325ddbe47a25d4ec3d23119336860242906
0xb3d03c68a4077366195510effee9f4ad90e84af45504474840
0x83ef3d446bd1220c8261251f83ac5cc51311d6003353246957
0x4d18548932c7564ec40889e9fd7bd44203c995582597457018
0x2805516b62af69c04368496588950cc4a75c6f342047324571
0x85c4209f7965d36742291f9222bd2cdb104df75c1820183819
0x4bd25d58869327446ee3a73d6021f51a4eb055dd1781123128
0xd4315668aa1d88b4c581ec6fa902e131286dd0ab1716085878
0x5026f006b85729a8b14553fae6af249ad16c9aab1398788492
0x9c22d5e2482bc5a753e508da75b5bb0f23682e361360187942

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

See you next Friday folks ✌️

· 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 ✌️

· 4 min read
Nick Vernij

Earlier this Week we launched our biggest feature yet, Basement SQL. Every Friday we will take a look at another query.

This week @opensea launched their Pro platform, for which they dropped the Gemesis NFT. Let’s take a look at who accumulated this NFT.

We use the owners example from our documentation as a view in the query. On this view we count how many tokens every address owns.

-- QUERY ALL GEMESIS OWNERS
WITH gemesis_owners AS (
SELECT DISTINCT ON (token_id)
"token_id",
"to" AS owner
FROM erc721_transfer
WHERE contract_address = '\xbe9371326F91345777b04394448c23E2BFEaa826'
ORDER BY token_id ASC, block_number DESC, log_index DESC
)
-- COUNT HOW MANY EVERY OWNER OWNS
SELECT
count("owner") AS number_of_tokens_owned,
"owner"
FROM gemesis_owners
GROUP BY "owner"
ORDER BY number_of_tokens_owned DESC;

This query runs in under a second, and 47,215 rows are returned. This tells us 47,215 addresses own the 83,872 tokens that have been minted 🤯.

We see some addresses accumulating a lot of tokens 🧹

number_of_tokens_ownedowner
1030b3aa9923489bc2bfec323bf05346acd4afbc92a0
891087ec385176f3c0e68d41b3e2f7fc72f0a6d1c2a
70787180ec01780ef48f9a581ee22ea8651357a464a
66029fee42319a41775679a244c4804784eb133e79d
6115a14d368aa7d1aecbc437822cb021bc26b28fd7b
556a8c9362e0218e6099ad692975722fe6be69719ba
50798f6d93ba4f14517d3655f4481118182200d1d59
500c58d63d59ad68930c9fdff6f1ac479c5c9941ef4
4907d21d6f8e03746bef7fb64710c1569505f76dc18
398a43435ff9f2ed85b63c53619ce0478f9fb0d0de0

Let’s find out how badly people have been sweeping 🧹 We can calculate the distribution of owners by writing some more complex SQL.

-- FIND ALL GEMESIS OWNERS
WITH gemesis_owners AS (
SELECT DISTINCT ON (token_id)
"token_id",
"to" AS owner
FROM erc721_transfer
WHERE contract_address = '\xbe9371326F91345777b04394448c23E2BFEaa826'
ORDER BY token_id ASC, block_number DESC, log_index DESC
),
-- GROUP THEM BY OWNER, COUNTING HOW MUCH THEY OWN
gemesis_unique_owners AS (
SELECT
COUNT("owner") AS number_of_tokens_owned,
"owner"
FROM gemesis_owners
GROUP BY "owner"
),
-- BUILD A CUMULATIVE DISTRIBUTION
owners_with_cumulative_tokens AS (
SELECT
"owner",
number_of_tokens_owned,
SUM(number_of_tokens_owned) OVER (ORDER BY number_of_tokens_owned DESC) AS cumulative_tokens
FROM gemesis_unique_owners
)
-- SELECT EVERY 10k SEGMENT
SELECT
CEIL(cumulative_tokens / 10000.0)::bigint * 10000 AS segment,
COUNT(owner) AS owner_count
FROM owners_with_cumulative_tokens
GROUP BY CEIL(cumulative_tokens / 10000.0)::bigint
ORDER BY segment ASC;
segmentowner_count
1000025
20000132
30000529
400001705
500001878
9000042976

157 addresses own 20k of the tokens. That’s a little less than 25% of the supply 🤯

But are the whales convinced? Are they on Blur, or back to the OG, OpenSea?

Let’s search the most used contracts among the top 500 owners. We use our previous query, find the top owners and any of the transactions they made. Then we group these and count them

-- QUERY ALL GEMESIS OWNERS
WITH gemesis_owners AS (
SELECT DISTINCT ON (token_id)
"token_id",
"to" AS owner
FROM erc721_transfer
WHERE contract_address = '\xbe9371326F91345777b04394448c23E2BFEaa826'
ORDER BY token_id ASC, block_number DESC, log_index DESC
),
-- FIND THE TOP 500 OWNERS
top_owners AS (
SELECT
COUNT("owner") AS number_of_tokens_owned,
"owner"
FROM gemesis_owners
GROUP BY "owner"
ORDER BY number_of_tokens_owned DESC LIMIT 500
)
-- FIND OUT TO WHICH CONTRACTS THEY TRANSACTED
SELECT
txs.to as contract,
count(txs.to) as transactions_made
FROM top_owners
INNER JOIN transaction AS txs
ON txs.block_number > 16975847 AND txs.from = top_owners.owner
GROUP BY txs.to
ORDER BY transactions_made DESC LIMIT 5;

Opensea is still king in number of txs! 4323 transactions went to Seaport since the Gemesis mint, whereas 2734 were made to Blur’s Marketplace contracts.

transactions_madecontract
432300000000000001ad428e4906ae43d8f9852d0dd6 (Seaport 1.4)
1791000000000000ad05ccc4f10045630fb830b95127 (Blur)
94339da41747a83aee658334415666f3ef92dd0d541 (Blur)
6740000000000a39bb272e79075ade125fd351887ac (Blur Pool)
371be9371326f91345777b04394448c23e2bfeaa826

These queries run super fast, and can be turned into production-grade endpoints with a single click.

Wanna try them yourself?

➡️ Create a Basement account and start building https://dashboard.basement.dev/auth/signup

See you next SQL Friday folks ✌️