Build a Counter App
In this tutorial, you’ll write, deploy, and interact with a Counter contract on Push Chain.
We will start with the most popular smart contract, i.e., Counter.sol, that all Solidity devs are familiar with. You would have done the following by the end:
- ✅ Build and deploy Counter.sol
- ✅ Interact with it from any chain
- ✅ Understand the benefits of building Universal Apps
- ✅ Use Live Playground to test and interact with Counter
- 🔜 Extend to a Universal Counter that tracks chain specific users
Write the Contract
The process of building a simple smart contract like a counter is exactly similar to any other EVM Chain. You can use the same tools, such as, remix, foundry, hardhat, etc.
To get started, you can use the following contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.22;
contract Counter {
uint256 public countPC;
event CountIncremented(uint256 indexed countPC, address indexed caller);
function increment() public {
countPC += 1;
emit CountIncremented(countPC, msg.sender);
}
function reset() public {
countPC = 0;
}
}
Understanding the Contract
This contract is a minimal counter:
- The variable
countstores the number of times the counter has been incremented. - The
increment()function adds+1each time it is called. - The
getCount()function lets anyone read the current counter value.
Key takeaway On Push Chain, this contract works universally. A user on Ethereum, Solana, Push or any other chain itself can all call
increment()— with no code changes.
Interact with Counter
The easiest way to interact with the contract is through the Live Playground. The Counter is already deployed on Push Chain Testnet.
Counter Contract Address: 0x5FbDB2315678afecb367f032d93F642f64180aa3
Steps to interact:
- Connect your wallet to the Live Playground.
- You can connect a wallet from any supported chain (Push Chain, Ethereum, or Solana).
- Click Increment Counter to increase the counter.
- Click Refresh Counter Values to update the display.
- After each transaction, use the transaction hash link to view details in Push Chain Explorer.
Live Playground
Source Code
What we Achieved
This was just a simple tutorial. What we did in this tutorial:
- Deployed a counter contract on Push Chain.
- Interacted seamlessly with the contract from any chain. (Ethereum, Solana or Push Chain)
What's Next?
The next tutorial introduces the true power of Universal Apps.
In the next part, we modify this contract to implement the following:
increment()called by users of any chain will now be attributed to them.- The contract will natively detect which chain the
msg.senderbelongs to. - The contract will maintain a
countfor each chain based on the caller’s origin.
All of these features will be natively supported in the contract with no requirement of third-party oracles, interop providers or packages. This is only possible on Push Chain.