Skip to content

Commit 14facf1

Browse files
Adamj1232lnbc1QWFyb24taylorjdawson
authored
[core-v2.3.0-alpha.1] : Feature - Transaction notifications (#1070)
* Initial Notify setup steps * Styling notification * Styling getting closer * Layout looking a lot better * Animations working properly for pending * Updated cleanliness * Demo stuff * Props passed in properly * Rename and refactors * Notify into AC * Cleanup and merge in develop * More cleanup and positioning * Add css variables * Fix and style timer * More cleanup * Updated structure and componentized further * cleanup * Add flags to denote v1 ref code * Playing around with organization * In progress * Progress * Progress * Working notifications * Latest SDK branch * Add timeout to unsub calls * Add timeout to disconnect * Use NotifyEventStyles type * Update comments * Tidy up import statements * Update docs * Update versions * Componentise notification * Add margin to modal * Remove network from customize notification * Add hint and error types back * Throw if invalid custom notification * Upgrade sdk and handle unsupported network error * Auto dismiss notification * More README updates * Update spacing and layout to display properly * cleanup and more positioning * Animations working * Fix animation speed, linking in transaction hash * Fix padding on AC * Add text color to non-linking transaction hash * More variablization and updating docs * Add demo with updates for testing in the future * Return the 4 second default dismiss to notifications, Fix margin causing view overflow in AC when switching chains * Remove margin * Update on hover to display close button, chain icon size, add close icon from mock, update margin * Add new hint notification * Update README for more info around notifications * Revert dismiss timeout used for testing * styling touchups * Add DAPP notifications * reset sizing for network select * Working but needs cleanup * Update action to return update and dismiss methods * Update docs and remove unneeded type * Cleanup of unneeded imports * Updated styling of notifications for second round of style updates, removed status icon if not defined * More styling of new info icon layout * Update padding for info icon * Handle replacement transactions * Working on scrolling * Update notification working * Revert scroll on container breaking animations * Updates SDK to version with multichain * Always display close button on mobile notifications * Fix scrolling * Fix styling on notification container * Handle newest notifications entering closest to AC despite location * Updated styling of container to allow more space on mobile * Update layout for single line notifications * Updates to styling of hash/time container * Update styling for new layout * Working but animation updates coming * Update layout for all animations to match mobile animations * Removes unused de * Update yarn and remove uuid from dep in core * Remove 1rem buffer for animations * Revert to 4 second dismiss * Update types in action * Add nanoId, update types for notification update method * Update animations of removal * Fix comment to reflect functionality * Fix pending on safari * Update packages/core/rollup.config.js Co-authored-by: Aaron Barnard <abarnard@protonmail.com> * Update packages/demo/src/App.svelte Co-authored-by: Aaron Barnard <abarnard@protonmail.com> * remove unused import * merge in dev * Update types and remove unneeded import * Fix animation cutoff on end of list when removing an element * Update readme * Update packages/core/README.md Co-authored-by: Taylor Dawson <taylorjdawson@gmail.com> * Update packages/core/README.md Co-authored-by: Taylor Dawson <taylorjdawson@gmail.com> * Update packages/core/README.md Co-authored-by: Taylor Dawson <taylorjdawson@gmail.com> * Update packages/core/README.md Co-authored-by: Taylor Dawson <taylorjdawson@gmail.com> * Update packages/core/README.md Co-authored-by: Taylor Dawson <taylorjdawson@gmail.com> * Update packages/demo/src/App.svelte * Update packages/core/README.md Co-authored-by: Taylor Dawson <taylorjdawson@gmail.com> * Update packages/core/README.md Co-authored-by: Taylor Dawson <taylorjdawson@gmail.com> * Update packages/core/README.md Co-authored-by: Taylor Dawson <taylorjdawson@gmail.com> * Update packages/core/README.md * Fix styling and scroll direction when notifications are placed on bottom of screen * Add latest packages to Core README * Handle scroll container withaccount center expanded * Update touch-events for scroll on mobile Co-authored-by: Aaron Barnard <abarnard@protonmail.com> Co-authored-by: Taylor Dawson <taylorjdwson@gmail.com?
1 parent 70d687a commit 14facf1

Some content is hidden

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

51 files changed

+1912
-228
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "2.0.2",
44
"private": true,
55
"workspaces": [
6-
"./packages/*"
6+
"packages/*"
77
],
88
"scripts": {
99
"install-m1-mac": "yarn install --ignore-optional",

packages/core/README.md

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Install the core module:
1010

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

13-
`npm i @web3-onboard/injected-wallets @web3-onboard/coinbase @web3-onboard/ledger @web3-onboard/trezor @web3-onboard/keepkey @web3-onboard/walletconnect @web3-onboard/torus @web3-onboard/portis @web3-onboard/mew @web3-onboard/gnosis @web3-onboard/magic @web3-onboard/fortmatic`
13+
`npm i @web3-onboard/injected-wallets @web3-onboard/coinbase @web3-onboard/ledger @web3-onboard/trezor @web3-onboard/keepkey @web3-onboard/walletconnect @web3-onboard/web3auth @web3-onboard/torus @web3-onboard/portis @web3-onboard/mew @web3-onboard/gnosis @web3-onboard/magic @web3-onboard/fortmatic @web3-onboard/dcent`
1414

1515
Note:
1616

@@ -29,6 +29,8 @@ type InitOptions {
2929
appMetadata?: AppMetadata
3030
i18n?: i18nOptions
3131
accountCenter?: AccountCenterOptions
32+
apiKey?: string
33+
notify?: Partial<NotifyOptions>
3234
}
3335
```
3436

@@ -116,6 +118,99 @@ type AccountCenterPosition =
116118
| 'topLeft'
117119
```
118120
121+
**`notify`**
122+
Notify provides by default transaction notifications for all connected wallets on the current blockchain. When switching chains the previous chain listeners remain active for 60 seconds to allow capture and report of an remaining transactions that may be in flight.
123+
By default transaction notifications are captured if a DAppID is provided in the Onboard config along with the Account Center being enabled.
124+
An object that defines whether transaction notifications will display (defaults to true if an API key is provided). This object contains an `enabled` flag prop and an optional `transactionHandler` which is a callback that can disable or allow customizations of notifications.
125+
Currently notifications are positioned in the same location as the account center (either below, if the Account Center is positioned along the top, or above if positioned on the bottom of the view).
126+
The `transactionHandler` can react off any property of the Ethereum TransactionData returned to the callback from the event (see console.log in example init). In turn, it can return a Custom `Notification` object to define the verbiage, styling, or add functionality:
127+
- `Notification.message` - to completely customize the message shown
128+
- `Notification.eventCode` - handle codes in your own way - see codes here under the notify prop [default en file here](src/i18n/en.json)
129+
- `Notification.type` - icon type displayed (see `NotificationType` below for options)
130+
- `Notification.autoDismiss` - time (in ms) after which the notification will be dismissed. If set to `0` the notification will remain on screen until the user dismisses the notification, refreshes the page or navigates away from the site with the notifications
131+
- `Notification.link` - add link to the transaction hash. For instance, a link to the transaction on etherscan
132+
- `Notification.onClick()` - onClick handler for when user clicks the notification element
133+
134+
Notify can also be styled by using the CSS variables found below. These are setup to allow maximum customization with base styling variables setting the global theme (i.e. `--onboard-grey-600`) along with more precise component level styling variables available (`--notify-onboard-grey-600`) with the latter taking precedent if defined
135+
136+
If notifications are enabled the notifications can be handled through onboard app state as seen below.
137+
```javascript
138+
const wallets = onboard.state.select('notifications')
139+
const { unsubscribe } = wallets.subscribe(update =>
140+
console.log('transaction notifications: ', update)
141+
)
142+
143+
// unsubscribe when updates are no longer needed
144+
unsubscribe()
145+
```
146+
147+
```typescript
148+
export type NotifyOptions = {
149+
enabled: boolean // default: true
150+
/**
151+
* Callback that receives all transaction events
152+
* Return a custom notification based on the event
153+
* Or return false to disable notification for this event
154+
* Or return undefined for a default notification
155+
*/
156+
transactionHandler?: (
157+
event: EthereumTransactionData
158+
) => TransactionHandlerReturn
159+
}
160+
161+
export type TransactionHandlerReturn = CustomNotification | boolean | void
162+
163+
export type CustomNotification = Partial<Omit<Notification, 'id' | 'startTime'>>
164+
165+
export type Notification = {
166+
id: string
167+
key: string
168+
type: NotificationType
169+
network: Network
170+
startTime?: number
171+
eventCode: string
172+
message: string
173+
autoDismiss: number
174+
link?: string
175+
onclick?: (event: Event) => void
176+
}
177+
178+
export type NotificationType = 'pending' | 'success' | 'error' | 'hint'
179+
180+
export declare type Network = 'main' | 'testnet' | 'ropsten' | 'rinkeby' | 'goerli' | 'kovan' | 'xdai' | 'bsc-main' | 'matic-main' | 'fantom-main' | 'matic-mumbai' | 'local';
181+
182+
export interface UpdateNotification {
183+
(notificationObject: CustomNotification): {
184+
dismiss: () => void
185+
update: UpdateNotification
186+
}
187+
}
188+
```
189+
190+
Notify can be used to deliver custom DApp notifications by passing a `CustomNotification` object to the `customNotification` action. This will return an `UpdateNotification` type.
191+
This `UpdateNotification` will return an `update` function that can be passed a new `CustomNotification` to update the existing notification.
192+
The `customNotification` method also returns a `dismiss` method that is called without any parameters to dismiss the notification.
193+
194+
```typescript
195+
const { update, dismiss } =
196+
onboard.state.actions.customNotification({
197+
type: 'pending',
198+
message:
199+
'This is a custom DApp pending notification to use however you want',
200+
autoDismiss: 0
201+
})
202+
setTimeout(
203+
() =>
204+
update({
205+
eventCode: 'dbUpdateSuccess',
206+
message: 'Updated status for custom notification',
207+
type: 'success',
208+
autoDismiss: 8000
209+
}),
210+
4000
211+
)
212+
```
213+
119214
### Initialization Example
120215

121216
Putting it all together, here is an example initialization with the injected wallet modules:
@@ -176,6 +271,19 @@ const onboard = Onboard({
176271
{ name: 'Coinbase', url: 'https://wallet.coinbase.com/' }
177272
]
178273
},
274+
apiKey: 'xxx387fb-bxx1-4xxc-a0x3-9d37e426xxxx'
275+
notify: {
276+
enabled: true,
277+
transactionHandler: transaction => {
278+
console.log({ transaction })
279+
if (transaction.eventCode === 'txPool') {
280+
return {
281+
type: 'success',
282+
message: 'Your transaction from #1 DApp is in the mempool',
283+
}
284+
}
285+
}
286+
},
179287
accountCenter: {
180288
desktop: {
181289
position: 'topRight',
@@ -194,6 +302,20 @@ const onboard = Onboard({
194302
selectingWallet: {
195303
header: 'custom text header'
196304
}
305+
},
306+
notify: {
307+
transaction: {
308+
txStuck: 'custom text for this notification event'
309+
},
310+
watched: {
311+
// Any words in brackets can be re-ordered or removed to fit your dapps desired verbiage
312+
"txPool": "Your account is {verb} {formattedValue} {asset} {preposition} {counterpartyShortened}"
313+
}
314+
}
315+
},
316+
es: {
317+
transaction: {
318+
txRequest: 'Su transacción está esperando que confirme'
197319
}
198320
}
199321
}
@@ -285,6 +407,9 @@ type AppState = {
285407
chains: Chain[]
286408
accountCenter: AccountCenter
287409
walletModules: WalletModule[]
410+
locale: Locale
411+
notify: NotifyOptions
412+
notifications: Notification[]
288413
}
289414

290415
type Chain {
@@ -605,6 +730,19 @@ The Onboard styles can customized via [CSS variables](https://developer.mozilla.
605730
/* SPACING */
606731
--account-select-modal-margin-4: 1rem;
607732
--account-select-modal-margin-5: 0.5rem;
733+
734+
/* notify STYLES */
735+
--notify-onboard-font-family-normal
736+
--notify-onboard-font-size-5
737+
--notify-onboard-gray-300
738+
--notify-onboard-gray-600
739+
--notify-onboard-border-radius
740+
--notify-onboard-font-size-7
741+
--notify-onboard-font-size-6
742+
--notify-onboard-line-height-4
743+
--notify-onboard-primary-100
744+
--notify-onboard-primary-400
745+
--notify-onboard-main-padding
608746
}
609747
```
610748

packages/core/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/core",
3-
"version": "2.2.13-alpha.3",
3+
"version": "2.3.0-alpha.1",
44
"scripts": {
55
"build": "rollup -c",
66
"dev": "rollup -c -w",
@@ -42,12 +42,15 @@
4242
},
4343
"dependencies": {
4444
"@web3-onboard/common": "^2.1.2-alpha.2",
45+
"bignumber.js": "^9.0.0",
46+
"bnc-sdk": "^4.4.0",
4547
"bowser": "^2.11.0",
4648
"ethers": "5.5.3",
4749
"eventemitter3": "^4.0.7",
4850
"joi": "17.6.0",
4951
"lodash.merge": "^4.6.2",
5052
"lodash.partition": "^4.6.0",
53+
"nanoid": "^4.0.0",
5154
"rxjs": "^7.5.2",
5255
"svelte": "^3.46.4",
5356
"svelte-i18n": "^3.3.13"

packages/core/rollup.config.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import replace from '@rollup/plugin-replace'
44
import json from '@rollup/plugin-json'
55
import sveltePreprocess from 'svelte-preprocess'
66
import typescript from '@rollup/plugin-typescript'
7-
import copy from '@rollup-extras/plugin-copy';
7+
import copy from '@rollup-extras/plugin-copy'
88

99
const production = !process.env.ROLLUP_WATCH
1010

@@ -51,6 +51,9 @@ export default {
5151
'svelte/store',
5252
'lodash.merge',
5353
'lodash.partition',
54-
'eventemitter3'
54+
'eventemitter3',
55+
'bignumber.js',
56+
'bnc-sdk',
57+
'nanoid'
5558
]
5659
}

packages/core/src/configuration.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { Configuration } from './types'
2+
import { getDevice } from './utils'
3+
4+
export let configuration: Configuration = {
5+
svelteInstance: null,
6+
appMetadata: null,
7+
apiKey: null,
8+
device: getDevice()
9+
}
10+
11+
export function updateConfiguration(update: Partial<Configuration>): void {
12+
configuration = { ...configuration, ...update }
13+
}

packages/core/src/constants.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { internalState } from './internals'
1+
import { configuration } from './configuration'
22
import type { AppState } from './types'
33

44
export const APP_INITIAL_STATE: AppState = {
@@ -9,8 +9,13 @@ export const APP_INITIAL_STATE: AppState = {
99
enabled: true,
1010
position: 'topRight',
1111
expanded: false,
12-
minimal: internalState.device.type === 'mobile'
12+
minimal: configuration.device.type === 'mobile'
1313
},
14+
notify: {
15+
enabled: true,
16+
transactionHandler: () => {}
17+
},
18+
notifications: [],
1419
locale: ''
1520
}
1621

packages/core/src/disconnect.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getBlocknativeSdk } from './services'
12
import { state } from './store'
23
import { removeWallet } from './store/actions'
34
import { disconnectWallet$ } from './streams'
@@ -12,6 +13,23 @@ async function disconnect(options: DisconnectOptions): Promise<WalletState[]> {
1213

1314
const { label } = options
1415

16+
if (state.get().notify.enabled) {
17+
// handle unwatching addresses
18+
const sdk = await getBlocknativeSdk()
19+
20+
if (sdk) {
21+
const wallet = state.get().wallets.find(wallet => wallet.label === label)
22+
23+
wallet.accounts.forEach(({ address }) => {
24+
sdk.unsubscribe({
25+
id: address,
26+
chainId: wallet.chains[0].id,
27+
timeout: 60000
28+
})
29+
})
30+
}
31+
}
32+
1533
disconnectWallet$.next(label)
1634
removeWallet(label)
1735

packages/core/src/i18n/en.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,37 @@
7171
"addAccount": "Add Account",
7272
"setPrimaryAccount": "Set Primary Account",
7373
"disconnectWallet": "Disconnect Wallet"
74+
},
75+
"notify": {
76+
"transaction": {
77+
"txRequest": "Your transaction is waiting for you to confirm",
78+
"nsfFail": "You have insufficient funds to complete this transaction",
79+
"txUnderpriced": "The gas price for your transaction is too low, try again with a higher gas price",
80+
"txRepeat": "This could be a repeat transaction",
81+
"txAwaitingApproval": "You have a previous transaction waiting for you to confirm",
82+
"txConfirmReminder": "Please confirm your transaction to continue, the transaction window may be behind your browser",
83+
"txSendFail": "You rejected the transaction",
84+
"txSent": "Your transaction has been sent to the network",
85+
"txStallPending": "Your transaction has stalled and has not entered the transaction pool",
86+
"txStuck": "Your transaction is stuck due to a nonce gap",
87+
"txPool": "Your transaction has started",
88+
"txStallConfirmed": "Your transaction has stalled and hasn't been confirmed",
89+
"txSpeedUp": "Your transaction has been sped up",
90+
"txCancel": "Your transaction is being canceled",
91+
"txFailed": "Your transaction has failed",
92+
"txConfirmed": "Your transaction has succeeded",
93+
"txError": "Oops something went wrong, please try again"
94+
},
95+
"watched": {
96+
"txPool": "Your account is {verb} {formattedValue} {asset} {preposition} {counterpartyShortened}",
97+
"txSpeedUp": "Transaction for {formattedValue} {asset} {preposition} {counterpartyShortened} has been sped up",
98+
"txCancel": "Transaction for {formattedValue} {asset} {preposition} {counterpartyShortened} has been canceled",
99+
"txConfirmed": "Your account successfully {verb} {formattedValue} {asset} {preposition} {counterpartyShortened}",
100+
"txFailed": "Your account failed to {verb} {formattedValue} {asset} {preposition} {counterpartyShortened}"
101+
},
102+
"time": {
103+
"minutes": "min",
104+
"seconds": "sec"
105+
}
74106
}
75107
}

packages/core/src/icons/checkmark.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default `
2+
<svg width="14" height="11" viewBox="0 0 14 11" fill="none" xmlns="http://www.w3.org/2000/svg">
3+
<path d="M4.48076 8.10881L1.33076 4.95881L0.280762 6.00881L4.48076 10.2088L13.4808 1.20881L12.4308 0.158813L4.48076 8.10881Z" fill="#A4F4C6"/>
4+
</svg>
5+
`
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default `
2+
<svg width="100%" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
3+
<path d="M10 0C4.47 0 0 4.47 0 10C0 15.53 4.47 20 10 20C15.53 20 20 15.53 20 10C20 4.47 15.53 0 10 0ZM15 13.59L13.59 15L10 11.41L6.41 15L5 13.59L8.59 10L5 6.41L6.41 5L10 8.59L13.59 5L15 6.41L11.41 10L15 13.59Z" fill="currentColor"/>
4+
</svg>
5+
`

0 commit comments

Comments
 (0)