Part II · Accounts, transactions, contracts
Lab: the Counter contract
Start by reading one on-chain state change, rather than equating smart contracts with finance from day one.
What the contract does
Counter is a teaching contract only a dozen lines long:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract Counter {
uint256 public count;
address public owner;
event Incremented(address indexed by, uint256 newCount);
constructor() { owner = msg.sender; }
function increment() external {
count += 1;
emit Incremented(msg.sender, count);
}
function reset() external {
require(msg.sender == owner, "not owner");
count = 0;
}
}
Anyone may call increment (add one and emit Incremented); only the deployer, owner, may call reset (set to zero). It was compiled with solc 0.8.34.
On-chain result
Contract address (computed in advance from account A’s nonce 0, and matching exactly after deployment):
View address on Sepolia Etherscan (opens in a new tab): 0xAf6f88Cf8bf6e9211CD11B375AECBa37d61CD00fRead-only check at the snapshot: count() = 2, owner() = account A.
Each Counter step is a mined transaction; reading count() needs no transaction.
| Nonce | Time (Singapore) | Action | Transaction | Block | Gas used | Fee (wei) |
|---|---|---|---|---|---|---|
| 0 | 2026-11-11 17:48:24 | Deploy Counter | View transaction on Sepolia Etherscan (opens in a new tab): 0x5619a1084d03bbf2101514963485178160ffb59a3876b2920123055ec09c65a6 | 12110961 | 1,652,641 | 115,684,870 |
| 1 | 17:49:00 | increment (0→1) | View transaction on Sepolia Etherscan (opens in a new tab): 0x9e63d7a2bf89d4dd7330623a6729ce35f1c249c9d735de0e7fb770b380a98e73 | 12110964 | 127,229 | 8,651,572 |
| 2 | 18:01:24 | reset (→0) | View transaction on Sepolia Etherscan (opens in a new tab): 0x6051310d025382a9b0b943fac0e8791036db4d3ededd27dedf333b616d60017a | 12111025 | 23,626 | 1,653,820 |
| 3 | 18:14:36 | increment, 1 wei tip (0→1) | View transaction on Sepolia Etherscan (opens in a new tab): 0xd61e57686959b7c4c9d774a6aa35b0fb9dac72e8317055146da4676a9e4e4b31 | 12111091 | 127,229 | 2,671,809 |
| 4 | 18:32:36 | increment, 2 wei tip (1→2) | View transaction on Sepolia Etherscan (opens in a new tab): 0x8141d5f30dc7abd76c7c2ebfcc9f4861af625e38ca11220605057e1275512da8 | 12111181 | 29,309 | 762,034 |
All five succeeded. Use the links or the Copy buttons for full hashes; Appendix A lists every transaction in full.
What the numbers teach
- The chain keeps the books for you:
count,ownerand the event logs all live on-chain. Changing state means paying gas yourself. - Deploying costs far more than calling: deployment used 1,652,641 gas, reset only 23,626. Most of the cost is writing the contract’s code into chain state, not the few lines of arithmetic.
- The same function can cost different gas: the increment from 0 to 1 used 127,229 gas, from 1 to 2 only 29,309. Writing a storage slot from zero to non-zero is expensive; changing an already non-zero value is much cheaper.
- Confirmed transactions cannot be undone: reset sets the number back to zero but cannot erase the earlier increment transactions or events. The history is still there.
- This contract cannot gain features: to add batch operations or upgradeability you must choose an upgradeable pattern at deployment time, which leads straight to UBOX (Chapter 13).
Everyday equivalent
Picture a public counter outside a shop that every partner merchant can check: each call changes the count by one. If the count tracks raffle entries or completed public tasks, an on-chain counter reduces disputes about one side changing the number. But it cannot prove that a real person ever stood at the counter.