Skip to content
This repository has been archived by the owner on Sep 6, 2023. It is now read-only.

Commit

Permalink
feat: update to sdk 0.5.4
Browse files Browse the repository at this point in the history
  • Loading branch information
abretonc7s committed Jul 20, 2023
1 parent e4d4394 commit e477d5e
Show file tree
Hide file tree
Showing 4 changed files with 523 additions and 613 deletions.
2 changes: 1 addition & 1 deletion packages/connectors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"dependencies": {
"@coinbase/wallet-sdk": "^3.6.6",
"@ledgerhq/connect-kit-loader": "^1.1.0",
"@metamask/sdk": "^0.5.0",
"@metamask/sdk": "^0.5.4",
"@safe-global/safe-apps-provider": "^0.17.1",
"@safe-global/safe-apps-sdk": "^8.0.0",
"@walletconnect/ethereum-provider": "2.9.0",
Expand Down
3 changes: 3 additions & 0 deletions packages/connectors/src/metaMask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export class MetaMaskConnector extends InjectedConnector {
chains?: Chain[]
options?: MetaMaskConnectorOptions
} = {}) {
console.warn(
'[DEPRECATED] The MetaMaskConnector is deprecated and will be removed in the next major version. Please use the MetaMaskSDKConnector instead.',
)
const options = {
name: 'MetaMask',
shimDisconnect: true,
Expand Down
57 changes: 2 additions & 55 deletions packages/connectors/src/metaMaskSDK.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { InjectedConnector } from './injected'
import { WindowProvider } from './types'
import {
EventType,
MetaMaskSDK,
MetaMaskSDKOptions,
SDKProvider,
} from '@metamask/sdk'
import { MetaMaskSDK, MetaMaskSDKOptions, SDKProvider } from '@metamask/sdk'
import {
Address,
Chain,
Expand All @@ -15,7 +10,6 @@ import {
} from 'viem'

export type MetaMaskSDKConnectorOptions = {
debug?: boolean
// Keep both sdk and sdkOptions as some users might want to use their own pre-defined sdk instance
sdk?: MetaMaskSDK
sdkOptions?: MetaMaskSDKOptions
Expand All @@ -26,7 +20,6 @@ export class MetaMaskSDKConnector extends InjectedConnector {

#sdk: MetaMaskSDK
#provider?: SDKProvider
#debug = false

constructor({
chains,
Expand Down Expand Up @@ -88,43 +81,6 @@ export class MetaMaskSDKConnector extends InjectedConnector {
this.#provider?.on('disconnect', this.onDisconnect as any)
}

// Two scenarios depending on wether browser extension is installed:
// - if installed and user chooses browser extension, then wait for SWITCH_PROVIDER event
// - if not installed, or user chooses mobile wallet, then wait for AUTHORIZED event
async #waitForSDK() {
if (this.#debug) {
console.log('MetaMaskSDKConnector waiting for SDK validation')
}

return new Promise((resolve) => {
this.#sdk.once(
EventType.PROVIDER_UPDATE,
(_accounts: string[] | undefined) => {
resolve(true)
},
)

// backward compatibility with older wallet version that return accounts before authorization
if (this.#sdk._getConnection()?.isAuthorized()) {
resolve(true)
} else {
const waitForAuthorized = () => {
return new Promise((resolve) => {
this.#sdk
._getConnection()
?.getConnector()
.once(EventType.AUTHORIZED, () => {
resolve(true)
})
})
}
waitForAuthorized().then(() => {
resolve(true)
})
}
})
}

async getProvider() {
if (!this.#sdk.isInitialized()) {
await this.#sdk.init()
Expand Down Expand Up @@ -153,20 +109,11 @@ export class MetaMaskSDKConnector extends InjectedConnector {
await this.#sdk.init()
}

this.#sdk.connect().catch((_error: unknown) => {
// Catch to prevent unhandled promise but can be ignored.
})

await this.#waitForSDK()
const accounts = (await this.#sdk.connect()) as Address[]

// Get latest provider instance (it may have changed based on user selection)
this.#updateProviderListeners()

const accounts: Address[] = (await this.#provider?.request({
method: 'eth_requestAccounts',
params: [],
})) as Address[]

const selectedAccount: Address = accounts?.[0] ?? '0x'

let providerChainId: string | null | undefined = this.#provider?.chainId
Expand Down
Loading

0 comments on commit e477d5e

Please sign in to comment.