viem client and app address for direct blockchain interactions:
bot.viem- Viem client with your gas wallet account for signing transactionsbot.appAddress- Your bot’s treasury wallet address (SimpleAccount)
Your gas wallet (
bot.viem.account) needs Base ETH to pay for gas fees when executing transactions. See Understanding Your Bot’s Wallet Architecture for details.Configuration
Base RPC URL
Bots that interact with smart contracts require a reliable RPC endpoint for Base chain. The default public RPC (mainnet.base.org) has strict rate limits and will cause your bot to fail quickly under normal usage.
You can configure a custom RPC URL by passing baseRpcUrl to makeTownsBot():
.env file:
Reading from Contracts
Read from any contract without making a transaction:Writing to Contracts
execute (Primary Method)
Useexecute from ERC-7821 for any onchain interaction. This is your main tool for blockchain operations.
Single Operation:
executeBatch
For advanced use cases requiring batches of batches:writeContract
Use only for your bot’s SimpleAccount contract operations:Utility Functions
getSmartAccountFromUserId
Get a user’s smart account address from their Towns user ID.null if no smart account exists.
Use cases:
- Send tokens/NFTs to users
- Check on-chain balances
- Airdrop rewards
- Verify asset ownership
When to Use Each Method
readContract- Read from any contract (no transaction, no gas)writeContract- Bot’s SimpleAccount contract operations onlyexecute- Primary method for any onchain interaction- Tips, swaps, staking, NFT minting, DeFi
- Single operations or batch operations
- Atomic execution (all succeed or all fail)
- Gas optimized for batches
executeBatch- Advanced batching (batches of batches)