Skip to content

Commit

Permalink
fix: avoid asking user to switch chain (#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuanddd authored Apr 23, 2024
1 parent 63c8e21 commit dbacd76
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-pots-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mochi-web3/connect-wallet-widget": patch
---

Avoid asking user to switch chain
25 changes: 18 additions & 7 deletions packages/web3/connect-wallet-widget/src/providers/evm-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,19 +223,25 @@ export class ProviderEVM extends ChainProvider {

if (!this.signClient) throw new Error('Cannot init/find signClient')

const chainIds = [
'1',
// , '56', '42161', '137', '10', '8453'
]
const chainIds = ['250', '56', '42161', '137', '10', '8453']

const { uri, approval } = await this.signClient.connect({
requiredNamespaces: {
eip155: {
methods: ['eth_sendTransaction', 'personal_sign'],
chains: chainIds.map((cid) => `eip155:${(+cid).toString(10)}`),
chains: [`eip155:1`],
events: ['chainChanged', 'accountsChanged'],
},
},
optionalNamespaces: Object.fromEntries(
chainIds.map((cid) => [
`eip155:${(+cid).toString(10)}`,
{
methods: ['eth_sendTransaction', 'personal_sign'],
events: ['chainChanged', 'accountsChanged'],
},
]),
),
})

if (!uri || !this.mobileProtocol)
Expand All @@ -246,13 +252,18 @@ export class ProviderEVM extends ChainProvider {

const session = await approval()
this.session = session
let chainId = 'eip155:1'
const accounts = session.namespaces.eip155.accounts
.map((a) => a.split(':').pop() ?? '')
.map((a) => {
const [ns, c, addr] = a.split(':')
chainId = `${ns}:${c}`
return addr
})
.filter(Boolean)

const sig = (await this.signClient.request({
topic: this.session.topic,
chainId: 'eip155:1',
chainId,
request: {
method: 'personal_sign',
params: [hexedMsg, accounts[0]],
Expand Down

0 comments on commit dbacd76

Please sign in to comment.