Skip to content

Commit a8877aa

Browse files
authored
[transaction-preview: v2.0.0-alpha.1 , core: v2.11.0-alpha.1, react, vue]: Feature - Transaction Preview Package (#1359)
* base stuff * Remove check for hash resolution when destroying the app, destroy app within catch * Update styles to match updated mocks * Remove unwanted cssvars * Handle for empty TPs * Build fix in dcent, check in TP for ethchain to avoid sending extra payloads * using SDK * Working with sdk * Working and refactored * Updating * Update readme for new usage * revert changes to sequence
1 parent e5962dd commit a8877aa

40 files changed

+1768
-47
lines changed

.circleci/config.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,12 @@ jobs:
327327
working_directory: ~/web3-onboard-monorepo/packages/uauth
328328
steps:
329329
- node-build-steps
330+
build-transaction-preview:
331+
docker:
332+
- image: cimg/node:16.13.1
333+
working_directory: ~/web3-onboard-monorepo/packages/transaction-preview
334+
steps:
335+
- node-build-steps
330336

331337
# Build staging/Alpha releases
332338
build-staging-core:
@@ -491,6 +497,12 @@ jobs:
491497
working_directory: ~/web3-onboard-monorepo/packages/uauth
492498
steps:
493499
- node-staging-build-steps
500+
build-staging-transaction-preview:
501+
docker:
502+
- image: cimg/node:16.13.1
503+
working_directory: ~/web3-onboard-monorepo/packages/transaction-preview
504+
steps:
505+
- node-staging-build-steps
494506

495507
workflows:
496508
version: 2
@@ -657,3 +669,9 @@ workflows:
657669
<<: *deploy_production_filters
658670
- build-staging-uauth:
659671
<<: *deploy_staging_filters
672+
transaction-preview:
673+
jobs:
674+
- build-transaction-preview:
675+
<<: *deploy_production_filters
676+
- build-staging-transaction-preview:
677+
<<: *deploy_staging_filters

packages/core/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/core",
3-
"version": "2.10.1",
3+
"version": "2.11.0-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",
@@ -84,7 +84,7 @@
8484
"dependencies": {
8585
"@web3-onboard/common": "^2.2.3",
8686
"bignumber.js": "^9.0.0",
87-
"bnc-sdk": "^4.4.1",
87+
"bnc-sdk": "^4.6.2",
8888
"bowser": "^2.11.0",
8989
"ethers": "5.5.3",
9090
"eventemitter3": "^4.0.7",

packages/core/src/configuration.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export let configuration: Configuration = {
88
device: getDevice(),
99
initialWalletInit: [],
1010
gas: null,
11-
containerElements: { accountCenter: null }
11+
containerElements: { accountCenter: null },
12+
transactionPreview: null
1213
}
1314

1415
export function updateConfiguration(update: Partial<Configuration>): void {

packages/core/src/disconnect.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getBlocknativeSdk } from './services.js'
1+
import { getBNMulitChainSdk } from './services.js'
22
import { state } from './store/index.js'
33
import { removeWallet } from './store/actions.js'
44
import { disconnectWallet$ } from './streams.js'
@@ -15,7 +15,7 @@ async function disconnect(options: DisconnectOptions): Promise<WalletState[]> {
1515

1616
if (state.get().notify.enabled) {
1717
// handle unwatching addresses
18-
const sdk = await getBlocknativeSdk()
18+
const sdk = await getBNMulitChainSdk()
1919

2020
if (sdk) {
2121
const wallet = state.get().wallets.find(wallet => wallet.label === label)

packages/core/src/index.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import connectWallet from './connect.js'
33
import disconnectWallet from './disconnect.js'
44
import setChain from './chain.js'
55
import { state } from './store/index.js'
6-
import { reset$ } from './streams.js'
6+
import { reset$, wallets$ } from './streams.js'
77
import initI18N from './i18n/index.js'
88
import App from './views/Index.svelte'
99
import type { InitOptions, Notify } from './types.js'
@@ -29,6 +29,8 @@ import {
2929
setWalletModules,
3030
updateConnectModal
3131
} from './store/actions.js'
32+
import type { PatchedEIP1193Provider } from '@web3-onboard/transaction-preview'
33+
import { getBlocknativeSdk } from './services'
3234

3335
const API = {
3436
connectWallet,
@@ -90,7 +92,8 @@ function init(options: InitOptions): OnboardAPI {
9092
notify,
9193
gas,
9294
connect,
93-
containerElements
95+
containerElements,
96+
transactionPreview
9497
} = options
9598

9699
updateConfiguration({ containerElements })
@@ -197,9 +200,26 @@ function init(options: InitOptions): OnboardAPI {
197200
svelteInstance: app,
198201
apiKey,
199202
initialWalletInit: wallets,
200-
gas
203+
gas,
204+
transactionPreview
201205
})
202206

207+
if (transactionPreview) {
208+
const getBnSDK = async () => {
209+
transactionPreview.init({
210+
containerElement: '#transaction-preview-container',
211+
sdk: await getBlocknativeSdk(),
212+
apiKey
213+
})
214+
wallets$.subscribe(wallets => {
215+
wallets.forEach(({ provider }) => {
216+
transactionPreview.patchProvider(provider as PatchedEIP1193Provider)
217+
})
218+
})
219+
}
220+
getBnSDK()
221+
}
222+
203223
return API
204224
}
205225

packages/core/src/provider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { updateAccount, updateWallet } from './store/actions.js'
88
import { validEnsChain } from './utils.js'
99
import disconnect from './disconnect.js'
1010
import { state } from './store/index.js'
11-
import { getBlocknativeSdk } from './services.js'
11+
import { getBNMulitChainSdk } from './services.js'
1212

1313
import type {
1414
ChainId,
@@ -157,7 +157,7 @@ export function trackWallet(
157157
// if not existing account and notifications,
158158
// then subscribe to transaction events
159159
if (state.get().notify.enabled && !existingAccount) {
160-
const sdk = await getBlocknativeSdk()
160+
const sdk = await getBNMulitChainSdk()
161161

162162
if (sdk) {
163163
const wallet = state
@@ -227,7 +227,7 @@ export function trackWallet(
227227
if (chainId === connectedWalletChain.id) return
228228

229229
if (state.get().notify.enabled) {
230-
const sdk = await getBlocknativeSdk()
230+
const sdk = await getBNMulitChainSdk()
231231

232232
if (sdk) {
233233
const wallet = state

packages/core/src/services.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,49 @@
11
import type { MultiChain } from 'bnc-sdk'
2+
import type SDK from 'bnc-sdk'
23
import { configuration } from './configuration.js'
34
import { handleTransactionUpdates } from './notify.js'
45

5-
let blocknativeSdk: MultiChain
6+
let blocknativeMultiChainSdk: MultiChain
7+
let blocknativeSdk: SDK
68

79
/**
810
*
9-
* @returns SDK if apikey
11+
* @returns MultiChain SDK if apiKey
1012
*/
11-
export async function getBlocknativeSdk(): Promise<MultiChain | null> {
13+
14+
export async function getBNMulitChainSdk(): Promise<MultiChain | null> {
1215
const { apiKey } = configuration
1316

1417
if (!apiKey) return null
1518

16-
if (!blocknativeSdk) {
19+
if (!blocknativeMultiChainSdk) {
1720
const { default: Blocknative } = await import('bnc-sdk')
18-
blocknativeSdk = Blocknative.multichain({
21+
blocknativeMultiChainSdk = Blocknative.multichain({
1922
apiKey: configuration.apiKey
2023
})
2124

22-
blocknativeSdk.transactions$.subscribe(handleTransactionUpdates)
25+
blocknativeMultiChainSdk.transactions$.subscribe(handleTransactionUpdates)
26+
}
27+
28+
return blocknativeMultiChainSdk
29+
}
30+
31+
/**
32+
*
33+
* @returns SDK if apiKey
34+
*/
35+
export async function getBlocknativeSdk(): Promise<SDK> {
36+
const { apiKey } = configuration
37+
38+
if (!apiKey) return null
39+
40+
if (!blocknativeSdk) {
41+
const { default: Blocknative } = await import('bnc-sdk')
42+
blocknativeSdk = new Blocknative({
43+
dappId: configuration.apiKey,
44+
networkId: 1
45+
})
46+
return blocknativeSdk
2347
}
2448

2549
return blocknativeSdk

packages/core/src/types.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
} from '@web3-onboard/common'
1313

1414
import type gas from '@web3-onboard/gas'
15+
import type { TransactionPreviewAPI } from '@web3-onboard/transaction-preview'
1516

1617
import type en from './i18n/en.json'
1718
import type { EthereumTransactionData, Network } from 'bnc-sdk'
@@ -53,10 +54,15 @@ export interface InitOptions {
5354
/** Gas module */
5455
gas?: typeof gas
5556
/**
56-
* Object mapping for W3O components with the key being the DOM element to mount
57-
* the component to, this defines the DOM container element for svelte to attach the component
57+
* Object mapping for W3O components with the key being the DOM
58+
* element to mount the component to, this defines the DOM container
59+
* element for svelte to attach the component
5860
*/
5961
containerElements?: Partial<ContainerElements>
62+
/**
63+
* Transaction Preview module
64+
*/
65+
transactionPreview?: TransactionPreviewAPI
6066
}
6167

6268
export interface ConnectOptions {
@@ -137,6 +143,7 @@ export type Configuration = {
137143
apiKey?: string
138144
gas?: typeof gas
139145
containerElements?: ContainerElements
146+
transactionPreview?: TransactionPreviewAPI
140147
}
141148

142149
export type Locale = string

packages/core/src/validation.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,11 @@ const initOptions = Joi.object({
185185
stream: Joi.function().required()
186186
}),
187187
connect: connectModalOptions,
188-
containerElements: containerElements
188+
containerElements: containerElements,
189+
transactionPreview: Joi.object({
190+
patchProvider: Joi.function().required(),
191+
init: Joi.function().required()
192+
})
189193
})
190194

191195
const connectOptions = Joi.object({

packages/core/src/views/Index.svelte

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,16 @@
394394
<SwitchChain />
395395
{/if}
396396

397+
{#if !$accountCenter$.enabled && !$notify$.enabled}
398+
<div
399+
class="container flex flex-column fixed z-indexed"
400+
style="top: 0; right: 0; {device.type === 'mobile'
401+
? 'padding-bottom: 0;'
402+
: ''} "
403+
id="transaction-preview-container"
404+
/>
405+
{/if}
406+
397407
{#if displayAccountCenterNotifySameContainer}
398408
<div
399409
class="container flex flex-column fixed z-indexed"
@@ -417,6 +427,9 @@
417427
{/if}
418428
{/await}
419429
{/if}
430+
{#if $accountCenter$.position.includes('bottom')}
431+
<div id="transaction-preview-container" style="margin-bottom: 8px;"/>
432+
{/if}
420433
<div
421434
style={!$accountCenter$.expanded &&
422435
$accountCenter$.minimal &&
@@ -427,13 +440,17 @@
427440
$accountCenter$.position.includes('Left')
428441
? 'margin-right: auto'
429442
: ''}
443+
id="account-center-with-notify"
430444
>
431445
{#await accountCenterComponent then AccountCenter}
432446
{#if AccountCenter}
433447
<svelte:component this={AccountCenter} />
434448
{/if}
435449
{/await}
436450
</div>
451+
{#if $accountCenter$.position.includes('top')}
452+
<div id="transaction-preview-container" style="margin-top: 8px;"/>
453+
{/if}
437454
{#if $notify$.position.includes('top') && $accountCenter$.position.includes('top') && samePositionOrMobile}
438455
{#await notifyComponent then Notify}
439456
{#if Notify}
@@ -459,6 +476,9 @@
459476
? 'padding-top:0;'
460477
: ''} "
461478
>
479+
{#if $accountCenter$.position.includes('bottom')}
480+
<div id="transaction-preview-container" style="margin-bottom: 8px;"/>
481+
{/if}
462482
<div
463483
style={!$accountCenter$.expanded &&
464484
$accountCenter$.minimal &&
@@ -478,6 +498,9 @@
478498
{/await}
479499
{/if}
480500
</div>
501+
{#if $accountCenter$.position.includes('top')}
502+
<div id="transaction-preview-container" style="margin-top: 8px;"/>
503+
{/if}
481504
</div>
482505
{/if}
483506
{#if displayNotifySeparate}
@@ -491,6 +514,9 @@
491514
? 'padding-top:0;'
492515
: ''} "
493516
>
517+
{#if $notify$.position.includes('top')}
518+
<div id="transaction-preview-container" />
519+
{/if}
494520
{#await notifyComponent then Notify}
495521
{#if Notify}
496522
<svelte:component
@@ -501,5 +527,8 @@
501527
/>
502528
{/if}
503529
{/await}
530+
{#if $notify$.position.includes('bottom')}
531+
<div id="transaction-preview-container" />
532+
{/if}
504533
</div>
505534
{/if}

packages/core/src/views/connect/Index.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import SelectingWallet from './SelectingWallet.svelte'
1818
import Sidebar from './Sidebar.svelte'
1919
import { configuration } from '../../configuration.js'
20-
import { getBlocknativeSdk } from '../../services.js'
20+
import { getBNMulitChainSdk } from '../../services.js'
2121
import { BigNumber } from 'ethers'
2222
2323
import {
@@ -211,7 +211,7 @@
211211
const chain = await getChainId(provider)
212212
213213
if (state.get().notify.enabled) {
214-
const sdk = await getBlocknativeSdk()
214+
const sdk = await getBNMulitChainSdk()
215215
216216
if (sdk) {
217217
try {

packages/dcent/src/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
Account,
1313
ScanAccountsOptions
1414
} from '@web3-onboard/hw-common'
15+
import { StaticJsonRpcProvider } from '@ethersproject/providers'
1516

1617
interface CustomWindow extends Window {
1718
ethereum: EIP1193Provider
@@ -36,7 +37,7 @@ const assets = [
3637

3738
const generateAccounts = async (
3839
keyring: any,
39-
provider: providers.StaticJsonRpcProvider
40+
provider: StaticJsonRpcProvider
4041
): Promise<Account[]> => {
4142
const accounts = []
4243

@@ -123,9 +124,7 @@ function dcent({
123124
currentChain =
124125
chains.find(({ id }: Chain) => id === chainId) || currentChain
125126

126-
const provider = new StaticJsonRpcProvider(
127-
currentChain.rpcUrl
128-
) as providers.StaticJsonRpcProvider
127+
const provider = new StaticJsonRpcProvider(currentChain.rpcUrl)
129128

130129
return generateAccounts(dcentKeyring, provider)
131130
}

0 commit comments

Comments
 (0)