Move reactive config part into session state

This PR moves the signer "clones" the signer property from the `Config`
object into the `session` state, to allow Svelte reactivity, e.g. in the
Testnet Faucet, where we rely on `config.signer` but since it only
changes when the `Config` object in `App.svelte` changes we can't react
on it.

For now I only cloned the signer property, if we see in the future that
there are other properties that need to change with session we can add
them.

Signed-off-by: Sebastian Martinez <sebastinez@me.com>
Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
Sebastian Martinez
2022-02-02 15:25:58 +01:00
committed by Alexis Sellier
parent eb3b683935
commit e3de337769
5 changed files with 71 additions and 42 deletions
+23 -15
View File
@@ -1,9 +1,9 @@
<script lang="ts">
import type { Config } from "@app/config";
import type { BigNumber } from "@ethersproject/bignumber";
import { session } from "@app/session";
import { toWei } from "@app/utils";
import { formatEther } from "@ethersproject/units";
import { onMount } from "svelte";
import { navigate } from "svelte-routing";
import { getMaxWithdrawAmount, lastWithdrawalByUser, calculateTimeLock } from "./lib";
@@ -21,22 +21,30 @@
}
async function isAbleToWithdraw(amount: string): Promise<[boolean, string?]> {
if (!amount || amount === "0") { return [false, "Not able to withdraw zero tokens"]; }
if (toWei(amount).gt(maxWithdrawAmount)) return [false, `Reduce amount, max withdrawal is ${formatEther(maxWithdrawAmount)}`];
let currentTime = new Date().getTime();
let timelock = await calculateTimeLock(amount, config);
// Converting a 10 digit to 13 digit timestamp by multiplying by 1000
// since JS doesn't display a correct Date string when passing a 10 digit timestamp.
let nextAvailableWithdraw = lastWithdrawal.add(timelock).mul(1000);
if (nextAvailableWithdraw.gt(currentTime)) return [false, `Not ready to withdraw, return after ${new Date(nextAvailableWithdraw.toNumber()).toLocaleString('en-GB')}`];
try {
if (!$session) { return [false]; }
if (!amount || amount === "0") { return [false, "Not able to withdraw zero tokens"]; }
if (toWei(amount).gt(maxWithdrawAmount)) return [false, `Reduce amount, max withdrawal is ${formatEther(maxWithdrawAmount)}`];
let currentTime = new Date().getTime();
let timelock = await calculateTimeLock(amount, $session.config.signer, config);
// Converting a 10 digit to 13 digit timestamp by multiplying by 1000
// since JS doesn't display a correct Date string when passing a 10 digit timestamp.
let nextAvailableWithdraw = lastWithdrawal.add(timelock).mul(1000);
if (nextAvailableWithdraw.gt(currentTime)) return [false, `Not ready to withdraw, return after ${new Date(nextAvailableWithdraw.toNumber()).toLocaleString('en-GB')}`];
return [true];
return [true];
} catch (e: any) {
console.error(e);
error = e.message;
return [false];
}
}
onMount(async () => {
maxWithdrawAmount = await getMaxWithdrawAmount(config);
lastWithdrawal = await lastWithdrawalByUser(config);
});
$: if ($session) {
getMaxWithdrawAmount($session.config.signer, config).then(x => maxWithdrawAmount = x);
lastWithdrawalByUser($session.config.signer, config).then(x => lastWithdrawal = x);
}
</script>
<style>
@@ -89,7 +97,7 @@
To get RAD tokens on <strong>{config.network.name}</strong>, please
check the known exchanges.
</div>
{:else if !config.signer}
{:else if !$session}
<div class="input-caption">
To get RAD tokens on <strong>{config.network.name}</strong>, please
connect your wallet.
+9 -5
View File
@@ -23,11 +23,15 @@
onMount(async () => {
try {
state.status = Status.Signing;
const tx = await withdraw(amount, config);
state.status = Status.Pending;
await tx.wait();
state.status = Status.Success;
if ($session) {
state.status = Status.Signing;
const tx = await withdraw(amount, $session.config.signer, config);
state.status = Status.Pending;
await tx.wait();
state.status = Status.Success;
} else {
back();
}
} catch (e: any) {
console.error(e);
error = e;
+17 -13
View File
@@ -4,52 +4,56 @@ import type { Config } from '@app/config';
import { assert } from '@app/error';
import type { TransactionResponse } from '@ethersproject/providers';
import { toWei } from '@app/utils';
import type { WalletConnectSigner } from "@app/WalletConnectSigner";
import type { TypedDataSigner } from '@ethersproject/abstract-signer';
export async function withdraw(amount: string, config: Config): Promise<TransactionResponse> {
assert(config.signer);
type Signer = ethers.Signer & TypedDataSigner | WalletConnectSigner | null;
export async function withdraw(amount: string, signer: Signer, config: Config): Promise<TransactionResponse> {
assert(signer);
const faucet = new ethers.Contract(
config.radToken.faucet,
config.abi.faucet,
config.signer
signer
);
return faucet.withdraw(config.radToken.address, toWei(amount));
}
export async function getMaxWithdrawAmount(config: Config): Promise<ethers.BigNumber> {
assert(config.signer);
export async function getMaxWithdrawAmount(signer: Signer, config: Config): Promise<ethers.BigNumber> {
assert(signer);
const faucet = new ethers.Contract(
config.radToken.faucet,
config.abi.faucet,
config.signer
signer
);
return faucet.maxWithdrawAmount();
}
export async function calculateTimeLock(amount: string, config: Config): Promise<ethers.BigNumber> {
assert(config.signer);
export async function calculateTimeLock(amount: string, signer: Signer, config: Config): Promise<ethers.BigNumber> {
assert(signer);
const faucet = new ethers.Contract(
config.radToken.faucet,
config.abi.faucet,
config.signer
signer
);
return faucet.calculateTimeLock(toWei(amount));
}
export async function lastWithdrawalByUser(config: Config): Promise<ethers.BigNumber> {
assert(config.signer);
export async function lastWithdrawalByUser(signer: Signer, config: Config): Promise<ethers.BigNumber> {
assert(signer);
const address = config.signer.getAddress();
const address = signer.getAddress();
const faucet = new ethers.Contract(
config.radToken.faucet,
config.abi.faucet,
config.signer
signer
);
return faucet.lastWithdrawalByUser(address);