Kaia (ex. Klaytn): Contract Sizzle 100
TLDR
- This tutorial shows how to scan recent blocks on Klaytn Mainnet to find the top 3 most interacted-with contracts.
- It uses web3.py, checks if addresses are contracts by retrieving bytecode, and caches results for efficiency.
- A heap is used to store and retrieve the top 3 busiest contracts, and the script supports multithreading.
- Can be adapted for analytics, alerts, or bot flows leveraging real-time contract interaction data.
Main article
Klaytn is an EVM-compatible protocol with a few modifications. Check the Klaytn docs for a full break-down on the compatibility: Ethereum Compatibility.
In this tutorial, we’ll build a quick Python project called Contract Sizzle 100. Contract Sizzle 100 prints the top 3 hottest contracts on the Klaytn Mainnet (aka Cypress Network) over the past 100 blocks.
The way it works is very simple: the script ingests each new incoming block from the Klaytn Mainnet, extracts all contract interactions, counts them and prints the top3 contracts that had the most interactions.
Prerequisites
- Chainstack account to deploy a Klaytn Mainnet node
- web3.py
Step-by-step
Get a Klaytn node
Log in to your Chainstack account and deploy a node.
Create the script
A few details on the implementation.
How do you actually identify the contract addresses? One easy way is to:
- Retrieve off the network each block with all transactions in the block.
- Extract the
to:
address. - Check if the extracted address is a contract by doing an
eth_getCode
to the address.
This process in Python, however, can be slow, so let’s optimize it a bit:
- After doing an
eth_getCode
to an extracted address, cache the results so that if we get this address again, we don’t do aeth_getCode
to it as we already know this is a contract address. - Multithread the script by using our tutorial Mastering multithreading in Python for Web3 requests: A comprehensive guide.
Here’s the final script:
where
- CHAINSTACK_NODE — your Klaytn node deployed with Chainstack
max_workers=10
— set to however parallel threads you feel is reasonable. Make sure don’t hit the limits: Limits.
Conclusion
This tutorial guided you through creating a basic setup to live-track the hottest contract on the Klaytn Network. There are many fun ways you can use it as basis to build upon — from passing the data to a Twitter bot account to setting up your own alerts or bot flow.