Skip to content

Commit aafdb78

Browse files
authored
[core-v2.4.0-alpha.5, react-v2.2.3-alpha.3, vue v2.1.3-alpha.3] : Feature - Update balances on success notification (#1128)
* Add function to updateBalance based on success message for the addresses affected, coerce case in updateBalance function * Bump version
1 parent e2e00a5 commit aafdb78

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
lines changed

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-alpha.4",
3+
"version": "2.4.0-alpha.5",
44
"repository": "blocknative/web3-onboard",
55
"scripts": {
66
"build": "rollup -c",

packages/core/src/notify.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
import { validateTransactionHandlerReturn } from './validation'
1414
import { state } from './store'
1515
import { addNotification } from './store/actions'
16+
import updateBalances from './updateBalances'
1617

1718
export function handleTransactionUpdates(
1819
transaction: EthereumTransactionData
@@ -47,6 +48,10 @@ export function transactionEventToNotification(
4748

4849
const type: NotificationType = eventToType(eventCode)
4950

51+
if (type === 'success') {
52+
updateWalletBalance(transaction)
53+
}
54+
5055
const key = `${id || hash}-${
5156
(typeof customization === 'object' && customization.eventCode) || eventCode
5257
}`
@@ -161,3 +166,30 @@ export function typeToDismissTimeout(type: string): number {
161166
return 0
162167
}
163168
}
169+
170+
const updateWalletBalance = (
171+
transaction: Pick<EthereumTransactionData, 'to' | 'from'>
172+
) => {
173+
let walletsToUpdate: string[] = []
174+
state.get().wallets.forEach(wallet => {
175+
if (
176+
wallet.accounts.some(
177+
e => e.address.toLowerCase() === transaction.to.toLowerCase()
178+
)
179+
) {
180+
walletsToUpdate = [...walletsToUpdate, transaction.to]
181+
}
182+
if (
183+
wallet.accounts.some(
184+
e => e.address.toLowerCase() === transaction.from.toLowerCase()
185+
)
186+
) {
187+
walletsToUpdate = [...walletsToUpdate, transaction.from]
188+
}
189+
})
190+
191+
// Pause to allow provider endpoint time to update
192+
setTimeout(() => {
193+
updateBalances(walletsToUpdate)
194+
}, 500)
195+
}

packages/core/src/updateBalances.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { updateAllWallets } from './store/actions'
44

55
async function updateBalances(addresses?: string[]): Promise<void> {
66
const { wallets, chains } = state.get()
7-
87
const updatedWallets = await Promise.all(
98
wallets.map(async wallet => {
109
const chain = chains.find(({ id }) => id === wallet.chains[0].id)
@@ -13,12 +12,16 @@ async function updateBalances(addresses?: string[]): Promise<void> {
1312
wallet.accounts.map(async account => {
1413
// if no provided addresses, we want to update all balances
1514
// otherwise check if address is in addresses array
16-
if (!addresses || addresses.includes(account.address)) {
15+
if (
16+
!addresses ||
17+
addresses.some(
18+
address => address.toLowerCase() === account.address.toLowerCase()
19+
)
20+
) {
1721
const updatedBalance = await getBalance(account.address, chain)
1822

1923
return { ...account, balance: updatedBalance }
2024
}
21-
2225
return account
2326
})
2427
)

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-alpha.2",
3+
"version": "2.2.3-alpha.3",
44
"description": "Collection of React Hooks for web3-onboard",
55
"repository": "blocknative/web3-onboard",
66
"module": "dist/index.js",
@@ -24,7 +24,7 @@
2424
"typescript": "^4.5.5"
2525
},
2626
"dependencies": {
27-
"@web3-onboard/core": "^2.4.0-alpha.4",
27+
"@web3-onboard/core": "^2.4.0-alpha.5",
2828
"@web3-onboard/common": "^2.1.4",
2929
"use-sync-external-store": "1.0.0"
3030
},

packages/vue/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/vue",
3-
"version": "2.1.3-alpha.2",
3+
"version": "2.1.3-alpha.3",
44
"description": "Vue Composable for web3-onboard",
55
"repository": "blocknative/web3-onboard",
66
"module": "dist/index.js",
@@ -25,7 +25,7 @@
2525
"@vueuse/core": "^8.4.2",
2626
"@vueuse/rxjs": "^8.2.0",
2727
"@web3-onboard/common": "^2.1.4",
28-
"@web3-onboard/core": "^2.4.0-alpha.4",
28+
"@web3-onboard/core": "^2.4.0-alpha.5",
2929
"vue-demi": "^0.12.4"
3030
},
3131
"peerDependencies": {

0 commit comments

Comments
 (0)