Files
radicle-explorer/src/base/faucet/Index.svelte
T
Sebastian Martinez be0ff349df Move signer into session store
The Metamask and WalletConnect signer are updated one any account
change, but currently we store them only in the static config object.
This commit moves this to the session store to improve this behaviour

Signed-off-by: Sebastian Martinez <sebastinez@me.com>
2022-05-20 13:10:53 +02:00

136 lines
4.0 KiB
Svelte

<script lang="ts">
import type { Config } from "@app/config";
import type { BigNumber } from "@ethersproject/bignumber";
import { session } from "@app/session";
import { setOpenGraphMetaTag, toWei } from "@app/utils";
import { formatEther } from "@ethersproject/units";
import { navigate } from "svelte-routing";
import { getMaxWithdrawAmount, lastWithdrawalByUser, calculateTimeLock } from "./lib";
export let config: Config;
let amount: string;
let maxWithdrawAmount: BigNumber;
let lastWithdrawal: BigNumber;
let error: string | undefined;
setOpenGraphMetaTag([
{ prop: "og:title", content: "Radicle Faucet" },
{ prop: "og:description", content: "Rinkeby Testnet Faucet" },
{ prop: "og:url", content: window.location.href }
]);
async function withdraw() {
const [state, message] = await isAbleToWithdraw(amount);
if (state === true) navigate("/faucet/withdraw", { state: { amount } });
else error = message;
}
async function isAbleToWithdraw(amount: string): Promise<[boolean, string?]> {
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];
} catch (e: any) {
console.error(e);
error = e.message;
return [false];
}
}
$: if ($session) {
getMaxWithdrawAmount($session.config.signer, config).then(x => maxWithdrawAmount = x);
lastWithdrawalByUser($session.config.signer, config).then(x => lastWithdrawal = x);
}
</script>
<style>
div.input-caption {
font-size: 1.25rem;
text-align: left;
margin-left: 1.5rem;
padding-left: 1.5rem;
color: var(--color-secondary);
}
div.input-main {
display: flex;
align-items: flex-start;
flex-direction: column;
margin: 1rem 1.5rem 0rem;
color: var(--color-secondary);
}
input[type="text"] {
margin: 0;
margin-right: 1.5rem;
}
.name {
display: flex;
flex-direction: row;
margin: 1rem;
margin-bottom: 0;
}
.error {
padding-left: 1rem;
padding-top: 1rem;
}
.description :global(p) {
padding: 0;
margin: 0;
}
.description.invalid {
color: var(--color-negative) !important;
}
</style>
<svelte:head>
<title>Radicle: Faucet</title>
</svelte:head>
<main class="off-centered">
<div>
{#if config.network.name == "homestead"}
<div class="input-caption">
To get RAD tokens on <strong>{config.network.name}</strong>, please
check the known exchanges.
</div>
{:else if !$session}
<div class="input-caption">
To get RAD tokens on <strong>{config.network.name}</strong>, please
connect your wallet.
</div>
{:else}
<div class="input-caption">
Obtain RAD tokens on <strong>{config.network.name}</strong>
</div>
<div class="input-main">
<div class="name">
<input
type="text"
placeholder="Set amount to withdraw"
bind:value={amount}
on:input={() => error = ""}
/>
<button disabled={false} class="primary" on:click={withdraw}>
Withdraw
</button>
</div>
{#if error}
<div class="error description invalid text-small faded">
{error}
</div>
{/if}
</div>
{/if}
</div>
</main>