-
Notifications
You must be signed in to change notification settings - Fork 537
Enh - Coinbase dependency upgrade to 4.0 #2204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
e0b20ed
c26445e
b38c871
4430ac5
5690fcc
efabd3b
131bc1b
bfc5f5c
ed00198
bc2a0ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,38 @@ | ||
import { WalletInit } from '@web3-onboard/common' | ||
import { | ||
createEIP1193Provider, | ||
fromHex, | ||
type WalletInit, | ||
type WalletInterface | ||
} from '@web3-onboard/common' | ||
|
||
function coinbaseWallet({ | ||
supportedWalletType = 'all', | ||
darkMode = false, | ||
enableMobileWalletLink = false, | ||
reloadOnDisconnect = true | ||
}: { | ||
/** @optional Use dark theme */ | ||
/** @deprecated Deprecated after version 2.2.7 of @web3-onboard/coinbase Use dark theme */ | ||
darkMode?: boolean | ||
/** @optional whether to connect mobile web app via WalletLink, defaults to false */ | ||
/** @deprecated Deprecated after version 2.2.7 of @web3-onboard/coinbase whether to connect mobile web app via WalletLink, defaults to false */ | ||
enableMobileWalletLink?: boolean | ||
/** @optional whether or not to reload dapp automatically after disconnect, defaults to true */ | ||
/** @deprecated Deprecated after version 2.2.7 of @web3-onboard/coinbase whether or not to reload dapp automatically after disconnect, defaults to true */ | ||
reloadOnDisconnect?: boolean | ||
/** Type of Coinbase wallets to support - options : 'all' | 'smartWalletOnly' | 'eoaOnly' - Default to `all` */ | ||
supportedWalletType?: 'all' | 'smartWalletOnly' | 'eoaOnly' | ||
} = {}): WalletInit { | ||
return () => { | ||
return { | ||
label: 'Coinbase Wallet', | ||
getIcon: async () => (await import('./icon.js')).default, | ||
getInterface: async ({ chains, appMetadata }) => { | ||
const [chain] = chains | ||
getInterface: async ({ | ||
chains, | ||
appMetadata | ||
}): Promise<WalletInterface> => { | ||
if (enableMobileWalletLink || reloadOnDisconnect || darkMode) { | ||
console.warn( | ||
'darkMode, enableMobileWalletLink and reloadOnDisconnect init props are deprecated after version 2.2.7 of @web3-onboard/coinbase' | ||
) | ||
} | ||
const { name, icon } = appMetadata || {} | ||
|
||
// according to https://github.com/wagmi-dev/wagmi/issues/383 | ||
|
@@ -31,29 +46,41 @@ function coinbaseWallet({ | |
? (CoinbaseWalletSDK as any).default | ||
: CoinbaseWalletSDK | ||
) as typeof CoinbaseWalletSDK | ||
const { isHex, toHex } = await import('@web3-onboard/common') | ||
|
||
const base64 = window.btoa(icon || '') | ||
const appLogoUrl = `data:image/svg+xml;base64,${base64}` | ||
|
||
const appChainIds = chains.map(({ id }) => | ||
fromHex(id as `0x${string}`, 'number') | ||
) | ||
|
||
const instance = new CoinbaseWalletSDKConstructor({ | ||
appName: name || '', | ||
appLogoUrl, | ||
darkMode, | ||
enableMobileWalletLink, | ||
reloadOnDisconnect | ||
appChainIds | ||
}) | ||
|
||
const coinbaseWalletProvider = instance.makeWeb3Provider( | ||
chain.rpcUrl, | ||
parseInt(chain.id) | ||
) | ||
const coinbaseWalletProvider = instance.makeWeb3Provider({ | ||
options: supportedWalletType | ||
}) | ||
|
||
// patch the chainChanged event | ||
const on = coinbaseWalletProvider.on.bind(coinbaseWalletProvider) | ||
|
||
coinbaseWalletProvider.on = (event, listener) => { | ||
// @ts-ignore | ||
on(event, val => { | ||
if (event === 'chainChanged') { | ||
listener(`0x${(val as number).toString(16)}`) | ||
let hexVal: string | ||
if (isHex(val)) { | ||
hexVal = val | ||
} else { | ||
hexVal = toHex(val) | ||
} | ||
|
||
// @ts-ignore | ||
listener(hexVal) | ||
return | ||
} | ||
|
||
|
@@ -62,9 +89,20 @@ function coinbaseWallet({ | |
|
||
return coinbaseWalletProvider | ||
} | ||
const provider = createEIP1193Provider(coinbaseWalletProvider, { | ||
eth_chainId: ({ baseRequest }) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nateReiners patched provider to handle number value returned from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Adamj1232 If you want to bump the sdk to 4.0.3 and get rid of this workaround, the version with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nateReiners when I remove the patch and |
||
baseRequest({ method: 'eth_chainId' }).then(id => { | ||
if (isHex(id)) { | ||
return id | ||
} else { | ||
return toHex(id) | ||
} | ||
}) | ||
}) | ||
provider.removeListener = (event, func) => {} | ||
|
||
return { | ||
provider: coinbaseWalletProvider, | ||
provider, | ||
instance | ||
} | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.