Skip to content
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

Fix select network and add token requests #11

Open
wants to merge 4 commits into
base: multi
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/config/tools/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import {
export function selectNetwork (chainID:any, type?: any) {
return new Promise(resolve => {
const { ethereum } = window
const ethereumFN:any = {
request: '',
...ethereum
}
const ethereumFN: any = ethereum;
ethereumFN.request = (ethereum as any).request ?? '';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calling ethereumFN.request will throw an error if ethereum obj is undefined. You dont need to set the request method as it should be part of the ethereum object you are referencing

Suggested change
const ethereumFN: any = ethereum;
ethereumFN.request = (ethereum as any).request ?? '';
const ethereumFN: any = ethereum

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is definitely better. I originally raised an issue (#10) about this, I'm not sure why the code is de-structuring ethereumFN from window.ethereum at all, but I tried to keep my PR as close to the original code as possible. But I should have made lines 9 & 10 like this to properly maintain consistency:

const ethereumFN: any = ethereum ?? {};
ethereumFN.request = (ethereum as any).request ?? '';

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I simplified the code in my PR slightly which should also fix the concern with window.ethereum being undefined.

window.localStorage.setItem(ENV_NODE_CONFIG, chainInfo[chainID].label)
if (ethereumFN && ethereumFN.request) {
// console.log(ethereumFN)
Expand Down Expand Up @@ -58,10 +56,8 @@ export function selectNetwork (chainID:any, type?: any) {
export function addToken (address:string, symbol: string, decimals: number, logoUrl?:string) {
return new Promise(resolve => {
const { ethereum } = window
const ethereumFN:any = {
request: '',
...ethereum
}
const ethereumFN: any = ethereum;
ethereumFN.request = (ethereum as any).request ?? '';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Suggested change
const ethereumFN: any = ethereum;
ethereumFN.request = (ethereum as any).request ?? '';
const ethereumFN: any = ethereum

if (ethereumFN && ethereumFN.request) {
const params = {
method: 'wallet_watchAsset',
Expand Down