Skip to content

Commit 8b84d1c

Browse files
authored
[react-v2.2.1-alpha.3, core-v2.3.1-alpha.3, vue-v2.1.1-alpha.2] : Feature - React hook updates (#1093)
* Update react package version * New react hoooks for all actions exposed through api + updateAccountCenter, Update OnboardAPI type * Update core version where needed * Rename for better alignment with core docs * Update export from core/import in react package, update docs * Update signup link * Update packages/react/README.md * Position AC docs
1 parent bb543ad commit 8b84d1c

File tree

8 files changed

+302
-18
lines changed

8 files changed

+302
-18
lines changed

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/core",
3-
"version": "2.3.1-alpha.2",
3+
"version": "2.3.1-alpha.3",
44
"scripts": {
55
"build": "rollup -c",
66
"dev": "rollup -c -w",

packages/core/src/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ const API = {
3434
setLocale,
3535
updateNotify,
3636
customNotification,
37-
updateBalances
37+
updateBalances,
38+
updateAccountCenter
3839
}
3940
}
4041
}
@@ -45,7 +46,13 @@ export type {
4546
ConnectOptions,
4647
DisconnectOptions,
4748
WalletState,
48-
ConnectedChain
49+
ConnectedChain,
50+
AccountCenter,
51+
AppState,
52+
CustomNotification,
53+
Notification,
54+
NotifyOptions,
55+
UpdateNotification
4956
} from './types'
5057

5158
export type { EIP1193Provider } from '@web3-onboard/common'

packages/core/src/types.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,29 @@ export interface OnboardAPI {
5353
connectWallet: typeof connect
5454
disconnectWallet: typeof disconnect
5555
setChain: typeof setChain
56-
state: typeof state
56+
state: {
57+
select: typeof state.select
58+
get: typeof state.get
59+
actions: ExposedActions
60+
}
5761
}
62+
63+
interface ExposedActions {
64+
setWalletModules: (wallets: WalletInit[]) => void
65+
setLocale: (locale: string) => void
66+
updateNotify: (update: Partial<NotifyOptions>) => void
67+
customNotification: (
68+
updatedNotification: CustomNotification
69+
) => {
70+
dismiss: () => void
71+
update: UpdateNotification
72+
}
73+
updateBalances: (addresses?: string[]) => Promise<void>
74+
updateAccountCenter: (
75+
update: AccountCenter | Partial<AccountCenter>
76+
) => void
77+
}
78+
5879
export interface ConnectOptions {
5980
autoSelect?: { label: string; disableModals: boolean }
6081
}

