Skip to main content

Example SQL Queries

You can create queries through the Basement dashboard. Create a free account here: dashboard.basement.dev. Below are some examples to get you going!

Get all owners of an NFT project

This query returns all owners of a given ERC721-like contract address.

SELECT DISTINCT ON (token_id)
token_id,
"to" AS owner,
transaction_hash as transferred_in_transaction
FROM
erc721_transfer
WHERE
-- BAYC contract
contract_address = '\xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d'
ORDER BY
token_id ASC,
block_number DESC,
log_index DESC

Get ERC20 transfers in a block

This query returns all ERC20 transfers within the given block_number.

SELECT "contract_address", "from", "to", "value"
FROM erc20_transfer WHERE block_number = 16981600

Get all ERC20 and ERC721 transfers in a transaction

This query leverages a union to get all ERC20 and ERC721 transfers inside of a transaction hash.

-- Try "0x8c5d235d70bab924a9173002423414ceb4b82cb378706f0eaf6edc095c28c045" as a transaction hash!
SELECT
"from",
"to",
"contract_address",
(NULL) as token_id,
'erc20' as transfer_type
FROM erc20_transfer WHERE transaction_hash = {{transaction_hash}}
UNION ALL
SELECT
"from",
"to",
"contract_address",
"token_id",
'erc721' as transfer_type
FROM erc721_transfer WHERE transaction_hash = {{transaction_hash}}