diff --git a/package.json b/package.json index 70cb90447f..a682902561 100644 --- a/package.json +++ b/package.json @@ -120,7 +120,7 @@ { "name": "viem/chains", "path": "./src/_esm/chains/index.js", - "limit": "28 kB", + "limit": "30 kB", "import": "*" }, { diff --git a/src/celo/fees.test.ts b/src/celo/fees.test.ts index e4a1527d8a..5983d6f892 100644 --- a/src/celo/fees.test.ts +++ b/src/celo/fees.test.ts @@ -1,7 +1,6 @@ import { describe, expect, test, vi } from 'vitest' import { celo } from '~viem/chains/index.js' import { http, createTestClient } from '~viem/index.js' -import type { ChainEstimateFeesPerGasFn } from '~viem/types/chain.js' const client = createTestClient({ transport: http(), @@ -10,8 +9,8 @@ const client = createTestClient({ }) describe('celo/fees', () => { - const celoestimateFeesPerGasFn = celo.fees - .estimateFeesPerGas as ChainEstimateFeesPerGasFn + const celoestimateFeesPerGasFn = celo.fees.estimateFeesPerGas + if (typeof celoestimateFeesPerGasFn !== 'function') return test("doesn't call the client when feeCurrency is not provided", async () => { const requestMock = vi.spyOn(client, 'request') diff --git a/src/celo/fees.ts b/src/celo/fees.ts index 8be6b495c0..7329f71687 100644 --- a/src/celo/fees.ts +++ b/src/celo/fees.ts @@ -5,7 +5,6 @@ import type { ChainFees, Hex, } from '../index.js' - import type { formatters } from './formatters.js' export const fees: ChainFees = { diff --git a/src/celo/formatters.test-d.ts b/src/celo/formatters.test-d.ts index f64c40606c..e83cc9ddc4 100644 --- a/src/celo/formatters.test-d.ts +++ b/src/celo/formatters.test-d.ts @@ -25,7 +25,7 @@ describe('block', () => { Assign< ExactPartial, CeloBlockOverrides & { - transactions: `0x${string}`[] | CeloRpcTransaction[] + transactions: readonly `0x${string}`[] | readonly CeloRpcTransaction[] } > >() diff --git a/src/celo/formatters.ts b/src/celo/formatters.ts index 6f7defe996..758a900ead 100644 --- a/src/celo/formatters.ts +++ b/src/celo/formatters.ts @@ -22,10 +22,10 @@ export const formatters = { exclude: ['difficulty', 'gasLimit', 'mixHash', 'nonce', 'uncles'], format( args: CeloBlockOverrides & { - transactions: Hash[] | CeloRpcTransaction[] + transactions: readonly Hash[] | readonly CeloRpcTransaction[] }, ): CeloBlockOverrides & { - transactions: Hash[] | CeloTransaction[] + transactions: readonly Hash[] | readonly CeloTransaction[] } { const transactions = args.transactions?.map((transaction) => { if (typeof transaction === 'string') return transaction @@ -41,7 +41,7 @@ export const formatters = { } : {}), } - }) as Hash[] | CeloTransaction[] + }) as readonly Hash[] | readonly CeloTransaction[] return { randomness: args.randomness, transactions, diff --git a/src/celo/serializers.ts b/src/celo/serializers.ts index 243403557b..b126d0c325 100644 --- a/src/celo/serializers.ts +++ b/src/celo/serializers.ts @@ -27,7 +27,6 @@ export function serializeTransaction( ) { if (isCIP64(transaction)) return serializeTransactionCIP64(transaction, signature) - return serializeTransaction_(transaction, signature) } diff --git a/src/celo/types.ts b/src/celo/types.ts index a57cc7afd1..3f869dcabc 100644 --- a/src/celo/types.ts +++ b/src/celo/types.ts @@ -7,19 +7,19 @@ import type { Index, Quantity, RpcBlock, - RpcTransactionRequest as RpcTransactionRequest_, - RpcTransaction as RpcTransaction_, TransactionType, + RpcTransaction as core_RpcTransaction, + RpcTransactionRequest as core_RpcTransactionRequest, } from '../types/rpc.js' import type { AccessList, TransactionBase, TransactionRequestBase, - TransactionRequest as TransactionRequest_, TransactionSerializable, TransactionSerializableBase, TransactionSerialized, - Transaction as Transaction_, + Transaction as core_Transaction, + TransactionRequest as core_TransactionRequest, } from '../types/transaction.js' import type { ExactPartial, NeverBy, OneOf } from '../types/utils.js' @@ -36,15 +36,16 @@ export type CeloBlockOverrides = { revealed: Hex } } + export type CeloBlock< - TIncludeTransactions extends boolean = boolean, - TBlockTag extends BlockTag = BlockTag, + includeTransactions extends boolean = boolean, + blockTag extends BlockTag = BlockTag, > = NeverBy< Block< bigint, - TIncludeTransactions, - TBlockTag, - CeloTransaction + includeTransactions, + blockTag, + CeloTransaction >, CeloBlockExclude > & @@ -57,31 +58,31 @@ export type CeloRpcBlockOverrides = { } } export type CeloRpcBlock< - TBlockTag extends BlockTag = BlockTag, - TIncludeTransactions extends boolean = boolean, + blockTag extends BlockTag = BlockTag, + includeTransactions extends boolean = boolean, > = NeverBy< RpcBlock< - TBlockTag, - TIncludeTransactions, - RpcTransaction + blockTag, + includeTransactions, + RpcTransaction >, CeloBlockExclude > & CeloRpcBlockOverrides -export type CeloRpcTransaction = - | RpcTransaction - | RpcTransactionCIP42 - | RpcTransactionCIP64 +export type CeloRpcTransaction = + | RpcTransaction + | RpcTransactionCIP42 + | RpcTransactionCIP64 export type CeloRpcTransactionRequest = | RpcTransactionRequest | RpcTransactionRequestCIP64 -export type CeloTransaction = - | Transaction - | TransactionCIP42 - | TransactionCIP64 +export type CeloTransaction = + | Transaction + | TransactionCIP42 + | TransactionCIP64 export type CeloTransactionRequest = | TransactionRequest @@ -100,111 +101,103 @@ export type CeloTransactionSerialized< export type CeloTransactionType = TransactionType | 'cip42' | 'cip64' -type RpcTransaction = - RpcTransaction_ & { +type RpcTransaction = + core_RpcTransaction & { feeCurrency: Address | null gatewayFee: Hex | null gatewayFeeRecipient: Address | null } -type RpcTransactionRequest = RpcTransactionRequest_ & { +type RpcTransactionRequest = core_RpcTransactionRequest & { feeCurrency?: Address | undefined } -export type RpcTransactionCIP42 = Omit< - TransactionBase, +export type RpcTransactionCIP42 = Omit< + TransactionBase, 'typeHex' -> & - FeeValuesEIP1559 & { - feeCurrency: Address | null - gatewayFee: Hex | null - gatewayFeeRecipient: Address | null - type: '0x7c' - } +> & { + feeCurrency: Address | null + gatewayFee: Hex | null + gatewayFeeRecipient: Address | null + type: '0x7c' +} & FeeValuesEIP1559 -export type RpcTransactionCIP64 = Omit< - TransactionBase, +export type RpcTransactionCIP64 = Omit< + TransactionBase, 'typeHex' -> & - FeeValuesEIP1559 & { - feeCurrency: Address | null - gatewayFee?: undefined - gatewayFeeRecipient?: undefined - type: '0x7b' - } +> & { + feeCurrency: Address | null + gatewayFee?: undefined + gatewayFeeRecipient?: undefined + type: '0x7b' +} & FeeValuesEIP1559 export type RpcTransactionRequestCIP64 = TransactionRequestBase< Quantity, Index -> & - ExactPartial> & { - accessList?: AccessList | undefined - feeCurrency?: Address | undefined - type?: '0x7b' | undefined - } +> & { + accessList?: AccessList | undefined + feeCurrency?: Address | undefined + type?: '0x7b' | undefined +} & ExactPartial> -type Transaction = Transaction_< +type Transaction = core_Transaction< bigint, number, - TPending + isPending > & { feeCurrency: Address | null gatewayFee?: undefined gatewayFeeRecipient?: undefined } -export type TransactionCIP42 = - TransactionBase & - FeeValuesEIP1559 & { - feeCurrency: Address | null - gatewayFee: bigint | null - gatewayFeeRecipient: Address | null - type: 'cip42' - } - -export type TransactionCIP64 = - TransactionBase & - FeeValuesEIP1559 & { - feeCurrency: Address | null - gatewayFee?: undefined - gatewayFeeRecipient?: undefined - type: 'cip64' - } - -type TransactionRequest = TransactionRequest_ & { +export type TransactionCIP42 = + TransactionBase & { + feeCurrency: Address | null + gatewayFee: bigint | null + gatewayFeeRecipient: Address | null + type: 'cip42' + } & FeeValuesEIP1559 + +export type TransactionCIP64 = + TransactionBase & { + feeCurrency: Address | null + gatewayFee?: undefined + gatewayFeeRecipient?: undefined + type: 'cip64' + } & FeeValuesEIP1559 + +type TransactionRequest = core_TransactionRequest & { feeCurrency?: Address | undefined } -export type TransactionRequestCIP64 = TransactionRequestBase & - ExactPartial & { - accessList?: AccessList | undefined - feeCurrency?: Address | undefined - type?: 'cip64' | undefined - } +export type TransactionRequestCIP64 = TransactionRequestBase & { + accessList?: AccessList | undefined + feeCurrency?: Address | undefined + type?: 'cip64' | undefined +} & ExactPartial export type TransactionSerializableCIP42< - TQuantity = bigint, - TIndex = number, -> = TransactionSerializableBase & - ExactPartial> & { - accessList?: AccessList | undefined - feeCurrency?: Address | undefined - gatewayFeeRecipient?: Address | undefined - gatewayFee?: TQuantity | undefined - chainId: number - type?: 'cip42' | undefined - } + quantity = bigint, + index = number, +> = TransactionSerializableBase & { + accessList?: AccessList | undefined + feeCurrency?: Address | undefined + gatewayFeeRecipient?: Address | undefined + gatewayFee?: quantity | undefined + chainId: number + type?: 'cip42' | undefined +} & ExactPartial> export type TransactionSerializableCIP64< - TQuantity = bigint, - TIndex = number, -> = TransactionSerializableBase & - ExactPartial> & { - accessList?: AccessList | undefined - feeCurrency?: Address | undefined - chainId: number - type?: 'cip64' | undefined - } + quantity = bigint, + index = number, +> = TransactionSerializableBase & { + accessList?: AccessList | undefined + feeCurrency?: Address | undefined + chainId: number + type?: 'cip64' | undefined +} & ExactPartial> export type TransactionSerializedCIP42 = `0x7c${string}` export type TransactionSerializedCIP64 = `0x7b${string}` diff --git a/src/clients/transports/fallback.test.ts b/src/clients/transports/fallback.test.ts index 3d3ad58907..5dae4c639d 100644 --- a/src/clients/transports/fallback.test.ts +++ b/src/clients/transports/fallback.test.ts @@ -750,7 +750,7 @@ describe('rankTransports', () => { const transport2 = http(server2.url, { key: '2' }) const transport3 = http(server3.url, { key: '3' }) - const rankedTransports: Transport[][] = [] + const rankedTransports: (readonly Transport[])[] = [] rankTransports({ chain: localhost, diff --git a/src/clients/transports/fallback.ts b/src/clients/transports/fallback.ts index f084fce526..0c7b28a628 100644 --- a/src/clients/transports/fallback.ts +++ b/src/clients/transports/fallback.ts @@ -80,20 +80,21 @@ export type FallbackTransportConfig = { retryDelay?: TransportConfig['retryDelay'] | undefined } -export type FallbackTransport = - Transport< - 'fallback', - { - onResponse: (fn: OnResponseFn) => void - transports: { - [key in keyof transports]: ReturnType - } +export type FallbackTransport< + transports extends readonly Transport[] = readonly Transport[], +> = Transport< + 'fallback', + { + onResponse: (fn: OnResponseFn) => void + transports: { + [key in keyof transports]: ReturnType } - > + } +> export type FallbackTransportErrorType = CreateTransportErrorType | ErrorType -export function fallback( +export function fallback( transports_: transports, config: FallbackTransportConfig = {}, ): FallbackTransport { @@ -205,10 +206,10 @@ export function rankTransports({ }: { chain?: Chain | undefined interval: RankOptions['interval'] - onTransports: (transports: Transport[]) => void + onTransports: (transports: readonly Transport[]) => void sampleCount?: RankOptions['sampleCount'] | undefined timeout?: RankOptions['timeout'] | undefined - transports: Transport[] + transports: readonly Transport[] weights?: RankOptions['weights'] | undefined }) { const { stability: stabilityWeight = 0.7, latency: latencyWeight = 0.3 } = diff --git a/src/package.json b/src/package.json index 09584538ce..f3b5cc2e83 100644 --- a/src/package.json +++ b/src/package.json @@ -137,5 +137,5 @@ "url": "https://github.com/sponsors/wevm" } ], - "keywords": ["eth", "ethereum", "dapps", "wallet", "web3"] + "keywords": ["eth", "ethereum", "dapps", "wallet", "web3", "typescript"] } diff --git a/src/types/block.ts b/src/types/block.ts index f40f9934a7..ef4b6dab8b 100644 --- a/src/types/block.ts +++ b/src/types/block.ts @@ -5,7 +5,7 @@ import type { Transaction } from './transaction.js' import type { Withdrawal } from './withdrawal.js' export type Block< - TQuantity = bigint, + quantity = bigint, TIncludeTransactions extends boolean = boolean, TBlockTag extends BlockTag = BlockTag, TTransaction = Transaction< @@ -15,19 +15,19 @@ export type Block< >, > = { /** Base fee per gas */ - baseFeePerGas: TQuantity | null + baseFeePerGas: quantity | null /** Total used blob gas by all transactions in this block */ - blobGasUsed: TQuantity + blobGasUsed: quantity /** Difficulty for this block */ - difficulty: TQuantity + difficulty: quantity /** Excess blob gas */ - excessBlobGas: TQuantity + excessBlobGas: quantity /** "Extra data" field of this block */ extraData: Hex /** Maximum gas allowed in this block */ - gasLimit: TQuantity + gasLimit: quantity /** Total used gas by all transactions in this block */ - gasUsed: TQuantity + gasUsed: quantity /** Block hash or `null` if pending */ hash: TBlockTag extends 'pending' ? null : Hash /** Logs bloom filter or `null` if pending */ @@ -39,7 +39,7 @@ export type Block< /** Proof-of-work hash or `null` if pending */ nonce: TBlockTag extends 'pending' ? null : Hex /** Block number or `null` if pending */ - number: TBlockTag extends 'pending' ? null : TQuantity + number: TBlockTag extends 'pending' ? null : quantity /** Parent block hash */ parentHash: Hash /** Root of the this block’s receipts trie */ @@ -48,13 +48,13 @@ export type Block< /** SHA3 of the uncles data in this block */ sha3Uncles: Hash /** Size of this block in bytes */ - size: TQuantity + size: quantity /** Root of this block’s final state trie */ stateRoot: Hash /** Unix timestamp of when this block was collated */ - timestamp: TQuantity + timestamp: quantity /** Total difficulty of the chain until this block */ - totalDifficulty: TQuantity | null + totalDifficulty: quantity | null /** List of transaction objects or hashes */ transactions: TIncludeTransactions extends true ? TTransaction[] : Hash[] /** Root of this block’s transaction trie */ @@ -67,13 +67,13 @@ export type Block< withdrawalsRoot?: Hex | undefined } -export type BlockIdentifier = { +export type BlockIdentifier = { /** Whether or not to throw an error if the block is not in the canonical chain as described below. Only allowed in conjunction with the blockHash tag. Defaults to false. */ requireCanonical?: boolean | undefined } & ( | { /** The block in the canonical chain with this number */ - blockNumber: BlockNumber + blockNumber: BlockNumber } | { /** The block uniquely identified by this hash. The `blockNumber` and `blockHash` properties are mutually exclusive; exactly one of them must be set. */ @@ -81,12 +81,12 @@ export type BlockIdentifier = { } ) -export type BlockNumber = TQuantity +export type BlockNumber = quantity export type BlockTag = 'latest' | 'earliest' | 'pending' | 'safe' | 'finalized' export type Uncle< - TQuantity = bigint, + quantity = bigint, TIncludeTransactions extends boolean = boolean, TBlockTag extends BlockTag = BlockTag, TTransaction = Transaction< @@ -94,4 +94,4 @@ export type Uncle< number, TBlockTag extends 'pending' ? true : false >, -> = Block +> = Block diff --git a/src/types/chain.test-d.ts b/src/types/chain.test-d.ts index 9fda3f5fc2..e55f23f090 100644 --- a/src/types/chain.test-d.ts +++ b/src/types/chain.test-d.ts @@ -1,8 +1,16 @@ import { expectTypeOf, test } from 'vitest' +import type { base } from '~viem/chains/index.js' +import type { celo } from '../chains/definitions/celo.js' import type { mainnet } from '../chains/definitions/mainnet.js' import type { optimism } from '../chains/definitions/optimism.js' -import type { Chain, DeriveChain } from './chain.js' +import type { + Chain, + DeriveChain, + ExtractChainFormatterParameters, + GetChainParameter, +} from './chain.js' +import type { TransactionRequest } from './transaction.js' test('DeriveChain', () => { type Result = DeriveChain @@ -20,3 +28,60 @@ test('DeriveChain', () => { type Result5 = DeriveChain expectTypeOf().toEqualTypeOf() }) + +test('ExtractChainFormatterParameters', () => { + type Result = ExtractChainFormatterParameters< + typeof mainnet, + 'transactionRequest', + TransactionRequest + > + expectTypeOf().toEqualTypeOf< + 'legacy' | 'eip2930' | 'eip1559' | 'eip4844' | undefined + >() + + type Result2 = ExtractChainFormatterParameters< + typeof base, + 'transactionRequest', + TransactionRequest + > + expectTypeOf().toEqualTypeOf< + 'legacy' | 'eip2930' | 'eip1559' | 'eip4844' | undefined + >() + + type Result3 = ExtractChainFormatterParameters< + typeof celo, + 'transactionRequest', + TransactionRequest + > + expectTypeOf().toEqualTypeOf< + 'legacy' | 'eip2930' | 'eip1559' | 'eip4844' | 'cip64' | undefined + >() + expectTypeOf().toEqualTypeOf< + `0x${string}` | undefined + >() +}) + +test('GetChainParameter', () => { + type Result = GetChainParameter + expectTypeOf().toEqualTypeOf<{ chain: Chain | null | undefined }>() + + type Result2 = GetChainParameter + expectTypeOf().toEqualTypeOf<{ chain?: Chain | null | undefined }>() + + type Result3 = GetChainParameter + expectTypeOf().toEqualTypeOf<{ chain: Chain | null }>() + + type Result4 = GetChainParameter + expectTypeOf().toEqualTypeOf<{ chain: Chain | null }>() + + type Result5 = GetChainParameter + expectTypeOf().toEqualTypeOf<{ chain?: undefined | null }>() + + type Result6 = GetChainParameter + expectTypeOf().toEqualTypeOf<{ chain?: undefined | null }>() + + type Result7 = GetChainParameter + expectTypeOf().toEqualTypeOf<{ + chain?: typeof optimism | undefined | null + }>() +}) diff --git a/src/types/chain.ts b/src/types/chain.ts index 2d652f4c10..2219ffead4 100644 --- a/src/types/chain.ts +++ b/src/types/chain.ts @@ -61,41 +61,12 @@ export type Chain< /** Custom chain data. */ custom?: custom | undefined - /** - * Modifies how chain data structures (ie. Blocks, Transactions, etc) - * are formatted & typed. - */ - formatters?: formatters | undefined - /** Modifies how data (ie. Transactions) is serialized. */ - serializers?: ChainSerializers | undefined /** Modifies how fees are derived. */ fees?: ChainFees | undefined -} - -///////////////////////////////////////////////////////////////////// -// Constants - -type ChainBlockExplorer = { - name: string - url: string - apiUrl?: string | undefined -} - -export type ChainContract = { - address: Address - blockCreated?: number | undefined -} - -type ChainNativeCurrency = { - name: string - /** 2-6 characters long */ - symbol: string - decimals: number -} - -type ChainRpcUrls = { - http: readonly string[] - webSocket?: readonly string[] | undefined + /** Modifies how data is formatted and typed (e.g. blocks and transactions) */ + formatters?: formatters | undefined + /** Modifies how data is serialized (e.g. transactions). */ + serializers?: ChainSerializers | undefined } ///////////////////////////////////////////////////////////////////// @@ -129,17 +100,13 @@ export type ChainFees< * Overrides the return value in the [`estimateFeesPerGas` Action](/docs/actions/public/estimateFeesPerGas). */ estimateFeesPerGas?: - | ChainEstimateFeesPerGasFn | bigint + | (( + args: ChainEstimateFeesPerGasFnParameters, + ) => Promise) | undefined } -export type ChainEstimateFeesPerGasFn< - formatters extends ChainFormatters | undefined = ChainFormatters | undefined, -> = ( - args: ChainEstimateFeesPerGasFnParameters, -) => Promise - export type ChainFormatters = { /** Modifies how the Block structure is formatted & typed. */ block?: ChainFormatter<'block'> | undefined @@ -158,18 +125,18 @@ export type ChainFormatter = { export type ChainSerializers< formatters extends ChainFormatters | undefined = undefined, + /// + transaction extends + TransactionSerializableGeneric = formatters extends ChainFormatters + ? formatters['transactionRequest'] extends ChainFormatter + ? TransactionSerializableGeneric & + Parameters[0] + : TransactionSerializable + : TransactionSerializable, > = { /** Modifies how Transactions are serialized. */ transaction?: - | SerializeTransactionFn< - formatters extends ChainFormatters - ? formatters['transactionRequest'] extends ChainFormatter - ? TransactionSerializableGeneric & - Parameters[0] - : TransactionSerializable - : TransactionSerializable, - TransactionSerializedGeneric - > + | SerializeTransactionFn | undefined } @@ -201,24 +168,15 @@ export type ChainFeesFnParameters< export type ChainEstimateFeesPerGasFnParameters< formatters extends ChainFormatters | undefined = ChainFormatters | undefined, > = { - /** - * A function to multiply the base fee based on the `baseFeeMultiplier` value. - */ + /** A function to multiply the base fee based on the `baseFeeMultiplier` value. */ multiply(x: bigint): bigint - /** - * The type of fees to return. - */ + /** The type of fees to return. */ type: FeeValuesType } & ChainFeesFnParameters ///////////////////////////////////////////////////////////////////// // Utils -export type ExtractChain< - chains extends readonly Chain[], - chainId extends Chain['id'], -> = Extract - export type ExtractChainFormatterExclude< chain extends Chain | undefined, type extends keyof ChainFormatters, @@ -267,3 +225,29 @@ export type GetChainParameter< > = IsUndefined extends true ? { chain: chainOverride | null } : { chain?: chainOverride | null | undefined } + +///////////////////////////////////////////////////////////////////// +// Constants + +type ChainBlockExplorer = { + name: string + url: string + apiUrl?: string | undefined +} + +export type ChainContract = { + address: Address + blockCreated?: number | undefined +} + +type ChainNativeCurrency = { + name: string + /** 2-6 characters long */ + symbol: string + decimals: number +} + +type ChainRpcUrls = { + http: readonly string[] + webSocket?: readonly string[] | undefined +} diff --git a/src/types/eip1193.ts b/src/types/eip1193.ts index ee81f77325..5a7fc58a5c 100644 --- a/src/types/eip1193.ts +++ b/src/types/eip1193.ts @@ -366,7 +366,7 @@ export type PublicRpcSchema = [ | [ transaction: TransactionRequest, block: BlockNumber | BlockTag, - RpcStateOverride, + stateOverride: RpcStateOverride, ] ReturnType: Quantity }, @@ -1237,7 +1237,7 @@ export type WalletRpcSchema = [ | [ transaction: TransactionRequest, block: BlockNumber | BlockTag, - RpcStateOverride, + stateOverride: RpcStateOverride, ] ReturnType: Quantity }, diff --git a/src/types/fee.ts b/src/types/fee.ts index 69a1badadf..62b855cfd3 100644 --- a/src/types/fee.ts +++ b/src/types/fee.ts @@ -1,48 +1,50 @@ -import type { Assign, OneOf } from './utils.js' +import type { OneOf } from './utils.js' -export type FeeHistory = { +export type FeeHistory = { /** * An array of block base fees per gas (in wei). This includes the next block after * the newest of the returned range, because this value can be derived from the newest block. * Zeroes are returned for pre-EIP-1559 blocks. */ - baseFeePerGas: TQuantity[] + baseFeePerGas: quantity[] /** An array of block gas used ratios. These are calculated as the ratio of gasUsed and gasLimit. */ gasUsedRatio: number[] /** Lowest number block of the returned range. */ - oldestBlock: TQuantity + oldestBlock: quantity /** An array of effective priority fees (in wei) per gas data points from a single block. All zeroes are returned if the block is empty. */ - reward?: TQuantity[][] | undefined + reward?: quantity[][] | undefined } -export type FeeValuesLegacy = { +export type FeeValuesLegacy = { /** Base fee per gas. */ - gasPrice: TQuantity + gasPrice: quantity maxFeePerBlobGas?: undefined maxFeePerGas?: undefined maxPriorityFeePerGas?: undefined } -export type FeeValuesEIP1559 = { +export type FeeValuesEIP1559 = { gasPrice?: undefined maxFeePerBlobGas?: undefined /** Total fee per gas in wei (gasPrice/baseFeePerGas + maxPriorityFeePerGas). */ - maxFeePerGas: TQuantity + maxFeePerGas: quantity /** Max priority fee per gas (in wei). */ - maxPriorityFeePerGas: TQuantity + maxPriorityFeePerGas: quantity } -export type FeeValuesEIP4844 = Assign< - FeeValuesEIP1559, - { - /** The maximum total fee per gas the sender is willing to pay for blob gas (in wei). */ - maxFeePerBlobGas: TQuantity - } -> +export type FeeValuesEIP4844 = { + gasPrice?: undefined + /** Maximum total fee per gas sender is willing to pay for blob gas (in wei). */ + maxFeePerBlobGas: quantity + /** Total fee per gas in wei (gasPrice/baseFeePerGas + maxPriorityFeePerGas). */ + maxFeePerGas: quantity + /** Max priority fee per gas (in wei). */ + maxPriorityFeePerGas: quantity +} -export type FeeValues = OneOf< - | FeeValuesLegacy - | FeeValuesEIP1559 - | FeeValuesEIP4844 +export type FeeValues = OneOf< + | FeeValuesLegacy + | FeeValuesEIP1559 + | FeeValuesEIP4844 > export type FeeValuesType = 'legacy' | 'eip1559' | 'eip4844' diff --git a/src/types/rpc.ts b/src/types/rpc.ts index e7afc51671..add792a9f2 100644 --- a/src/types/rpc.ts +++ b/src/types/rpc.ts @@ -1,4 +1,5 @@ import type { Address } from 'abitype' + import type { Block, BlockIdentifier, @@ -21,7 +22,7 @@ import type { TransactionRequestEIP4844, TransactionRequestLegacy, } from './transaction.js' -import type { OneOf, UnionOmit, UnionPartialBy } from './utils.js' +import type { Omit, OneOf, PartialBy } from './utils.js' export type Index = `0x${string}` export type Quantity = `0x${string}` @@ -52,47 +53,49 @@ export type RpcTransactionRequest = OneOf< | TransactionRequestEIP1559 | TransactionRequestEIP4844 > -export type RpcTransaction = UnionOmit< - UnionPartialBy< - OneOf< - | TransactionLegacy - | TransactionEIP2930 - | TransactionEIP1559 - | TransactionEIP4844 - >, - // `yParity` is optional on the RPC type as some nodes do not return it - // for 1559 & 2930 transactions (they should!). - 'yParity' - >, - 'typeHex' +// `yParity` is optional on the RPC type as some nodes do not return it +// for 1559 & 2930 transactions (they should!). +export type RpcTransaction = OneOf< + | Omit, 'typeHex'> + | PartialBy< + Omit, 'typeHex'>, + 'yParity' + > + | PartialBy< + Omit, 'typeHex'>, + 'yParity' + > + | PartialBy< + Omit, 'typeHex'>, + 'yParity' + > > -type SuccessResult = { +type SuccessResult = { method?: undefined - result: T + result: result error?: undefined } -type ErrorResult = { +type ErrorResult = { method?: undefined result?: undefined - error: T + error: error } -type Subscription = { +type Subscription = { method: 'eth_subscription' error?: undefined result?: undefined - params: { - subscription: string - } & ( + params: | { - result: TResult + subscription: string + result: result error?: undefined } | { + subscription: string result?: undefined - error: TError + error: error } - ) } export type RpcRequest = { @@ -102,14 +105,10 @@ export type RpcRequest = { id?: number | undefined } -export type RpcResponse = { +export type RpcResponse = { jsonrpc: `${number}` id: number -} & ( - | SuccessResult - | ErrorResult - | Subscription -) +} & (SuccessResult | ErrorResult | Subscription) /** A key-value mapping of slot and storage values (supposedly 32 bytes each) */ export type RpcStateMapping = { diff --git a/src/types/transaction.ts b/src/types/transaction.ts index 5323927a6e..04b9b7675c 100644 --- a/src/types/transaction.ts +++ b/src/types/transaction.ts @@ -13,11 +13,15 @@ import type { Branded, ExactPartial, IsNever, + Omit, OneOf, RequiredBy, } from './utils.js' -export type AccessList = { address: Address; storageKeys: Hex[] }[] +export type AccessList = readonly { + address: Address + storageKeys: readonly Hex[] +}[] export type TransactionType = | 'legacy' @@ -27,66 +31,66 @@ export type TransactionType = | (string & {}) export type TransactionReceipt< - TQuantity = bigint, - TIndex = number, - TStatus = 'success' | 'reverted', - TType = TransactionType, + quantity = bigint, + index = number, + status = 'success' | 'reverted', + type = TransactionType, > = { /** The actual value per gas deducted from the sender's account for blob gas. Only specified for blob transactions as defined by EIP-4844. */ - blobGasPrice?: TQuantity | undefined + blobGasPrice?: quantity | undefined /** The amount of blob gas used. Only specified for blob transactions as defined by EIP-4844. */ - blobGasUsed?: TQuantity | undefined + blobGasUsed?: quantity | undefined /** Hash of block containing this transaction */ blockHash: Hash /** Number of block containing this transaction */ - blockNumber: TQuantity + blockNumber: quantity /** Address of new contract or `null` if no contract was created */ contractAddress: Address | null | undefined /** Gas used by this and all preceding transactions in this block */ - cumulativeGasUsed: TQuantity + cumulativeGasUsed: quantity /** Pre-London, it is equal to the transaction's gasPrice. Post-London, it is equal to the actual gas price paid for inclusion. */ - effectiveGasPrice: TQuantity + effectiveGasPrice: quantity /** Transaction sender */ from: Address /** Gas used by this transaction */ - gasUsed: TQuantity + gasUsed: quantity /** List of log objects generated by this transaction */ - logs: Log[] + logs: Log[] /** Logs bloom filter */ logsBloom: Hex /** The post-transaction state root. Only specified for transactions included before the Byzantium upgrade. */ root?: Hash | undefined /** `success` if this transaction was successful or `reverted` if it failed */ - status: TStatus + status: status /** Transaction recipient or `null` if deploying a contract */ to: Address | null /** Hash of this transaction */ transactionHash: Hash /** Index of this transaction in the block */ - transactionIndex: TIndex + transactionIndex: index /** Transaction type */ - type: TType + type: type } export type TransactionBase< - TQuantity = bigint, - TIndex = number, - TPending extends boolean = boolean, + quantity = bigint, + index = number, + isPending extends boolean = boolean, > = { /** Hash of block containing this transaction or `null` if pending */ - blockHash: TPending extends true ? null : Hash + blockHash: isPending extends true ? null : Hash /** Number of block containing this transaction or `null` if pending */ - blockNumber: TPending extends true ? null : TQuantity + blockNumber: isPending extends true ? null : quantity /** Transaction sender */ from: Address /** Gas provided for transaction execution */ - gas: TQuantity + gas: quantity /** Hash of this transaction */ hash: Hash /** Contract code or a hashed method call */ input: Hex /** Unique number identifying this transaction */ - nonce: TIndex + nonce: index /** ECDSA signature r */ r: Hex /** ECDSA signature s */ @@ -94,233 +98,241 @@ export type TransactionBase< /** Transaction recipient or `null` if deploying a contract */ to: Address | null /** Index of this transaction in the block or `null` if pending */ - transactionIndex: TPending extends true ? null : TIndex + transactionIndex: isPending extends true ? null : index /** The type represented as hex. */ typeHex: Hex | null /** ECDSA recovery ID */ - v: TQuantity + v: quantity /** Value in wei sent with this transaction */ - value: TQuantity + value: quantity /** The parity of the y-value of the secp256k1 signature. */ - yParity: TIndex + yParity: index } + export type TransactionLegacy< - TQuantity = bigint, - TIndex = number, - TPending extends boolean = boolean, - TType = 'legacy', -> = Omit, 'yParity'> & - FeeValuesLegacy & { - /** EIP-2930 Access List. */ - accessList?: undefined - blobVersionedHashes?: undefined - /** Chain ID that this transaction is valid on. */ - chainId?: TIndex | undefined - yParity?: undefined - type: TType - } + quantity = bigint, + index = number, + isPending extends boolean = boolean, + type = 'legacy', +> = Omit, 'yParity'> & { + /** EIP-2930 Access List. */ + accessList?: undefined + blobVersionedHashes?: undefined + /** Chain ID that this transaction is valid on. */ + chainId?: index | undefined + yParity?: undefined + type: type +} & FeeValuesLegacy + export type TransactionEIP2930< - TQuantity = bigint, - TIndex = number, - TPending extends boolean = boolean, - TType = 'eip2930', -> = TransactionBase & - FeeValuesLegacy & { - /** EIP-2930 Access List. */ - accessList: AccessList - blobVersionedHashes?: undefined - /** Chain ID that this transaction is valid on. */ - chainId: TIndex - type: TType - } + quantity = bigint, + index = number, + isPending extends boolean = boolean, + type = 'eip2930', +> = TransactionBase & { + /** EIP-2930 Access List. */ + accessList: AccessList + blobVersionedHashes?: undefined + /** Chain ID that this transaction is valid on. */ + chainId: index + type: type +} & FeeValuesLegacy + export type TransactionEIP1559< - TQuantity = bigint, - TIndex = number, - TPending extends boolean = boolean, - TType = 'eip1559', -> = TransactionBase & - FeeValuesEIP1559 & { - /** EIP-2930 Access List. */ - accessList: AccessList - blobVersionedHashes?: undefined - /** Chain ID that this transaction is valid on. */ - chainId: TIndex - type: TType - } + quantity = bigint, + index = number, + isPending extends boolean = boolean, + type = 'eip1559', +> = TransactionBase & { + /** EIP-2930 Access List. */ + accessList: AccessList + blobVersionedHashes?: undefined + /** Chain ID that this transaction is valid on. */ + chainId: index + type: type +} & FeeValuesEIP1559 + export type TransactionEIP4844< - TQuantity = bigint, - TIndex = number, - TPending extends boolean = boolean, - TType = 'eip4844', -> = TransactionBase & - FeeValuesEIP4844 & { - /** EIP-2930 Access List. */ - accessList: AccessList - /** List of versioned blob hashes associated with the transaction's blobs. */ - blobVersionedHashes: readonly Hex[] - /** Chain ID that this transaction is valid on. */ - chainId: TIndex - type: TType - } + quantity = bigint, + index = number, + isPending extends boolean = boolean, + type = 'eip4844', +> = TransactionBase & { + /** EIP-2930 Access List. */ + accessList: AccessList + /** List of versioned blob hashes associated with the transaction's blobs. */ + blobVersionedHashes: readonly Hex[] + /** Chain ID that this transaction is valid on. */ + chainId: index + type: type +} & FeeValuesEIP4844 + export type Transaction< - TQuantity = bigint, - TIndex = number, - TPending extends boolean = boolean, + quantity = bigint, + index = number, + isPending extends boolean = boolean, > = OneOf< - | TransactionLegacy - | TransactionEIP2930 - | TransactionEIP1559 - | TransactionEIP4844 + | TransactionLegacy + | TransactionEIP2930 + | TransactionEIP1559 + | TransactionEIP4844 > -export type TransactionRequestBase = { +//////////////////////////////////////////////////////////////////////////////////////////// +// Request +//////////////////////////////////////////////////////////////////////////////////////////// + +export type TransactionRequestBase = { /** Contract code or a hashed method call with encoded args */ data?: Hex | undefined /** Transaction sender */ from: Address /** Gas provided for transaction execution */ - gas?: TQuantity | undefined + gas?: quantity | undefined /** Unique number identifying this transaction */ - nonce?: TIndex | undefined + nonce?: index | undefined /** Transaction recipient */ to?: Address | null | undefined /** Value in wei sent with this transaction */ - value?: TQuantity | undefined + value?: quantity | undefined } + export type TransactionRequestLegacy< - TQuantity = bigint, - TIndex = number, - TTransactionType = 'legacy', -> = TransactionRequestBase & - ExactPartial> & { - accessList?: undefined - blobs?: undefined - type?: TTransactionType | undefined - } + quantity = bigint, + index = number, + type = 'legacy', +> = TransactionRequestBase & { + accessList?: undefined + blobs?: undefined + type?: type | undefined +} & ExactPartial> + export type TransactionRequestEIP2930< - TQuantity = bigint, - TIndex = number, - TTransactionType = 'eip2930', -> = TransactionRequestBase & - ExactPartial> & { - accessList?: AccessList | undefined - blobs?: undefined - type?: TTransactionType | undefined - } + quantity = bigint, + index = number, + type = 'eip2930', +> = TransactionRequestBase & { + accessList?: AccessList | undefined + blobs?: undefined + type?: type | undefined +} & ExactPartial> + export type TransactionRequestEIP1559< - TQuantity = bigint, - TIndex = number, - TTransactionType = 'eip1559', -> = TransactionRequestBase & - ExactPartial> & { + quantity = bigint, + index = number, + type = 'eip1559', +> = TransactionRequestBase & + ExactPartial> & { accessList?: AccessList | undefined blobs?: undefined - type?: TTransactionType | undefined + type?: type | undefined } + export type TransactionRequestEIP4844< - TQuantity = bigint, - TIndex = number, - TTransactionType = 'eip4844', -> = RequiredBy, 'to'> & - RequiredBy>, 'maxFeePerBlobGas'> & { - accessList?: AccessList | undefined - /** The blobs associated with this transaction. */ - blobs: readonly Hex[] | readonly ByteArray[] - blobVersionedHashes?: readonly Hex[] | undefined - kzg?: Kzg | undefined - sidecars?: readonly BlobSidecar[] | undefined - type?: TTransactionType | undefined - } -export type TransactionRequest = OneOf< - | TransactionRequestLegacy - | TransactionRequestEIP2930 - | TransactionRequestEIP1559 - | TransactionRequestEIP4844 + quantity = bigint, + index = number, + type = 'eip4844', +> = RequiredBy, 'to'> & { + accessList?: AccessList | undefined + /** The blobs associated with this transaction. */ + blobs: readonly Hex[] | readonly ByteArray[] + blobVersionedHashes?: readonly Hex[] | undefined + kzg?: Kzg | undefined + sidecars?: readonly BlobSidecar[] | undefined + type?: type | undefined +} & RequiredBy>, 'maxFeePerBlobGas'> + +export type TransactionRequest = OneOf< + | TransactionRequestLegacy + | TransactionRequestEIP2930 + | TransactionRequestEIP1559 + | TransactionRequestEIP4844 > export type TransactionRequestGeneric< - TQuantity = bigint, - TIndex = number, -> = TransactionRequestBase & { + quantity = bigint, + index = number, +> = TransactionRequestBase & { accessList?: AccessList | undefined blobs?: readonly Hex[] | readonly ByteArray[] | undefined blobVersionedHashes?: readonly Hex[] | undefined - gasPrice?: TQuantity | undefined - maxFeePerBlobGas?: TQuantity | undefined - maxFeePerGas?: TQuantity | undefined - maxPriorityFeePerGas?: TQuantity | undefined + gasPrice?: quantity | undefined + maxFeePerBlobGas?: quantity | undefined + maxFeePerGas?: quantity | undefined + maxPriorityFeePerGas?: quantity | undefined type?: string | undefined } +//////////////////////////////////////////////////////////////////////////////////////////// +// Serializable +//////////////////////////////////////////////////////////////////////////////////////////// + export type TransactionSerializedEIP1559 = `0x02${string}` export type TransactionSerializedEIP2930 = `0x01${string}` export type TransactionSerializedEIP4844 = `0x03${string}` export type TransactionSerializedLegacy = Branded<`0x${string}`, 'legacy'> export type TransactionSerializedGeneric = `0x${string}` export type TransactionSerialized< - TType extends TransactionType = TransactionType, + type extends TransactionType = TransactionType, result = - | (TType extends 'eip1559' ? TransactionSerializedEIP1559 : never) - | (TType extends 'eip2930' ? TransactionSerializedEIP2930 : never) - | (TType extends 'eip4844' ? TransactionSerializedEIP4844 : never) - | (TType extends 'legacy' ? TransactionSerializedLegacy : never), + | (type extends 'eip1559' ? TransactionSerializedEIP1559 : never) + | (type extends 'eip2930' ? TransactionSerializedEIP2930 : never) + | (type extends 'eip4844' ? TransactionSerializedEIP4844 : never) + | (type extends 'legacy' ? TransactionSerializedLegacy : never), > = IsNever extends true ? TransactionSerializedGeneric : result export type TransactionSerializableBase< - TQuantity = bigint, - TIndex = number, -> = Omit, 'from'> & + quantity = bigint, + index = number, +> = Omit, 'from'> & ExactPartial export type TransactionSerializableLegacy< - TQuantity = bigint, - TIndex = number, -> = TransactionSerializableBase & - ExactPartial> & { - accessList?: undefined - blobs?: undefined - blobVersionedHashes?: undefined - chainId?: number | undefined - type?: 'legacy' | undefined - } + quantity = bigint, + index = number, +> = TransactionSerializableBase & { + accessList?: undefined + blobs?: undefined + blobVersionedHashes?: undefined + chainId?: number | undefined + type?: 'legacy' | undefined +} & ExactPartial> export type TransactionSerializableEIP2930< - TQuantity = bigint, - TIndex = number, -> = TransactionSerializableBase & - ExactPartial> & { - accessList?: AccessList | undefined - blobs?: undefined - blobVersionedHashes?: undefined - chainId: number - type?: 'eip2930' | undefined - yParity?: number | undefined - } + quantity = bigint, + index = number, +> = TransactionSerializableBase & { + accessList?: AccessList | undefined + blobs?: undefined + blobVersionedHashes?: undefined + chainId: number + type?: 'eip2930' | undefined + yParity?: number | undefined +} & ExactPartial> export type TransactionSerializableEIP1559< - TQuantity = bigint, - TIndex = number, -> = TransactionSerializableBase & - ExactPartial> & { - accessList?: AccessList | undefined - blobs?: undefined - blobVersionedHashes?: undefined - chainId: number - type?: 'eip1559' | undefined - yParity?: number | undefined - } + quantity = bigint, + index = number, +> = TransactionSerializableBase & { + accessList?: AccessList | undefined + blobs?: undefined + blobVersionedHashes?: undefined + chainId: number + type?: 'eip1559' | undefined + yParity?: number | undefined +} & ExactPartial> export type TransactionSerializableEIP4844< - TQuantity = bigint, - TIndex = number, -> = TransactionSerializableBase & - ExactPartial> & { - accessList?: AccessList | undefined - chainId: number - sidecars?: readonly BlobSidecar[] | false | undefined - type?: 'eip4844' | undefined - yParity?: number | undefined - } & OneOf< + quantity = bigint, + index = number, +> = TransactionSerializableBase & { + accessList?: AccessList | undefined + chainId: number + sidecars?: readonly BlobSidecar[] | false | undefined + type?: 'eip4844' | undefined + yParity?: number | undefined +} & ExactPartial> & + OneOf< | { blobs?: readonly Hex[] | readonly ByteArray[] | undefined blobVersionedHashes: readonly Hex[] @@ -332,28 +344,25 @@ export type TransactionSerializableEIP4844< } > +export type TransactionSerializable = OneOf< + | TransactionSerializableLegacy + | TransactionSerializableEIP2930 + | TransactionSerializableEIP1559 + | TransactionSerializableEIP4844 +> + export type TransactionSerializableGeneric< - TQuantity = bigint, - TIndex = number, -> = TransactionSerializableBase & { + quantity = bigint, + index = number, +> = TransactionSerializableBase & { accessList?: AccessList | undefined blobs?: readonly Hex[] | readonly ByteArray[] | undefined blobVersionedHashes?: readonly Hex[] | undefined chainId?: number | undefined - gasPrice?: TQuantity | undefined - maxFeePerBlobGas?: TQuantity | undefined - maxFeePerGas?: TQuantity | undefined - maxPriorityFeePerGas?: TQuantity | undefined + gasPrice?: quantity | undefined + maxFeePerBlobGas?: quantity | undefined + maxFeePerGas?: quantity | undefined + maxPriorityFeePerGas?: quantity | undefined sidecars?: readonly BlobSidecar[] | false | undefined type?: string | undefined } - -export type TransactionSerializable< - TQuantity = bigint, - TIndex = number, -> = OneOf< - | TransactionSerializableLegacy - | TransactionSerializableEIP2930 - | TransactionSerializableEIP1559 - | TransactionSerializableEIP4844 -> diff --git a/src/types/transport.test-d.ts b/src/types/transport.test-d.ts new file mode 100644 index 0000000000..cd596ffc1d --- /dev/null +++ b/src/types/transport.test-d.ts @@ -0,0 +1,45 @@ +import { expectTypeOf, test } from 'vitest' + +import { fallback } from '../clients/transports/fallback.js' +import { http } from '../clients/transports/http.js' +import { webSocket } from '../clients/transports/webSocket.js' +import type { HasTransportType } from './transport.js' + +test('HasTransportType', () => { + { + const transport = webSocket() + expectTypeOf< + HasTransportType + >().toEqualTypeOf() + expectTypeOf< + HasTransportType + >().toEqualTypeOf() + } + { + const transport = http() + expectTypeOf< + HasTransportType + >().toEqualTypeOf() + expectTypeOf< + HasTransportType + >().toEqualTypeOf() + } + { + const transport = fallback([http(), webSocket()]) + expectTypeOf< + HasTransportType + >().toEqualTypeOf() + expectTypeOf< + HasTransportType + >().toEqualTypeOf() + } + { + const transport = fallback([webSocket()]) + expectTypeOf< + HasTransportType + >().toEqualTypeOf() + expectTypeOf< + HasTransportType + >().toEqualTypeOf() + } +}) diff --git a/src/types/transport.ts b/src/types/transport.ts index caf7cd639d..9b4bc06ce0 100644 --- a/src/types/transport.ts +++ b/src/types/transport.ts @@ -36,7 +36,9 @@ export type HasTransportType< type extends string, > = GetTransportConfig['type'] extends type ? true - : transport extends FallbackTransport + : transport extends FallbackTransport< + infer transports extends readonly Transport[] + > ? Some< { [key in keyof transports]: GetTransportConfig['type'] diff --git a/src/types/utils.ts b/src/types/utils.ts index 9058eabb67..4d95e022e0 100644 --- a/src/types/utils.ts +++ b/src/types/utils.ts @@ -57,6 +57,11 @@ export type IsNarrowable = IsNever< */ export type IsNever = [T] extends [never] ? true : false +/** Removes `readonly` from all properties of an object. */ +export type Mutable = { + -readonly [key in keyof type]: type[key] +} + /** * @description Returns type {@link T} if it is an opaque type of {@link U} * @param T - Type to check @@ -157,6 +162,7 @@ export type NoInfer = [type][type extends any ? 0 : never] */ export type NoUndefined = T extends undefined ? never : T +/** Strict version of built-in Omit type */ export type Omit = Pick< type, Exclude @@ -184,6 +190,10 @@ export type Prettify = { [K in keyof T]: T[K] } & {} +export type Evaluate = { + [K in keyof T]: T[K] +} & {} + /** * @description Creates a type that is T with the required keys K. * @@ -201,12 +211,12 @@ export type RequiredBy = Omit & * Some<[1, 2, 3], 2> * => true */ -export type Some = array extends [ +export type Some< + array extends readonly unknown[], value, - ...unknown[], -] +> = array extends readonly [value, ...unknown[]] ? true - : array extends [unknown, ...infer rest] + : array extends readonly [unknown, ...infer rest] ? Some : false diff --git a/src/utils/chain/extractChain.test.ts b/src/utils/chain/extractChain.test.ts index 8daecbb7e5..150faa3d90 100644 --- a/src/utils/chain/extractChain.test.ts +++ b/src/utils/chain/extractChain.test.ts @@ -1,4 +1,4 @@ -import { expect, test } from 'vitest' +import { expect, expectTypeOf, test } from 'vitest' import * as chains from '../../chains/index.js' import { extractChain } from './extractChain.js' @@ -8,6 +8,7 @@ test('default', async () => { chains: Object.values(chains), id: 1, }) + expectTypeOf(mainnet).toEqualTypeOf() expect(mainnet).toMatchInlineSnapshot(` { "blockExplorers": { @@ -54,6 +55,7 @@ test('default', async () => { chains: Object.values(chains), id: 10, }) + expectTypeOf(optimism).toEqualTypeOf() expect(optimism).toMatchInlineSnapshot(` { "blockExplorers": { diff --git a/src/utils/chain/extractChain.ts b/src/utils/chain/extractChain.ts index 0e9e187ffe..2bb828d433 100644 --- a/src/utils/chain/extractChain.ts +++ b/src/utils/chain/extractChain.ts @@ -1,5 +1,5 @@ import type { ErrorType } from '../../errors/utils.js' -import type { Chain, ExtractChain } from '../../types/chain.js' +import type { Chain } from '../../types/chain.js' export type ExtractChainParameters< chains extends readonly Chain[], @@ -12,7 +12,7 @@ export type ExtractChainParameters< export type ExtractChainReturnType< chains extends readonly Chain[], chainId extends chains[number]['id'], -> = ExtractChain +> = Extract export type ExtractChainErrorType = ErrorType diff --git a/src/utils/transaction/getTransactionType.test-d.ts b/src/utils/transaction/getTransactionType.test-d.ts index fea5b8cda7..2648fbe90a 100644 --- a/src/utils/transaction/getTransactionType.test-d.ts +++ b/src/utils/transaction/getTransactionType.test-d.ts @@ -72,6 +72,16 @@ test('const: eip1559 attributes', () => { }) test('const: eip2930 attributes', () => { + expectTypeOf( + getTransactionType({ + accessList: [ + { + address: '0x', + storageKeys: ['0x'], + }, + ], + }), + ).toEqualTypeOf<'eip2930'>() expectTypeOf( getTransactionType({ accessList: [] }), ).toEqualTypeOf<'eip2930'>() diff --git a/src/utils/transaction/parseTransaction.ts b/src/utils/transaction/parseTransaction.ts index 07c6198e59..baa9fdfb25 100644 --- a/src/utils/transaction/parseTransaction.ts +++ b/src/utils/transaction/parseTransaction.ts @@ -26,7 +26,7 @@ import type { TransactionSerializedGeneric, TransactionType, } from '../../types/transaction.js' -import type { IsNarrowable } from '../../types/utils.js' +import type { IsNarrowable, Mutable } from '../../types/utils.js' import { type IsAddressErrorType, isAddress } from '../address/isAddress.js' import { toBlobSidecars } from '../blob/toBlobSidecars.js' import { type IsHexErrorType, isHex } from '../data/isHex.js' @@ -442,7 +442,7 @@ export type ParseAccessListErrorType = | ErrorType export function parseAccessList(accessList_: RecursiveArray): AccessList { - const accessList: AccessList = [] + const accessList: Mutable = [] for (let i = 0; i < accessList_.length; i++) { const [address, storageKeys] = accessList_[i] as [Hex, Hex[]] diff --git a/src/zksync/actions/sendTransaction.ts b/src/zksync/actions/sendTransaction.ts index d0de3052b9..4c4cc8f9ad 100644 --- a/src/zksync/actions/sendTransaction.ts +++ b/src/zksync/actions/sendTransaction.ts @@ -73,7 +73,6 @@ export async function sendTransaction< client: Client, args: SendTransactionParameters, ): Promise { - if (isEIP712Transaction(args as any)) - return sendEip712Transaction(client, args) + if (isEIP712Transaction(args)) return sendEip712Transaction(client, args) return sendTransaction_(client, args) } diff --git a/test/src/anvil.ts b/test/src/anvil.ts index e753091157..62481e179c 100644 --- a/test/src/anvil.ts +++ b/test/src/anvil.ts @@ -128,13 +128,16 @@ type DefineAnvilReturnType = { start(): Promise<() => Promise> } -function defineAnvil({ - chain: chain_, - forkUrl, - forkBlockNumber, - port, - ...options -}: DefineAnvilParameters): DefineAnvilReturnType { +function defineAnvil( + parameters: DefineAnvilParameters, +): DefineAnvilReturnType { + const { + chain: chain_, + forkUrl, + forkBlockNumber, + port, + ...options + } = parameters const rpcUrl = { http: `http://127.0.0.1:${port}/${poolId}`, ipc: `/tmp/anvil-${poolId}.ipc`, diff --git a/test/tsconfig.json b/test/tsconfig.json index 47ab3780ca..2c59e6da5c 100644 --- a/test/tsconfig.json +++ b/test/tsconfig.json @@ -1,4 +1,5 @@ { + "extends": "../tsconfig.base.json", "compilerOptions": { "module": "nodenext", "moduleResolution": "nodenext",