Building your own faucet for your own testnet chain built on Saga
Introduction
A “Faucet” in the crypto world refers to a service dispensing small amounts of cryptocurrency, and specifically addresses the needs of those working on test networks. These test networks, distinct from the main blockchain, provide a sandbox environment for experimentation without the fear of financial loss.
A Faucet operates on various test networks, such as Ethereum’s Rinkeby, Ropsten, or Kovan networks.
Unlike the main blockchain, these networks use test tokens or “testnet coins,” providing a risk-free space for developers and users to explore new features, test smart contracts, and identify potential vulnerabilities.
Users seeking testnet coins simply need to provide their public wallet address associated with the specific test network, and upon verification, Faucet dispenses a small amount of these testnet coins.
The Testnet serves as a multifaceted tool with essential purposes for both developers and the broader crypto community. For developers, it offers a risk-free testing ground, allowing them to identify and rectify issues before deploying applications on the main blockchain. It facilitates debugging, improvements, and serves as a valuable learning platform for users new to blockchain development.
Developers leveraging the Testnet gain numerous advantages, including cost-efficient development through the use of testnet coins instead of real cryptocurrencies. This approach enables faster development cycles and instills confidence in deployment by thoroughly testing applications on test networks.
Now, let’s dive into the details of our guide: building your faucet for your own testnet chain built on Saga.
This is the third article in the Saga series:
- How configuring a Personalized Web3 Experience using the power of Saga
- How deploy your smart contract in your chain
- How to create a faucet for your chain (This article)
The plan
Establish a faucet smart contract wherein the owner funds the contract. The smart contract (faucet) will then send tokens to users as needed.
However, there’s a caveat: Every operation on the smart contract, even on a chain with the lowest gas fees, requires a minimum balance. If a user has a balance of 0, they cannot execute any operations, even if the operation costs as little as 0.000000000001 token.
The Solution: Delegate the execution of operations to an external entity, who can perform the operation on behalf of the user and receive the corresponding tokens.
This approach allows users with a balance of 0 to still access and benefit from the faucet by relying on the external person to initiate the necessary operations.
The solution will be implemented as follows:
The smart contract
This Solidity smart contract is a faucet that dispenses a fixed amount of Ether (0.5 Ether) to addresses requesting tokens.
The requestTokens
function can be called only by the owner and transfers 0.5 Ether to the requested address if certain conditions, such as time-lock expiration and sufficient funds, are met.
The project that I am using is called multicallAddressBook, where you can check:
- my faucet: https://multicall-address-book-git-saga-chefleo.vercel.app/faucet
- my github: https://github.com/chefleo/multicallAddressBook/tree/saga
Also used in my two previous article:
- How configuring a Personalized Web3 Experience using the power of Saga (Where I also explained how I launched my own chain using SagaCli.)
- How deploy your smart contract in your chain
The Frond End code
From the front end perceptive:
The user simply puts their wallet address in the field and clicks the ‘Send Me Tutorials token’ button to submit a request to me, the owner of the contract. I then use my wallet to call the requestTokens
function from the smart contract, and subsequently, the smart contract faucet sends the Tutorials tokens to the user.
From a code perceptive, in the Faucet.tsx component:
This is where the faucet mostly operates:
- Where i instantiate a Private Key Account (To get your private key follow this guide from Metamask.) using
privateKeyToAccount
and create a Wallet Client which provides the ability to execute transactions, sign messages, etc through Wallet Actions. - Next, I call the
requestTokens
function on behalf of the user usingsimulateContract
to check if the transaction will proceed smoothly. Subsequently, I usewriteContract
to execute the function that modifies the state of the blockchain and sends the TutorialsToken to the user.
⚠️ Note: Please ensure that you keep your private key hidden from anyone by adding it to the .env file and including the .env file in your .gitignore.
If you are a beginner check this guide: How to Avoid Uploading Your Private Key to GitHub and use your dev/test wallet for your projects NOT your main one.
Before reaching the conclusion, I suggest adding a reCAPTCHA to prevent bots from executing multiple transactions and depleting the tokens in the smart contract.
Conclusion
In summary, creating a faucet for your custom testnet chain built Saga is a valuable tool for developers and crypto enthusiasts. These faucets enable developers to gain access to testnets that provide a safe space for testing and experimenting with small amounts of cryptocurrency on test networks. They’re like a sandbox for developers, allowing them to try out ideas, fix issues, and refine their applications without any real financial risk.
Beyond just testing, the Testnet serve as a practical bridge between learning blockchain concepts and applying that knowledge. They help developers fine-tune their projects, validate applications, and understand how blockchain functions in a risk-free environment.
In essence, building a faucet simplifies the journey of creating and testing decentralized applications. It’s not just about dispensing tokens; it’s about making blockchain experimentation accessible to everyone. This approach contributes to the collaborative and inclusive spirit of the crypto community, where each faucet not only gives tokens but opens doors for growth, learning, and the continuous evolution of decentralized technologies.