forked from ubiquity/pay.ubq.fi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: optimal rpc for writes, no more infinite loading
- Loading branch information
Showing
8 changed files
with
112 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"$schema": "https://unpkg.com/knip@5/schema.json", | ||
"entry": ["static/scripts/rewards/init.ts"], | ||
"ignore": ["lib/**"] | ||
"ignore": ["lib/**"], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,77 @@ | ||
import { JsonRpcSigner } from "@ethersproject/providers"; | ||
import { ethers } from "ethers"; | ||
import { buttonController, toaster } from "../toaster"; | ||
import { useFastestRpc } from "../rpc-optimization/get-optimal-provider"; | ||
import { AppState } from "../app-state"; | ||
import { verifyCurrentNetwork } from "./verify-current-network"; | ||
|
||
export async function connectWallet(): Promise<JsonRpcSigner | null> { | ||
export async function connectWallet(app: AppState): Promise<JsonRpcSigner | null> { | ||
try { | ||
// take the wallet provider from the window | ||
const wallet = new ethers.providers.Web3Provider(window.ethereum); | ||
|
||
// the rewards are populated before the provider is so this is safe | ||
const rewardNetworkId = app.reward.networkId; | ||
|
||
// verify we are on the correct network immediately | ||
await verifyCurrentNetwork(rewardNetworkId); | ||
|
||
// get account access from the wallet provider i.e metamask | ||
await wallet.send("eth_requestAccounts", []); | ||
|
||
const signer = wallet.getSigner(); | ||
// get our optimal rpc provider | ||
const rpc = await useFastestRpc(app); | ||
|
||
// take the signer from the wallet | ||
const walletSigner = wallet.getSigner(); | ||
|
||
// take the address of the signer | ||
const walletAddress = await walletSigner.getAddress(); | ||
|
||
// use the rpc and connect the signer to an UncheckedRpcSigner instance | ||
const rpcSigner = rpc.getUncheckedSigner(walletAddress) as JsonRpcSigner; | ||
|
||
const address = await signer.getAddress(); | ||
// get the address of the signer | ||
const newJsonSignerAddress = await rpcSigner.getAddress(); | ||
|
||
if (!address) { | ||
// if we have window.eth then we should have an address | ||
if (walletAddress == "" || newJsonSignerAddress == "" || !walletAddress || !newJsonSignerAddress) { | ||
toaster.create("info", "Please connect your wallet to collect this reward."); | ||
buttonController.hideAll(); | ||
console.error("Wallet not connected"); | ||
return null; | ||
} | ||
|
||
return signer; | ||
if (walletAddress !== newJsonSignerAddress) { | ||
// although this should never happen just use the wallet provider if it does | ||
return walletSigner; | ||
} else { | ||
return rpcSigner; | ||
} | ||
} catch (error: unknown) { | ||
if (error instanceof Error) { | ||
console.error(error); | ||
if (error?.message?.includes("missing provider")) { | ||
toaster.create("info", "Please use a web3 enabled browser to collect this reward."); | ||
} else { | ||
toaster.create("info", "Please connect your wallet to collect this reward."); | ||
connectErrorHandler(error); | ||
} | ||
return null; | ||
} | ||
|
||
function connectErrorHandler(error: unknown) { | ||
if (error instanceof Error) { | ||
console.error(error); | ||
if (error?.message?.includes("missing provider")) { | ||
// mobile browsers don't really support window.ethereum | ||
const mediaQuery = window.matchMedia("(max-width: 768px)"); | ||
|
||
if (mediaQuery.matches) { | ||
// "Please use a mobile-friendly Web3 or desktop browser to collect this reward." push to desktop if possible? | ||
toaster.create("warning", "Please use a mobile-friendly Web3 browser such as MetaMask to collect this reward", Infinity); | ||
} else if (!window.ethereum) { | ||
toaster.create("warning", "Please use a web3 enabled browser to collect this reward.", Infinity); | ||
buttonController.hideAll(); | ||
} | ||
} else { | ||
toaster.create("error", error.message); | ||
} | ||
return null; | ||
} else { | ||
toaster.create("error", "An unknown error occurred."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2700,7 +2700,7 @@ esutils@^2.0.2: | |
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64" | ||
integrity sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g== | ||
|
||
ethers@^5.3.1, ethers@^5.7.2: | ||
ethers@5.7.2, ethers@^5.3.1: | ||
version "5.7.2" | ||
resolved "https://registry.yarnpkg.com/ethers/-/ethers-5.7.2.tgz#3a7deeabbb8c030d4126b24f84e525466145872e" | ||
integrity sha512-wswUsmWo1aOK8rR7DIKiWSw9DbLWe6x98Jrn8wcTflTVvaXhAMaB5zGAXy0GYQEQp9iO1iSHWVyARQm11zUtyg== | ||
|
@@ -3974,6 +3974,11 @@ [email protected], minimist@^1.2.5, minimist@^1.2.6: | |
resolved "https://registry.yarnpkg.com/minipass/-/minipass-7.0.4.tgz#dbce03740f50a4786ba994c1fb908844d27b038c" | ||
integrity sha512-jYofLM5Dam9279rdkWzqHozUo4ybjdZmCsDHePy5V/PbBcVMiSZR97gmAy45aqi8CK1lG2ECd356FU86avfwUQ== | ||
|
||
mipd@^0.0.7: | ||
version "0.0.7" | ||
resolved "https://registry.yarnpkg.com/mipd/-/mipd-0.0.7.tgz#bb5559e21fa18dc3d9fe1c08902ef14b7ce32fd9" | ||
integrity sha512-aAPZPNDQ3uMTdKbuO2YmAw2TxLHO0moa4YKAyETM/DTj5FloZo+a+8tU+iv4GmW+sOxKLSRwcSFuczk+Cpt6fg== | ||
|
||
[email protected]: | ||
version "2.1.2" | ||
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009" | ||
|