Programmatic contract interaction
About 2167 wordsAbout 7 min
Week 5 · Part 3 — Programmatic contract interaction
Core question — how do I operate a contract from code, and know for sure that my transaction did what I meant?
In Remix you clicked a button and watched a result appear. A script has to do every one of those steps itself: read, check, send, wait and confirm.
In this Part you write that script, first against the shared starter Registry, then against your own contract with the feature you built in Part 2.
Picture sending a parcel
You check the address, ask the counter whether the parcel is allowed, pay and hand it over, get a tracking number, then check it arrived.
Where the picture stops: a blockchain lets you ask "would this be accepted?" for free before you send anything. That check, called a simulation, is the step most beginners skip, and the one that saves them most.
The hard skill
Write a script that reads a contract, simulates a change, sends it, waits for the receipt, handles a failure by name and verifies the result on-chain.
After this Part you can:
- tell a public client (reads) from a wallet client (signs and sends);
- catch a contract error before it costs gas;
- explain why a transaction hash is not proof of success;
- confirm a change on-chain and on Etherscan.
Core / reference material
Three things make a contract callable
To call any contract from code you need its address, its chain and its Application Binary Interface (ABI). The ABI is the machine-readable description that tells viem which functions and events exist and how their arguments and results are encoded. In the starter, all three come from shared/contract.ts, which npm run compile and npm run deploy generate for you. If any one is wrong, calls fail in confusing ways, as you will see in Week 6.
The arguments and function name are packed into calldata, the encoded input carried by a contract call. You do not hand-encode it here: viem uses the ABI to build it, which is why the address, chain and ABI must agree.
Two clients
A client is the code object that knows how to talk to a chain. The public client asks questions and simulates; the wallet client uses an account to authorise writes.
| Client | What it does | Needs a key? |
|---|---|---|
Public client (createPublicClient) | Reads state, simulates calls, waits for receipts | No |
Wallet client (createWalletClient) | Signs and sends transactions | Yes, an account |
In a script, the account comes from a private key in .env, through privateKeyToAccount. In W6P1, the browser wallet signs instead, and your code never sees a key.
Use a disposable test account
The key in .env should belong to an account made only for Sepolia, holding nothing but free test ETH. Never use an account that has ever held real money, and never commit .env.
The five steps
The same write has a lifecycle. The visual is a compact map; the table below names the viem function and the evidence produced at each stage.
Simulation is a read-only rehearsal against the state visible at that moment; the transaction is the later state-changing action. The receipt is the execution result recorded after inclusion in a block, including its status, block number and logs.
| Step | viem function | What you get back |
|---|---|---|
| 1. Read | readContract | The current value |
| 2. Simulate | simulateContract | A ready-to-send request, or the contract's error, before anything is sent |
| 3. Send | writeContract | A transaction hash, straight away |
| 4. Wait | waitForTransactionReceipt | The receipt: block, status (success or reverted), event logs |
| 5. Verify | readContract again, and Etherscan | Proof the state changed |
Two details that catch people out:
- A transaction hash is only an identifier, not success.
writeContractreturns the hash as soon as the wallet submits the transaction. It can still revert when mined. Only the receipt'sstatustells you what happened on-chain. - Simulate first. If the contract would reject the call at the state used for simulation, the simulation fails with the error's name. No on-chain transaction is sent and no on-chain gas is paid. Your RPC service may still count or limit the simulation request. Passing its
requesttowriteContractalso means you send the call you just simulated.
Errors by name
When a contract reverts with a custom error, viem decodes it using the ABI. The revert can arrive wrapped inside a larger viem error. This pattern searches inside that error for the decoded contract revert:
if (error instanceof BaseError) {
const revert = error.walk((item) => item instanceof ContractFunctionRevertedError)
// revert.data?.errorName might be "EmptyRecord"
}You may copy and adapt this pattern. Understand which failure it detects; you do not need to memorise viem's nested error syntax.
This is why custom errors from Part 2 matter: your script can react to AlreadyEndorsed differently from RecordNotFound.
Worked example: update your record from a script
The starter Registry lets any account set its own record. Save this as scripts/write.ts. It reads, simulates, sends, waits, verifies and handles a contract error:
Read the script by its stages rather than as one block of code:
| Stage | What to look for |
|---|---|
| 1. Imports | Names this file reuses from other files or viem. |
| 2. Input/configuration | process.argv[2] reads the text after the command; ?? '' uses an empty string only when no value was supplied. |
| 3. Account/client setup | Checks Sepolia and creates the account and wallet client. |
| 4. Read | Captures the current record before the change. |
| 5. Simulate | Checks whether the contract accepts the request before sending it. |
| 6. Send and wait | Sends the transaction, then waits for the mined receipt. |
| 7. Verify | Reads the record again and prints an explorer link. |
| 8. Handle failure | catch reports a rejected request; instanceof checks its type, and ?. reads a field only if it exists. |
The syntax is a pattern to inspect and adapt, not a memorisation test. In particular, main().catch(...) makes failures from the asynchronous workflow reach the final error handler instead of disappearing.
import { BaseError, ContractFunctionRevertedError, createWalletClient, isHex } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { address, abi, chain } from '../shared/contract.js'
import { client, transport, requireSepolia } from './client.js'
// Usage: npx tsx scripts/write.ts "your new record"
const value = process.argv[2] ?? ''
async function main() {
await requireSepolia()
if (!address) throw new Error('No contract address in shared/contract.ts.')
const key = process.env.PRIVATE_KEY
if (!key || !isHex(key) || key.length !== 66) throw new Error('Set a disposable Sepolia PRIVATE_KEY in .env.')
const account = privateKeyToAccount(key)
const wallet = createWalletClient({ account, chain, transport })
// 1. Read the current state
const before = await client.readContract({ address, abi, functionName: 'records', args: [account.address] })
console.log(`Account: ${account.address}`)
console.log(`Before: "${before}"`)
// 2. Simulate: run the call against current state without sending it
const { request } = await client.simulateContract({
account, address, abi, functionName: 'setRecord', args: [value],
})
console.log('Simulation passed')
// 3. Send the transaction
const hash = await wallet.writeContract(request)
console.log(`Sent: ${hash}`)
// 4. Wait for the receipt
const receipt = await client.waitForTransactionReceipt({ hash })
console.log(`Mined in block ${receipt.blockNumber}, status: ${receipt.status}`)
// 5. Verify against on-chain state
const after = await client.readContract({ address, abi, functionName: 'records', args: [account.address] })
console.log(`After: "${after}"`)
console.log(`Check it: ${chain.blockExplorers.default.url}/tx/${hash}`)
}
main().catch((error: unknown) => {
// 6. Handle the expected failure: a revert from the contract, by name
if (error instanceof BaseError) {
const revert = error.walk((item) => item instanceof ContractFunctionRevertedError)
if (revert instanceof ContractFunctionRevertedError) {
console.error(`Rejected by the contract: ${revert.data?.errorName ?? revert.reason}`)
} else {
console.error(error.shortMessage)
}
} else {
console.error(error instanceof Error ? error.message : error)
}
process.exitCode = 1
})Create a new disposable Sepolia-only account for this script. From the starter folder, run Foundry's key generator in your local terminal. cast is Foundry's command-line tool for wallet and blockchain operations. It is installed together with Foundry.
cast wallet newIt prints a new address and private key locally. Copy that private key directly into .env as PRIVATE_KEY; do not use a wallet that has ever held real assets, share the key, include it in a screenshot, commit .env, or place it in a VITE_ variable. This account is only for free Sepolia test ETH and must never be reused for real funds. Keep the private key only in your local .env. The command creates a key; it does not send a transaction.
Make sure the disposable account has a little Sepolia ETH, then run it. The output below is from a test run; your account, hash and block number will be different.
npx tsx scripts/write.ts "gm from my first script"Account: 0x9b72A74f04016025F492DadBD59771758b1f5B30
Before: ""
Simulation passed
Sent: 0x<your transaction hash>
Mined in block 11926675, status: success
After: "gm from my first script"
Check it: https://sepolia.etherscan.io/tx/0x<your transaction hash>Now the expected failures. An empty record and one over 140 bytes:
npx tsx scripts/write.ts ""Account: 0x9b72A74f04016025F492DadBD59771758b1f5B30
Before: "gm from my first script"
Rejected by the contract: EmptyRecordA 141-character record prints Rejected by the contract: RecordTooLong. In both cases the simulation stopped the call, so no transaction was sent and no gas was spent.
Verify on Etherscan. Open your "Check it" link. The transaction page should show Success, the setRecord call, and under Logs one RecordUpdated event with your address and the new value. That is the chain's own record of what happened, independent of your script.
Hands-on task
Operate your own contract from a script.
Adapt the working script pattern to your own contract function. Blank-page TypeScript is not the skill being assessed.
- Deploy your Part 2 contract. With your test key in
.env, runnpm run deploy. It compiles, deploys to Sepolia, waits for two confirmations, and writes the new address and ABI intoshared/contract.ts. - Write a script in
scripts/that uses the function you added in Part 2. It must:- read the state your feature changes;
- simulate the call;
- send it;
- wait for the receipt and check its
status; - handle one expected failure from your feature, printing the custom error's name;
- read the state again and print the Etherscan link.
- Run it twice: once so it succeeds, once so it hits your expected failure.
If your feature needs a second account (for example, someone else's record), make a second test account in MetaMask and send it a little Sepolia ETH.
Out of scope: the frontend (W6P1), event history beyond what Etherscan shows (W6P2), and verifying source code on Etherscan (W8P2).
Evidence required
- Your script (a link to the file or commit).
- The Sepolia transaction hash of your successful run.
- The output of both runs, and two or three sentences on what changed on-chain and where on Etherscan you confirmed it.
Completion and revision
This Part is worth 100 points in the Academy progression. Academy participants submit it for a binary Complete or Revise review against the criteria above. Independent readers may use those criteria as a self-check; formal points are tracked through organised Academy participation. There is no partial-score rubric.
Further exploration — optional, not assessed
- Send a transaction without simulating it first, using a value you know will fail. Compare what happens and what it costs.
- Print
receipt.gasUsedandreceipt.logs, and match the log to the event you see on Etherscan. - Use
cast sendto make the same call from the terminal, and compare it with your script.
Sources and attribution
- viem — readContract — Link, referenced only
- viem — simulateContract — Link, referenced only
- viem — writeContract — Link, referenced only
- viem — waitForTransactionReceipt — Link, referenced only
- viem — Error handling — Link, referenced only
- viem — privateKeyToAccount — Link, referenced only
- Foundry — Cast wallet operations — Link, referenced only
- Blockchain@NTU Academy Builder starter — Reuse (MIT),
scripts/client.tsand theRegistryused in the worked example
Changelog
f0652-docs(curriculum): migrate Academy to 10-week structure (#34)on4fee9-docs(curriculum): close prerequisite gaps across Foundation and Deep Dive (#33)on69ce3-docs(builder): improve Deep Dive teaching clarity and engineering guidance (#31)one1f77-docs(track): add Builder W5 Parts 1 to 4 and 6 (#27)on