Skip to content

Commit 843cb20

Browse files
authored
Merge pull request #1522 from blocknative/release/2.20.0
2 parents c47f6a8 + c3e50bf commit 843cb20

Some content is hidden

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

60 files changed

+6514
-2029
lines changed

.circleci/config.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,12 @@ jobs:
357357
working_directory: ~/web3-onboard-monorepo/packages/phantom
358358
steps:
359359
- node-build-steps
360+
build-xdefi:
361+
docker:
362+
- image: cimg/node:16.13.1
363+
working_directory: ~/web3-onboard-monorepo/packages/xdefi
364+
steps:
365+
- node-build-steps
360366

361367
# Build staging/Alpha releases
362368
build-staging-core:
@@ -551,6 +557,12 @@ jobs:
551557
working_directory: ~/web3-onboard-monorepo/packages/phantom
552558
steps:
553559
- node-staging-build-steps
560+
build-staging-xdefi:
561+
docker:
562+
- image: cimg/node:16.13.1
563+
working_directory: ~/web3-onboard-monorepo/packages/xdefi
564+
steps:
565+
- node-staging-build-steps
554566

555567
workflows:
556568
version: 2
@@ -747,3 +759,9 @@ workflows:
747759
<<: *deploy_production_filters
748760
- build-staging-phantom:
749761
<<: *deploy_staging_filters
762+
xdefi:
763+
jobs:
764+
- build-xdefi:
765+
<<: *deploy_production_filters
766+
- build-staging-xdefi:
767+
<<: *deploy_staging_filters

assets/transaction-preview.png

101 KB
Loading
13 KB
Loading

