Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Aug 22, 2023
1 parent b562324 commit 6c371be
Show file tree
Hide file tree
Showing 194 changed files with 548 additions and 4,185 deletions.
8 changes: 6 additions & 2 deletions chains/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
"scripts": {
"build": "pnpm run clean && pnpm run build:cjs && pnpm run build:esm && pnpm run build:types",
"build:cjs": "tsc --project tsconfig.build.json --module commonjs --outDir ./dist/cjs --removeComments --verbatimModuleSyntax false && echo > ./dist/cjs/package.json '{\"type\":\"commonjs\"}'",
"build:esm": "tsc --project tsconfig.build.json --module es2015 --outDir ./dist/esm && echo > ./dist/esm/package.json '{\"type\":\"module\",\"sideEffects\":false}'",
"build:esm": "tsc --project tsconfig.build.json --module es2020 --outDir ./dist/esm && echo > ./dist/esm/package.json '{\"type\":\"module\",\"sideEffects\":false}'",
"build:types": "tsc --project tsconfig.build.json --module esnext --declarationDir ./dist/types --emitDeclarationOnly --declaration --declarationMap",
"clean": "rimraf dist"
"clean": "rimraf dist",
"typecheck": "tsc --noEmit"
},
"type": "module",
"main": "./dist/cjs/index.js",
Expand Down Expand Up @@ -37,6 +38,9 @@
"optional": true
}
},
"dependencies": {
"@wagmi/chains": "1.7.0"
},
"devDependencies": {
"viem": "workspace:*"
},
Expand Down
2 changes: 1 addition & 1 deletion chains/src/celo/formatters.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
http,
type Hash,
type RpcBlock,
type RpcTransaction,
type RpcTransactionReceipt,
type TransactionRequest,
createPublicClient,
createWalletClient,
http,
} from 'viem'
import {
getBlock,
Expand Down
2 changes: 1 addition & 1 deletion chains/src/celo/formatters.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { http, createPublicClient } from 'viem'
import { createPublicClient, http } from 'viem'
import { getBlock, getTransaction, getTransactionReceipt } from 'viem/actions'
import { describe, expect, test } from 'vitest'

Expand Down
11 changes: 4 additions & 7 deletions chains/src/celo/formatters.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { type Hash, formatTransaction, hexToBigInt, numberToHex } from 'viem'

import type { ChainFormatters } from '../types.js'
import {
defineBlock,
formatTransaction,
hexToBigInt,
type Hash,
defineTransaction,
defineTransactionReceipt,
defineTransactionRequest,
numberToHex,
} from 'viem'

} from '../utils.js'
import type {
CeloBlockOverrides,
CeloRpcTransaction,
Expand All @@ -20,7 +18,6 @@ import type {
CeloTransactionReceiptOverrides,
CeloTransactionRequestOverrides,
} from './types.js'
import type { ChainFormatters } from '../types.js'

export const formattersCelo = {
block: /*#__PURE__*/ defineBlock({
Expand Down
68 changes: 0 additions & 68 deletions chains/src/errors.ts

This file was deleted.

3 changes: 3 additions & 0 deletions chains/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ export const zoraTestnet = /*#__PURE__*/ defineChain(chains.zoraTestnet, {
export type {
Chain,
ChainContract,
ChainConfig,
ChainEstimateFeesPerGasFnParameters,
ChainFees,
ChainFeesFnParameters,
ChainFormatter,
ChainFormatters,
ChainSerializers,
Expand Down
2 changes: 1 addition & 1 deletion chains/src/optimism/formatters.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { http, type Hash, type RpcBlock, createPublicClient } from 'viem'
import { type Hash, type RpcBlock, createPublicClient, http } from 'viem'
import { getBlock, getTransaction, getTransactionReceipt } from 'viem/actions'
import type { Assign } from 'viem/internal'
import { describe, expectTypeOf, test } from 'vitest'
Expand Down
2 changes: 1 addition & 1 deletion chains/src/optimism/formatters.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { http, createPublicClient } from 'viem'
import { createPublicClient, http } from 'viem'
import { getBlock, getTransaction, getTransactionReceipt } from 'viem/actions'
import { describe, expect, test } from 'vitest'

Expand Down
8 changes: 5 additions & 3 deletions chains/src/optimism/formatters.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import {
type Hash,
type RpcTransaction,
defineBlock,
defineTransaction,
defineTransactionReceipt,
formatTransaction,
hexToBigInt,
} from 'viem'

import type { ChainFormatters } from '../types.js'
import {
defineBlock,
defineTransaction,
defineTransactionReceipt,
} from '../utils.js'
import type {
OptimismBlockOverrides,
OptimismRpcBlockOverrides,
Expand Down
14 changes: 13 additions & 1 deletion chains/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import type { Address } from 'abitype'
import type { EstimateFeesPerGasReturnType } from 'viem'
import type {
Account,
Client,
EstimateFeesPerGasReturnType,
FeeValuesType,
FormattedBlock,
PrepareRequestParameters,
SerializeTransactionFn,
TransactionSerializable,
TransactionSerializableGeneric,
Transport,
} from 'viem'
import type { Prettify } from 'viem/internal'

export type Chain<
formatters extends ChainFormatters | undefined = ChainFormatters | undefined,
Expand Down
109 changes: 54 additions & 55 deletions chains/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
import type { Assign } from 'viem/internal'

import type {
Chain,
ChainConfig,
ChainContract,
ChainFormatters,
} from './types.js'
import {
ChainDoesNotSupportContract,
ChainMismatchError,
ChainNotFoundError,
} from './errors.js'

export type AssertCurrentChainParameters = {
chain?: Chain
currentChainId: number
}
formatBlock,
formatTransaction,
formatTransactionReceipt,
formatTransactionRequest,
} from 'viem'
import type { Assign, Prettify } from 'viem/internal'

export function assertCurrentChain({
chain,
currentChainId,
}: AssertCurrentChainParameters): void {
if (!chain) throw new ChainNotFoundError()
if (currentChainId !== chain.id)
throw new ChainMismatchError({ chain, currentChainId })
}
import type { Chain, ChainConfig, ChainFormatters } from './types.js'

export function defineChain<
chain extends Chain,
Expand All @@ -46,39 +28,56 @@ export function defineChain<
} as unknown as Assign<chain, ChainConfig<formatters>>
}

export function getChainContractAddress({
blockNumber,
chain,
contract: name,
}: {
blockNumber?: bigint
chain: Chain
contract: string
}) {
const contract = (chain?.contracts as Record<string, ChainContract>)?.[name]
if (!contract)
throw new ChainDoesNotSupportContract({
chain,
contract: { name },
})

if (
blockNumber &&
contract.blockCreated &&
contract.blockCreated > blockNumber
)
throw new ChainDoesNotSupportContract({
blockNumber,
chain,
contract: {
name,
blockCreated: contract.blockCreated,
export function defineFormatter<TType extends string, TParameters, TReturnType>(
type: TType,
format: (_: TParameters) => TReturnType,
) {
return <
TOverrideParameters,
TOverrideReturnType,
TExclude extends (keyof TParameters)[] = [],
>({
exclude,
format: overrides,
}: {
exclude?: TExclude
format: (_: TOverrideParameters) => TOverrideReturnType
}) => {
return {
exclude,
format: (args: Assign<TParameters, TOverrideParameters>) => {
const formatted = format(args as any)
if (exclude) {
for (const key of exclude) {
delete (formatted as any)[key]
}
}
return {
...formatted,
...overrides(args),
} as Prettify<Assign<TReturnType, TOverrideReturnType>> & {
[K in TExclude[number]]: never
}
},
})

return contract.address
type,
}
}
}

export const defineBlock = /*#__PURE__*/ defineFormatter('block', formatBlock)
export const defineTransaction = /*#__PURE__*/ defineFormatter(
'transaction',
formatTransaction,
)
export const defineTransactionReceipt = /*#__PURE__*/ defineFormatter(
'transactionReceipt',
formatTransactionReceipt,
)
export const defineTransactionRequest = /*#__PURE__*/ defineFormatter(
'transactionRequest',
formatTransactionRequest,
)

///////////////////////////////////////////////////////////////
// Exports

Expand Down
45 changes: 45 additions & 0 deletions chains/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
// This tsconfig file contains the shared config for the build (tsconfig.build.json) and type checking (tsconfig.json) config.
"include": [],
"compilerOptions": {
// Incremental builds
// NOTE: Enabling incremental builds speeds up `tsc`. Keep in mind though that it does not reliably bust the cache when the `tsconfig.json` file changes.
"incremental": true,

// Type checking
"strict": true,
"useDefineForClassFields": true, // Not enabled by default in `strict` mode unless we bump `target` to ES2022.
"noFallthroughCasesInSwitch": true, // Not enabled by default in `strict` mode.
"noImplicitReturns": true, // Not enabled by default in `strict` mode.
"useUnknownInCatchVariables": true, // TODO: This would normally be enabled in `strict` mode but would require some adjustments to the codebase.
"noImplicitOverride": true, // Not enabled by default in `strict` mode.
"noUnusedLocals": true, // Not enabled by default in `strict` mode.
"noUnusedParameters": true, // Not enabled by default in `strict` mode.
// TODO: The following options are also not enabled by default in `strict` mode and would be nice to have but would require some adjustments to the codebase.
// "exactOptionalPropertyTypes": true,
// "noUncheckedIndexedAccess": true,

// JavaScript support
"allowJs": false,
"checkJs": false,

// Interop constraints
"esModuleInterop": false,
"allowSyntheticDefaultImports": false,
"forceConsistentCasingInFileNames": true,
"verbatimModuleSyntax": true,
"importHelpers": true, // This is only used for build validation. Since we do not have `tslib` installed, this will fail if we accidentally make use of anything that'd require injection of helpers.

// Language and environment
"moduleResolution": "NodeNext",
"module": "ESNext",
"target": "ES2021", // Setting this to `ES2021` enables native support for `Node v16+`: https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping.
"lib": [
"ES2022", // By using ES2022 we get access to the `.cause` property on `Error` instances.
"DOM" // We are adding `DOM` here to get the `fetch`, etc. types. This should be removed once these types are available via DefinitelyTyped.
],

// Skip type checking for node modules
"skipLibCheck": true
}
}
11 changes: 2 additions & 9 deletions chains/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
{
// This configuration is used for local development and type checking.
"extends": "../tsconfig.base.json",
"extends": "./tsconfig.base.json",
"include": ["src"],
"exclude": [],
"compilerOptions": {
"baseUrl": ".",
"paths": {
"viem": ["../src/index.ts"],
"viem/*": ["../src/*.ts", "../src/*/index.ts"],
}
}
"exclude": []
}
Loading

0 comments on commit 6c371be

Please sign in to comment.