Skip to content

Commit

Permalink
Merge pull request #446 from blocknative/enhancement/metamask-update
Browse files Browse the repository at this point in the history
Enhancement: MetaMask update

Closes #445
  • Loading branch information
lnbc1QWFyb24 authored Nov 9, 2020
2 parents 8625f5a + 88c8bdf commit 8ea4a20
Showing 1 changed file with 42 additions and 17 deletions.
59 changes: 42 additions & 17 deletions src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@ import { WalletInterface } from './interfaces'

export function getNetwork(provider: any): Promise<number | any> {
return new Promise((resolve, reject) => {
const params = {
const options = {
jsonrpc: '2.0',
method: 'net_version',
params: [],
id: 42
}

// use MetaMask parameter if there
if (provider.chainId) {
return resolve(Number(provider.chainId))
}

const callback = (e: any, res: any) => {
e && reject(e)
const result = res && res.result
resolve(result && Number(result))
}

if (typeof provider.sendAsync === 'function') {
provider.sendAsync(params, callback)
provider.sendAsync(options, callback)
} else if (typeof provider.send === 'function') {
provider.send(params, callback)
provider.send(options, callback)
} else {
resolve(null)
}
Expand All @@ -30,23 +35,30 @@ export function getNetwork(provider: any): Promise<number | any> {

export function getAddress(provider: any): Promise<string | any> {
return new Promise((resolve, reject) => {
const params = {
const options = {
jsonrpc: '2.0',
method: 'eth_accounts',
params: [],
id: 42
}

// use MetaMask request method if there
if (provider.request) {
return provider.request(options).then((res: string[]) => {
return resolve(res[0])
})
}

const callback = (e: any, res: any) => {
e && reject(e)
const result = res && res.result && res.result[0]
resolve(result)
}

if (typeof provider.sendAsync === 'function') {
provider.sendAsync(params, callback)
provider.sendAsync(options, callback)
} else if (typeof provider.send === 'function') {
provider.send(params, callback)
provider.send(options, callback)
} else {
resolve(null)
}
Expand All @@ -65,23 +77,31 @@ export function getBalance(
return
}

const params = {
const options = {
jsonrpc: '2.0',
method: 'eth_getBalance',
params: [currentAddress, 'latest'],
id: 42
}

// use MetaMask request method if there
if (provider.request) {
return provider
.request(options)
.then((res: string) => (res ? new BigNumber(res).toString(10) : null))
.then(resolve)
}

const callback = (e: any, res: any) => {
e && reject(e)
const result = res && res.result
resolve(result && new BigNumber(result).toString(10))
}

if (typeof provider.sendAsync === 'function') {
provider.sendAsync(params, callback)
provider.sendAsync(options, callback)
} else if (typeof provider.send === 'function') {
provider.send(params, callback)
provider.send(options, callback)
} else {
resolve(null)
}
Expand Down Expand Up @@ -131,14 +151,19 @@ export function createModernProviderInterface(provider: any): WalletInterface {
connect: () =>
new Promise(
(resolve: () => void, reject: (err: { message: string }) => void) => {
provider
.enable()
.then(resolve)
.catch(() =>
reject({
message: 'This dapp needs access to your account information.'
const request = provider.request
? getAddress(provider).then((address: string) => {
return address
? address
: provider.request({ method: 'eth_requestAccounts' })
})
)
: provider.enable()

return request.then(resolve).catch(() =>
reject({
message: 'This dapp needs access to your account information.'
})
)
}
),
name: getProviderName(provider)
Expand All @@ -164,7 +189,7 @@ export function getProviderName(provider: any): string | undefined {
if (!provider) return

if (provider.isWalletIO) {
return 'wallet.io';
return 'wallet.io'
}

if (provider.wallet === 'MEETONE') {
Expand Down

0 comments on commit 8ea4a20

Please sign in to comment.