Skip to content

Commit 1f8646d

Browse files
authored
[core-v2.4.1-alpha.1, react-v2.2.4-alpha.1, vue-v2.1.4-alpha.1] : Update - Preflight notification updates(react hook, positioning variables, cleanup) (#1144)
* Add hook for preflight notifications, update types to break out txDetails, update transactionTypes to all preflight to hints per Gustavo * Add independent positioning for notifications and account center through CSS variables * return preflight errors to error notifications, remove network icon for them * Remove unused type * Add hook for PreFlight notifications * Update import type * Update docs * Case conforming
1 parent 45d0e77 commit 1f8646d

File tree

13 files changed

+165
-55
lines changed

13 files changed

+165
-55
lines changed

packages/core/README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ onboard.state.actions.updateAccountCenter({
713713
```
714714

715715
**`setPrimaryWallet`**
716-
The primary wallet (first in the list of connected wallets) and primary account (first in the list of connected accounts for a wallet) can be set by using the `setPrimaryWallet` function. The wallet that is to be set needs to be passed in for the first parameter and if you would like to set the primary account, the address of that account also needs to be passed in:
716+
The primary wallet (first in the list of connected wallets) and primary account (first in the list of connected accounts for a wallet) can be set by using the `setPrimaryWallet` function. The wallet that is set needs to be passed in for the first parameter and if you would like to set the primary account, the address of that account also needs to be passed in:
717717

718718
```typescript
719719
// set the second wallet in the wallets array as the primary
@@ -792,6 +792,10 @@ The Onboard styles can customized via [CSS variables](https://developer.mozilla.
792792

793793
/* CUSTOMIZE ACCOUNT CENTER*/
794794
--account-center-z-index
795+
--account-center-position-top
796+
--account-center-position-bottom
797+
--account-center-position-right
798+
--account-center-position-left
795799
--account-center-minimized-background
796800
--account-center-maximized-upper-background
797801
--account-center-maximized-network-section
@@ -925,7 +929,12 @@ The Onboard styles can customized via [CSS variables](https://developer.mozilla.
925929
--account-select-modal-margin-4: 1rem;
926930
--account-select-modal-margin-5: 0.5rem;
927931

928-
/* notify STYLES */
932+
/* NOTIFY STYLES */
933+
/* Notify Positioning variables only take effect if Notify is Positioned separate of Account Center */
934+
--notify-onboard-container-position-top
935+
--notify-onboard-container-position-bottom
936+
--notify-onboard-container-position-right
937+
--notify-onboard-container-position-left
929938
--notify-onboard-font-family-normal
930939
--notify-onboard-font-size-5
931940
--notify-onboard-gray-300

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.4.0",
3+
"version": "2.4.1-alpha.1",
44
"description": "Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardized spec compliant web3 providers for all supported wallets, framework agnostic modern javascript UI with code splitting, CSS customization, multi-chain and multi-account support, reactive wallet state subscriptions and real-time transaction state change notifications.",
55
"keywords": [
66
"Ethereum",

packages/core/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ export type {
6161
CustomNotification,
6262
Notification,
6363
Notify,
64-
UpdateNotification
64+
UpdateNotification,
65+
PreflightNotificationsOptions
6566
} from './types'
6667

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

packages/core/src/preflight-notifications.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ export async function preflightNotifications(
6363
if (transactionCost.gt(new BigNumber(balance))) {
6464
const eventCode = 'nsfFail'
6565

66-
const newNotification = buildNotification(eventCode, id)
67-
addNotification(newNotification)
66+
addNotification(buildNotification(eventCode, id))
6867
}
6968
}
7069

@@ -93,8 +92,7 @@ export async function preflightNotifications(
9392
}, reminderTimeout)
9493

9594
const eventCode = 'txRequest'
96-
const newNotification = buildNotification(eventCode, id)
97-
addNotification(newNotification)
95+
addNotification(buildNotification(eventCode, id))
9896

9997
// if not provided with sendTransaction function,
10098
// resolve with transaction hash(or void) so dev can initiate transaction
@@ -112,8 +110,7 @@ export async function preflightNotifications(
112110
}
113111
const { eventCode, errorMsg } = extractMessageFromError(error as CatchError)
114112

115-
const newNotification = buildNotification(eventCode, id)
116-
addNotification(newNotification)
113+
addNotification(buildNotification(eventCode, id))
117114
console.error(errorMsg)
118115
return
119116
}

packages/core/src/types.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,16 @@ export interface PreflightNotificationsOptions {
213213
estimateGas?: () => Promise<string>
214214
gasPrice?: () => Promise<string>
215215
balance?: string | number
216-
txDetails?: {
217-
value: string | number
218-
to?: string
219-
from?: string
220-
}
216+
txDetails?: TxDetails
221217
txApproveReminderTimeout?: number
222218
}
223219

220+
export interface TxDetails {
221+
value: string | number
222+
to?: string
223+
from?: string
224+
}
225+
224226
// ==== ACTIONS ==== //
225227
export type Action =
226228
| AddChainsAction

packages/core/src/views/Index.svelte

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,23 @@
2424
.select('notifications')
2525
.pipe(startWith(state.get().notifications))
2626
27-
const positioningDefaults = {
28-
topLeft: 'top: 0; left: 0;',
29-
topRight: 'top: 0; right: 0;',
30-
bottomRight: 'bottom: 0; right: 0;',
31-
bottomLeft: 'bottom: 0; left: 0;'
27+
const accountCenterPositioning = 'account-center'
28+
const notifyPositioning = 'notify-onboard-container'
29+
const setPositioningDefaults = (targetComponentVariable: string) => {
30+
return {
31+
topLeft: `
32+
top: var(--${targetComponentVariable}-position-top, 0);
33+
left: var(--${targetComponentVariable}-position-left, 0);`,
34+
topRight: `
35+
top: var(--${targetComponentVariable}-position-top, 0);
36+
right: var(--${targetComponentVariable}-position-right, 0);`,
37+
bottomRight: `
38+
bottom: var(--${targetComponentVariable}-position-bottom, 0);
39+
right: var(--${targetComponentVariable}-position-right, 0);`,
40+
bottomLeft: `
41+
bottom: var(--${targetComponentVariable}-position-bottom, 0);
42+
left: var(--${targetComponentVariable}-position-left, 0);`
43+
}
3244
}
3345
3446
$: sharedContainer =
@@ -334,8 +346,9 @@
334346
{#if displayAccountCenterNotifySameContainer}
335347
<div
336348
class="container flex flex-column fixed z-indexed"
337-
style="{positioningDefaults[$accountCenter$.position]}; {device.type ===
338-
'mobile' && $accountCenter$.position.includes('top')
349+
style="{setPositioningDefaults(accountCenterPositioning)[
350+
$accountCenter$.position
351+
]}; {device.type === 'mobile' && $accountCenter$.position.includes('top')
339352
? 'padding-bottom: 0;'
340353
: device.type === 'mobile' && $accountCenter$.position.includes('bottom')
341354
? 'padding-top:0;'
@@ -373,8 +386,9 @@
373386
{#if displayAccountCenterSeparate}
374387
<div
375388
class="container flex flex-column fixed z-indexed"
376-
style="{positioningDefaults[$accountCenter$.position]}; {device.type ===
377-
'mobile' && $accountCenter$.position.includes('top')
389+
style="{setPositioningDefaults(accountCenterPositioning)[
390+
$accountCenter$.position
391+
]}; {device.type === 'mobile' && $accountCenter$.position.includes('top')
378392
? 'padding-bottom: 0;'
379393
: device.type === 'mobile' && $accountCenter$.position.includes('bottom')
380394
? 'padding-top:0;'
@@ -400,8 +414,9 @@
400414
{#if displayNotifySeparate}
401415
<div
402416
class="container flex flex-column fixed z-indexed"
403-
style="{positioningDefaults[$notify$.position]}; {device.type ===
404-
'mobile' && $notify$.position.includes('top')
417+
style="{setPositioningDefaults(notifyPositioning)[
418+
$notify$.position
419+
]}; {device.type === 'mobile' && $notify$.position.includes('top')
405420
? 'padding-bottom: 0;'
406421
: device.type === 'mobile' && $notify$.position.includes('bottom')
407422
? 'padding-top:0;'

packages/core/src/views/notify/NotificationContent.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
{notification.message}
7575
</span>
7676

77-
{#if notification.id && (!notification.id.includes('customNotification') && !notification.id.includes('preflight') )}
77+
{#if notification.id && !notification.id.includes('customNotification') && !notification.id.includes('preflight')}
7878
<span class="hash-time">
7979
{#if notification.link}
8080
<a

packages/demo/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
},
2424
"dependencies": {
2525
"@web3-onboard/coinbase": "^2.0.8",
26-
"@web3-onboard/core": "^2.4.0",
27-
"@web3-onboard/dcent": "^2.0.6",
26+
"@web3-onboard/core": "^2.4.1-alpha.1",
27+
"@web3-onboard/dcent": "^2.0.5",
2828
"@web3-onboard/fortmatic": "^2.0.6",
2929
"@web3-onboard/gnosis": "^2.0.5",
3030
"@web3-onboard/injected-wallets": "^2.0.12",

packages/react/README.md

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ type UseConnectWallet = (): [
8080
(options: ConnectOptions) => Promise<void>,
8181
(wallet: DisconnectOptions) => Promise<void>,
8282
(addresses?: string[]) => Promise<void>,
83-
(wallets: WalletInit[]) => void
83+
(wallets: WalletInit[]) => void,
84+
(wallet: WalletState, address?: string) => void
8485
]
8586

8687
type ConnectOptions = {
@@ -110,8 +111,25 @@ const [
110111
connect, // function to call to initiate user to connect wallet
111112
disconnect, // function to call with wallet<DisconnectOptions> to disconnect wallet
112113
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
113-
setWalletModules // function to be called with an array of wallet modules to conditionally allow connection of wallet types i.e. setWalletModules([ledger, trezor, injected])
114+
setWalletModules, // function to be called with an array of wallet modules to conditionally allow connection of wallet types i.e. setWalletModules([ledger, trezor, injected])
115+
setPrimaryWallet // function that can set the primary wallet and/or primary account within that wallet. The wallet that is set needs to be passed in for the first parameter and if you would like to set the primary account, the address of that account also needs to be passed in
114116
] = useConnectWallet()
117+
118+
119+
```
120+
**`setPrimaryWallet`**
121+
The primary wallet (first in the list of connected wallets) and primary account (first in the list of connected accounts for a wallet) can be set by using the `setPrimaryWallet` function. The wallet that is set needs to be passed in for the first parameter and if you would like to set the primary account, the address of that account also needs to be passed in:
122+
123+
```typescript
124+
// set the second wallet in the wallets array as the primary
125+
setPrimaryWallet(wallets[1])
126+
127+
// set the second wallet in the wallets array as the primary wallet
128+
// as well as setting the third account in that wallet as the primary account
129+
setPrimaryWallet(
130+
wallets[1],
131+
wallets[1].accounts[2].address
132+
)
115133
```
116134

117135
## `useSetChain`
@@ -152,17 +170,17 @@ const [
152170

153171
This hook allows the dev to access all notifications if enabled, send custom notifications and update notify <enable/disable & update transactionHandler function>
154172
**note** requires an API key be added to the initialization, enabled by default if API key exists
173+
For full Notification documentation please see [Notify section within the `@web3-onboard/core` docs](../core/README.md#options)
155174

156175
```typescript
157-
import { useNotifications } from '@web3-onboard/react'
158-
159176
type UseNotifications = (): [
160177
Notification[],
161178
(updatedNotification: CustomNotification) => {
162179
dismiss: () => void
163180
update: UpdateNotification
164181
},
165-
(update: Partial<Notify>) => void
182+
(update: Partial<Notify>) => void,
183+
(options: PreflightNotificationsOptions) => Promise<void | string>
166184
]
167185

168186
type Notification = {
@@ -209,18 +227,75 @@ type Notify = {
209227
transactionHandler: (
210228
event: EthereumTransactionData
211229
) => TransactionHandlerReturn
230+
/**
231+
* Position of notifications that defaults to the same position as the
232+
* Account Center (if enabled) of the top right if AC is disabled
233+
* and notifications are enabled (enabled by default with API key)
234+
*/
235+
position?: NotificationPosition
236+
}
237+
238+
type PreflightNotificationsOptions = {
239+
sendTransaction?: () => Promise<string | void>
240+
estimateGas?: () => Promise<string>
241+
gasPrice?: () => Promise<string>
242+
balance?: string | number
243+
txDetails?: TxDetails
244+
txApproveReminderTimeout?: number
245+
}
246+
type TxDetails = {
247+
value: string | number
248+
to?: string
249+
from?: string
250+
}
251+
```
252+
253+
```typescript
254+
import { useNotifications } from '@web3-onboard/react'
212255

213256
const [
214257
notifications, // the list of all notifications that update when notifications are added, updated or removed
215258
customNotification, // a function that takes a customNotification object and allows custom notifications to be shown to the user, returns an update and dismiss callback
216-
updateNotify // a function that takes a Notify object to allow updating of the properties
259+
updateNotify, // a function that takes a Notify object to allow updating of the properties
260+
preflightNotifications // a function that takes a PreflightNotificationsOption to create preflight notifications
217261
] = useNotifications()
218262

219263
// View notifications as they come in if you would like to handle them independent of the notification display
220264
useEffect(() => {
221265
console.log(notifications)
222266
}, [notifications])
223267

268+
const sendTransactionWithPreFlightNotifications = async () => {
269+
const balanceValue = Object.values(wallet.accounts[0].balance)[0]
270+
271+
const signer = provider.getUncheckedSigner()
272+
273+
const txDetails = {
274+
to: toAddress,
275+
value: 1000000000000000
276+
}
277+
278+
const sendTransaction = () => {
279+
return signer.sendTransaction(txDetails).then(tx => tx.hash)
280+
}
281+
282+
const gasPrice = () => provider.getGasPrice().then(res => res.toString())
283+
284+
const estimateGas = () => {
285+
return provider.estimateGas(txDetails).then(res => res.toString())
286+
}
287+
288+
const transactionHash =
289+
await preflightNotifications({
290+
sendTransaction,
291+
gasPrice,
292+
estimateGas,
293+
balance: balanceValue,
294+
txDetails: txDetails
295+
})
296+
console.log(transactionHash)
297+
}
298+
224299
// Custom notification example
225300
<button
226301
className="bn-demo-button"
@@ -248,6 +323,14 @@ useEffect(() => {
248323
>
249324
Custom Hint Notification
250325
</button>
326+
<button
327+
className="bn-demo-button"
328+
onClick={async () => {
329+
sendTransactionWithPreFlightNotifications()
330+
}}
331+
>
332+
Send with In Flight and Pre Flight Notifications
333+
</button>
251334
```
252335

253336
## `useWallets`

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.3",
3+
"version": "2.2.4-alpha.1",
44
"description": "A collection of React hooks for integrating Web3-Onboard in to React and Next.js projects. Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardised spec compliant web3 providers for all supported wallets, modern javascript UI with code splitting, CSS customization, multi-chain and multi-account support, reactive wallet state subscriptions and real-time transaction state change notifications.",
55
"keywords": [
66
"Ethereum",
@@ -59,7 +59,7 @@
5959
"typescript": "^4.5.5"
6060
},
6161
"dependencies": {
62-
"@web3-onboard/core": "^2.4.0",
62+
"@web3-onboard/core": "^2.4.1-alpha.1",
6363
"@web3-onboard/common": "^2.1.5",
6464
"use-sync-external-store": "1.0.0"
6565
},

0 commit comments

Comments
 (0)