@tevm/decorators
The @tevm/decorators
package provides action decorators and utilities for extending Tevm clients with additional functionality. It includes EIP-1193 compatible providers, Ethereum JSON-RPC methods, and Tevm-specific actions.
Generated API Documentation: View the full API documentation in the evmts/tevm-monorepo/packages/decorators/docs folder.
Installation
npm install @tevm/decorators
Overview
The @tevm/decorators
package provides:
- EIP-1193 compatible provider decorators
- Ethereum JSON-RPC method implementations
- Tevm-specific action decorators
- Type-safe request and response handling
- Chain and network utilities
API Reference
Core Functions
ethActions()
Creates an extension providing standard Ethereum JSON-RPC methods.
requestEip1193()
Creates an EIP-1193 compatible provider extension.
tevmActions()
Creates an extension providing Tevm-specific actions.
tevmSend()
Creates an extension for sending Tevm JSON-RPC requests.
Core Types
Provider Types
Eip1193RequestProvider
- EIP-1193 compatible provider interfaceEIP1193Parameters
- Parameters for EIP-1193 requestsEIP1193RequestFn
- Request function type for EIP-1193EIP1193RequestOptions
- Options for EIP-1193 requests
Action Types
EthActionsApi
- Standard Ethereum JSON-RPC actionsTevmActionsApi
- Tevm-specific actionsTevmSendApi
- API for sending Tevm requests
RPC Schema Types
RpcSchema
- Base RPC schema typeRpcSchemaOverride
- Schema override typeDerivedRpcSchema
- Derived schema typeJsonRpcSchemaPublic
- Public JSON-RPC schemaJsonRpcSchemaTevm
- Tevm-specific JSON-RPC schemaJsonRpcSchemaWallet
- Wallet JSON-RPC schemaTestRpcSchema
- Test-specific RPC schema
Ethereum Types
AddEthereumChainParameter
- Parameters for adding a chainWatchAssetParams
- Parameters for watching assetsWalletPermission
- Wallet permission typeWalletPermissionCaveat
- Wallet permission caveat type
Utility Types
Hash
- Ethereum hash typeLogTopic
- Log topic typeNetworkSync
- Network sync status typeQuantity
- Ethereum quantity type
Usage Examples
Creating an EIP-1193 Provider
import { requestEip1193 } from '@tevm/decorators'
const provider = requestEip1193()
const client = createClient({
transport: provider
})
// Make EIP-1193 requests
const result = await client.request({
method: 'eth_call',
params: [{
to: '0x...',
data: '0x...'
}]
})
Using Ethereum Actions
import { ethActions } from '@tevm/decorators'
const eth = ethActions()
const client = createClient({
transport: eth
})
// Use standard Ethereum methods
const balance = await client.eth.getBalance({
address: '0x...'
})
const code = await client.eth.getCode({
address: '0x...'
})
Using Tevm Actions
import { tevmActions } from '@tevm/decorators'
const tevm = tevmActions()
const client = createClient({
transport: tevm
})
// Use Tevm-specific actions
const result = await client.transport.tevm.call({
to: '0x...',
data: '0x...'
})
const state = await client.transport.tevm.dumpState()
Error Handling
try {
const result = await client.request({
method: 'eth_call',
params: [{
to: '0x...',
data: '0x...'
}]
})
} catch (error) {
if (error.code === -32000) {
// Handle execution error
}
throw error
}
Best Practices
1. Type Safety
Always leverage TypeScript types for request parameters and responses:
import type { EIP1193RequestFn } from '@tevm/decorators'
const request: EIP1193RequestFn = async (params) => {
// Type-safe parameters and return value
}
2. Error Handling
Handle both standard JSON-RPC errors and Tevm-specific errors:
try {
await client.request(...)
} catch (error) {
if (error.code === 4001) {
// User rejected request
} else if (error.code === -32000) {
// Execution error
}
}
3. Chain Management
Use proper chain parameters when adding new chains:
const chainParams: AddEthereumChainParameter = {
chainId: '0x1',
chainName: 'Ethereum Mainnet',
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
},
rpcUrls: ['https://...']
}