packages/demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
"dependencies": {
2525
"@web3-onboard/coinbase": "^2.0.6-alpha.1",
26-
"@web3-onboard/core": "^2.3.1-alpha.1",
26+
"@web3-onboard/core": "^2.3.1-alpha.3",
2727
"@web3-onboard/dcent": "^2.0.3-alpha.1",
2828
"@web3-onboard/fortmatic": "^2.0.5-alpha.1",
2929
"@web3-onboard/gnosis": "^2.0.4-alpha.1",

packages/react/README.md

Lines changed: 214 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ A collection of React hooks for implementing web3-onboard in to a React project
1010

1111
```javascript
1212
import React from 'react'
13+
import { ethers } from 'ethers'
1314
import {
1415
init,
1516
useConnectWallet,
@@ -27,6 +28,9 @@ import torusModule from '@web3-onboard/torus'
2728
import keepkeyModule from '@web3-onboard/keepkey'
2829
import dcentModule from '@web3-onboard/dcent'
2930

31+
// Sign up to get your free API key at https://explorer.blocknative.com/?signup=true
32+
const dappId = '1730eff0-9d50-4382-a3fe-89f0d34a2070'
33+
3034
const injected = injectedModule()
3135
const coinbase = coinbaseModule()
3236
const walletConnect = walletConnectModule()
@@ -52,6 +56,18 @@ const trezor = trezorModule(trezorOptions)
5256

5357
const dcent = dcentModule()
5458

59+
const magic = magicModule({
60+
// Example api key, may need to be updated when max hits reached
61+
// Get one to test with for free from https://magic.link/
62+
apiKey: 'pk_live_02207D744E81C2BA',
63+
userEmail: localStorage.getItem('magicUserEmail')
64+
})
65+
66+
const web3auth = web3authModule({
67+
clientId:
68+
'DJuUOKvmNnlzy6ruVgeWYWIMKLRyYtjYa9Y10VCeJzWZcygDlrYLyXsBQjpJ2hxlBO9dnl8t9GmAC2qOP5vnIGo'
69+
})
70+
5571
const web3Onboard = init({
5672
wallets: [
5773
injected,
@@ -60,7 +76,8 @@ const web3Onboard = init({
6076
trezor,
6177
walletConnect,
6278
keepkey,
63-
fortmatic,
79+
web3auth,
80+
magic,
6481
portis,
6582
torus,
6683
dcent
@@ -119,6 +136,23 @@ const web3Onboard = init({
119136
minimal: true
120137
}
121138
},
139+
apiKey: dappId,
140+
notify: {
141+
enabled: true,
142+
transactionHandler: transaction => {
143+
console.log({ transaction })
144+
if (transaction.eventCode === 'txPool') {
145+
return {
146+
// autoDismiss set to `0` will persist the notification until the user excuses it
147+
autoDismiss: 0,
148+
// message: `Your transaction is pending, click <a href="https://rinkeby.etherscan.io/tx/${transaction.hash}">here</a> for more info.`,
149+
// or you could use onClick for when someone clicks on the notification itself
150+
onClick: () =>
151+
window.open(`https://rinkeby.etherscan.io/tx/${transaction.hash}`)
152+
}
153+
}
154+
}
155+
}
122156
i18n: {
123157
en: {
124158
connect: {
@@ -141,9 +175,23 @@ const web3Onboard = init({
141175
})
142176

143177
function App() {
144-
const [{ wallet, connecting }, connect, disconnect] = useConnectWallet()
178+
const [{ wallet, connecting }, connect, disconnect, updateBalances, setWalletModules] =
179+
useConnectWallet()
145180
const [{ chains, connectedChain, settingChain }, setChain] = useSetChain()
181+
const [notifications, customNotification, updateNotify] = useNotifications()
146182
const connectedWallets = useWallets()
183+
const updateAccountCenter = useAccountCenter()
184+
const updateLocale = useSetLocale()
185+
186+
let provider
187+
188+
useEffect(() => {
189+
if (!wallet?.provider) {
190+
provider = null
191+
} else {
192+
provider = new ethers.providers.Web3Provider(wallet.provider, 'any')
193+
}
194+
}, [wallet]
147195

148196
return (
149197
<div>
@@ -169,6 +217,19 @@ function App() {
169217
)}
170218
<button onClick={() => disconnect(wallet)}>Disconnect Wallet</button>
171219
</div>
220+
<button
221+
className="bn-demo-button"
222+
onClick={() => {
223+
// Default position of AccountCenter it top right
224+
updateAccountCenter({
225+
position: 'bottomLeft',
226+
enabled: true,
227+
minimal: true
228+
})
229+
}}
230+
>
231+
Account Center to Bottom Left
232+
</button>
172233
)}
173234

174235
{connectedWallets.map(({ label, accounts }) => {
@@ -200,7 +261,9 @@ import { useConnectWallet } from '@web3-onboard/react'
200261
type UseConnectWallet = (): [
201262
{ wallet: WalletState | null; connecting: boolean },
202263
(options: ConnectOptions) => Promise<void>,
203-
(wallet: DisconnectOptions) => Promise<void>
264+
(wallet: DisconnectOptions) => Promise<void>,
265+
(addresses?: string[]) => Promise<void>,
266+
(wallets: WalletInit[]) => void
204267
]
205268

206269
type ConnectOptions = {
@@ -220,13 +283,17 @@ type WalletState = {
220283
instance?: unknown
221284
}
222285

286+
type WalletInit = (helpers: WalletHelpers) => WalletModule | WalletModule[] | null;
287+
223288
const [
224289
{
225290
wallet, // the wallet that has been connected or null if not yet connected
226291
connecting // boolean indicating if connection is in progress
227292
},
228293
connect, // function to call to initiate user to connect wallet
229-
disconnect // function to call to with wallet<DisconnectOptions> to disconnect wallet
294+
disconnect, // function to call with wallet<DisconnectOptions> to disconnect wallet
295+
updateBalances, // function to be called with an optional array of wallet addresses connected through Onboard to update balance or empty/no params to update all connected wallets
296+
setWalletModules // function to be called with an array of wallet modules to conditionally allow connection of wallet types i.e. setWalletModules([ledger, trezor, injected])
230297
] = useConnectWallet()
231298
```
232299
@@ -264,6 +331,108 @@ const [
264331
] = useSetChain()
265332
```
266333
334+
## `useNotifications`
335+
336+
This hook allows the dev to access all notifications if enabled, send custom notifications and update notify <enable/disable & update transactionHandler function>
337+
**note** requires an API key be added to the initialization, enabled by default if API key exists
338+
339+
```typescript
340+
import { useNotifications } from '@web3-onboard/react'
341+
342+
type UseNotifications = (): [
343+
Notification[],
344+
(updatedNotification: CustomNotification) => {
345+
dismiss: () => void
346+
update: UpdateNotification
347+
},
348+
(update: Partial<NotifyOptions>) => void
349+
]
350+
351+
type Notification = {
352+
id: string
353+
key: string
354+
type: NotificationType
355+
network: Network
356+
startTime?: number
357+
eventCode: string
358+
message: string
359+
autoDismiss: number
360+
link?: string
361+
onClick?: (event: Event) => void
362+
}
363+
type TransactionHandlerReturn =
364+
| CustomNotification
365+
| boolean
366+
| void
367+
type CustomNotification = Partial<
368+
Omit<Notification, 'startTime' | 'network' | 'id' | 'key'>
369+
>
370+
type CustomNotificationUpdate = Partial<
371+
Omit<Notification, 'startTime' | 'network'>
372+
>
373+
type NotificationType = 'pending' | 'success' | 'error' | 'hint'
374+
interface UpdateNotification {
375+
(notificationObject: CustomNotification): {
376+
dismiss: () => void
377+
update: UpdateNotification
378+
}
379+
}
380+
type NotifyOptions = {
381+
/**
382+
* Defines whether to subscribe to transaction events or not
383+
* default: true
384+
*/
385+
enabled?: boolean
386+
/**
387+
* Callback that receives all transaction events
388+
* Return a custom notification based on the event
389+
* Or return false to disable notification for this event
390+
* Or return undefined for a default notification
391+
*/
392+
transactionHandler: (
393+
event: EthereumTransactionData
394+
) => TransactionHandlerReturn
395+
396+
const [
397+
notifications, // the list of all notifications that update when notifications are added, updated or removed
398+
customNotification, // a function that takes a customNotification object and allows custom notifications to be shown to the user, returns an update and dismiss callback
399+
updateNotify // a function that takes a NotifyOptions object to allow updating of the properties
400+
] = useNotifications()
401+
402+
// View notifications as they come in if you would like to handle them independent of the notification display
403+
useEffect(() => {
404+
console.log(notifications)
405+
}, [notifications])
406+
407+
// Custom notification example
408+
<button
409+
className="bn-demo-button"
410+
onClick={() => {
411+
const { update } =
412+
customNotification({
413+
eventCode: 'dbUpdate',
414+
type: 'hint',
415+
message: 'Custom hint notification created by the dapp',
416+
onClick: () =>
417+
window.open(`https://www.blocknative.com`)
418+
})
419+
// Update your notification example below
420+
setTimeout(
421+
() =>
422+
update({
423+
eventCode: 'dbUpdateSuccess',
424+
message: 'Hint notification reason resolved!',
425+
type: 'success',
426+
autoDismiss: 5000
427+
}),
428+
4000
429+
)
430+
}}
431+
>
432+
Custom Hint Notification
433+
</button>
434+
```
435+
267436
## `useWallets`
268437
269438
This hook allows you to track the state of all the currently connected wallets.
@@ -275,3 +444,44 @@ type UseWallets = (): WalletState[]
275444

276445
const connectedWallets = useWallets()
277446
```
447+
448+
## `useAccountCenter`
449+
450+
This hook allows you to track and update the state of the AccountCenter
451+
452+
```typescript
453+
import { useAccountCenter } from '@web3-onboard/react'
454+
455+
type UseAccountCenter = (): ((
456+
update: AccountCenter | Partial<AccountCenter>
457+
) => void)
458+
459+
type AccountCenterPosition =
460+
| 'topRight'
461+
| 'bottomRight'
462+
| 'bottomLeft'
463+
| 'topLeft'
464+
465+
type AccountCenter = {
466+
enabled: boolean
467+
position?: AccountCenterPosition
468+
expanded?: boolean
469+
minimal?: boolean
470+
}
471+
472+
const updateAccountCenter = useAccountCenter()
473+
```
474+
475+
## `useSetLocale`
476+
477+
This hook allows you to set the locale of your application to allow language updates associated with the i18n config
478+
479+
```typescript
480+
import { useSetLocale } from '@web3-onboard/react'
481+
482+
type useSetLocale = (): ((locale: string) => void)
483+
484+
const updateLocale = useSetLocale()
485+
486+
updateLocale('es')
487+
```

packages/react/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/react",
3-
"version": "2.2.1-alpha.2",
3+
"version": "2.2.1-alpha.3",
44
"description": "Collection of React Hooks for web3-onboard",
55
"module": "dist/index.js",
66
"browser": "dist/index.js",
@@ -23,7 +23,7 @@
2323
"typescript": "^4.5.5"
2424
},
2525
"dependencies": {
26-
"@web3-onboard/core": "^2.3.1-alpha.2",
26+
"@web3-onboard/core": "^2.3.1-alpha.3",
2727
"@web3-onboard/common": "^2.1.3-alpha.1",
2828
"use-sync-external-store": "1.0.0"
2929
},

0 commit comments

Comments
 (0)