In this guide, you'll learn how to create an ERC-20 token staking application using thirdweb's Connect SDK and pre-built smart contracts. By the end, your app will enable users to:
- Stake ERC-20 tokens to earn rewards
- Withdraw staked tokens
- Claim accumulated rewards
- View staking balances and rewards
👉 Explore advanced staking strategies
Prerequisites
Before starting, ensure you have:
- A thirdweb account
- Node.js installed
- A Web3 wallet (e.g., MetaMask)
- Testnet funds for deployment
Deploying Smart Contracts
The staking app requires three contracts:
- ERC-20 Staking Token: Token users stake.
- ERC-20 Reward Token: Token distributed as rewards.
- Staking Contract: Manages staking logic.
Steps:
- Deploy two ERC-20 tokens (staking and reward) via thirdweb’s dashboard.
- Deploy the Staking Contract, linking the two token addresses.
- Mint initial token supplies (e.g., 1,000,000 tokens).
Depositing Reward Tokens
To enable reward distribution:
- Approve the Staking Contract to spend reward tokens.
- Deposit rewards into the Staking Contract via
depositRewardTokens.
Project Setup
Tools:
- Next.js
- thirdweb Connect SDK
Steps:
Create a Next.js project:
npx create-next-app erc20-stakingInstall thirdweb SDK:
npm i thirdweb- Configure
ThirdwebProviderin your app.
Building the Staking Interface
Key Components:
- Token Balances: Display staking/reward token balances using
useReadContract. - Staking Info: Fetch staked amounts and rewards via
getStakeInfo. Actions:
- Stake: Approve tokens, then stake.
- Withdraw: Unstake tokens.
- Claim: Harvest rewards.
Example Code:
const { data: stakeInfo } = useReadContract({
contract: STAKING_CONTRACT,
method: "getStakeInfo",
params: [userAddress],
});👉 Optimize your staking rewards
FAQ
1. How do I fund the reward pool?
Deposit tokens into the Staking Contract after approving it via approve.
2. Can I use mainnet instead of testnet?
Yes, but ensure you have sufficient ETH for gas fees.
3. How are rewards calculated?
Rewards are distributed based on the staking duration and pool allocation.
4. Is thirdweb’s SDK free?
Yes, thirdweb offers free tools for smart contract interaction.
Conclusion
You’ve built a functional ERC-20 staking app with:
- Token staking/withdrawal
- Reward claiming
- Real-time balance tracking
Leverage thirdweb’s SDK to streamline development and focus on user experience.
Next Steps:
- Add a leaderboard for top stakers.
- Integrate multi-chain support.
Happy coding!