Skip to content

Commit 8b84fc5

Browse files
authored
Update Transaction Preview Gas Used view - Add TP example to the Docs (#1527)
* Start * TP gasUsed working as expected and docs updates * Revert demo app updates * i18n the Gas Used text in the table * Refine css * Revert WC updates saved for different PR * Update screenshots of TP * Yarnit * Remove gas heading as no longer in use * Update h2 -> div and return css for div section * Move TP vars into index, remove unneeded code copying stylesheets from w3o * Bump versions in the docs * remove unnecessary css * Merge in develop and add bifrost to docs * Bump versions in demo and docs
1 parent 0b8c5af commit 8b84fc5

File tree

26 files changed

+346
-111
lines changed

26 files changed

+346
-111
lines changed

assets/transaction-preview.png

-37.6 KB
Loading

docs/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,15 @@
5454
},
5555
"type": "module",
5656
"dependencies": {
57+
"bnc-sdk": "^4.6.6",
5758
"@web3-onboard/coinbase": "^2.1.4",
58-
"@web3-onboard/core": "^2.14.0",
59+
"@web3-onboard/core": "^2.15.1-alpha.1",
5960
"@web3-onboard/dcent": "^2.2.3",
6061
"@web3-onboard/enkrypt": "^2.0.0",
6162
"@web3-onboard/fortmatic": "^2.0.14",
6263
"@web3-onboard/gas": "^2.1.4",
6364
"@web3-onboard/gnosis": "^2.1.6",
64-
"@web3-onboard/injected-wallets": "^2.6.2",
65+
"@web3-onboard/injected-wallets": "^2.8.0-alpha.1",
6566
"@web3-onboard/keepkey": "^2.3.3",
6667
"@web3-onboard/keystone": "^2.3.3",
6768
"@web3-onboard/ledger": "^2.4.2",
@@ -71,6 +72,7 @@
7172
"@web3-onboard/sequence": "^2.0.4",
7273
"@web3-onboard/tallyho": "^2.0.1",
7374
"@web3-onboard/torus": "^2.2.0",
75+
"@web3-onboard/transaction-preview": "^2.0.3-alpha.1",
7476
"@web3-onboard/trezor": "^2.3.3",
7577
"@web3-onboard/trust": "^2.0.0",
7678
"@web3-onboard/uauth": "^2.0.1",
-37.6 KB
Loading
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<script>
2+
import { onMount } from 'svelte'
3+
import { ethers } from 'ethers'
4+
5+
let transactionPreview
6+
let blocknativeSdk
7+
8+
const buildTransaction = async () => {
9+
const addressFrom = '0xab5801a7d398351b8be11c439e05c5b3259aec9b'
10+
11+
const CONTRACT_ADDRESS = '0x7a250d5630b4cf539739df2c5dacb4c659f2488d'
12+
const erc20_interface = [
13+
'function approve(address _spender, uint256 _value) public returns (bool success)',
14+
'function transferFrom(address sender, address recipient, uint256 amount) external returns (bool)',
15+
'function balanceOf(address owner) view returns (uint256)'
16+
]
17+
18+
const uniswapV2router_interface = [
19+
'function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)'
20+
]
21+
22+
const weth = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'
23+
const uni = '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984'
24+
let swapTxData
25+
let approveTxData
26+
const createTransaction = async () => {
27+
const swapContract = new ethers.Contract(CONTRACT_ADDRESS, uniswapV2router_interface)
28+
const erc20_contract = new ethers.Contract(weth, erc20_interface)
29+
const oneHundredUni = ethers.BigNumber.from('100000000000000000000')
30+
approveTxData = await erc20_contract.populateTransaction.approve(
31+
CONTRACT_ADDRESS,
32+
oneHundredUni
33+
)
34+
35+
const amountOutMin = 0
36+
const amountOutMinHex = ethers.BigNumber.from(amountOutMin.toString())._hex
37+
38+
const path = [uni, weth]
39+
const deadline = Math.floor(Date.now() / 1000) + 60 * 1 // 1 minutes from the current Unix time
40+
41+
const inputAmountHex = oneHundredUni.toHexString()
42+
43+
swapTxData = await swapContract.populateTransaction.swapExactTokensForETH(
44+
inputAmountHex,
45+
amountOutMinHex,
46+
path,
47+
addressFrom,
48+
deadline
49+
)
50+
}
51+
await createTransaction()
52+
const account_address = '0xab5801a7d398351b8be11c439e05c5b3259aec9b'
53+
const uniswapV2Router = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D'
54+
55+
return [
56+
{
57+
from: account_address,
58+
to: uni,
59+
input: approveTxData.data,
60+
gas: 1000000,
61+
gasPrice: 48000000000,
62+
value: 0
63+
},
64+
{
65+
from: account_address,
66+
to: uniswapV2Router,
67+
input: swapTxData.data,
68+
gas: 1000000,
69+
gasPrice: 48000000000,
70+
value: 0
71+
}
72+
]
73+
}
74+
75+
76+
const handlePreview = async () => {
77+
await transactionPreview.init({
78+
apiKey: '133a026b-c7a0-419c-a00b-66255b3cd487',
79+
sdk: blocknativeSdk,
80+
containerElement: '#tp-container'
81+
})
82+
83+
const stubTrans = await buildTransaction()
84+
await transactionPreview.previewTransaction(stubTrans)
85+
86+
}
87+
88+
onMount(async () => {
89+
const { default: Blocknative } = await import('bnc-sdk')
90+
const { default: transactionPreviewModule } = await import('@web3-onboard/transaction-preview')
91+
92+
blocknativeSdk = new Blocknative({
93+
dappId: '133a026b-c7a0-419c-a00b-66255b3cd487',
94+
networkId: 1
95+
})
96+
97+
transactionPreview = transactionPreviewModule({
98+
requireTransactionApproval: true
99+
})
100+
})
101+
</script>
102+
103+
<div>
104+
{#await blocknativeSdk && transactionPreview then Preview}
105+
{#if Preview}
106+
<button
107+
class="rounded-lg bg-gray-inverse hover:bg-gray-hover hover:text-gray-inverse transition-all px-4 h-10 text-base text-gray-current"
108+
on:click={() => handlePreview()}
109+
>
110+
Preview Transaction
111+
</button>
112+
113+
<div id="tp-container" />
114+
{/if}
115+
{/await}
116+
</div>
117+
118+
<style>
119+
#tp-container {
120+
height: auto;
121+
width: 316px;
122+
margin-top: 12px;
123+
}
124+
</style>

docs/src/lib/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './examples'
22
export * from './gas'
33
export { default as ThemeCustomizer } from './ThemeCustomizer.svelte'
4+
export { default as TransactionPreviewButton } from './TransactionPreviewButton.svelte'

docs/src/lib/services/onboard.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ const intiOnboard = async (theme) => {
181181
]
182182
},
183183
accountCenter: { desktop: { enabled: true }, mobile: { enabled: true } },
184-
theme: theme || 'system'
184+
theme: theme || 'system',
185+
apiKey: 'da1b962d-314d-4903-bfe1-426821d14a35'
185186
})
186187
}
187188

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script>
22
import previewGif from '$lib/assets/transaction-preview.gif'
33
import previewImg from '$lib/assets/transaction-preview.png'
4+
import { TransactionPreviewButton } from '$lib/components'
45
</script>
56

