Skip to content

Commit 6200474

Browse files
authored
Multi Chain API preparation (blocknative#834)
* Start implementing CAIP * Further implementation * Updated imp and better provider handling * Update package names * Update docs * Fix chain reference * Fix account select validation * Switch to Static provider for less reqs * Remove template strings * Remove unused types * Fix demo
1 parent b47bcb8 commit 6200474

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+297
-241
lines changed

packages/common/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# @bn-onboard/common
1+
# @web3-onboard/common
22

33
## A collection of functions and types that are shared across various packages in the Onboard V2 monorepo

packages/common/src/account-select.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const accountSelect = async (
1313
): Promise<Account[]> => {
1414
if (options) {
1515
const error = validateSelectAccountOptions(options)
16+
1617
if (error) {
1718
throw error
1819
}

packages/common/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ export enum ProviderRpcErrorCode {
525525
}
526526

527527
export interface Chain {
528+
namespace: 'evm'
528529
id: ChainId
529530
rpcUrl: string
530531
label?: string

packages/common/src/validation.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const basePath = Joi.object({
88
const basePaths = Joi.array().items(basePath)
99

1010
const chain = Joi.object({
11+
namespace: Joi.string(),
1112
id: Joi.string().required(),
1213
label: Joi.string(),
1314
token: Joi.string().required(),

packages/core/README.md

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# @bn-onboard/core
1+
# @web3-onboard/core
22

33
This is the core package that contains all of the UI and logic to be able to seamlessly connect user's wallets to your app and track the state of those wallets. Onboard no longer contains any wallet specific code, so wallets need to be passed in upon initialization.
44

55
## Installation
66

77
Install the core module:
88

9-
`npm i @bn-onboard/core`
9+
`npm i @web3-onboard/core`
1010

1111
If you would like to support all wallets, then you can install all of the wallet modules:
1212

13-
`npm i @bn-onboard/injected-wallets @bn-onboard/ledger @bn-onboard/trezor @bn-onboard/keepkey @bn-onboard/walletconnect @bn-onboard/walletlink @bn-onboard/torus @bn-onboard/portis @bn-onboard/mew @bn-onboard/gnosis @bn-onboard/fortmatic`
13+
`npm i @web3-onboard/injected-wallets @web3-onboard/ledger @web3-onboard/trezor @web3-onboard/keepkey @web3-onboard/walletconnect @web3-onboard/walletlink @web3-onboard/torus @web3-onboard/portis @web3-onboard/mew @web3-onboard/gnosis @web3-onboard/fortmatic`
1414

1515
Note:
1616

@@ -41,6 +41,7 @@ An array of Chains that your app supports:
4141
```typescript
4242
type Chain = {
4343
id: ChainId // hex encoded string, eg '0x1' for Ethereum Mainnet
44+
namespace?: 'evm' // string indicating chain namespace. Defaults to 'evm' but will allow other chain namespaces in the future
4445
rpcUrl: string // used for network requests
4546
label?: string // used for display, eg Ethereum Mainnet
4647
token?: TokenSymbol // the native token symbol, eg ETH, BNB, MATIC
@@ -56,6 +57,8 @@ type AppMetadata = {
5657
name: string
5758
// SVG icon string, with height or width (whichever is larger) set to 100% or a valid image URL
5859
icon: string
60+
// Optional wide format logo (ie icon and text) to be displayed in the sidebar of connect modal. Defaults to icon if not provided
61+
logo?: string
5962
// description of app
6063
description?: string
6164
// url to a getting started guide for app
@@ -88,8 +91,8 @@ Onboard is using the [ICU syntax](https://formatjs.io/docs/core-concepts/icu-syn
8891
Putting it all together, here is an example initialization with the injected wallet modules:
8992
9093
```javascript
91-
import Onboard from '@bn-onboard/core'
92-
import injectedModule from '@bn-onboard/injected-wallets'
94+
import Onboard from '@web3-onboard/core'
95+
import injectedModule from '@web3-onboard/injected-wallets'
9396

9497
const ETH_MAINNET_RPC = `https://mainnet.infura.io/v3/${INFURA_KEY}`
9598
const ETH_RINKEBY_RPC = `https://rinkeby.infura.io/v3/${INFURA_KEY}`
@@ -223,7 +226,7 @@ type WalletState = {
223226
icon: string
224227
provider: EIP1193Provider
225228
accounts: Account[]
226-
chain: ChainId
229+
chains: ConnectedChain[]
227230
instance?: unknown
228231
}
229232

@@ -238,6 +241,11 @@ type Account = {
238241
balance: Record<TokenSymbol, string>
239242
}
240243

244+
type ConnectedChain = {
245+
namespace: 'evm'
246+
id: ChainId
247+
}
248+
241249
type ChainId = string
242250
type TokenSymbol = string
243251
```
@@ -282,6 +290,7 @@ When initializing Onboard you define a list of chains/networks that your app sup
282290
type SetChain = (options: SetChainOptions) => Promise<boolean>
283291
type SetChainOptions = {
284292
chainId: string // hex encoded string
293+
chainNamespace?: 'evm' // defaults to 'evm' (currently the only valid value, but will add more in future updates)
285294
wallet?: string // the wallet.label of the wallet to set chain
286295
}
287296

@@ -370,11 +379,6 @@ The Onboard styles can customized via [CSS variables](https://developer.mozilla.
370379
--onboard-font-size-6: 0.875rem;
371380
--onboard-font-size-7: 0.75rem;
372381

373-
--onboard-font-line-height-1: 24px;
374-
--onboard-font-line-height-2: 20px;
375-
--onboard-font-line-height-3: 16px;
376-
--onboard-font-line-height-4: 12px;
377-
378382
/* SPACING */
379383
--onboard-spacing-1: 3rem;
380384
--onboard-spacing-2: 2rem;

packages/core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"typescript": "4.5.5"
4040
},
4141
"dependencies": {
42-
"@bn-onboard/common": "^2.0.0-alpha.1",
42+
"@web3-onboard/common": "^2.0.0-alpha.1",
4343
"bowser": "^2.11.0",
4444
"ethers": "5.5.3",
4545
"eventemitter3": "^4.0.7",

packages/core/rollup.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default {
3636
})
3737
],
3838
external: [
39-
'@bn-onboard/common',
39+
'@web3-onboard/common',
4040
'ethers',
4141
'bowser',
4242
'joi',

packages/core/src/chain.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { WalletState } from './types'
99

1010
async function setChain(options: {
1111
chainId: string
12+
chainNamespace?: string
1213
wallet?: WalletState['label']
1314
}): Promise<boolean> {
1415
const error = validateSetChainOptions(options)
@@ -18,13 +19,16 @@ async function setChain(options: {
1819
}
1920

2021
const { wallets, chains } = state.get()
21-
const { chainId, wallet: walletToSet } = options
22+
const { chainId, chainNamespace = 'evm', wallet: walletToSet } = options
2223

2324
// validate that chainId has been added to chains
24-
const chain = chains.find(({ id }) => id === chainId)
25+
const chain = chains.find(
26+
({ namespace, id }) => namespace === chainNamespace && id === chainId
27+
)
28+
2529
if (!chain) {
2630
throw new Error(
27-
`Chain with chainId: ${chainId} has not been set and must be added when Onboard is initialized.`
31+
`Chain with chainId: ${chainId} and chainNamespace: ${chainNamespace} has not been set and must be added when Onboard is initialized.`
2832
)
2933
}
3034

@@ -41,8 +45,13 @@ async function setChain(options: {
4145
)
4246
}
4347

48+
const [walletConnectedChain] = wallet.chains
49+
4450
// check if wallet is already connected to chainId
45-
if (wallet.chain === chainId) {
51+
if (
52+
walletConnectedChain.namespace === chainNamespace &&
53+
walletConnectedChain.id === chainId
54+
) {
4655
return true
4756
}
4857

0 commit comments

Comments
 (0)