Move rad token contract to config

This commit is contained in:
Alexis Sellier
2021-06-16 12:37:54 +02:00
parent f8e0e181dd
commit ebf99178f4
3 changed files with 12 additions and 18 deletions
+2 -6
View File
@@ -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)
+6
View File
@@ -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,
);
}
}
+4 -12
View File
@@ -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();