June 16, 2022

Tracking NFT Transfers using Astra DB and Web3.js

Alex Leventer
Alex LeventerEcosystem
Tracking NFT Transfers using Astra DB and Web3.js
web3.eth.subscribe("newBlockHeaders", (err, result) => {
const { number } = result;
});
const block = await web3.eth.getBlock(blockNumber);
for (
let transactionNo = 0;
transactionNo < block.transactions.length;
transactionNo++
) {
const receipt = web3.eth.getTransactionReceipt(transaction);
}
const ERC1155_TRANSFER_SINGLE_TOPIC =
"0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62";
for (let logNo = 0; logNo < transactionReceipt.logs.length; logNo++) {
const firstTopic = transactionReceipt.logs[logNo].topics[0];
if (firstTopic === ERC1155_TRANSFER_SINGLE_TOPIC) {
console.log('NFT Transfer Detected');
}
}
const abi: AbiItem = isErc1155
? {
name: "uri(uint256)",
type: "function",
inputs: [
{
type: "uint256",
name: "id",
},
],
}
: {
name: "tokenURI(uint256)",
type: "function",
inputs: [
{
type: "uint256",
name: "_tokenId",
},
],
};
const res = await web3.eth.call({
to: contractAddress,
data: web3.eth.abi.encodeFunctionCall(abi, [tokenId.toString()]),
});
uri = web3.eth.abi.decodeParameters(["string"], res);
const res = await axios.get(
`https://gateway.ipfs.io/ipfs/${url?.replace("ipfs://", "")}`
);
return res?.data;
const astraClient = await createClient({
astraDatabaseId: process.env.ASTRA_DB_ID,
astraDatabaseRegion: process.env.ASTRA_DB_REGION,
applicationToken: process.env.ASTRA_DB_APPLICATION_TOKEN,
});
const nftsCollection = astraClient.namespace("app").collection("eth");
const nft = await nftsCollection.create(contractAddress, {
tokenId,
metadata
});
SELECT * FROM eth.nfts WHERE contract_address='0x36D7b711390D34e8fe26ad8f2bB14E7C8f0c56e9' AND token_id='4665';
Your output should look like this:
contract_address | 0x36D7b711390D34e8fe26ad8f2bB14E7C8f0c56e9
token_id | 699
block_number | 14843353
metadata | {"name":"Okay Duck Yacht Club #699","description":"Ducks are quackin'. Join the Yacht Club and be Okay.","image":"ipfs://QmUmnn8CrWyukriFL2ZMA4jVGVTAYMgwfGXxWVvBEGQ3nu/699.png","dna":"834ac0d3ee85574099881370a3cc19dec924d8c5","edition":699,"attributes":[{"trait_type":"Background","value":"Purple"},{"trait_type":"Body","value":"Cream"},{"trait_type":"Eyes","value":"Not Okay"},{"trait_type":"Eyewear","value":"Eyepatch"},{"trait_type":"Beak","value":"Cigar"},{"trait_type":"Clothes","value":"Leather Jacket"},{"trait_type":"Hat","value":"Sailor Hat"}]}
name | Okay Duck Yacht Club
owner_of | 0x6f04aa40c062a35d5149b85d62db94e29025bf17
symbol | ODYC
token_standard | ERC721
token_uri | ipfs://QmUapkBYTtVpahj7gmZiAGDeSmBy5hG4HkUW1yiye1eQNC/699.json
SELECT * FROM eth.nfts WHERE block_number=14843498 ALLOW FILTERING;
SELECT * FROM eth.nfts WHERE name='Okay Duck Yacht Club' ALLOW FILTERING;
...rows 1-17
contract_address | 0x36D7b711390D34e8fe26ad8f2bB14E7C8f0c56e9
token_id | 730
block_number | 14843493
metadata | {"name":"Okay Duck Yacht Club #730","description":"Ducks are quackin'. Join the Yacht Club and be Okay.","image":"ipfs://QmUmnn8CrWyukriFL2ZMA4jVGVTAYMgwfGXxWVvBEGQ3nu/730.png","dna":"e1e3ec3fdf469d10e31ab6f8192fa770794b6e3b","edition":730,"attributes":[{"trait_type":"Background","value":"Lavender"},{"trait_type":"Body","value":"Bloody"},{"trait_type":"Eyes","value":"Doped"},{"trait_type":"Eyewear","value":"None"},{"trait_type":"Beak","value":"Quackin'"},{"trait_type":"Clothes","value":"Skeleton Shirt"},{"trait_type":"Hat","value":"Halo"}]}
name | Okay Duck Yacht Club
owner_of | 0x0000000000000000000000005d987f2f1689ecd4c21f1e73e2f12762570fded5
symbol | ODYC
token_standard | ERC721
token_uri | ipfs://QmUapkBYTtVpahj7gmZiAGDeSmBy5hG4HkUW1yiye1eQNC/730.json
@ Row 19
contract_address | 0x36D7b711390D34e8fe26ad8f2bB14E7C8f0c56e9
token_id | 987
block_number | 14843324
metadata | {"name":"Okay Duck Yacht Club #987","description":"Ducks are quackin'. Join the Yacht Club and be Okay.","image":"ipfs://QmUmnn8CrWyukriFL2ZMA4jVGVTAYMgwfGXxWVvBEGQ3nu/987.png","dna":"68813b24a7c642676b3e63dd0b05c22947f0335e","edition":987,"attributes":[{"trait_type":"Background","value":"Light Blue"},{"trait_type":"Body","value":"Cream"},{"trait_type":"Eyes","value":"Not Okay"},{"trait_type":"Eyewear","value":"Heart Shaped Glasses "},{"trait_type":"Beak","value":"Quackin'"},{"trait_type":"Clothes","value":"Black Sleeveless Shirt"},{"trait_type":"Hat","value":"Halo"}]}
name | Okay Duck Yacht Club
owner_of | 0x0000000000000000000000001ebc712c4fe85d8bf73be303028311d600cb8ec4
symbol | ODYC
token_standard | ERC721
token_uri | ipfs://QmUapkBYTtVpahj7gmZiAGDeSmBy5hG4HkUW1yiye1eQNC/987.json
Discover more
Data ProcessingDataStax Astra DB
Share

One-stop Data API for Production GenAI

Astra DB gives JavaScript developers a complete data API and out-of-the-box integrations that make it easier to build production RAG apps with high relevancy and low latency.