Skip to content

Commit 9dcb0ca

Browse files
authored
Merge pull request #419 from blocknative/fix/mykey-balance
Fix: Mykey balance Closes #418
2 parents 982c2b2 + 213145d commit 9dcb0ca

File tree

6 files changed

+68
-34
lines changed

6 files changed

+68
-34
lines changed

src/interfaces.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,7 @@ export interface WalletLinkOptions extends CommonWalletOptions {
321321
rpcUrl: string
322322
}
323323

324-
export interface ImTokenOptions extends CommonWalletOptions {
325-
rpcUrl?: string
326-
}
327-
328-
export interface TrustWalletOptions extends CommonWalletOptions {
324+
export interface InjectedWithBalanceOptions extends CommonWalletOptions {
329325
rpcUrl?: string
330326
}
331327

@@ -337,8 +333,7 @@ export type WalletInitOptions =
337333
| TrezorOptions
338334
| AuthereumOptions
339335
| LedgerOptions
340-
| ImTokenOptions
341-
| TrustWalletOptions
336+
| InjectedWithBalanceOptions
342337

343338
export type AllWalletInitOptions = CommonWalletOptions &
344339
SdkWalletOptions &
@@ -348,8 +343,7 @@ export type AllWalletInitOptions = CommonWalletOptions &
348343
AuthereumOptions &
349344
LedgerOptions &
350345
WalletLinkOptions &
351-
ImTokenOptions &
352-
TrustWalletOptions & { networkId: number }
346+
InjectedWithBalanceOptions & { networkId: number }
353347

354348
export interface WalletCheckCustomOptions {
355349
heading?: string

src/modules/select/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { WalletModule, WalletInitOptions } from '../../interfaces'
22
import { isWalletInit } from '../../validation'
33

44
// wallets that qualify for default wallets need to have no
5-
// init parameters that are required
5+
// init parameters that are required for full functionality
66
const defaultWalletNames = [
77
'metamask',
88
'dapper',
@@ -13,8 +13,7 @@ const defaultWalletNames = [
1313
'opera',
1414
'operaTouch',
1515
'status',
16-
'unilogin',
17-
'mykey'
16+
'unilogin'
1817
]
1918

2019
function select(

src/modules/select/wallets/imtoken.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { mobileWalletInstallMessage } from '../content'
2-
import { WalletModule, Helpers, ImTokenOptions } from '../../../interfaces'
2+
import {
3+
WalletModule,
4+
Helpers,
5+
InjectedWithBalanceOptions
6+
} from '../../../interfaces'
37

48
import imTokenIcon from '../wallet-icons/icon-imtoken'
59

6-
function imtoken(options: ImTokenOptions): WalletModule {
10+
function imtoken(options: InjectedWithBalanceOptions): WalletModule {
711
const { preferred, label, iconSrc, svg, rpcUrl } = options
812

913
return {

src/modules/select/wallets/mykey.ts

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,69 @@
11
import { mobileWalletInstallMessage } from '../content'
2-
import { WalletModule, Helpers, CommonWalletOptions } from '../../../interfaces'
2+
import {
3+
WalletModule,
4+
Helpers,
5+
InjectedWithBalanceOptions
6+
} from '../../../interfaces'
37

48
import mykeyIcon from '../wallet-icons/icon-mykey.png'
59
import mykeyIcon2x from '../wallet-icons/icon-mykey@2x.png'
610

7-
function mykey(options: CommonWalletOptions): WalletModule {
8-
const { preferred, label, iconSrc, svg } = options
11+
function mykey(options: InjectedWithBalanceOptions): WalletModule {
12+
const { preferred, label, iconSrc, svg, rpcUrl } = options
913

1014
return {
1115
name: label || 'MYKEY',
1216
iconSrc: iconSrc || mykeyIcon,
1317
iconSrcSet: iconSrc || mykeyIcon2x,
1418
svg,
1519
wallet: async (helpers: Helpers) => {
16-
const {
17-
getProviderName,
18-
createModernProviderInterface,
19-
createLegacyProviderInterface
20-
} = helpers
21-
22-
const provider =
20+
const { getProviderName, getAddress, getNetwork, getBalance } = helpers
21+
const myKeyProvider =
2322
(window as any).ethereum ||
2423
((window as any).web3 && (window as any).web3.currentProvider)
2524

25+
const isMyKey = getProviderName(myKeyProvider) === 'MYKEY'
26+
let createProvider
27+
28+
if (isMyKey && rpcUrl) {
29+
createProvider = (await import('./providerEngine')).default
30+
}
31+
32+
const provider = createProvider ? createProvider({ rpcUrl }) : null
33+
34+
let warned = false
35+
2636
return {
27-
provider,
28-
interface:
29-
provider && getProviderName(provider) === 'mykey'
30-
? typeof provider.enable === 'function'
31-
? createModernProviderInterface(provider)
32-
: createLegacyProviderInterface(provider)
33-
: null
37+
provider: myKeyProvider,
38+
interface: isMyKey
39+
? {
40+
address: {
41+
get: () => getAddress(myKeyProvider)
42+
},
43+
network: {
44+
get: () => getNetwork(myKeyProvider)
45+
},
46+
balance: {
47+
get: async () => {
48+
if (!provider) {
49+
if (!warned) {
50+
console.warn(
51+
'The MYKEY provider does not allow rpc calls preventing Onboard.js from getting the balance. You can pass in a "rpcUrl" to the MYKEY wallet initialization object to get the balance.'
52+
)
53+
warned = true
54+
}
55+
56+
return null
57+
}
58+
59+
const address = await getAddress(myKeyProvider)
60+
61+
return getBalance(provider, address)
62+
}
63+
},
64+
name: getProviderName(myKeyProvider)
65+
}
66+
: null
3467
}
3568
},
3669
type: 'injected',

src/modules/select/wallets/trust.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { mobileWalletInstallMessage } from '../content'
2-
import { WalletModule, Helpers, TrustWalletOptions } from '../../../interfaces'
2+
import {
3+
WalletModule,
4+
Helpers,
5+
InjectedWithBalanceOptions
6+
} from '../../../interfaces'
37

48
import trustIcon from '../wallet-icons/icon-trust'
59

6-
function trust(options: TrustWalletOptions): WalletModule {
10+
function trust(options: InjectedWithBalanceOptions): WalletModule {
711
const { preferred, label, iconSrc, svg, rpcUrl } = options
812

913
return {

src/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ export function getProviderName(provider: any): string | undefined {
212212
}
213213

214214
if (provider.isMYKEY) {
215-
return 'mykey'
215+
return 'MYKEY'
216216
}
217217

218218
if (provider.host && provider.host.indexOf('localhost') !== -1) {

0 commit comments

Comments
 (0)