Skip to content

Commit

Permalink
chore: tweak default pending
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Sep 30, 2024
1 parent 104ef49 commit ee10db4
Show file tree
Hide file tree
Showing 40 changed files with 651 additions and 483 deletions.
23 changes: 18 additions & 5 deletions src/internal/Block/fromRpc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { GlobalErrorType } from '../Errors/error.js'
import { Transaction_fromRpc } from '../Transaction/isomorphic/fromRpc.js'
import { Withdrawal_fromRpc } from '../Withdrawal/fromRpc.js'
import type { Block, Block_Rpc } from './types.js'
import type { Block, Block_Rpc, Block_Tag } from './types.js'

/**
* Converts a {@link ox#Block.Rpc} to an {@link ox#Block.Block}.
Expand Down Expand Up @@ -64,9 +64,14 @@ import type { Block, Block_Rpc } from './types.js'
* @param block - The RPC block to convert.
* @returns An instantiated {@link ox#Block.Block}.
*/
export function Block_fromRpc<const block extends Block_Rpc | null>(
export function Block_fromRpc<
const block extends Block_Rpc | null,
includeTransactions extends boolean = false,
blockTag extends Block_Tag = 'latest',
>(
block: block | Block_Rpc | null,
): block extends Block_Rpc ? Block : null {
_options: Block_fromRpc.Options<includeTransactions, blockTag> = {},
): block extends Block_Rpc ? Block<includeTransactions, blockTag> : null {
if (!block) return null as never

const transactions = block.transactions.map((transaction) => {
Expand Down Expand Up @@ -105,11 +110,19 @@ export function Block_fromRpc<const block extends Block_Rpc | null>(
withdrawals: block.withdrawals?.map(Withdrawal_fromRpc),
withdrawalsRoot: block.withdrawalsRoot,
uncles: block.uncles,
} as never
} as Block as never
}

export declare namespace Block_fromRpc {
export type ErrorType = GlobalErrorType
type Options<
includeTransactions extends boolean = false,
blockTag extends Block_Tag = 'latest',
> = {
blockTag?: blockTag | Block_Tag | undefined
includeTransactions?: includeTransactions | boolean | undefined
}

type ErrorType = GlobalErrorType
}

Block_fromRpc.parseError = (error: unknown) =>
Expand Down
237 changes: 121 additions & 116 deletions src/internal/Block/toRpc.test.ts

Large diffs are not rendered by default.

27 changes: 21 additions & 6 deletions src/internal/Block/toRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { GlobalErrorType } from '../Errors/error.js'
import { Hex_fromNumber } from '../Hex/fromNumber.js'
import { Transaction_toRpc } from '../Transaction/isomorphic/toRpc.js'
import { Withdrawal_toRpc } from '../Withdrawal/toRpc.js'
import type { Block, Block_Rpc } from './types.js'
import type { Block, Block_Rpc, Block_Tag } from './types.js'

/**
* Converts a {@link ox#Block.Block} to an {@link ox#Block.Rpc}.
Expand Down Expand Up @@ -33,10 +33,16 @@ import type { Block, Block_Rpc } from './types.js'
* @param block - The Block to convert.
* @returns An RPC Block.
*/
export function Block_toRpc(block: Block): Block_Rpc {
export function Block_toRpc<
includeTransactions extends boolean = false,
blockTag extends Block_Tag = 'latest',
>(
block: Block<includeTransactions, blockTag>,
_options: Block_toRpc.Options<includeTransactions, blockTag> = {},
): Block_Rpc<boolean, blockTag> {
const transactions = block.transactions.map((transaction) => {
if (typeof transaction === 'string') return transaction
return Transaction_toRpc(transaction) as any
return Transaction_toRpc(transaction as any) as any
})
return {
baseFeePerGas:
Expand All @@ -63,8 +69,9 @@ export function Block_toRpc(block: Block): Block_Rpc {
miner: block.miner,
mixHash: block.mixHash,
nonce: block.nonce,
number:
typeof block.number === 'bigint' ? Hex_fromNumber(block.number) : null,
number: (typeof block.number === 'bigint'
? Hex_fromNumber(block.number)
: null) as never,
parentBeaconBlockRoot: block.parentBeaconBlockRoot,
parentHash: block.parentHash,
receiptsRoot: block.receiptsRoot,
Expand All @@ -86,7 +93,15 @@ export function Block_toRpc(block: Block): Block_Rpc {
}

export declare namespace Block_toRpc {
export type ErrorType = GlobalErrorType
type Options<
includeTransactions extends boolean = false,
blockTag extends Block_Tag = 'latest',
> = {
blockTag?: blockTag | Block_Tag | undefined
includeTransactions?: includeTransactions | boolean | undefined
}

type ErrorType = GlobalErrorType
}

Block_toRpc.parseError = (error: unknown) =>
Expand Down
6 changes: 3 additions & 3 deletions src/internal/Block/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import type { Compute } from '../types.js'

/** A Block as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/block.yaml). */
export type Block<
includeTransactions extends boolean = boolean,
blockTag extends Block_Tag = Block_Tag,
includeTransactions extends boolean = false,
blockTag extends Block_Tag = 'latest',
bigintType = bigint,
numberType = number,
transaction = Transaction<
Expand Down Expand Up @@ -84,7 +84,7 @@ export type Block_Number<bigintType = bigint> = bigintType
/** An RPC Block as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/block.yaml). */
export type Block_Rpc<
includeTransactions extends boolean = boolean,
blockTag extends Block_Tag = Block_Tag,
blockTag extends Block_Tag = 'latest',
transaction = Transaction_Rpc<blockTag extends 'pending' ? true : false>,
> = Block<includeTransactions, blockTag, Hex, Hex, transaction>

Expand Down
35 changes: 19 additions & 16 deletions src/internal/Log/fromRpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,25 @@ test('default', () => {
})

test('behavior: nullish values', () => {
const log = Log.fromRpc({
address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2',
topics: [
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0000000000000000000000000c04d9e9278ec5e4d424476d3ebec70cb5d648d1',
'0x000000000000000000000000000000000000000000000000000000000000025b',
],
data: '0x',
blockHash: null,
blockNumber: null,
transactionHash: null,
transactionIndex: null,
logIndex: null,
removed: false,
})
const log = Log.fromRpc(
{
address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2',
topics: [
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0000000000000000000000000c04d9e9278ec5e4d424476d3ebec70cb5d648d1',
'0x000000000000000000000000000000000000000000000000000000000000025b',
],
data: '0x',
blockHash: null,
blockNumber: null,
transactionHash: null,
transactionIndex: null,
logIndex: null,
removed: false,
},
{ pending: true },
)
expect(log).toMatchInlineSnapshot(`
{
"address": "0xfba3912ca04dd458c843e2ee08967fc04f3579c2",
Expand Down
16 changes: 13 additions & 3 deletions src/internal/Log/fromRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ import type { Log, Log_Rpc } from './types.js'
* @param log - The RPC log to convert.
* @returns An instantiated {@link ox#Log.Log}.
*/
export function Log_fromRpc(log: Log_Rpc): Log {
export function Log_fromRpc<
const log extends Log_Rpc<boolean>,
pending extends boolean = false,
>(
log: log | Log_Rpc<boolean>,
_options: Log_fromRpc.Options<pending> = {},
): Log<pending> {
return {
address: log.address,
blockHash: log.blockHash,
Expand All @@ -113,11 +119,15 @@ export function Log_fromRpc(log: Log_Rpc): Log {
? Number(log.transactionIndex)
: null,
removed: log.removed,
}
} as Log<pending>
}

export declare namespace Log_fromRpc {
export type ErrorType = GlobalErrorType
type Options<pending extends boolean = false> = {
pending?: pending | boolean | undefined
}

type ErrorType = GlobalErrorType
}

Log_fromRpc.parseError = (error: unknown) =>
Expand Down
35 changes: 19 additions & 16 deletions src/internal/Log/toRpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,25 @@ test('default', () => {
})

test('behavior: nullish values', () => {
const log = Log.toRpc({
address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2',
blockHash: null,
blockNumber: null,
data: '0x',
logIndex: null,
removed: false,
topics: [
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0000000000000000000000000c04d9e9278ec5e4d424476d3ebec70cb5d648d1',
'0x000000000000000000000000000000000000000000000000000000000000025b',
],
transactionHash: null,
transactionIndex: null,
})
const log = Log.toRpc(
{
address: '0xfba3912ca04dd458c843e2ee08967fc04f3579c2',
blockHash: null,
blockNumber: null,
data: '0x',
logIndex: null,
removed: false,
topics: [
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0000000000000000000000000c04d9e9278ec5e4d424476d3ebec70cb5d648d1',
'0x000000000000000000000000000000000000000000000000000000000000025b',
],
transactionHash: null,
transactionIndex: null,
},
{ pending: true },
)
expect(log).toMatchInlineSnapshot(`
{
"address": "0xfba3912ca04dd458c843e2ee08967fc04f3579c2",
Expand Down
13 changes: 10 additions & 3 deletions src/internal/Log/toRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ import type { Log, Log_Rpc } from './types.js'
* @param log - The log to convert.
* @returns An RPC log.
*/
export function Log_toRpc(log: Log): Log_Rpc {
export function Log_toRpc<
const log extends Log<boolean>,
pending extends boolean = false,
>(log: log, _options: Log_toRpc.Options<pending> = {}): Log_Rpc<pending> {
return {
address: log.address,
blockHash: log.blockHash,
Expand All @@ -67,11 +70,15 @@ export function Log_toRpc(log: Log): Log_Rpc {
? Hex_fromNumber(log.transactionIndex)
: null,
removed: log.removed,
}
} as Log_Rpc as never
}

export declare namespace Log_toRpc {
export type ErrorType = GlobalErrorType
type Options<pending extends boolean = false> = {
pending?: pending | boolean | undefined
}

type ErrorType = GlobalErrorType
}

Log_toRpc.parseError = (error: unknown) =>
Expand Down
4 changes: 2 additions & 2 deletions src/internal/Log/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Compute } from '../types.js'

/** A Log as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/receipt.yaml). */
export type Log<
pending extends boolean = boolean,
pending extends boolean = false,
bigintType = bigint,
numberType = number,
> = Compute<{
Expand All @@ -29,4 +29,4 @@ export type Log<
}>

/** An RPC Log as defined in the [Execution API specification](https://github.com/ethereum/execution-apis/blob/main/src/schemas/receipt.yaml). */
export type Log_Rpc<pending extends boolean = boolean> = Log<pending, Hex, Hex>
export type Log_Rpc<pending extends boolean = false> = Log<pending, Hex, Hex>
47 changes: 25 additions & 22 deletions src/internal/Transaction/eip1559/fromRpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,28 +118,31 @@ test('behavior: nullish', () => {

test('behavior: pending', () => {
expect(
TransactionEip1559.fromRpc({
hash: '0x353fdfc38a2f26115daadee9f5b8392ce62b84f410957967e2ed56b35338cdd0',
nonce: '0x357',
blockHash: null,
blockNumber: null,
transactionIndex: null,
from: '0x814e5e0e31016b9a7f138c76b7e7b2bb5c1ab6a6',
to: '0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad',
value: '0x9b6e64a8ec60000',
gas: '0x43f5d',
maxFeePerGas: '0x2ca6ae494',
maxPriorityFeePerGas: '0x41cc3c0',
input:
'0x3593564c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000006643504700000000000000000000000000000000000000000000000000000000000000040b080604000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000009b6e64a8ec600000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000009b6e64a8ec60000000000000000000000000000000000000000000000000000019124bb5ae978c000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000c56c7a0eaa804f854b536a5f3d5f49d2ec4b12b80000000000000000000000000000000000000000000000000000000000000060000000000000000000000000c56c7a0eaa804f854b536a5f3d5f49d2ec4b12b8000000000000000000000000000000fee13a103a10d593b9ae06b3e05f2e7e1c00000000000000000000000000000000000000000000000000000000000000190000000000000000000000000000000000000000000000000000000000000060000000000000000000000000c56c7a0eaa804f854b536a5f3d5f49d2ec4b12b800000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000190240001b9872b',
r: '0x635dc2033e60185bb36709c29c75d64ea51dfbd91c32ef4be198e4ceb169fb4d',
s: '0x50c2667ac4c771072746acfdcf1f1483336dcca8bd2df47cd83175dbe60f0540',
v: '0x0',
yParity: '0x0',
chainId: '0x1',
accessList: [],
type: '0x2',
}),
TransactionEip1559.fromRpc(
{
hash: '0x353fdfc38a2f26115daadee9f5b8392ce62b84f410957967e2ed56b35338cdd0',
nonce: '0x357',
blockHash: null,
blockNumber: null,
transactionIndex: null,
from: '0x814e5e0e31016b9a7f138c76b7e7b2bb5c1ab6a6',
to: '0x3fc91a3afd70395cd496c647d5a6cc9d4b2b7fad',
value: '0x9b6e64a8ec60000',
gas: '0x43f5d',
maxFeePerGas: '0x2ca6ae494',
maxPriorityFeePerGas: '0x41cc3c0',
input:
'0x3593564c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000006643504700000000000000000000000000000000000000000000000000000000000000040b080604000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000009b6e64a8ec600000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000009b6e64a8ec60000000000000000000000000000000000000000000000000000019124bb5ae978c000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000c56c7a0eaa804f854b536a5f3d5f49d2ec4b12b80000000000000000000000000000000000000000000000000000000000000060000000000000000000000000c56c7a0eaa804f854b536a5f3d5f49d2ec4b12b8000000000000000000000000000000fee13a103a10d593b9ae06b3e05f2e7e1c00000000000000000000000000000000000000000000000000000000000000190000000000000000000000000000000000000000000000000000000000000060000000000000000000000000c56c7a0eaa804f854b536a5f3d5f49d2ec4b12b800000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000190240001b9872b',
r: '0x635dc2033e60185bb36709c29c75d64ea51dfbd91c32ef4be198e4ceb169fb4d',
s: '0x50c2667ac4c771072746acfdcf1f1483336dcca8bd2df47cd83175dbe60f0540',
v: '0x0',
yParity: '0x0',
chainId: '0x1',
accessList: [],
type: '0x2',
},
{ pending: true },
),
).toMatchInlineSnapshot(`
{
"accessList": [],
Expand Down
16 changes: 12 additions & 4 deletions src/internal/Transaction/eip1559/fromRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ import type { TransactionEip1559, TransactionEip1559_Rpc } from './types.js'
* @returns An instantiated {@link ox#Transaction.Eip1559}.
*/
export function TransactionEip1559_fromRpc<
const transaction extends TransactionEip1559_Rpc | null,
const transaction extends TransactionEip1559_Rpc<boolean> | null,
pending extends boolean = false,
>(
transaction: transaction | TransactionEip1559_Rpc | null,
): transaction extends TransactionEip1559_Rpc ? TransactionEip1559 : null {
transaction: transaction | TransactionEip1559_Rpc<boolean> | null,
_options: TransactionEip1559_fromRpc.Options<pending> = {},
): transaction extends TransactionEip1559_Rpc
? TransactionEip1559<pending>
: null {
if (!transaction) return null as never

const signature = Signature_extract(transaction)!
Expand Down Expand Up @@ -72,7 +76,11 @@ export function TransactionEip1559_fromRpc<
}

export declare namespace TransactionEip1559_fromRpc {
export type ErrorType = Signature_extract.ErrorType | GlobalErrorType
type Options<pending extends boolean = false> = {
pending?: pending | boolean | undefined
}

type ErrorType = Signature_extract.ErrorType | GlobalErrorType
}

TransactionEip1559_fromRpc.parseError = (error: unknown) =>
Expand Down
Loading

0 comments on commit ee10db4

Please sign in to comment.