Radish alpha
r
Radicle web interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
Always try to use Alchemy as provider
Alexis Sellier committed 4 years ago
commit b2e46122de5ddb0aff7c09920d9fd12b480cf84f
parent 48ead9656f93d2de0ac5012d3a4546e261fee61e
3 files changed +34 -18
modified 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"
modified 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 = (<Record<string, any>> 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<Config> {
  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);

modified src/utils.ts
@@ -186,3 +186,13 @@ export async function getSafe(address: string, config: Config): Promise<Safe | n
    threshold: json.threshold
  };
}
+

+
// Get token balances for an address.
+
export async function getTokens(address: string, config: Config):
+
  Promise<Array<{ tokenName: string, tokenLogo: string }>>
+
{
+
  const result = await config.provider.send("alchemy_getTokenBalances", [address, config.tokens]);
+

+
  // TODO
+
  return [];
+
}