From b2e46122de5ddb0aff7c09920d9fd12b480cf84f Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Fri, 4 Jun 2021 14:33:20 +0200 Subject: [PATCH] Always try to use Alchemy as provider --- src/config.json | 9 ++++++--- src/config.ts | 33 ++++++++++++++++++--------------- src/utils.ts | 10 ++++++++++ 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/config.json b/src/config.json index ad72e7d90..89508decb 100644 --- a/src/config.json +++ b/src/config.json @@ -13,7 +13,8 @@ "safe": { "api": "https://safe-transaction.gnosis.io/api/v1", "viewer": "https://gnosis-safe.io/app/#/safes" - } + }, + "tokens": [] }, "ropsten": { "registrar": { @@ -33,7 +34,8 @@ "safe": { "api": null, "viewer": null - } + }, + "tokens": [] }, "rinkeby": { "registrar": { @@ -53,7 +55,8 @@ "safe": { "api": "https://safe-transaction.rinkeby.gnosis.io/api/v1", "viewer": "https://rinkeby.gnosis-safe.io/app/#/safes" - } + }, + "tokens": [] }, "seed": { "url": "https://sprout.radicle.xyz" diff --git a/src/config.ts b/src/config.ts index 45c6b9b6d..24055a36a 100644 --- a/src/config.ts +++ b/src/config.ts @@ -26,20 +26,15 @@ export class Config { signer: ethers.Signer & TypedDataSigner | null; seed: { url: string }; safe: { api: string | null, viewer: string | null }; - abi: { [contract:string]: string[] } + abi: { [contract: string]: string[] } + tokens: string[]; constructor( network: { name: string, chainId: number }, - provider: ethers.providers.JsonRpcProvider + provider: ethers.providers.JsonRpcProvider, + signer: ethers.Signer & TypedDataSigner | null, ) { let cfg = (> config)[network.name]; - let signer = null; - - try { - signer = provider.getSigner(); - } catch (e) { - console.log(e.message); - } this.network = network; this.registrar = cfg.registrar; @@ -52,6 +47,7 @@ export class Config { this.gasLimits = gasLimits; this.seed = config.seed; this.abi = config.abi; + this.tokens = cfg.tokens; } } @@ -67,18 +63,25 @@ function isMetamaskInstalled(): boolean { export async function getConfig(): Promise { let config: Config; + const alchemyApiKey = import.meta.env.RADICLE_ALCHEMY_API_KEY; if (isMetamaskInstalled()) { - const provider = new ethers.providers.Web3Provider(window.ethereum); - const network = await provider.ready; + // If we have Metamask, use it as the signer, but try to use Alchemy + // as the provider. + const metamask = new ethers.providers.Web3Provider(window.ethereum); + const network = await metamask.ready; + const provider = alchemyApiKey ? + new ethers.providers.AlchemyProvider(network.name, alchemyApiKey) + : metamask; - config = new Config(network, provider); + config = new Config(network, provider, metamask.getSigner()); } else { + // If we don't have Metamask, we use Alchemy as the provider, and we don't + // provide a signer. We default to Homestead. const network = { name: "homestead", chainId: 1 }; - const apiKey = import.meta.env.RADICLE_ALCHEMY_API_KEY; - const provider = new ethers.providers.AlchemyProvider(network.name, apiKey); + const provider = new ethers.providers.AlchemyProvider(network.name, alchemyApiKey); - config = new Config(network, provider); + config = new Config(network, provider, null); } console.log(config); diff --git a/src/utils.ts b/src/utils.ts index eef3ad7a1..4e855423d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -186,3 +186,13 @@ export async function getSafe(address: string, config: Config): Promise> +{ + const result = await config.provider.send("alchemy_getTokenBalances", [address, config.tokens]); + + // TODO + return []; +}