Skip to content
LogoLogo

Contract & Interface

import { Contract, Interface } from '@tevm/ethers'

Both are the same runtime values as their ethers counterparts, re-exported with narrowed constructor types. There is no wrapper, no proxy, and no runtime overhead.

Contract

const Contract: TypesafeEthersContractConstructor

Constructor overloads

new Contract<TAbi extends Abi>(
	target: string | Addressable,
	abi: { fragments: TAbi } | TAbi,
	runner?: null | ContractRunner,
	_deployTx?: null | TransactionResponse,
): TypesafeEthersContract<TAbi> & EthersContract
 
new Contract(
	target: string | Addressable,
	abi: EthersInterface | InterfaceAbi,
	runner?: null | ContractRunner,
	_deployTx?: null | TransactionResponse,
): EthersContract

The first overload applies when the ABI is a const array (or a typed Interface) and returns a TypesafeEthersContract. The second is the untyped ethers fallback for runtime ABIs.

Throws Error — if the address or ABI is invalid (thrown by ethers).

import { ,  } from '@tevm/ethers'
 
const  = [
	{
		: 'function',
		: 'balanceOf',
		: 'view',
		: [{ : 'account', : 'address' }],
		: [{ : 'balance', : 'uint256' }],
	},
] as 
 
const  = await .createMemoryProvider({})
 
const  = new ('0x0000000000000000000000000000000000000001', , )
 
// `balance` is inferred as bigint; a typo in the method name is a compile error
const  = await .balanceOf('0x0000000000000000000000000000000000000002')
.() // 0n

Pass a signer as the runner to send transactions:

import { ,  } from '@tevm/ethers'
import {  } from '@tevm/contract'
import { ,  } from '@tevm/utils'
import {  } from 'ethers'
 
const  = await .createMemoryProvider({ : { : 'auto' } })
 
const  = await .tevm.deploy({
	....('Token', 'TKN'),
	: .,
	: .,
	: [0].,
	: true,
})
 
const  = new (
	.createdAddress,
	.,
	new ([0], ),
)
 
.(await .name()) // 'Token'

Interface

const Interface: TypesafeEthersInterfaceConstructor

Constructor

new Interface<TAbi extends Abi>(abi: InterfaceAbi): Omit<EthersInterface, 'fragments'> & { fragments: TAbi }

Identical to ethers.Interface except that fragments keeps the const ABI's type.

Throws Error — if the ABI is malformed (thrown by ethers).

import {  } from '@tevm/ethers'
 
const  = [
	{
		: 'function',
		: 'balanceOf',
		: 'view',
		: [{ : 'account', : 'address' }],
		: [{ : 'balance', : 'uint256' }],
	},
] as 
 
const  = new <typeof >()
 
const  = .encodeFunctionData('balanceOf', ['0x0000000000000000000000000000000000000001'])
.(.startsWith('0x70a08231')) // true

See also