This repository has been archived by the owner on Mar 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Strong type support for Eth API (#4513)
* ✨ Add eth execution API types * 🎨 Update base provider to use eth api types * ✨ Add json-rpc helpers * 🎨 Update exports for web3-common * 🎨 Update API types * 🎨 Update http-provider types * 🎨 Update web3-core types * Apply suggestions from code review Co-authored-by: Wyatt Barnes <[email protected]> * ✨ Add common types to utils * 🎨 Updated the types as per feedback * 🎨 Update the code with feedback * 🎨 Fix ReceiptInfo object * 🎨 Fix eth_submitWork spec * 🎨 Update eth_estimateGas spec to make all properties of tx optional * 🎨 Update transaction spec * Apply suggestions from code review Co-authored-by: Wyatt Barnes <[email protected]> * Update packages/web3-common/src/eth_execution_api.ts Co-authored-by: Wyatt Barnes <[email protected]> Co-authored-by: Wyatt Barnes <[email protected]>
- Loading branch information
1 parent
2d85ff0
commit d0a8493
Showing
21 changed files
with
556 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,298 @@ | ||
import { | ||
Address, | ||
HexString, | ||
HexString256Bytes, | ||
HexString8Bytes, | ||
HexString32Bytes, | ||
HexStringBytes, | ||
HexStringSingleByte, | ||
Uint, | ||
Uint256, | ||
} from 'web3-utils'; | ||
|
||
// The types are generated manually by referring to following doc | ||
// https://github.com/ethereum/execution-apis | ||
|
||
export interface AccessListEntry { | ||
readonly address?: Address; | ||
readonly storageKeys?: HexString32Bytes[]; | ||
} | ||
export type AccessList = AccessListEntry[]; | ||
export type TransactionHash = HexString; | ||
export type Uncles = HexString32Bytes[]; | ||
export type BlockTag = 'earliest' | 'latest' | 'pending'; | ||
export type BlockNumberOrTag = Uint | BlockTag; | ||
|
||
export interface TransactionCall { | ||
readonly from?: Address; | ||
readonly to: Address; | ||
readonly gas?: Uint; | ||
readonly gasPrice?: Uint; | ||
readonly value?: Uint; | ||
readonly data?: HexString; | ||
} | ||
|
||
export interface BaseTransaction { | ||
readonly to?: Address | null; | ||
readonly type: HexStringSingleByte; | ||
readonly nonce: Uint; | ||
readonly gas: Uint; | ||
readonly value: Uint; | ||
readonly input: HexStringBytes; | ||
} | ||
|
||
export interface Transaction1559Unsigned extends BaseTransaction { | ||
readonly maxFeePerGas: Uint; | ||
readonly maxPriorityFeePerGas: Uint; | ||
readonly accessList: AccessList; | ||
} | ||
|
||
export interface Transaction1559Signed extends Transaction1559Unsigned { | ||
readonly yParity: Uint; | ||
readonly r: Uint; | ||
readonly s: Uint; | ||
} | ||
|
||
export interface Transaction2930Unsigned extends BaseTransaction { | ||
readonly gasPrice: Uint; | ||
readonly accessList: AccessList; | ||
} | ||
|
||
export interface Transaction2930Signed extends Transaction2930Unsigned { | ||
readonly yParity: Uint; | ||
readonly r: Uint; | ||
readonly s: Uint; | ||
} | ||
|
||
export interface TransactionLegacyUnsigned extends BaseTransaction { | ||
readonly gasPrice: Uint; | ||
} | ||
|
||
export interface TransactionLegacySigned extends TransactionLegacyUnsigned { | ||
readonly v: Uint; | ||
readonly r: Uint; | ||
readonly s: Uint; | ||
} | ||
|
||
// https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.json#L178 | ||
export type TransactionUnsigned = | ||
| Transaction1559Unsigned | ||
| Transaction2930Unsigned | ||
| TransactionLegacyUnsigned; | ||
|
||
// https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.json#L262 | ||
export type TransactionSigned = | ||
| Transaction1559Signed | ||
| Transaction2930Signed | ||
| TransactionLegacySigned; | ||
|
||
// https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.json#L269 | ||
export type TransactionInfo = TransactionSigned & { | ||
readonly blockHash: HexString32Bytes | null; | ||
readonly blockNumber: Uint | null; | ||
readonly from: Address; | ||
readonly hash: HexString32Bytes; | ||
readonly transactionIndex: Uint | null; | ||
}; | ||
|
||
// https://github.com/ethereum/execution-apis/blob/main/src/schemas/transaction.json#L24 | ||
export type TransactionWithSender = TransactionUnsigned & { from: Address }; | ||
|
||
// https://github.com/ethereum/execution-apis/blob/main/src/schemas/block.json#L2 | ||
export interface Block { | ||
readonly parentHash: HexString32Bytes; | ||
readonly sha3Uncles: HexString32Bytes; | ||
readonly miner: HexString; | ||
readonly stateRoot: HexString32Bytes; | ||
readonly transactionsRoot: HexString32Bytes; | ||
readonly receiptsRoot: HexString32Bytes; | ||
readonly logsBloom: HexString256Bytes | null; | ||
readonly difficulty?: Uint; | ||
readonly number: Uint | null; | ||
readonly gasLimit: Uint; | ||
readonly gasUsed: Uint; | ||
readonly timestamp: Uint; | ||
readonly extraData: HexStringBytes; | ||
readonly mixHash: HexString32Bytes; | ||
readonly nonce: Uint | null; | ||
readonly totalDifficulty: Uint; | ||
readonly baseFeePerGas?: Uint; | ||
readonly size: Uint; | ||
readonly transactions: TransactionHash[] | TransactionSigned[]; | ||
readonly uncles: Uncles; | ||
readonly hash: HexString32Bytes | null; | ||
} | ||
|
||
// https://github.com/ethereum/execution-apis/blob/main/src/schemas/filter.json#L59 | ||
export type Topic = HexString256Bytes; | ||
|
||
// https://github.com/ethereum/execution-apis/blob/main/src/schemas/receipt.json#L2 | ||
export interface Log { | ||
readonly removed?: boolean; | ||
readonly logIndex?: Uint | null; | ||
readonly transactionIndex?: Uint | null; | ||
readonly transactionHash?: HexString32Bytes | null; | ||
readonly blockHash?: HexString32Bytes | null; | ||
readonly blockNumber?: Uint | null; | ||
readonly address?: Address; | ||
readonly data?: HexStringBytes; | ||
readonly topics?: Topic[]; | ||
} | ||
|
||
// https://github.com/ethereum/execution-apis/blob/main/src/schemas/receipt.json#L44 | ||
export interface ReceiptInfo { | ||
readonly transactionHash: HexString32Bytes; | ||
readonly transactionIndex: HexString32Bytes; | ||
readonly blockHash: HexString32Bytes; | ||
readonly blockNumber: Uint; | ||
readonly from: Address; | ||
readonly to: Address; | ||
readonly cumulativeGasUsed: Uint; | ||
readonly gasUsed: Uint; | ||
readonly contractAddress: Address | null; | ||
readonly logs: Log[]; | ||
readonly logsBloom: HexString256Bytes; | ||
readonly root: HexString32Bytes; | ||
readonly status: '0x1' | '0x0'; | ||
} | ||
|
||
// https://github.com/ethereum/execution-apis/blob/main/src/schemas/client.json#L2 | ||
export type SyncingStatus = | ||
| { startingBlock: Uint; currentBlock: Uint; highestBlock: Uint } | ||
| boolean; | ||
|
||
// https://github.com/ethereum/execution-apis/blob/main/src/eth/fee_market.json#L53 | ||
export interface FeeHistoryResult { | ||
readonly oldestBlock: Uint; | ||
readonly baseFeePerGas: Uint; | ||
readonly reward: number[][]; | ||
} | ||
|
||
// https://github.com/ethereum/execution-apis/blob/main/src/schemas/filter.json#L28 | ||
export interface Filter { | ||
readonly fromBlock?: BlockNumberOrTag; | ||
readonly toBlock?: BlockNumberOrTag; | ||
readonly address?: Address | Address[]; | ||
readonly topics?: (Topic | Topic[] | null)[]; | ||
} | ||
|
||
// https://github.com/ethereum/execution-apis/blob/main/src/schemas/filter.json#L2 | ||
export type FilterResults = HexString32Bytes[] | Log[]; | ||
|
||
export interface CompileResult { | ||
readonly code: HexStringBytes; | ||
readonly info: { | ||
readonly source: string; | ||
readonly language: string; | ||
readonly languageVersion: string; | ||
readonly compilerVersion: string; | ||
readonly abiDefinition: Record<string, unknown>[]; | ||
readonly userDoc: { | ||
readonly methods: Record<string, unknown>; | ||
}; | ||
readonly developerDoc: { | ||
readonly methods: Record<string, unknown>; | ||
}; | ||
}; | ||
} | ||
|
||
/* eslint-disable camelcase */ | ||
export type EthExecutionAPI = { | ||
// https://github.com/ethereum/execution-apis/blob/main/src/eth/block.json | ||
eth_getBlockByHash: (blockHash: HexString32Bytes, hydrated: boolean) => Block; | ||
eth_getBlockByNumber: (blockNumber: BlockNumberOrTag, hydrated: boolean) => Block; | ||
eth_getBlockTransactionCountByHash: (blockHash: HexString32Bytes) => Uint; | ||
eth_getBlockTransactionCountByNumber: (blockNumber: BlockNumberOrTag) => Uint; | ||
eth_getUncleCountByBlockHash: (blockHash: HexString32Bytes) => Uint; | ||
eth_getUncleCountByBlockNumber: (blockNumber: BlockNumberOrTag) => Uint; | ||
eth_getUncleByBlockHashAndIndex: (blockHash: HexString32Bytes, uncleIndex: Uint) => Block; | ||
eth_getUncleByBlockNumberAndIndex: (blockNumber: BlockNumberOrTag, uncleIndex: Uint) => Block; | ||
|
||
// https://github.com/ethereum/execution-apis/blob/main/src/eth/transaction.json | ||
eth_getTransactionByHash: (transactionHash: HexString32Bytes) => TransactionInfo | null; | ||
eth_getTransactionByBlockHashAndIndex: ( | ||
blockHash: HexString32Bytes, | ||
transactionIndex: Uint, | ||
) => TransactionInfo | null; | ||
eth_getTransactionByBlockNumberAndIndex: ( | ||
blockNumber: BlockNumberOrTag, | ||
transactionIndex: Uint, | ||
) => TransactionInfo | null; | ||
eth_getTransactionReceipt: (transactionHash: HexString32Bytes) => ReceiptInfo | null; | ||
|
||
// https://github.com/ethereum/execution-apis/blob/main/src/eth/client.json | ||
eth_protocolVersion: () => string; | ||
eth_syncing: () => SyncingStatus; | ||
eth_coinbase: () => Address; | ||
eth_accounts: () => Address[]; | ||
eth_blockNumber: () => Uint; | ||
|
||
// https://github.com/ethereum/execution-apis/blob/main/src/eth/execute.json | ||
eth_call: (transaction: TransactionCall, blockNumber: BlockNumberOrTag) => HexStringBytes; | ||
eth_estimateGas: ( | ||
transaction: Partial<TransactionWithSender>, | ||
blockNumber: BlockNumberOrTag, | ||
) => Uint; | ||
|
||
// https://github.com/ethereum/execution-apis/blob/main/src/eth/fee_market.json | ||
eth_gasPrice: () => Uint; | ||
eth_feeHistory: ( | ||
blockCount: Uint, | ||
newestBlock: BlockNumberOrTag, | ||
rewardPercentiles: number[], | ||
) => FeeHistoryResult; | ||
|
||
// https://github.com/ethereum/execution-apis/blob/main/src/eth/filter.json | ||
eth_newFilter: (filter: Filter) => Uint; | ||
eth_newBlockFilter: () => Uint; | ||
eth_newPendingTransactionFilter: () => Uint; | ||
eth_uninstallFilter: (filterIdentifier: Uint) => boolean; | ||
eth_getFilterChanges: (filterIdentifier: Uint) => FilterResults; | ||
eth_getFilterLogs: (filterIdentifier: Uint) => FilterResults; | ||
eth_getLogs: (filter: Filter) => FilterResults; | ||
|
||
// https://github.com/ethereum/execution-apis/blob/main/src/eth/mining.json | ||
eth_mining: () => boolean; | ||
eth_hashrate: () => Uint; | ||
eth_getWork: () => [HexString32Bytes, HexString32Bytes, HexString32Bytes]; | ||
eth_submitWork: ( | ||
nonce: HexString8Bytes, | ||
seedHash: HexString32Bytes, | ||
difficulty: HexString32Bytes, | ||
) => boolean; | ||
eth_submitHashrate: (hashRate: HexString32Bytes, id: HexString32Bytes) => boolean; | ||
|
||
// https://github.com/ethereum/execution-apis/blob/main/src/eth/sign.json | ||
eth_sign: (address: Address, message: HexStringBytes) => HexString256Bytes; | ||
eth_signTransaction: (transaction: TransactionWithSender) => HexStringBytes; | ||
|
||
// https://github.com/ethereum/execution-apis/blob/main/src/eth/state.json | ||
eth_getBalance: (address: Address, blockNumber: BlockNumberOrTag) => Uint; | ||
eth_getStorageAt: ( | ||
address: Address, | ||
storageSlot: Uint256, | ||
blockNumber: BlockNumberOrTag, | ||
) => HexStringBytes; | ||
eth_getTransactionCount: (address: Address, blockNumber: BlockNumberOrTag) => Uint; | ||
eth_getCode: (address: Address, blockNumber: BlockNumberOrTag) => HexStringBytes; | ||
|
||
// https://github.com/ethereum/execution-apis/blob/main/src/eth/submit.json | ||
eth_sendTransaction: (transaction: TransactionWithSender) => HexString32Bytes; | ||
eth_sendRawTransaction: (transaction: HexStringBytes) => HexString32Bytes; | ||
|
||
// https://geth.ethereum.org/docs/rpc/pubsub | ||
eth_subscribe: ( | ||
...params: | ||
| ['newHeads'] | ||
| ['newPendingTransactions'] | ||
| ['syncing'] | ||
| ['logs', { address: HexString; topic: HexString[] }] | ||
) => HexString; | ||
eth_unsubscribe: (subscriptionId: HexString) => HexString; | ||
|
||
// Non-supported by execution-apis specs | ||
eth_getCompilers: () => string[]; | ||
eth_compileSolidity: (code: string) => CompileResult; | ||
eth_compileLLL: (code: string) => HexStringBytes; | ||
eth_compileSerpent: (code: string) => HexStringBytes; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.