Skip to content

Commit 9bb2d92

Browse files
committed
Refine docs, examples and use of wallet state to pass wagmiConnector
1 parent 47a3e6f commit 9bb2d92

File tree

7 files changed

+77
-56
lines changed

7 files changed

+77
-56
lines changed

docs/src/routes/docs/[...3]modules/[...3]react/+page.md

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -772,18 +772,13 @@ const sendTransaction = async () => {
772772
console.log(result)
773773
}
774774

775-
async function switchWagmiChain(chainId) {
775+
async function signMessage(chainId) {
776776
// current primary wallet - as multiple wallets can connect this value is the currently active
777777
const [activeWallet] = onboard.state.get().wallets
778-
let chainAsNumber
779-
if (isHex(chainId)) {
780-
chainAsNumber = fromHex(chainId, 'number')
781-
} else if (!isHex(chainId) && typeof chainId === 'number') {
782-
chainAsNumber = chainId
783-
} else {
784-
throw new Error('Invalid chainId')
785-
}
786778
const wagmiConfig = onboard.state.get().wagmiConfig
787-
await switchChain(wagmiConfig, {
788-
chainId: chainAsNumber,
789-
connector:
779+
await wagmiSignMessage(wagmiConfig, {
780+
message: 'This is my message to you',
781+
connector: activeWallet.wagmiConnector
782+
})
783+
}
784+
```

docs/src/routes/docs/[...3]modules/[...8]wagmi/+page.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import { parseEther, isHex, fromHex } from 'viem'
5050
const injected = injectedModule()
5151

5252
const onboard = Onboard({
53+
// This javascript object is unordered meaning props do not require a certain order
5354
wagmi,
5455
wallets: [injected],
5556
chains: [
@@ -77,6 +78,16 @@ const sendTransaction = async () => {
7778
console.log(result)
7879
}
7980

81+
async function signMessage(chainId) {
82+
// current primary wallet - as multiple wallets can connect this value is the currently active
83+
const [activeWallet] = onboard.state.get().wallets
84+
const wagmiConfig = onboard.state.get().wagmiConfig
85+
await wagmiSignMessage(wagmiConfig, {
86+
message: 'This is my message to you',
87+
connector: activeWallet.wagmiConnector
88+
})
89+
}
90+
8091
async function switchWagmiChain(chainId) {
8192
// current primary wallet - as multiple wallets can connect this value is the currently active
8293
const [activeWallet] = onboard.state.get().wallets

packages/core/src/store/actions.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { nanoid } from 'nanoid'
99
import { dispatch } from './index.js'
1010
import { configuration } from '../configuration.js'
1111
import { handleThemeChange, returnTheme } from '../themes.js'
12+
import { state } from '../store/index.js'
1213

1314
import type {
1415
Account,
@@ -192,6 +193,9 @@ export function setPrimaryWallet(wallet: WalletState, address?: string): void {
192193
}
193194
}
194195

196+
// Update wagmi config if wagmi is being used
197+
handleWagmiConnectorUpdate(wallet)
198+
195199
// add wallet will set it to first wallet since it already exists
196200
addWallet(wallet)
197201
}
@@ -478,14 +482,30 @@ export function updateAppMetadata(
478482
dispatch(action as UpdateAppMetadataAction)
479483
}
480484

481-
export function updateWagmiConfig(
482-
update: Config
483-
): void {
484-
485+
export function updateWagmiConfig(update: Config): void {
485486
const action = {
486487
type: UPDATE_WAGMI_CONFIG,
487488
payload: update
488489
}
489490

490491
dispatch(action as UpdateWagmiConfigAction)
491492
}
493+
494+
function handleWagmiConnectorUpdate(wallet: WalletState) {
495+
const { wagmi } = configuration
496+
if (!wagmi) return
497+
498+
try {
499+
const { label } = wallet
500+
const { wagmiConnect, getWagmiConnector } = wagmi
501+
const wagmiConfig = state.get().wagmiConfig
502+
const wagmiConnector = getWagmiConnector(label)
503+
wagmiConnect(wagmiConfig, { connector: wagmiConnector }).then(() => {
504+
updateWallet(label, { wagmiConnector })
505+
})
506+
} catch (e) {
507+
console.error(
508+
`Error updating Wagmi connector on primary wallet switch ${e}`
509+
)
510+
}
511+
}

packages/demo/src/App.svelte

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
switchChain,
5858
disconnect,
5959
signMessage as wagmiSignMessage,
60-
getConnectors
60+
getConnectors,
61+
watchConnectors
6162
} from '@web3-onboard/wagmi'
6263
import { parseEther, isHex, fromHex } from 'viem'
6364
import passportModule, { Network } from '@web3-onboard/passport'
@@ -510,11 +511,11 @@
510511
511512
// const receipt = await txn.wait()
512513
// console.log(receipt)
513-
const wagmiConfig = onboard.state.get().wagmiConfig
514-
console.log(wagmiConfig)
514+
const [primaryWallet] = onboard.state.get().wallets
515515
const result = await wagmiSendTransaction(wagmiConfig, {
516516
to: toAddress,
517-
value: parseEther('0.001')
517+
value: parseEther('0.001'),
518+
connector: primaryWallet.wagmiConnector
518519
})
519520
console.log(result)
520521
}
@@ -555,7 +556,7 @@
555556
console.log(transactionHash)
556557
}
557558
558-
const signMessage = async (provider, address) => {
559+
const signMessage = async (wagmiConnector, address) => {
559560
// if using ethers v6 this is:
560561
// ethersProvider = new ethers.BrowserProvider(wallet.provider, 'any')
561562
// const ethersProvider = new ethers.providers.Web3Provider(provider, 'any')
@@ -564,9 +565,10 @@
564565
// const signature = await signer?.signMessage(signMsg)
565566
// let verifySign = false
566567
// let recoveredAddress = null
567-
const wagmiConfig = onboard.state.get().wagmiConfig
568-
console.log('signMessage', wagmiConfig)
569-
await wagmiSignMessage(wagmiConfig, { message: signMsg })
568+
await wagmiSignMessage(wagmiConfig, {
569+
message: signMsg,
570+
connector: wagmiConnector
571+
})
570572
// try {
571573
// recoveredAddress = recoverAddress(
572574
// arrayify(hashMessage(signMsg)),
@@ -675,8 +677,12 @@
675677
} else {
676678
throw new Error('Invalid chainId')
677679
}
678-
const wagmiConfig = onboard.state.get().wagmiConfig
679-
await switchChain(wagmiConfig, { chainId: chainAsNumber })
680+
const [primaryWallet] = onboard.state.get().wallets
681+
const { wagmiConnector } = primaryWallet
682+
await switchChain(wagmiConfig, {
683+
chainId: chainAsNumber,
684+
connector: wagmiConnector
685+
})
680686
}
681687
</script>
682688

@@ -889,7 +895,7 @@
889895
{/if}
890896
</div>
891897
{#if $wallets$}
892-
{#each $wallets$ as { icon, label, accounts, chains, provider, instance }}
898+
{#each $wallets$ as { icon, label, accounts, chains, provider, instance, wagmiConnector }}
893899
<div class="connected-wallet" data-testid="connected-wallet">
894900
<div class="flex-centered" style="width: 10rem;">
895901
<div style="width: 2rem; height: 2rem">
@@ -945,7 +951,7 @@
945951
bind:value={toAddress}
946952
data-testid="sendTransaction"
947953
/>
948-
<button on:click={sendTransaction(provider)}>
954+
<button on:click={sendTransaction(wagmiConnector)}>
949955
Send Transaction
950956
</button>
951957
</div>
@@ -969,7 +975,7 @@
969975
placeholder="Message..."
970976
bind:value={signMsg}
971977
/>
972-
<button on:click={signMessage(provider, address)}>
978+
<button on:click={signMessage(wagmiConnector, address)}>
973979
Sign Message
974980
</button>
975981
</div>
@@ -979,7 +985,7 @@
979985
type="text"
980986
class="sign-transaction-textarea"
981987
/>
982-
<button on:click={signTypedMessage(provider, address)}>
988+
<button on:click={signTypedMessage(wagmiConnector, address)}>
983989
Sign Typed Message
984990
</button>
985991
</div>
@@ -991,7 +997,7 @@
991997
class="sign-transaction-textarea"
992998
/>
993999
<button
994-
on:click={signTransactionMessage(provider)}
1000+
on:click={signTransactionMessage(wagmiConnector)}
9951001
style="margin: 0 0 0 .5rem"
9961002
>
9971003
Sign Transaction
@@ -1005,7 +1011,7 @@
10051011
const disconnectThisWallet = getConnectors(wagmiConfig).find(
10061012
connector => connector.name === label
10071013
)
1008-
disconnect(wagmiConfig, { connector: disconnectThisWallet })
1014+
disconnect(wagmiConfig, { connector: wagmiConnector })
10091015
}}
10101016
>
10111017
Disconnect Wallet

packages/react/README.md

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -495,18 +495,13 @@ const sendTransaction = async () => {
495495
console.log(result)
496496
}
497497

498-
async function switchWagmiChain(chainId) {
498+
async function signMessage(chainId) {
499499
// current primary wallet - as multiple wallets can connect this value is the currently active
500500
const [activeWallet] = onboard.state.get().wallets
501-
let chainAsNumber
502-
if (isHex(chainId)) {
503-
chainAsNumber = fromHex(chainId, 'number')
504-
} else if (!isHex(chainId) && typeof chainId === 'number') {
505-
chainAsNumber = chainId
506-
} else {
507-
throw new Error('Invalid chainId')
508-
}
509501
const wagmiConfig = onboard.state.get().wagmiConfig
510-
await switchChain(wagmiConfig, {
511-
chainId: chainAsNumber,
512-
connector:
502+
await wagmiSignMessage(wagmiConfig, {
503+
message: 'This is my message to you',
504+
connector: activeWallet.wagmiConnector
505+
})
506+
}
507+
```

packages/wagmi/README.md

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { parseEther, isHex, fromHex } from 'viem'
3535
const injected = injectedModule()
3636

3737
const onboard = Onboard({
38+
// This javascript object is unordered meaning props do not require a certain order
3839
wagmi,
3940
wallets: [injected],
4041
chains: [
@@ -62,21 +63,13 @@ const sendTransaction = async () => {
6263
console.log(result)
6364
}
6465

65-
async function switchWagmiChain(chainId) {
66+
async function signMessage(chainId) {
6667
// current primary wallet - as multiple wallets can connect this value is the currently active
6768
const [activeWallet] = onboard.state.get().wallets
68-
let chainAsNumber
69-
if (isHex(chainId)) {
70-
chainAsNumber = fromHex(chainId, 'number')
71-
} else if (!isHex(chainId) && typeof chainId === 'number') {
72-
chainAsNumber = chainId
73-
} else {
74-
throw new Error('Invalid chainId')
75-
}
7669
const wagmiConfig = onboard.state.get().wagmiConfig
77-
await switchChain(wagmiConfig, {
78-
chainId: chainAsNumber,
79-
connector: wagmiConnector
70+
await wagmiSignMessage(wagmiConfig, {
71+
message: 'This is my message to you',
72+
connector: activeWallet.wagmiConnector
8073
})
8174
}
8275

packages/wagmi/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ async function buildWagmiConfig(
8787
return undefined
8888
}
8989
}
90+
console.log('wagmiConnectorFn', wagmiConnectorFn)
9091
const connectors: CreateConnectorFn[] = [...Object.values(wagmiConnectorFn)]
9192
const viemChains = await createWagmiChains(chainsList || [], transports)
9293

0 commit comments

Comments
 (0)