Skip to content

Commit

Permalink
Update version metamask sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiendekaco committed May 8, 2024
1 parent 0c27303 commit cf20656
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 254 deletions.
2 changes: 1 addition & 1 deletion packages/metamask/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"typescript": "^5.2.2"
},
"dependencies": {
"@metamask/sdk": "^0.13.0",
"@metamask/sdk": "^0.18.6",
"@subwallet-connect/common": "^1.0.4"
},
"author": ""
Expand Down
37 changes: 12 additions & 25 deletions packages/metamask/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const loadImports = async () => {
const { createEIP1193Provider } = await import('@subwallet-connect/common')
const importedSDK = await import('@metamask/sdk')
const MetaMaskSDKConstructor =
// @ts-ignore
importedSDK.MetaMaskSDK || importedSDK.default.MetaMaskSDK
// @ts-ignore
importedSDK.MetaMaskSDK || importedSDK.default.MetaMaskSDK

if (!MetaMaskSDKConstructor) {
throw new Error('Error importing and initializing MetaMask SDK')
Expand All @@ -27,7 +27,6 @@ const loadImports = async () => {

let importPromise: Promise<ImportSDK> | null = null
let sdk: MetaMaskSDK | null = null
let createInstance: typeof createEIP1193Provider

function metamask({
options
Expand All @@ -39,14 +38,6 @@ function metamask({
throw error
})

const getProvider = (_sdk: MetaMaskSDK) => {
const provider = createInstance(_sdk.getProvider(), {})
provider.disconnect = () => {
sdk?.terminate()
}
return provider
}

return {
label: 'MetaMask',
type: 'evm',
Expand All @@ -67,37 +58,33 @@ function metamask({
const appLogoUrl = `data:image/svg+xml;base64,${base64}`
const imports = await importPromise

// Patch issue with MetaMask SDK, remove after SDK is fixed
localStorage.removeItem('providerType')

if (
!imports?.MetaMaskSDKConstructor ||
!imports?.createEIP1193Provider
!imports?.MetaMaskSDKConstructor ||
!imports?.createEIP1193Provider
) {
throw new Error('Error importing and initializing MetaMask SDK')
}

const { createEIP1193Provider, MetaMaskSDKConstructor } = imports

createInstance = createEIP1193Provider
sdk = new MetaMaskSDKConstructor({
...options,
dappMetadata: {
name: options.dappMetadata?.name || name || '',
url: options.dappMetadata?.url || window.location.origin,
base64Icon: appLogoUrl
},
_source: 'web3-onboard'
})
await sdk.init()

const provider = getProvider(sdk)
await sdk.init();
const provider = sdk.getProvider();

const _request = provider.request
provider.request = async ({ method, params }) => {
if (sdk?.isExtensionActive()) {
return (window.extension as any).request({ method, params })
const _disconnect = sdk.disconnect
if(provider) {
(provider as any).disconnect = () => {
sdk?.terminate();
}
return _request({ method, params }) as Promise<any>
}

return {
Expand All @@ -109,4 +96,4 @@ function metamask({
}
}

export default metamask
export default metamask
Loading

0 comments on commit cf20656

Please sign in to comment.