Skip to content

Commit

Permalink
Merge pull request #37 from tokenbound/bj/debug-large-package
Browse files Browse the repository at this point in the history
Eliminate non-deployed chains from build
  • Loading branch information
bjfresh authored Oct 10, 2023
2 parents 29041aa + 5285b23 commit e09329d
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 212 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-pigs-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tokenbound/sdk': patch
---

Eliminate non-deployed chains from build
5 changes: 4 additions & 1 deletion .github/workflows/on-create-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ jobs:
- name: Change to package directory
run: cd packages/sdk

- name: Install dependencies and build 🔧
- name: Install dependencies
run: npm install

- name: Clean install and build 🔧
run: npm ci && npm build
# run: pnpm ci && pnpm build // pnpm ci not yet implemented: https://github.com/pnpm/pnpm/issues/6100

Expand Down
2 changes: 1 addition & 1 deletion examples/vite-wagmi-ethers-rainbowkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"util": "^0.12.5",
"viem": "^1.15.1",
"viem": "^1.16.2",
"wagmi": "^1.4.3"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/vite-wagmi-ethers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"util": "^0.12.5",
"viem": "^1.15.1",
"viem": "^1.16.2",
"wagmi": "^1.4.3"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/vite-wagmi-ethers6/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"util": "^0.12.5",
"viem": "^1.15.1",
"viem": "^1.16.2",
"wagmi": "^1.4.3"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/vite-wagmi-viem/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"util": "^0.12.5",
"viem": "^1.15.1",
"viem": "^1.16.2",
"wagmi": "^1.4.3"
},
"devDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"keywords": [],
"author": "",
"license": "ISC",
"publishConfig.directory": {
"directory": "packages/sdk"
},
"devDependencies": {
"@changesets/cli": "^2.26.2",
"eslint": "^8.50.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tokenbound/sdk",
"version": "0.3.9",
"version": "0.3.11",
"type": "module",
"files": [
"dist"
Expand All @@ -25,7 +25,7 @@
"wagmi": "wagmi generate"
},
"dependencies": {
"viem": "^1.14.0"
"viem": "^1.16.2"
},
"devDependencies": {
"@tanstack/react-query": "4.29.1",
Expand All @@ -45,7 +45,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"typescript": "^5.2.2",
"viem": "^1.15.1",
"viem": "^1.16.2",
"@viem/anvil": "^0.0.6",
"vite": "^4.4.9",
"vite-plugin-dts": "^3.5.1",
Expand Down
10 changes: 5 additions & 5 deletions packages/sdk/src/TokenboundClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class TokenboundClient {
constructor(options: TokenboundClientOptions) {
const {
chainId,
chain,
signer,
walletClient,
publicClient,
Expand All @@ -75,8 +76,8 @@ class TokenboundClient {
publicClientRPCUrl,
} = options

if (!chainId) {
throw new Error('chainId is required.')
if (!chainId && !chain) {
throw new Error('chain or chainId required.')
}

if (signer && walletClient) {
Expand All @@ -93,7 +94,7 @@ class TokenboundClient {
throw new Error('`publicClient` cannot be provided when using Ethers `signer`.')
}

this.chainId = chainId
this.chainId = chainId ?? chain!.id

if (signer) {
this.signer = signer
Expand All @@ -108,11 +109,10 @@ class TokenboundClient {
this.registryAddress = registryAddress
}

//
this.publicClient =
publicClient ??
createPublicClient({
chain: chainIdToChain(this.chainId),
chain: chain ?? chainIdToChain(this.chainId),
transport: http(publicClientRPCUrl ?? undefined),
})

Expand Down
15 changes: 14 additions & 1 deletion packages/sdk/src/test/TestAll.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// This test suite is for testing the SDK methods with
// viem walletClient + publicClient and with Ethers 5/6.

import { zora } from 'viem/chains'
import { describe, expect, it, vi } from 'vitest'
import { providers } from 'ethers'
import { waitFor } from './mockWallet'
Expand Down Expand Up @@ -768,7 +769,7 @@ function runTxTests({
})
}

describe('Custom publicClient RPC URL', () => {
describe('Custom client configurations', () => {
it('can use a custom publicClient RPC URL', async () => {
const customPublicClientRPCUrl = 'https://cloudflare-eth.com'
const tokenboundClient = new TokenboundClient({
Expand All @@ -781,4 +782,16 @@ describe('Custom publicClient RPC URL', () => {
expect(tokenboundClient.publicClient?.transport?.url).toBe(customPublicClientRPCUrl)
})
})
it('can use a custom chain as parameter', async () => {
const ZORA_CHAIN_ID = 7777777

const tokenboundClient = new TokenboundClient({
walletClient,
chain: zora,
})

await waitFor(() => {
expect(tokenboundClient.publicClient?.chain?.id).toBe(ZORA_CHAIN_ID)
})
})
})
5 changes: 3 additions & 2 deletions packages/sdk/src/types/params.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WalletClient, PublicClient } from 'viem'
import { WalletClient, PublicClient, Chain } from 'viem'
import { Prettify } from './prettify'
import { UniversalSignableMessage } from './messages'
import { PossibleENSAddress } from './addresses'
Expand Down Expand Up @@ -48,7 +48,8 @@ export type ERC20TransferParams = Prettify<{
}>

export type TokenboundClientOptions = Prettify<{
chainId: number
chainId?: number
chain?: Chain
signer?: any
walletClient?: WalletClient
publicClient?: PublicClient
Expand Down
31 changes: 27 additions & 4 deletions packages/sdk/src/utils/chainIdToChain.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
import * as allChains from "viem/chains"
import { Chain } from "viem"
import { Chain } from 'viem'
import {
mainnet,
goerli,
polygon,
polygonMumbai,
sepolia,
optimism,
arbitrum,
base,
gnosis,
} from 'viem/chains'

const allChains = {
mainnet,
goerli,
polygon,
polygonMumbai,
sepolia,
optimism,
arbitrum,
base,
gnosis,
}

/**
* Gets the chain object for the given chain id.
* @param chainId - Chain id of the target EVM chain.
* @returns Viem's chain object.
*/

export function chainIdToChain(chainId: number): Chain {
for (const chain of Object.values(allChains)) {
if (chain.id === chainId) {
return chain
}
}

throw new Error(`Chain with id ${chainId} not found`)
}
}
Loading

0 comments on commit e09329d

Please sign in to comment.