Skip to content

Commit

Permalink
refactor: core types (#2369)
Browse files Browse the repository at this point in the history
* wip: transactions

* chore: merge

* chore: up types

* chore: up size limit
  • Loading branch information
tmm committed Jun 4, 2024
1 parent cfcc51f commit cc05da8
Show file tree
Hide file tree
Showing 27 changed files with 599 additions and 477 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
{
"name": "viem/chains",
"path": "./src/_esm/chains/index.js",
"limit": "28 kB",
"limit": "30 kB",
"import": "*"
},
{
Expand Down
5 changes: 2 additions & 3 deletions src/celo/fees.test.ts
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -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')
Expand Down
1 change: 0 additions & 1 deletion src/celo/fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type {
ChainFees,
Hex,
} from '../index.js'

import type { formatters } from './formatters.js'

export const fees: ChainFees<typeof formatters> = {
Expand Down
2 changes: 1 addition & 1 deletion src/celo/formatters.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('block', () => {
Assign<
ExactPartial<RpcBlock>,
CeloBlockOverrides & {
transactions: `0x${string}`[] | CeloRpcTransaction[]
transactions: readonly `0x${string}`[] | readonly CeloRpcTransaction[]
}
>
>()
Expand Down
6 changes: 3 additions & 3 deletions src/celo/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,7 +41,7 @@ export const formatters = {
}
: {}),
}
}) as Hash[] | CeloTransaction[]
}) as readonly Hash[] | readonly CeloTransaction[]
return {
randomness: args.randomness,
transactions,
Expand Down
1 change: 0 additions & 1 deletion src/celo/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export function serializeTransaction(
) {
if (isCIP64(transaction))
return serializeTransactionCIP64(transaction, signature)

return serializeTransaction_(transaction, signature)
}

Expand Down
185 changes: 89 additions & 96 deletions src/celo/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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<TBlockTag extends 'pending' ? true : false>
includeTransactions,
blockTag,
CeloTransaction<blockTag extends 'pending' ? true : false>
>,
CeloBlockExclude
> &
Expand All @@ -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<TBlockTag extends 'pending' ? true : false>
blockTag,
includeTransactions,
RpcTransaction<blockTag extends 'pending' ? true : false>
>,
CeloBlockExclude
> &
CeloRpcBlockOverrides

export type CeloRpcTransaction<TPending extends boolean = boolean> =
| RpcTransaction<TPending>
| RpcTransactionCIP42<TPending>
| RpcTransactionCIP64<TPending>
export type CeloRpcTransaction<isPending extends boolean = boolean> =
| RpcTransaction<isPending>
| RpcTransactionCIP42<isPending>
| RpcTransactionCIP64<isPending>

export type CeloRpcTransactionRequest =
| RpcTransactionRequest
| RpcTransactionRequestCIP64

export type CeloTransaction<TPending extends boolean = boolean> =
| Transaction<TPending>
| TransactionCIP42<TPending>
| TransactionCIP64<TPending>
export type CeloTransaction<isPending extends boolean = boolean> =
| Transaction<isPending>
| TransactionCIP42<isPending>
| TransactionCIP64<isPending>

export type CeloTransactionRequest =
| TransactionRequest
Expand All @@ -100,111 +101,103 @@ export type CeloTransactionSerialized<

export type CeloTransactionType = TransactionType | 'cip42' | 'cip64'

type RpcTransaction<TPending extends boolean = boolean> =
RpcTransaction_<TPending> & {
type RpcTransaction<isPending extends boolean = boolean> =
core_RpcTransaction<isPending> & {
feeCurrency: Address | null
gatewayFee: Hex | null
gatewayFeeRecipient: Address | null
}

type RpcTransactionRequest = RpcTransactionRequest_ & {
type RpcTransactionRequest = core_RpcTransactionRequest & {
feeCurrency?: Address | undefined
}

export type RpcTransactionCIP42<TPending extends boolean = boolean> = Omit<
TransactionBase<Quantity, Index, TPending>,
export type RpcTransactionCIP42<isPending extends boolean = boolean> = Omit<
TransactionBase<Quantity, Index, isPending>,
'typeHex'
> &
FeeValuesEIP1559<Quantity> & {
feeCurrency: Address | null
gatewayFee: Hex | null
gatewayFeeRecipient: Address | null
type: '0x7c'
}
> & {
feeCurrency: Address | null
gatewayFee: Hex | null
gatewayFeeRecipient: Address | null
type: '0x7c'
} & FeeValuesEIP1559<Quantity>

export type RpcTransactionCIP64<TPending extends boolean = boolean> = Omit<
TransactionBase<Quantity, Index, TPending>,
export type RpcTransactionCIP64<isPending extends boolean = boolean> = Omit<
TransactionBase<Quantity, Index, isPending>,
'typeHex'
> &
FeeValuesEIP1559<Quantity> & {
feeCurrency: Address | null
gatewayFee?: undefined
gatewayFeeRecipient?: undefined
type: '0x7b'
}
> & {
feeCurrency: Address | null
gatewayFee?: undefined
gatewayFeeRecipient?: undefined
type: '0x7b'
} & FeeValuesEIP1559<Quantity>

export type RpcTransactionRequestCIP64 = TransactionRequestBase<
Quantity,
Index
> &
ExactPartial<FeeValuesEIP1559<Quantity>> & {
accessList?: AccessList | undefined
feeCurrency?: Address | undefined
type?: '0x7b' | undefined
}
> & {
accessList?: AccessList | undefined
feeCurrency?: Address | undefined
type?: '0x7b' | undefined
} & ExactPartial<FeeValuesEIP1559<Quantity>>

type Transaction<TPending extends boolean = boolean> = Transaction_<
type Transaction<isPending extends boolean = boolean> = core_Transaction<
bigint,
number,
TPending
isPending
> & {
feeCurrency: Address | null
gatewayFee?: undefined
gatewayFeeRecipient?: undefined
}

export type TransactionCIP42<TPending extends boolean = boolean> =
TransactionBase<bigint, number, TPending> &
FeeValuesEIP1559 & {
feeCurrency: Address | null
gatewayFee: bigint | null
gatewayFeeRecipient: Address | null
type: 'cip42'
}

export type TransactionCIP64<TPending extends boolean = boolean> =
TransactionBase<bigint, number, TPending> &
FeeValuesEIP1559 & {
feeCurrency: Address | null
gatewayFee?: undefined
gatewayFeeRecipient?: undefined
type: 'cip64'
}

type TransactionRequest = TransactionRequest_ & {
export type TransactionCIP42<isPending extends boolean = boolean> =
TransactionBase<bigint, number, isPending> & {
feeCurrency: Address | null
gatewayFee: bigint | null
gatewayFeeRecipient: Address | null
type: 'cip42'
} & FeeValuesEIP1559

export type TransactionCIP64<isPending extends boolean = boolean> =
TransactionBase<bigint, number, isPending> & {
feeCurrency: Address | null
gatewayFee?: undefined
gatewayFeeRecipient?: undefined
type: 'cip64'
} & FeeValuesEIP1559

type TransactionRequest = core_TransactionRequest & {
feeCurrency?: Address | undefined
}

export type TransactionRequestCIP64 = TransactionRequestBase &
ExactPartial<FeeValuesEIP1559> & {
accessList?: AccessList | undefined
feeCurrency?: Address | undefined
type?: 'cip64' | undefined
}
export type TransactionRequestCIP64 = TransactionRequestBase & {
accessList?: AccessList | undefined
feeCurrency?: Address | undefined
type?: 'cip64' | undefined
} & ExactPartial<FeeValuesEIP1559>

export type TransactionSerializableCIP42<
TQuantity = bigint,
TIndex = number,
> = TransactionSerializableBase<TQuantity, TIndex> &
ExactPartial<FeeValuesEIP1559<TQuantity>> & {
accessList?: AccessList | undefined
feeCurrency?: Address | undefined
gatewayFeeRecipient?: Address | undefined
gatewayFee?: TQuantity | undefined
chainId: number
type?: 'cip42' | undefined
}
quantity = bigint,
index = number,
> = TransactionSerializableBase<quantity, index> & {
accessList?: AccessList | undefined
feeCurrency?: Address | undefined
gatewayFeeRecipient?: Address | undefined
gatewayFee?: quantity | undefined
chainId: number
type?: 'cip42' | undefined
} & ExactPartial<FeeValuesEIP1559<quantity>>

export type TransactionSerializableCIP64<
TQuantity = bigint,
TIndex = number,
> = TransactionSerializableBase<TQuantity, TIndex> &
ExactPartial<FeeValuesEIP1559<TQuantity>> & {
accessList?: AccessList | undefined
feeCurrency?: Address | undefined
chainId: number
type?: 'cip64' | undefined
}
quantity = bigint,
index = number,
> = TransactionSerializableBase<quantity, index> & {
accessList?: AccessList | undefined
feeCurrency?: Address | undefined
chainId: number
type?: 'cip64' | undefined
} & ExactPartial<FeeValuesEIP1559<quantity>>

export type TransactionSerializedCIP42 = `0x7c${string}`
export type TransactionSerializedCIP64 = `0x7b${string}`
2 changes: 1 addition & 1 deletion src/clients/transports/fallback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 13 additions & 12 deletions src/clients/transports/fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,21 @@ export type FallbackTransportConfig = {
retryDelay?: TransportConfig['retryDelay'] | undefined
}

export type FallbackTransport<transports extends Transport[] = Transport[]> =
Transport<
'fallback',
{
onResponse: (fn: OnResponseFn) => void
transports: {
[key in keyof transports]: ReturnType<transports[key]>
}
export type FallbackTransport<
transports extends readonly Transport[] = readonly Transport[],
> = Transport<
'fallback',
{
onResponse: (fn: OnResponseFn) => void
transports: {
[key in keyof transports]: ReturnType<transports[key]>
}
>
}
>

export type FallbackTransportErrorType = CreateTransportErrorType | ErrorType

export function fallback<const transports extends Transport[]>(
export function fallback<const transports extends readonly Transport[]>(
transports_: transports,
config: FallbackTransportConfig = {},
): FallbackTransport<transports> {
Expand Down Expand Up @@ -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 } =
Expand Down
Loading

0 comments on commit cc05da8

Please sign in to comment.