Skip to content

Commit e47459b

Browse files
authored
[core-v2.2.11-alpha.2 , react-v2.1.8-alpha.2]: enhancement - update connected wallets balance (#1038)
* async * updatedBalances * update_balance * update balances * merge develop * cleanup * cleanup * cleanup * Readme update * Readme update * cleanup * package bump * package bump and merge develop * package bump * core version bump
1 parent fd145de commit e47459b

File tree

12 files changed

+129
-12
lines changed

12 files changed

+129
-12
lines changed

packages/core/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,18 @@ const onboard = Onboard({
416416
onboard.state.actions.setWalletModules([ledger, trezor])
417417
```
418418

419+
**updatedBalances**
420+
You may decide to get updated balances for connected wallets after a user action by calling the `updatedBalances` function, which expects a conditional array of addresses.
421+
422+
```
423+
onboard.state.actions.updateBalances() // update all balances for all connected addresses
424+
onboard.state.actions.updateBalances(['0xfdadfadsadsadsadasdsa']) // update balance for one address
425+
onboard.state.actions.updateBalances([
426+
'0xfdadfadsadsadsadasdsa',
427+
'0xfdsafdsfdsfdsfds'
428+
]) // update balance for two addresses
429+
```
430+
419431
## Setting the User's Chain/Network
420432

421433
When initializing Onboard you define a list of chains/networks that your app supports. If you would like to prompt the user to switch to one of those chains, you can use the `setChain` method on an initialized instance of Onboard:

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.2.11-alpha.1",
3+
"version": "2.2.11-alpha.2",
44
"scripts": {
55
"build": "rollup -c",
66
"dev": "rollup -c -w",

packages/core/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import App from './views/Index.svelte'
1717
import type { InitOptions, OnboardAPI } from './types'
1818
import { APP_INITIAL_STATE } from './constants'
1919
import { internalState } from './internals'
20+
import updateBalances from './updateBalances'
2021

2122
const API = {
2223
connectWallet,
@@ -27,7 +28,8 @@ const API = {
2728
select: state.select,
2829
actions: {
2930
setWalletModules,
30-
setLocale
31+
setLocale,
32+
updateBalances
3133
}
3234
}
3335
}
@@ -224,4 +226,4 @@ function mountApp() {
224226
return app
225227
}
226228

227-
export default init
229+
export default init

packages/core/src/store/actions.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ import type {
1313
UpdateAccountAction,
1414
UpdateAccountCenterAction,
1515
UpdateWalletAction,
16-
WalletState
16+
WalletState,
17+
UpdateAllWalletsAction
1718
} from '../types'
1819

1920
import {
2021
validateAccountCenterUpdate,
2122
validateLocale,
2223
validateString,
2324
validateWallet,
24-
validateWalletInit
25+
validateWalletInit,
26+
validateUpdateBalances
2527
} from '../validation'
2628

2729
import {
@@ -33,7 +35,8 @@ import {
3335
UPDATE_ACCOUNT,
3436
UPDATE_ACCOUNT_CENTER,
3537
SET_WALLET_MODULES,
36-
SET_LOCALE
38+
SET_LOCALE,
39+
UPDATE_ALL_WALLETS
3740
} from './constants'
3841
import { internalState } from '../internals'
3942

@@ -177,6 +180,21 @@ export function setLocale(locale: string): void {
177180
dispatch(action as SetLocaleAction)
178181
}
179182

183+
export function updateAllWallets(wallets: WalletState[]): void {
184+
const error = validateUpdateBalances(wallets)
185+
186+
if (error) {
187+
throw error
188+
}
189+
190+
const action = {
191+
type: UPDATE_ALL_WALLETS,
192+
payload: wallets
193+
}
194+
195+
dispatch(action as UpdateAllWalletsAction)
196+
}
197+
180198
// ==== HELPERS ==== //
181199
export function initializeWalletModules(modules: WalletInit[]): WalletModule[] {
182200
const { device } = internalState

packages/core/src/store/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export const UPDATE_ACCOUNT = 'update_account'
77
export const UPDATE_ACCOUNT_CENTER = 'update_account_center'
88
export const SET_WALLET_MODULES = 'set_wallet_modules'
99
export const SET_LOCALE= 'set_locale'
10+
export const UPDATE_ALL_WALLETS = 'update_balance'

packages/core/src/store/index.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import type {
1414
AddWalletAction,
1515
UpdateAccountAction,
1616
UpdateAccountCenterAction,
17-
Locale
17+
Locale,
18+
UpdateAllWalletsAction
1819
} from '../types'
1920

2021
import {
@@ -26,7 +27,8 @@ import {
2627
UPDATE_ACCOUNT,
2728
UPDATE_ACCOUNT_CENTER,
2829
SET_WALLET_MODULES,
29-
SET_LOCALE
30+
SET_LOCALE,
31+
UPDATE_ALL_WALLETS
3032
} from './constants'
3133

3234
function reducer(state: AppState, action: Action): AppState {
@@ -102,6 +104,14 @@ function reducer(state: AppState, action: Action): AppState {
102104
}
103105
}
104106

107+
case UPDATE_ALL_WALLETS : {
108+
const updatedWallets = payload as UpdateAllWalletsAction['payload']
109+
return {
110+
...state,
111+
wallets: updatedWallets
112+
}
113+
}
114+
105115
case UPDATE_ACCOUNT_CENTER: {
106116
const update = payload as UpdateAccountCenterAction['payload']
107117
return {
@@ -112,6 +122,7 @@ function reducer(state: AppState, action: Action): AppState {
112122
}
113123
}
114124
}
125+
115126
case SET_WALLET_MODULES: {
116127
return {
117128
...state,
@@ -175,4 +186,4 @@ function get(): AppState {
175186
export const state = {
176187
select,
177188
get
178-
}
189+
}

packages/core/src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export type Action =
136136
| UpdateAccountCenterAction
137137
| SetWalletModulesAction
138138
| SetLocaleAction
139+
| UpdateAllWalletsAction
139140

140141
export type AddChainsAction = { type: 'add_chains'; payload: Chain[] }
141142
export type AddWalletAction = { type: 'add_wallet'; payload: WalletState }
@@ -175,6 +176,11 @@ export type SetLocaleAction = {
175176
payload: string
176177
}
177178

179+
export type UpdateAllWalletsAction = {
180+
type: 'update_balance'
181+
payload: WalletState[]
182+
}
183+
178184
// ==== MISC ==== //
179185
export type ChainStyle = {
180186
icon: string

packages/core/src/updateBalances.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { state } from './store'
2+
import { getBalance } from './provider'
3+
import { updateAllWallets } from './store/actions'
4+
5+
async function updateBalances(addresses?: string[]): Promise<void> {
6+
const { wallets, chains } = state.get()
7+
8+
const updatedWallets = await Promise.all(
9+
wallets.map(async wallet => {
10+
const chain = chains.find(({ id }) => id === wallet.chains[0].id)
11+
12+
const updatedAccounts = await Promise.all(
13+
wallet.accounts.map(async account => {
14+
// if no provided addresses, we want to update all balances
15+
// otherwise check if address is in addresses array
16+
if (!addresses || addresses.includes(account.address)) {
17+
18+
const updatedBalance = await getBalance(account.address, chain)
19+
20+
return { ...account, balance: updatedBalance }
21+
}
22+
23+
return account
24+
})
25+
)
26+
return { ...wallet, accounts: updatedAccounts }
27+
})
28+
)
29+
30+
updateAllWallets(updatedWallets)
31+
}
32+
33+
export default updateBalances

packages/core/src/validation.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import type {
1313
const chainId = Joi.string().pattern(/^0x[0-9a-fA-F]+$/)
1414
const chainNamespace = Joi.string().valid('evm')
1515
const unknownObject = Joi.object().unknown()
16-
// const address = Joi.string().regex(/^0x[a-fA-F0-9]{40}$/)
1716

1817
const chain = Joi.object({
1918
namespace: chainNamespace,
@@ -65,6 +64,8 @@ const wallet = Joi.object({
6564
chains: Joi.array().items(connectedChain)
6665
})
6766

67+
const wallets = Joi.array().items(wallet)
68+
6869
const recommendedWallet = Joi.object({
6970
name: Joi.string().required(),
7071
url: Joi.string().uri().required()
@@ -209,3 +210,8 @@ export function validateWalletInit(data: WalletInit[]): ValidateReturn {
209210
export function validateLocale(data: string): ValidateReturn {
210211
return validate(locale, data)
211212
}
213+
214+
export function validateUpdateBalances(data:
215+
WalletState[]): ValidateReturn {
216+
return validate(wallets, data)
217+
}

packages/demo/src/App.svelte

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@
276276
<button on:click={() => onboard.setChain({ chainId: '0x89' })}
277277
>Set Chain to Matic</button
278278
>
279+
<button on:click={() => onboard.state.actions.updateBalances()}
280+
>Update Wallet Balance</button
281+
>
279282
{/if}
280283
281284
{#if $wallets$}

0 commit comments

Comments
 (0)