Radish alpha
r
Radicle web interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
Move rad token contract to config
Alexis Sellier committed 4 years ago
commit ebf99178f44886bcf0e9d3b4c58d923826163388
parent f8e0e181dd91b89c95da95bbf57f370d9047aa28
3 files changed +12 -18
modified src/base/registrations/registrar.ts
@@ -67,10 +67,6 @@ export function registrar(config: Config): ethers.Contract {
  return new ethers.Contract(config.registrar.address, config.abi.registrar, config.provider);
}

-
export function radToken(config: Config): ethers.Contract {
-
  return new ethers.Contract(config.radToken.address, config.abi.token, config.provider);
-
}
-

export async function registrationFee(config: Config): Promise<BigNumber> {
  return await registrar(config).registrationFeeRad();
}
@@ -101,7 +97,7 @@ async function commitAndRegister(name: string, owner: string, config: Config): P
  const fee = await registrationFee(config);
  // Avoids gas spent by the owner, trying to commit to a name and not having
  // enough RAD balance
-
  if ((await radToken(config).balanceOf(owner)).lt(fee)) {
+
  if ((await config.token.balanceOf(owner)).lt(fee)) {
    throw { type: Failure.InsufficientBalance, message: "Not enough RAD funds" };
  }

@@ -126,7 +122,7 @@ async function commit(commitment: string, fee: BigNumber, minAge: number, config
  const ownerAddr = await owner.getAddress();
  const spender = config.registrar.address;
  const deadline = ethers.BigNumber.from(unixTime()).add(3600); // Expire one hour from now.
-
  const token = session.token(config);
+
  const token = config.token;
  const signature = await permitSignature(owner, token, spender, fee, deadline);
  const tx = await registrar(config)
    .connect(config.signer)
modified src/config.ts
@@ -29,6 +29,7 @@ export class Config {
  abi: { [contract: string]: string[] };
  seed: { api: string | null };
  tokens: string[];
+
  token: ethers.Contract;

  constructor(
    network: { name: string; chainId: number },
@@ -54,6 +55,11 @@ export class Config {
    this.gasLimits = gasLimits;
    this.abi = config.abi;
    this.tokens = cfg.tokens;
+
    this.token = new ethers.Contract(
+
      this.radToken.address,
+
      this.abi.token,
+
      this.provider,
+
    );
  }
}

modified src/session.ts
@@ -1,5 +1,4 @@
import { get, writable, derived, Readable } from "svelte/store";
-
import { ethers } from "ethers";
import type { BigNumber } from 'ethers';
import type { TransactionReceipt, TransactionResponse } from '@ethersproject/providers';
import { Config, getConfig } from "@app/config";
@@ -61,12 +60,11 @@ export const loadState = (initial: State): Store => {
        console.error(e);
      }

-
      const token = new ethers.Contract(config.radToken.address, config.abi.token, config.provider);
      const signer = config.provider.getSigner();
      const address = await signer.getAddress();

      try {
-
        const tokenBalance: BigNumber = await token.balanceOf(address);
+
        const tokenBalance: BigNumber = await config.token.balanceOf(address);
        store.set({
          connection: Connection.Connected,
          session: { address, tokenBalance, tx: null }
@@ -91,8 +89,7 @@ export const loadState = (initial: State): Store => {
      const addr = state.session.address;

      try {
-
        const token = new ethers.Contract(config.radToken.address, config.abi.token, config.provider);
-
        const tokenBalance: BigNumber = await token.balanceOf(addr);
+
        const tokenBalance: BigNumber = await config.token.balanceOf(addr);

        state.session.tokenBalance = tokenBalance;
        store.set(state);
@@ -200,22 +197,17 @@ state.subscribe(s => {
});

export async function approveSpender(spender: string, amount: BigNumber, config: Config): Promise<void> {
-
  const token = new ethers.Contract(config.radToken.address, config.abi.token, config.provider);
  const signer = config.provider.getSigner();
  const addr = await signer.getAddress();

-
  const allowance = await token.allowance(addr, spender);
+
  const allowance = await config.token.allowance(addr, spender);

  if (allowance < amount) {
-
    const tx = await token.connect(signer).approve(spender, amount);
+
    const tx = await config.token.connect(signer).approve(spender, amount);
    await tx.wait();
  }
}

-
export function token(config: Config): ethers.Contract {
-
  return new ethers.Contract(config.radToken.address, config.abi.token, config.provider);
-
}
-

export function disconnectWallet(): void {
  window.localStorage.removeItem("session");
  location.reload();