@tevm/contract
Type-safe contract interactions, deployments, events, and pre-built ERC20/ERC721 contracts.
Full API: packages/contract/docs.
Installation
npm install @tevm/contractAPI Reference
Type Aliases
- Contract
- CreateContractFn
- CreateContractParams
- EventActionCreator
- MaybeExtractEventArgsFromAbi
- ReadActionCreator
- ValueOf
- WriteActionCreator
Functions
Pre-built Contracts
Usage Examples
import { createContract, ERC20, ERC721 } from '@tevm/contract'
const contract = createContract({
humanReadableAbi: [
'function balanceOf(address) view returns (uint256)',
'function transfer(address to, uint256 amount) returns (bool)',
'event Transfer(address indexed from, address indexed to, uint256 value)',
] as const,
name: 'MyToken',
})
const readAction = contract.read.balanceOf('0x...')
const writeAction = contract.write.transfer('0x...', 1000n)
const token = contract.withAddress('0x1234...')
const balance = token.read.balanceOf('0x...')
const erc20 = ERC20.withAddress('0x...')
const nameAction = erc20.read.name()
const transferAction = erc20.write.transfer('0x...', 1000n)
const nft = ERC721.withAddress('0x...')
const ownerAction = nft.read.ownerOf(1n)Deployless Scripts
import { ERC20 } from '@tevm/contract'
const script = ERC20.script({
bytecode: '0x...',
args: ['MyToken', 'MTK', 1000000n],
})
const name = await client.contract(script.read.name())Event Filters
const filter = contract.events.Transfer({ fromBlock: 'latest' })
client.watchEvent(filter, (event) => {
console.log(event.args.from, event.args.to, event.args.value)
})Best Practices
Use as const with ABIs for full type inference:
const abi = ['function example(uint256 value) returns (bool)'] as const
const contract = createContract({ humanReadableAbi: abi, name: 'Example' })Set explicit gas for writes that need it:
const tx = contract.write.complexOperation('0x...', {
gas: 500000n,
maxFeePerGas: 30000000000n,
})Exported Types
import type {
Contract,
CreateContractFn,
CreateContractParams,
EventActionCreator,
ReadActionCreator,
WriteActionCreator,
} from '@tevm/contract'
type MyContract = Contract<typeof myAbi>
