Contract
Contract<
TName
,THumanReadableAbi
,TAddress
,TBytecode
,TDeployedBytecode
,TCode
>:object
Represents a specific contract abi and optional bytecode.
Examples
import {createContract} from 'tevm/contract'
const contract = createContract({ address, bytecode, deployedBytecode, humanReadableAbi,})
Contracts have actions creators for read methods, write methods, deploying, and events
tevm.contract(- { abi: [...], args: ['0x1234...'], functionName: 'balanceOf' },+ MyContract.read.balanceOf('0x1234...'),)
These contracts can be automatically generated by using @tevm/bundler and then importing it. The Tevm bundler will automatically resolve your solidity imports into tevm contract instances
import { MyContract } from './MyContract.sol'
console.log(MyContract.humanReadableAbi)
Address can be added to a contract using the withAddress
method
import { MyContract } from './MyContract.sol'
const MyContractOptimism = MyContract.withAddress('0x420...')
Contracts can also be used with other libraries such as Viem and ethers.
import { MyContract } from './MyContract.sol'import { createPublicClient } from 'viem'
// see viem docsconst client = createPublicClient({...})
const result = await client.readContract( MyContract.withAddress('0x420...').read.balanceOf('0x1234...'),)
## Type Parameters
• **TName** *extends* `string`
• **THumanReadableAbi** *extends* `ReadonlyArray`\<`string`\>
• **TAddress** *extends* `undefined` \| [`Address`](/reference/tevm/utils/type-aliases/address/) = `undefined`
• **TBytecode** *extends* `undefined` \| [`Hex`](/reference/tevm/utils/type-aliases/hex/) = `undefined`
• **TDeployedBytecode** *extends* `undefined` \| [`Hex`](/reference/tevm/utils/type-aliases/hex/) = `undefined`
• **TCode** *extends* `undefined` \| [`Hex`](/reference/tevm/utils/type-aliases/hex/) = `undefined`
## Type declaration
### abi
> **abi**: [`ParseAbi`](/reference/tevm/utils/type-aliases/parseabi/)\<`THumanReadableAbi`\>
The json abi of the contract
#### Example
```typescriptimport { MyContract } from './MyContract.sol'console.log(MyContract.abi) // [{name: 'balanceOf', inputs: [...], outputs: [...], ...}]
address
address:
TAddress
Configured address of the contract. If not set it will be undefined
To set use the withAddress
method
bytecode
bytecode:
TBytecode
The contract bytecode is not defined on Contract objects are expected
to be deployed to the chain. See Script
type which is a contract with bytecode
It’s provided here to allow easier access of the property when using a
Contract | Script
union type
code
code:
TCode
Code i
deploy()
deploy: (…
args
) =>EncodeDeployDataParameters
<ParseAbi
<THumanReadableAbi
>>
Action creator for deploying the contract
Parameters
• …args: EncodeDeployDataParameters
<ParseAbi
<THumanReadableAbi
>> extends object
? TArgs
extends ReadonlyArray
<any
> ? TArgs
: [] : []
Returns
EncodeDeployDataParameters
<ParseAbi
<THumanReadableAbi
>>
deployedBytecode
deployedBytecode:
TDeployedBytecode
The contract deployedBytecode is not defined on Contract objects are expected
to be deployed to the chain. See Script
type which is a contract with deployedBytecode
It’s provided here to allow easier access of the property when using a
Contract | Script
union type
events
events:
EventActionCreator
<THumanReadableAbi
,TAddress
,TBytecode
,TDeployedBytecode
>
Action creators for events. Can be used to create event filters in a typesafe way
Example
tevm.eth.getLog( MyContract.withAddress('0x420...').events.Transfer({ from: '0x1234...' }),)===
### humanReadableAbi
> **humanReadableAbi**: `THumanReadableAbi`
The human readable abi of the contract
#### Example
```typescriptimport { MyContract } from './MyContract.sol'console.log(MyContract.humanReadableAbi)// ['function balanceOf(address): uint256', ...]
name?
optional
name:TName
The name of the contract. If imported this will match the name of the contract import
read
read:
ReadActionCreator
<THumanReadableAbi
,TAddress
,TCode
>
Action creators for contract view and pure functions
Example
tevm.contract( MyContract.withAddress('0x420...').read.balanceOf('0x1234...'),)
script
script:
CreateScript
<TName
,THumanReadableAbi
,TAddress
,TBytecode
>
Creates a deployless instance of the contract that can be used with tevm and viem as deployless contracts
withAddress()
withAddress: <
TAddress
>(address
) =>Contract
<TName
,THumanReadableAbi
,TAddress
,TBytecode
,TDeployedBytecode
,TCode
>
Adds an address to the contract. All action creators will return the address property if added. THis method returns a new contract it does not modify the existing contract.
Example
import { MyContract } from './MyContract.sol'const MyContractOptimism = MyContract.withAddress('0x420...')
Type Parameters
• TAddress extends Address
Parameters
• address: TAddress
Returns
Contract
<TName
, THumanReadableAbi
, TAddress
, TBytecode
, TDeployedBytecode
, TCode
>
write
write:
WriteActionCreator
<THumanReadableAbi
,TAddress
,TCode
>
Action creators for contract payable and nonpayable functions
Example
tevm.contract( MyContract.withAddress('0x420...').read.balanceOf('0x1234...'),)