|
| 1 | +--- |
| 2 | +title: Solid.js |
| 3 | +--- |
| 4 | + |
| 5 | +# {$frontmatter.title} |
| 6 | + |
| 7 | +A collection of composable functions for implementing web3-onboard in to a Solid project; |
| 8 | + |
| 9 | +## Quickstart with Injected Wallets and Ethers Provider |
| 10 | + |
| 11 | +### Install |
| 12 | + |
| 13 | +<Tabs values={['yarn', 'npm']}> |
| 14 | +<TabPanel value="yarn"> |
| 15 | + |
| 16 | +```sh copy |
| 17 | +yarn add @web3-onboard/solid @web3-onboard/injected-wallets |
| 18 | +``` |
| 19 | + |
| 20 | + </TabPanel> |
| 21 | + <TabPanel value="npm"> |
| 22 | + |
| 23 | +```sh copy |
| 24 | +npm install @web3-onboard/solid @web3-onboard/injected-wallets |
| 25 | +``` |
| 26 | + |
| 27 | + </TabPanel> |
| 28 | +</Tabs> |
| 29 | + |
| 30 | + |
| 31 | +## Quickstart |
| 32 | + |
| 33 | +```typescript |
| 34 | +import { init } from '@web3-onboard/solid' |
| 35 | +import injectedModule from '@web3-onboard/injected-wallets' |
| 36 | + |
| 37 | +const injected = injectedModule() |
| 38 | + |
| 39 | +// Only one RPC endpoint required per chain |
| 40 | +const rpcAPIKey = '<INFURA_KEY>' || '<ALCHEMY_KEY>' |
| 41 | +const rpcUrl = |
| 42 | + `https://eth-mainnet.g.alchemy.com/v2/${rpcAPIKey}` || |
| 43 | + `https://mainnet.infura.io/v3/${rpcAPIKey}` |
| 44 | + |
| 45 | +const web3Onboard = init({ |
| 46 | + wallets: [injected], |
| 47 | + chains: [ |
| 48 | + { |
| 49 | + id: '0x1', |
| 50 | + token: 'ETH', |
| 51 | + label: 'Ethereum Mainnet', |
| 52 | + rpcUrl |
| 53 | + }, |
| 54 | + { |
| 55 | + id: '0x2105', |
| 56 | + token: 'ETH', |
| 57 | + label: 'Base', |
| 58 | + rpcUrl: 'https://mainnet.base.org' |
| 59 | + } |
| 60 | + ] |
| 61 | +}) |
| 62 | + |
| 63 | +const { wallets, connectWallet, disconnectConnectedWallet, connectedWallet } = |
| 64 | + useOnboard() |
| 65 | + |
| 66 | +if (connectedWallet) { |
| 67 | + // if using ethers v6 this is: |
| 68 | + // ethersProvider = new ethers.BrowserProvider(wallet.provider, 'any') |
| 69 | + const ethersProvider = new ethers.providers.Web3Provider( |
| 70 | + connectedWallet.provider, |
| 71 | + 'any' |
| 72 | + ) |
| 73 | + // ..... do stuff with the provider |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +## Functions |
| 78 | + |
| 79 | +### `init` |
| 80 | + |
| 81 | +The `init` function initializes `web3-onboard` and makes it available to the `useOnboard()` composable. For references check out the [initialization docs for `@web3-onboard/core`](../core/README.md#initialization) |
| 82 | + |
| 83 | +#### Example usage |
| 84 | + |
| 85 | +```typescript |
| 86 | +import { init } from '@web3-onboard/solid' |
| 87 | +import injectedModule from '@web3-onboard/injected-wallets' |
| 88 | + |
| 89 | +const injected = injectedModule() |
| 90 | +const infuraKey = '<INFURA_KEY>' |
| 91 | +const rpcUrl = `https://mainnet.infura.io/v3/${infuraKey}` |
| 92 | + |
| 93 | +const web3Onboard = init({ |
| 94 | + wallets: [injected], |
| 95 | + chains: [ |
| 96 | + { |
| 97 | + id: '0x1', |
| 98 | + token: 'ETH', |
| 99 | + label: 'Ethereum Mainnet', |
| 100 | + rpcUrl |
| 101 | + } |
| 102 | + ] |
| 103 | +}) |
| 104 | +``` |
| 105 | + |
| 106 | +### `useOnboard` |
| 107 | + |
| 108 | +`useOnboard` must be used after the `init` function has been called - it will return an object that can be destructured to obtain the following reactive variables and functions: |
| 109 | + |
| 110 | +#### Example usage |
| 111 | + |
| 112 | +```typescript |
| 113 | +import { useOnboard } from '@web3-onboard/solid' |
| 114 | +// Use the composable |
| 115 | +const onboard = useOnboard() |
| 116 | +// Or destructure it |
| 117 | +const { |
| 118 | + wallets, |
| 119 | + connectedWallet, |
| 120 | + connectedChain, |
| 121 | + connectWallet, |
| 122 | + disconnectConnectedWallet |
| 123 | +} = useOnboard() |
| 124 | +// do stuff |
| 125 | +``` |
| 126 | + |
| 127 | +### `connectWallet` |
| 128 | + |
| 129 | +Function to open the onboard modal and connect to a wallet provider. For reference check out the [connecting a wallet for `@web3-onboard/core`](../core/README.md#connecting-a-wallet) |
| 130 | + |
| 131 | +#### Example usage |
| 132 | + |
| 133 | +```tsx |
| 134 | +function SampleConnect() { |
| 135 | + const { connectWallet } = useOnboard() |
| 136 | + |
| 137 | + return <button onClick={() => connectWallet()}> connect wallet</button> |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +### `connectedChain` |
| 142 | + |
| 143 | +Property that contains the current chain to which `connectedChain` is connected |
| 144 | + |
| 145 | +#### Example usage |
| 146 | + |
| 147 | +```tsx |
| 148 | +function SampleConnect() { |
| 149 | + const { connectedChain } = useOnboard() |
| 150 | + |
| 151 | + return <span>Connected Chain: { connectedChain() }</span> |
| 152 | +``` |
| 153 | +
|
| 154 | +### `connectedWallet` |
| 155 | +
|
| 156 | +Property that contains the latest connected wallet |
| 157 | +
|
| 158 | +#### Example usage |
| 159 | +
|
| 160 | +```tsx |
| 161 | +function SampleConnect() { |
| 162 | + const { connectedWallet } = useOnboard() |
| 163 | + return <span>Connected Wallet: {connectedWallet()?.label}</span> |
| 164 | +} |
| 165 | +``` |
| 166 | +
|
| 167 | +### `disconnectConnectedWallet` |
| 168 | +
|
| 169 | +Function to disconnect the `connectedWallet` |
| 170 | +
|
| 171 | +#### Example usage |
| 172 | +
|
| 173 | +```tsx |
| 174 | +import { useOnboard } from '@web3-onboard/solid' |
| 175 | +function SampleConnect() { |
| 176 | + const { disconnectConnectedWallet } = useOnboard() |
| 177 | + return ( |
| 178 | + <button onClick={() => disconnectConnectedWallet()}> |
| 179 | + {' '} |
| 180 | + disconnect wallet |
| 181 | + </button> |
| 182 | + ) |
| 183 | +} |
| 184 | +``` |
| 185 | +
|
| 186 | +### `getChain` |
| 187 | +
|
| 188 | +Function that returns the current chain a wallet is connected to |
| 189 | +
|
| 190 | +#### Example usage |
| 191 | +
|
| 192 | +```tsx |
| 193 | +import { useOnboard } from '@web3-onboard/solid' |
| 194 | +function SampleConnect() { |
| 195 | + const { getChain } = useOnboard() |
| 196 | + return <span>MetaMask is connected to: {getChain('MetaMask')}</span> |
| 197 | +} |
| 198 | +``` |
| 199 | +
|
| 200 | +### `setChain` |
| 201 | +
|
| 202 | +Function to set the chain of a wallet |
| 203 | +
|
| 204 | +#### Example usage |
| 205 | +
|
| 206 | +```tsx |
| 207 | +import { useOnboard } from '@web3-onboard/solid' |
| 208 | +function SampleConnect() { |
| 209 | + const { setChain } = useOnboard() |
| 210 | + const set = () => setChain({ wallet: 'MetaMask', chainId: '0x1' }) |
| 211 | + return ( |
| 212 | + <button type="button" onClick={set}> |
| 213 | + Set MetaMask chain to mainnet |
| 214 | + </button> |
| 215 | + ) |
| 216 | +} |
| 217 | +``` |
| 218 | +
|
| 219 | +### `settingChain` |
| 220 | +
|
| 221 | +Readonly boolean ref that tracks the status of setting the chain |
| 222 | +
|
| 223 | +#### Example usage |
| 224 | +
|
| 225 | +```tsx |
| 226 | +import { useOnboard } from '@web3-onboard/solid' |
| 227 | +function SampleConnect() { |
| 228 | + const { settingChain } = useOnboard() |
| 229 | + return { settingChain } |
| 230 | +} |
| 231 | +``` |
| 232 | +
|
| 233 | +### `wallets` |
| 234 | +
|
| 235 | +Readonly ref that contains every wallet that has been connected |
| 236 | +
|
| 237 | +#### Example usage |
| 238 | +
|
| 239 | +```tsx |
| 240 | +import { useOnboard } from '@web3-onboard/solid' |
| 241 | +function SampleConnect() { |
| 242 | + const { wallets } = useOnboard() |
| 243 | + return( |
| 244 | + <ul> |
| 245 | + <For each={wallets()}>{wallet=>{ |
| 246 | + return <li> |
| 247 | + {wallet.label} |
| 248 | + </li> |
| 249 | + }} |
| 250 | + </For> |
| 251 | + </ul> |
| 252 | + ) |
| 253 | + } |
| 254 | +} |
| 255 | +``` |
| 256 | + |
| 257 | +### `lastConnectedTimestamp` |
| 258 | + |
| 259 | +Readonly ref that contains the last time that the user connected a wallet in milliseconds |
| 260 | + |
| 261 | +#### Example usage |
| 262 | + |
| 263 | +```tsx |
| 264 | +import { useOnboard } from '@web3-onboard/solid' |
| 265 | +function SampleConnect() { |
| 266 | + const { lastConnectedTimestamp } = useOnboard() |
| 267 | + return ( |
| 268 | + <span>Your last connection timestamp was: {lastConnectedTimestamp}</span> |
| 269 | + ) |
| 270 | +} |
| 271 | +``` |
0 commit comments