Skip to content

Commit

Permalink
feat: checks providers if there are multiple
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Dec 6, 2023
1 parent 45fb2cc commit 6eb83d6
Showing 1 changed file with 48 additions and 13 deletions.
61 changes: 48 additions & 13 deletions src/app/hooks/use-ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,16 @@ export function useEthereum(): UseEthereumReturnType {
newEthereumNetwork: EthereumNetwork,
): Promise<void> {
const { ethereum } = window;
const metamaskProvider = ethereum.providers.find(
(provider: any) => provider.isMetaMask,
);

let metamaskProvider;
if ("providers" in ethereum) {
metamaskProvider = ethereum.providers.find(
(provider: any) => provider.isMetaMask,
);
} else {
metamaskProvider = ethereum;
}

try {
await metamaskProvider.request({
method: "wallet_addEthereumChain",
Expand All @@ -149,9 +156,16 @@ export function useEthereum(): UseEthereumReturnType {
newEthereumNetwork: EthereumNetwork,
): Promise<void> {
const { ethereum } = window;
const metamaskProvider = ethereum.providers.find(
(provider: any) => provider.isMetaMask,
);

let metamaskProvider;
if ("providers" in ethereum) {
metamaskProvider = ethereum.providers.find(
(provider: any) => provider.isMetaMask,
);
} else {
metamaskProvider = ethereum;
}

try {
await metamaskProvider.request({
method: "wallet_switchEthereumChain",
Expand Down Expand Up @@ -183,8 +197,17 @@ export function useEthereum(): UseEthereumReturnType {
async function getEthereumProvider(network: Network) {
try {
const { ethereum } = window;

let metamaskProvider;
if ("providers" in ethereum) {
metamaskProvider = ethereum.providers.find(
(provider: any) => provider.isMetaMask,
);
} else {
metamaskProvider = ethereum;
}
const browserProvider = new ethers.providers.Web3Provider(
ethereum.providers.find((provider: any) => provider.isMetaMask),
metamaskProvider,
"any",
);
const signer = browserProvider.getSigner();
Expand Down Expand Up @@ -250,9 +273,14 @@ export function useEthereum(): UseEthereumReturnType {
try {
const { ethereum } = window;

const metamaskProvider = ethereum.providers.find(
(provider: any) => provider.isMetaMask,
);
let metamaskProvider;
if ("providers" in ethereum) {
metamaskProvider = ethereum.providers.find(
(provider: any) => provider.isMetaMask,
);
} else {
metamaskProvider = ethereum;
}

if (!metamaskProvider) {
alert("Install MetaMask!");
Expand Down Expand Up @@ -410,9 +438,16 @@ export function useEthereum(): UseEthereumReturnType {
async function recommendTokenToMetamask(): Promise<boolean> {
try {
const { ethereum } = window;
const metamaskProvider = ethereum.providers.find(
(provider: any) => provider.isMetaMask,
);

let metamaskProvider;
if ("providers" in ethereum) {
metamaskProvider = ethereum.providers.find(
(provider: any) => provider.isMetaMask,
);
} else {
metamaskProvider = ethereum;
}

if (!metamaskProvider) return false;
const response = await metamaskProvider.request({
method: "wallet_watchAsset",
Expand Down

0 comments on commit 6eb83d6

Please sign in to comment.