docs/src/lib/components/ThemeCustomizer.svelte

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
let wallets$
99
1010
const themes = ['system', 'default', 'light', 'dark', 'custom']
11-
let selectedTheme = 'custom'
11+
let selectedTheme = 'system'
1212
1313
let webURL = ''
1414
let iframeUsed = false
@@ -30,13 +30,23 @@
3030
3131
const addURLToIFrame = () => {
3232
if (!webURL || !isValidUrl(webURL)) {
33-
alert('Invaled URL entered')
33+
alert('Invalid URL entered')
3434
return
3535
}
36-
iframeUsed = true
37-
document.querySelector('#iframe_underlay').setAttribute('src', webURL)
38-
hideDirections = true
39-
onboard.connectWallet()
36+
37+
fetch(webURL)
38+
.then(() => {
39+
iframeUsed = true
40+
document.querySelector('#iframe_underlay').setAttribute('src', webURL)
41+
hideDirections = true
42+
!onboard && getOnboard()
43+
onboard.connectWallet()
44+
})
45+
.catch(() => {
46+
alert(
47+
'The website entered cannot be displayed within an iframe. Please try a different URL. See the browser console for more information.'
48+
)
49+
})
4050
}
4151
4252
const resetPage = () => {
@@ -47,6 +57,10 @@
4757
uploaded_image = undefined
4858
webURL = ''
4959
resetTheme()
60+
closeOnboard()
61+
}
62+
63+
const closeOnboard = () => {
5064
const onboardCloseBtnVisible = document
5165
?.querySelector('body > onboard-v2')
5266
?.shadowRoot?.querySelector('.close-button')
@@ -266,9 +280,9 @@
266280
placeholder="Enter your Website URL"
267281
bind:value={webURL}
268282
/>
269-
<button on:click={addURLToIFrame}>Preview On Your Website</button>
283+
<button type="submit">Preview On Your Website</button>
270284
<button
271-
on:click={resetPage}
285+
on:click={() => resetPage()}
272286
type="button"
273287
disabled={iframeUsed || !!uploaded_image ? false : true}>Reset</button
274288
>

docs/src/routes/docs/[...3]modules/core.md

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ type ConnectModalOptions = {
167167
* Defaults to false
168168
*/
169169
disableClose?: boolean
170+
/**If set to true, the last connected wallet will store in local storage.
171+
* Then on init, onboard will try to reconnect to that wallet with
172+
* no modals displayed
173+
*/
174+
autoConnectLastWallet?: boolean // defaults to false
170175
}
171176
```
172177
@@ -482,40 +487,16 @@ connectWallet()
482487

483488
### Auto Selecting a Wallet
484489

485-
A common UX pattern is to remember the wallet(s) that a user has previously connected by storing them in localStorage and then automatically selecting them for the user next time they visit your app.
486-
You could enable this in your app by first syncing the `wallets` array to localStorage:
490+
A common UX pattern is to remember the last wallet that a user has previously connected by storing it in localStorage and then automatically selecting them for the user next time they visit your app.
491+
You can enable this in your app by using the `autoConnectLastWallet` parameter when initializing and Onboard will take care of it:
487492

488493
```javascript
489-
const walletsSub = onboard.state.select('wallets')
490-
const { unsubscribe } = walletsSub.subscribe((wallets) => {
491-
const connectedWallets = wallets.map(({ label }) => label)
492-
window.localStorage.setItem('connectedWallets', JSON.stringify(connectedWallets))
494+
const onboard = Onboard({
495+
// ... other options
496+
connect: {
497+
autoConnectLastWallet: true
498+
}
493499
})
494-
495-
// Don't forget to unsubscribe when your app or component un mounts to prevent memory leaks
496-
// unsubscribe()
497-
```
498-
499-
Now that you have the most recent wallets connected saved in local storage, you can auto select those wallet(s) when your app loads:
500-
501-
```javascript
502-
const previouslyConnectedWallets = JSON.parse(window.localStorage.getItem('connectedWallets'))
503-
504-
if (previouslyConnectedWallets) {
505-
// Connect the most recently connected wallet (first in the array)
506-
await onboard.connectWallet({ autoSelect: previouslyConnectedWallets[0] })
507-
508-
// You can also auto connect "silently" and disable all onboard modals to avoid them flashing on page load
509-
await onboard.connectWallet({
510-
autoSelect: { label: previouslyConnectedWallets[0], disableModals: true }
511-
})
512-
513-
// OR - loop through and initiate connection for all previously connected wallets
514-
// note: This UX might not be great as the user may need to login to each wallet one after the other
515-
// for (walletLabel in previouslyConnectedWallets) {
516-
// await onboard.connectWallet({ autoSelect: walletLabel })
517-
// }
518-
}
519500
```
520501

521502
## Disconnecting a Wallet
@@ -1318,3 +1299,11 @@ build: {
13181299
standalone: true,
13191300
}
13201301
```
1302+
1303+
### Next.js
1304+
1305+
:::admonition type=note
1306+
1307+
If you are seeing an error during builds when dynamically importing Web3Onboard in a NextJS v13 project, try upgrading to to the Canary beta release of NextJS where this issue is fixed.
1308+
1309+
:::

docs/src/routes/docs/[...3]modules/transaction-preview.md

Lines changed: 131 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
<script>
32
import previewGif from '$lib/assets/transaction-preview.gif'
43
import previewImg from '$lib/assets/transaction-preview.png'
@@ -73,24 +72,146 @@ const onboard = Onboard({
7372
// The transaction will automatically be picked up and simulated with a UI displaying in the upper right corner
7473
```
7574

75+
### Standalone Usage
76+
77+
78+
To use the Transaction Preview package without web3-onboard all a developer needs to do is:
79+
- Execute the entry function from the `@web3-onboard/transaction-preview` package and optional params
80+
- Run the returned `init` function with their [Blocknative API key](https://onboard.blocknative.com/docs/overview/introduction#optional-use-an-api-key-to-fetch-real-time-transaction-data-balances-gas), an initialized instance of their [Blocknative SDK](https://www.npmjs.com/package/bnc-sdk) and a containerElement string with the html ID of the target element to append the visualization to
81+
- Finally pass a transaction meant for a wallet provider (created using libraries like Ethers or Web3)
82+
83+
With the above steps a UI will be rendered with the balance changes and gas used.
84+
```typescript
85+
import transactionPreviewModule from '@web3-onboard/transaction-preview'
86+
87+
const {init, previewTransaction} = transactionPreviewModule({
88+
// Optional: Require balance change approval prior to sending transaction to wallet
89+
// Defaults to true
90+
// requireTransactionApproval?: false
91+
92+
// i18n?: i18nOptions - Internationalization options
93+
})
94+
await init({
95+
/**
96+
* Blocknative API key (https://explorer.blocknative.com/account)
97+
*/
98+
apiKey: string
99+
/**
100+
* Your Blocknative SDK instance
101+
* */
102+
sdk: SDK
103+
/**
104+
* Optional dom query string to mount UI to
105+
* */
106+
containerElement: string})
107+
108+
// Transaction code here using Ether.js or Web3.js or construct your own transactions
109+
const simulate = async provider => {
110+
const ethersProvider = new ethers.providers.Web3Provider(provider, 'any')
111+
112+
const signer = ethersProvider.getSigner()
113+
const addressFrom = '0xcxxxxxx11111999991111'
114+
115+
// Uniswap V2
116+
const CONTRACT_ADDRESS = '0x7a250d5630b4cf539739df2c5dacb4c659f2488d'
117+
const erc20_interface = [
118+
'function approve(address _spender, uint256 _value) public returns (bool success)',
119+
'function transferFrom(address sender, address recipient, uint256 amount) external returns (bool)',
120+
'function balanceOf(address owner) view returns (uint256)'
121+
]
122+
123+
const uniswapV2router_interface = [
124+
'function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)'
125+
]
126+
127+
const weth = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'
128+
const oneInch = '0x111111111117dc0aa78b770fa6a738034120c302'
129+
let swapTxData
130+
let approveTxData
131+
const swapContract = new ethers.Contract(
132+
CONTRACT_ADDRESS,
133+
uniswapV2router_interface
134+
)
135+
const erc20_contract = new ethers.Contract(oneInch, erc20_interface)
136+
const oneEther = ethers.BigNumber.from('9000000000000000000')
137+
approveTxData = await erc20_contract.populateTransaction.approve(
138+
CONTRACT_ADDRESS,
139+
oneEther
140+
)
141+
142+
const amountOutMin = 0
143+
const amountOutMinHex = ethers.BigNumber.from(amountOutMin).toHexString()
144+
145+
const path = [oneInch, weth]
146+
const deadline = Math.floor(Date.now() / 1000) + 60 * 1 // 1 minutes from the current Unix time
147+
148+
const inputAmountHex = oneEther.toHexString()
149+
150+
swapTxData = await swapContract.populateTransaction.swapExactTokensForETH(
151+
inputAmountHex,
152+
amountOutMinHex,
153+
path,
154+
addressFrom,
155+
deadline
156+
)
157+
const uniswapV2Router = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D'
158+
159+
const popApproveTransaction = await signer.populateTransaction(approveTxData)
160+
const popTransaction = await signer.populateTransaction(swapTxData)
161+
const transactions = [
162+
{ ...popApprovedTransaction, value: 0 },
163+
{
164+
...popTransaction,
165+
from: addressFrom,
166+
to: uniswapV2Router,
167+
value: 0
168+
}
169+
]
170+
await previewTransaction(transactions)
171+
}
172+
173+
return await previewTransaction(transactions)
174+
}
175+
176+
const simData = simulate(ethereumProvider)
177+
console.log(simData)
178+
```
179+
76180
### Options & Types
77181

78182
```typescript
79183
export type TransactionPreviewModule = (options: TransactionPreviewOptions) => TransactionPreviewAPI
80184

185+
export type FullPreviewOptions = TransactionPreviewOptions &
186+
TransactionPreviewInitOptions
187+
81188
export type TransactionPreviewAPI = {
82189
/**
83-
* Pass this method a standard EIP1193 provider
190+
* This Method accepts a standard EIP1193 provider
84191
* (such as an injected wallet from window.ethereum)
85192
* and it will be patched to allow for transaction previewing
86193
*/
87194
patchProvider: (provider: PatchedEIP1193Provider) => PatchedEIP1193Provider
195+
88196
/**
89-
* Pass this method a standard EIP1193 provider
90-
* (such as an injected wallet from window.ethereum)
91-
* and it will be patched to allow for transaction previewing
197+
* This Method accepts:
198+
* apiKey: string - Blocknative API key (https://explorer.blocknative.com/)
199+
* sdk: instance of an initialized bnc-sdk (www.npmjs.com/package/bnc-sdk)
200+
* containerElement: string of an html id selector (e.g. "#my-html-el")
92201
*/
93202
init: (initializationOptions: TransactionPreviewInitOptions) => void
203+
204+
/**
205+
* This method accepts a transaction meant for a wallet provider
206+
* (created using libraries like Ethers or Web3),
207+
* simulates the transaction and generates a corresponding UI and
208+
* return a response from the Blocknative Transaction Preview API.
209+
* Note: the package will need to initialized with the `init`
210+
* function prior to usage
211+
*/
212+
previewTransaction: (
213+
transaction: TransactionForSim[]
214+
) => Promise<MultiSimOutput>
94215
}
95216

96217
export type PatchedEIP1193Provider = EIP1193Provider & { simPatched: boolean }
@@ -108,7 +229,7 @@ export type TransactionPreviewInitOptions = {
108229
*/
109230
apiKey: string
110231
/**
111-
* Your Blocknative SDK instance
232+
* Your Blocknative SDK instance (https://www.npmjs.com/package/bnc-sdk)
112233
* */
113234
sdk: SDK
114235
/**
@@ -178,6 +299,7 @@ export type MultiSimOutput = {
178299
export interface ContractCall {
179300
contractType?: string
180301
contractAddress?: string
302+
contractAlias?: string
181303
methodName: string
182304
params: Record<string, unknown>
183305
contractName?: string
@@ -194,6 +316,8 @@ export interface InternalTransaction {
194316
gasUsed: number
195317
value: string
196318
contractCall: ContractCall
319+
error?: string
320+
errorReason?: string
197321
}
198322

199323
export interface NetBalanceChange {
@@ -261,4 +385,5 @@ export interface SimDetails {
261385
```
262386

263387
## Build Environments
388+
264389
For build env configurations and setups please see the Build Env section [here](/docs/modules/core#build-environments)

docs/src/routes/docs/[...4]wallets/injected.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ const injected = injectedModule({
311311
- Frontier - _Desktop & Mobile_
312312
- Rainbow - _Desktop & Mobile_
313313
- DeFiWallet - _Desktop & Mobile_
314+
- ApexWallet - _Desktop_
314315

315316
## Build Environments
316317

0 commit comments

Comments
 (0)