Building your first Web3 application doesn’t have to be intimidating. This step-by-step guide will walk you through creating a simple counter app using thirdweb's Connect SDK and deploying your own smart contract. By the end, you’ll have a fully functional Web3 app that allows users to:
- Connect their wallet
- View the current count stored on the blockchain
- Increment or decrement the value
Ready to dive in? Let’s get started!
Prerequisites
Before we begin, ensure you have:
✅ Basic knowledge of React and TypeScript
✅ Node.js installed
✅ A code editor like VS Code
✅ A Web3 wallet (e.g., MetaMask)
✅ Test funds on a testnet
👉 Need a reliable crypto wallet? Explore options here
Step 1: Create the Smart Contract
We’ll start by writing a Counter smart contract to store and modify a count value.
Key Features:
- Stores the count on-chain
- Includes
incrementanddecrementfunctions - Prevents negative values
Code Example:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Counter {
uint256 public count;
constructor() {
count = 0;
}
function increment() public {
count++;
}
function decrement() public {
require(count > 0, "Count cannot go below 0");
count--;
}
function getCount() public view returns (uint256) {
return count;
}
}💡 Pro Tip:
- Use
thirdweb create contractto scaffold your project. - Forge/Hardhat users: Files are in
src/orcontracts/folders.
Step 2: Deploy the Contract
Deploy your contract to an EVM-compatible testnet:
- Navigate to your contract directory.
Run:
npx thirdweb deploy- Follow the prompts to deploy to your chosen blockchain.
Step 3: Set Up the Project
Initialize Your Frontend:
- Clone a Next.js starter template or create a new project.
Install the thirdweb SDK:
npm install thirdweb
Configure the SDK:
const CLIENT_ID = process.env.NEXT_PUBLIC_CLIENT_ID;
export const client = createThirdwebClient({
clientId: CLIENT_ID as string,
});
export const chain = defineChain("Your_Chain_ID");Wrap Your App:
<ThirdwebProvider client={client}>
{children}
</ThirdwebProvider>Step 4: Connect a Wallet
Use thirdweb’s ConnectButton to enable wallet connections:
const Login = () => (
<ConnectButton client={client} chain={chain} />
);Step 5: Interact with the Contract
Connect to Your Deployed Contract:
export const CONTRACT = getContract({
client: client,
chain: chain,
address: "0x1234...", // Your contract address
abi: [...] // Contract ABI
});Fetch the Count:
const { data: count } = useReadContract({
contract: CONTRACT,
method: "getCount"
});Build the UI:
<button
onClick={() => executeContractCall(CONTRACT, "increment")}
>
+ Increment
</button>
<button
onClick={() => executeContractCall(CONTRACT, "decrement")}
>
- Decrement
</button>FAQs
1. How do I fund my testnet wallet?
Use faucets like Ethereum’s Goerli or Polygon’s Mumbai to get free test tokens.
2. Can I deploy to mainnet?
Yes! Replace the testnet configuration with mainnet details and ensure sufficient gas fees.
3. What if my transaction fails?
Check MetaMask for error details or monitor the transaction on a blockchain explorer.
4. Is thirdweb free to use?
thirdweb offers free tiers for development, with paid options for scaling.
Conclusion
You’ve just built a Web3 counter app from scratch! Key takeaways:
- Smart contracts enable on-chain state management.
- thirdweb simplifies wallet integration and contract interactions.
- Frontend frameworks like Next.js pair seamlessly with Web3 tools.
👉 Ready to explore more Web3 projects? Start here
Next Steps:
- Add user authentication.
- Implement multi-chain support.
- Extend the contract with new features.
Happy coding! 🚀