67
# Transaction Preview
@@ -9,8 +10,12 @@ A modular UI for previewing a single or set of unsigned Ethereum transactions.
910

1011
<img src="{previewImg}" alt="Transaction Preview Flow image"/>
1112

12-
Full Simulation Platform API documentation can be found [here](https://docs.blocknative.com/transaction-preview-api)
13+
### Try Transaction Preview
14+
15+
Preview Vitalik swapping 100 UNI tokens for ETH using Transaction Preview
16+
<TransactionPreviewButton/>
1317

18+
Full Simulation Platform API documentation can be found [here](https://docs.blocknative.com/transaction-preview-api)
1419
### Install
1520

1621
<Tabs values={['yarn', 'npm']}>
@@ -74,13 +79,14 @@ const onboard = Onboard({
7479

7580
### Standalone Usage
7681

82+
To use the Transaction Preview package without web3-onboard all a developer needs to do is:
7783

78-
To use the Transaction Preview package without web3-onboard all a developer needs to do is:
7984
- Execute the entry function from the `@web3-onboard/transaction-preview` package and optional params
8085
- 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
8186
- Finally pass a transaction meant for a wallet provider (created using libraries like Ethers or Web3)
8287

8388
With the above steps a UI will be rendered with the balance changes and gas used.
89+
8490
```typescript
8591
import transactionPreviewModule from '@web3-onboard/transaction-preview'
8692

@@ -182,8 +188,7 @@ console.log(simData)
182188
```typescript
183189
export type TransactionPreviewModule = (options: TransactionPreviewOptions) => TransactionPreviewAPI
184190

185-
export type FullPreviewOptions = TransactionPreviewOptions &
186-
TransactionPreviewInitOptions
191+
export type FullPreviewOptions = TransactionPreviewOptions & TransactionPreviewInitOptions
187192

188193
export type TransactionPreviewAPI = {
189194
/**
@@ -209,9 +214,7 @@ export type TransactionPreviewAPI = {
209214
* Note: the package will need to initialized with the `init`
210215
* function prior to usage
211216
*/
212-
previewTransaction: (
213-
transaction: TransactionForSim[]
214-
) => Promise<MultiSimOutput>
217+
previewTransaction: (transaction: TransactionForSim[]) => Promise<MultiSimOutput>
215218
}
216219

217220
export type PatchedEIP1193Provider = EIP1193Provider & { simPatched: boolean }

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ const injected = injectedModule({
312312
- Rainbow - _Desktop & Mobile_
313313
- DeFiWallet - _Desktop & Mobile_
314314
- ApexWallet - _Desktop_
315+
- BifrostWallet - _Desktop & Mobile_
315316

316317
## Build Environments
317318

docs/yarn.lock

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3138,6 +3138,19 @@
31383138
"@toruslabs/torus-embed" "1.38.2"
31393139
"@web3-onboard/common" "^2.2.3"
31403140

3141+
"@web3-onboard/transaction-preview@^2.0.2":
3142+
version "2.0.2"
3143+
resolved "https://registry.yarnpkg.com/@web3-onboard/transaction-preview/-/transaction-preview-2.0.2.tgz#5c96587b70def019eb699b4369230f365e06a96e"
3144+
integrity sha512-0Oe1Wioo1rpfKI6BACQUKWGY64vUtiLZ1fb54Umd91shm4PNTBhRKD03DSXhGbq5JsHROLE3VoFfRvTKClx+cw==
3145+
dependencies:
3146+
"@web3-onboard/common" "^2.2.3"
3147+
bnc-sdk "^4.6.6"
3148+
bowser "^2.11.0"
3149+
joi "^17.6.1"
3150+
rxjs "^7.5.2"
3151+
svelte "^3.49.0"
3152+
svelte-i18n "^3.3.13"
3153+
31413154
"@web3-onboard/trezor@^2.3.3":
31423155
version "2.3.3"
31433156
resolved "https://registry.yarnpkg.com/@web3-onboard/trezor/-/trezor-2.3.3.tgz#0a9f2296e4e3365637a798eacfb3d2c0696e0b40"
@@ -3928,6 +3941,16 @@ bnc-sdk@^4.6.5:
39283941
rxjs "^6.6.3"
39293942
sturdy-websocket "^0.1.12"
39303943

3944+
bnc-sdk@^4.6.6:
3945+
version "4.6.6"
3946+
resolved "https://registry.yarnpkg.com/bnc-sdk/-/bnc-sdk-4.6.6.tgz#ef5501a0c68014efae24d00d2e3fb706318fa00d"
3947+
integrity sha512-cpavy/WBQrkw5PZpnuUAvxzj/RjmP1vSldOEG+nonf7n/4sykScDO6KrJN2oVhEMaxHOqOVf2rOugSL5t515eA==
3948+
dependencies:
3949+
crypto-es "^1.2.2"
3950+
nanoid "^3.3.1"
3951+
rxjs "^6.6.3"
3952+
sturdy-websocket "^0.1.12"
3953+
39313954
boolbase@^1.0.0:
39323955
version "1.0.0"
39333956
resolved "https://registry.yarnpkg.com/boolbase/-/boolbase-1.0.0.tgz#68dff5fbe60c51eb37725ea9e3ed310dcc1e776e"

packages/core/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/core",
3-
"version": "2.15.0",
3+
"version": "2.15.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",
@@ -70,7 +70,7 @@
7070
"@typescript-eslint/eslint-plugin": "^4.31.1",
7171
"@typescript-eslint/parser": "^4.31.1",
7272
"@web3-onboard/gas": "^2.0.0",
73-
"@web3-onboard/transaction-preview": "^2.0.2",
73+
"@web3-onboard/transaction-preview": "^2.0.3-alpha.1",
7474
"eslint": "^7.32.0",
7575
"eslint-config-prettier": "^8.3.0",
7676
"eslint-plugin-svelte3": "^3.2.1",
@@ -87,7 +87,7 @@
8787
"@unstoppabledomains/resolution": "^8.0",
8888
"@web3-onboard/common": "^2.2.3",
8989
"bignumber.js": "^9.0.0",
90-
"bnc-sdk": "^4.6.6",
90+
"bnc-sdk": "^4.6.7",
9191
"bowser": "^2.11.0",
9292
"ethers": "5.5.3",
9393
"eventemitter3": "^4.0.7",

0 commit comments

Comments
 (0)