Improve vesting discovery and error messages

When loading a ETH address as vesting contract we show a cryptic error
message, which non tech-savy end users don't understand, this commit
changes it to something more human readable.

We allow users to enter and load a contract that probably isn't a
vesting contract.

The `getInfo` function in the `vesting` folder is very prone to possible
errors, and benefits from some parallelization between all the method
calls.

We allow hereby to enter a contract address in search and once on the
contract profile page we provide a link to the detailed vesting
information if available.

Signed-off-by: Sebastian Martinez <me@sebastinez.dev>
This commit is contained in:
Sebastian Martinez
2023-01-10 14:15:08 +01:00
parent 8df7d66419
commit b2c82b06bd
12 changed files with 489 additions and 321 deletions
+135 -6
View File
@@ -3,6 +3,8 @@
import type { Wallet } from "@app/lib/wallet";
import type { Seed, Stats } from "@app/lib/seed";
import type { ProjectInfo } from "@app/lib/project";
import type { VestingInfo } from "@app/lib/vesting";
import * as utils from "@app/lib/utils";
import Address from "@app/components/Address.svelte";
import Async from "@app/components/Async.svelte";
@@ -18,20 +20,31 @@
import RadicleId from "@app/components/RadicleId.svelte";
import SeedAddress from "@app/components/SeedAddress.svelte";
import SetName from "./SetName.svelte";
import Withdraw from "@app/views/vesting/Withdraw.svelte";
import { MissingReverseRecord, NotFoundError } from "@app/lib/error";
import { User, Profile, ProfileType } from "@app/lib/profile";
import { defaultNodePort } from "@app/lib/seed";
import {
getInfo,
getVestingContract,
handleEtherErrorState,
parseVestingPeriods,
} from "@app/lib/vesting";
import { onMount } from "svelte";
import { session } from "@app/lib/session";
export let wallet: Wallet;
export let addressOrName: string;
export let action: string | null = null;
let setNameForm: typeof SvelteComponent | null =
action === "setName" ? SetName : null;
let vestingInfo: VestingInfo | undefined = undefined;
let setNameForm: typeof SvelteComponent | undefined = undefined;
let withdrawVestingModal: typeof SvelteComponent | undefined = undefined;
const setName = () => {
setNameForm = SetName;
};
const withdrawVesting = () => {
withdrawVestingModal = Withdraw;
};
const getProjectsAndStats = async (
seed: Seed,
@@ -45,6 +58,49 @@
return { stats, projects };
};
// Refresh vestingInfo and close modal if addressOrName changes
$: {
vestingInfo = undefined;
withdrawVestingModal = undefined;
getInfo(addressOrName, wallet)
.then(info => {
vestingInfo = info;
})
.catch(() => {
console.warn("Not able to get vesting contract info");
});
}
onMount(async () => {
const addressType = await utils.identifyAddress(addressOrName, wallet);
if (addressType === utils.AddressType.Contract) {
try {
vestingInfo = await getInfo(addressOrName, wallet);
} catch (e) {
handleEtherErrorState(e, "Not able to get vesting contract info");
}
}
});
const vestingContract = getVestingContract(addressOrName, wallet);
wallet.provider.on("block", async () => {
if (vestingInfo) {
try {
const updatedAmounts = await Promise.all([
vestingContract.withdrawableBalance(),
vestingContract.withdrawn(),
]);
vestingInfo.withdrawableBalance = utils.formatBalance(
updatedAmounts[0],
);
vestingInfo.withdrawn = utils.formatBalance(updatedAmounts[1]);
} catch (e) {
handleEtherErrorState(e, "Not able to update the balance");
}
}
});
$: isUserAuthorized = (address: string): boolean | null => {
return $session && utils.isAddressEqual(address, $session.address);
};
@@ -75,7 +131,7 @@
}
.fields {
display: grid;
grid-template-columns: 5rem 4fr 2fr;
grid-template-columns: max-content 4fr 2fr;
gap: 1rem 2rem;
margin-bottom: 1rem;
}
@@ -116,7 +172,7 @@
padding: 1.5rem;
}
.fields {
grid-template-columns: 5rem auto;
grid-template-columns: max-content auto;
}
}
</style>
@@ -169,6 +225,14 @@
<Icon name="github" />
</a>
{/if}
{#if utils.isAddress(profile.address)}
<a
class="url"
title="Lookup address on Etherscan"
href={utils.explorerLink(profile.address, wallet)}>
<Icon name="etherscan" />
</a>
{/if}
</div>
</div>
</header>
@@ -255,6 +319,64 @@
{/if}
</div>
{/if}
{#if vestingInfo}
<div class="txt-highlight">Vesting Beneficiary</div>
<div style:display="flex" style:gap="1rem">
<Address address={vestingInfo.beneficiary} {wallet} resolve compact />
</div>
<div class="layout-desktop" />
<div class="txt-highlight">Allocation</div>
<div>
{vestingInfo.totalVesting}
<span class="txt-bold">{vestingInfo.symbol}</span>
</div>
<div class="layout-desktop" />
<div class="txt-highlight">Withdrawn</div>
<div>
{vestingInfo.withdrawn}
<span class="txt-bold">{vestingInfo.symbol}</span>
</div>
<div class="layout-desktop" />
<div class="txt-highlight">Withdrawable</div>
<div>
{vestingInfo.withdrawableBalance}
<span class="txt-bold">{vestingInfo.symbol}</span>
</div>
<div class="layout-desktop">
{#if isUserAuthorized(vestingInfo.beneficiary) && parseFloat(vestingInfo.withdrawableBalance) > 0}
<Button variant="secondary" size="small" on:click={withdrawVesting}>
Withdraw
</Button>
{/if}
</div>
<div class="txt-highlight">Start Time</div>
<div>
<span>
{parseVestingPeriods(vestingInfo.vestingStartTime)}
</span>
</div>
<div class="layout-desktop" />
<div class="txt-highlight">Cliff Period End</div>
<div>
<span>
{parseVestingPeriods(
vestingInfo.vestingStartTime,
vestingInfo.cliffPeriod,
)}
</span>
</div>
<div class="layout-desktop" />
<div class="txt-highlight">Vesting Period End</div>
<div>
<span>
{parseVestingPeriods(
vestingInfo.vestingStartTime,
vestingInfo.vestingPeriod,
)}
</span>
</div>
<div class="layout-desktop" />
{/if}
</div>
{#if profile.seed?.valid}
@@ -268,11 +390,18 @@
{/if}
</main>
<svelte:component
this={withdrawVestingModal}
info={vestingInfo}
contractAddress={addressOrName}
{wallet}
on:close={() => (withdrawVestingModal = undefined)} />
<svelte:component
this={setNameForm}
entity={new User(profile.address)}
{wallet}
on:close={() => (setNameForm = null)} />
on:close={() => (setNameForm = undefined)} />
{:catch err}
{#if err instanceof NotFoundError}
<NotFound
+15 -16
View File
@@ -18,24 +18,23 @@
: "";
const loadContract = async () => {
state.set("loading");
state.set({ type: "loading" });
try {
const info = await getInfo(contractAddress, wallet);
router.push({
resource: "vesting",
params: {
view: {
resource: "view",
params: { contract: contractAddress, info },
},
},
});
if (info) {
router.push({
resource: "profile",
params: { addressOrName: contractAddress },
});
} else {
validationMessage = "No vesting account found under this address.";
}
} catch (error) {
validationMessage =
"Couldn't load contract, check dev console for details.";
console.error(error);
console.warn(error);
}
state.set("idle");
state.set({ type: "idle" });
};
</script>
@@ -75,16 +74,16 @@
placeholder="Enter vesting contract address"
{valid}
{validationMessage}
loading={$state === "loading"}
disabled={$state === "loading"}
loading={$state.type === "loading"}
disabled={$state.type === "loading"}
on:submit={loadContract}
bind:value={contractAddress} />
<Button
on:click={loadContract}
variant="primary"
waiting={$state === "loading"}
disabled={!valid || $state === "loading"}>
waiting={$state.type === "loading"}
disabled={!valid || $state.type === "loading"}>
Load
</Button>
</div>
+3 -9
View File
@@ -1,22 +1,16 @@
<script lang="ts">
import type { Wallet } from "@app/lib/wallet";
import type { VestingRoute } from "@app/lib/router/definitions";
import type { Session } from "@app/lib/session";
import Form from "./Form.svelte";
import View from "./View.svelte";
import Form from "@app/views/vesting/Form.svelte";
import Profile from "@app/views/profiles/Profile.svelte";
export let activeRoute: VestingRoute;
export let wallet: Wallet;
export let session: Session | null;
</script>
{#if activeRoute.params.view.resource === "form"}
<Form {wallet} />
{:else if activeRoute.params.view.resource === "view"}
<View
{wallet}
{session}
info={activeRoute.params.view.params.info}
contractAddress={activeRoute.params.view.params.contract} />
<Profile {wallet} addressOrName={activeRoute.params.view.params.contract} />
{/if}
-160
View File
@@ -1,160 +0,0 @@
<script lang="ts">
import type { Session } from "@app/lib/session";
import type { Wallet } from "@app/lib/wallet";
import type { VestingInfo } from "@app/lib/vesting";
import * as router from "@app/lib/router";
import * as utils from "@app/lib/utils";
import Address from "@app/components/Address.svelte";
import Button from "@app/components/Button.svelte";
import Modal from "@app/components/Modal.svelte";
import { state, getInfo, withdrawVested } from "@app/lib/vesting";
import { onMount } from "svelte";
import ErrorModal from "@app/components/ErrorModal.svelte";
import Loading from "@app/components/Loading.svelte";
export let contractAddress: string;
export let info: VestingInfo | null = null;
export let session: Session | null;
export let wallet: Wallet;
let error: Error | undefined = undefined;
onMount(async () => {
if (!info) {
state.set("loading");
try {
info = await getInfo(contractAddress, wallet);
} catch (e) {
error = e as Error;
}
}
state.set("idle");
});
const parseVestingPeriods = (input: string[]): string => {
const total = input
.map(s => parseInt(s))
.reduce((prev, curr) => prev + curr, 0);
return new Date(total * 1000).toDateString();
};
</script>
<style>
table {
table-layout: fixed;
border-collapse: separate;
border-spacing: 2rem 0;
}
td {
text-align: left;
text-overflow: ellipsis;
}
</style>
<svelte:head>
<title>Radicle &ndash; Vesting</title>
</svelte:head>
{#if error}
<ErrorModal
title="Failed to obtain contract information"
message={error.message}
on:close={() => router.pop()} />
{:else if $state === "loading"}
<Loading center />
{:else if info}
{@const isBeneficiary =
session && utils.isAddressEqual(info.beneficiary, session.address)}
<Modal>
<span slot="title">
{contractAddress}
</span>
<span slot="body">
{#if $state === "withdrawn"}
Tokens successfully withdrawn to {utils.formatAddress(
info.beneficiary,
)}.
{:else}
<table>
<tr>
<td class="txt-highlight">Beneficiary</td>
<td>
<Address {wallet} address={info.beneficiary} compact resolve />
</td>
</tr>
<tr>
<td class="txt-highlight">Allocation</td>
<td>
{info.totalVesting}
<span class="txt-bold">{info.symbol}</span>
</td>
</tr>
<tr>
<td class="txt-highlight">Withdrawn</td>
<td>
{info.withdrawn}
<span class="txt-bold">{info.symbol}</span>
</td>
</tr>
<tr>
<td class="txt-highlight">Withdrawable</td>
<td>
{info.withdrawableBalance}
<span class="txt-bold">{info.symbol}</span>
</td>
</tr>
<tr>
<td class="txt-highlight">Start Time</td>
<td>
<span class="txt-bold">
{parseVestingPeriods([info.vestingStartTime])}
</span>
</td>
</tr>
<tr>
<td class="txt-highlight">Cliff Period End</td>
<td>
<span class="txt-bold">
{parseVestingPeriods([info.vestingStartTime, info.cliffPeriod])}
</span>
</td>
</tr>
<tr>
<td class="txt-highlight">Vesting Period End</td>
<td>
<span class="txt-bold">
{parseVestingPeriods([
info.vestingStartTime,
info.vestingPeriod,
])}
</span>
</td>
</tr>
</table>
{/if}
</span>
<span slot="actions">
{#if isBeneficiary}
{#if $state === "withdrawingSign"}
<Button disabled waiting={true} variant="primary">
Waiting for signature…
</Button>
{:else if $state === "withdrawing"}
<Button disabled waiting={true} variant="primary">
Withdrawing…
</Button>
{:else if $state === "idle"}
<Button
on:click={() => withdrawVested(contractAddress, wallet)}
variant="primary">
Withdraw
</Button>
{/if}
{/if}
<Button on:click={() => router.pop()} variant="primary">Back</Button>
</span>
</Modal>
{/if}
+72
View File
@@ -0,0 +1,72 @@
<script lang="ts" strictEvents>
import type { VestingInfo } from "@app/lib/vesting";
import type { Wallet } from "@app/lib/wallet";
import * as utils from "@app/lib/utils";
import Button from "@app/components/Button.svelte";
import ErrorModal from "@app/components/ErrorModal.svelte";
import Loading from "@app/components/Loading.svelte";
import Modal from "@app/components/Modal.svelte";
import { createEventDispatcher, onMount } from "svelte";
import { state } from "@app/lib/vesting";
import { withdrawVested } from "@app/lib/vesting";
export let contractAddress: string;
export let info: VestingInfo;
export let wallet: Wallet;
const dispatch = createEventDispatcher<{ close: never }>();
onMount(async () => {
await withdrawVested(contractAddress, wallet);
});
</script>
<style>
.actions {
display: flex;
justify-content: center;
flex-direction: row;
gap: 1rem;
}
</style>
{#if $state.type === "error"}
<ErrorModal
floating
title="Withdraw failed"
message={$state.error}
on:close={() => dispatch("close")} />
{:else}
<Modal on:close floating>
<span slot="title">
{utils.formatAddress(contractAddress)}
</span>
<span slot="subtitle">
{#if $state.type === "withdrawingSign"}
<span class="txt-missing">Waiting for a signature…</span>
{:else if $state.type === "withdrawing"}
<span class="txt-missing">Waiting for confirmation…</span>
{/if}
</span>
<span slot="body">
{#if $state.type === "withdrawn"}
<span>
Tokens have been withdrawn to <span class="txt-highlight">
{utils.formatAddress(info.beneficiary)}
</span>
</span>
{:else}
<Loading small center />
{/if}
</span>
<span class="actions" slot="actions">
<Button variant="foreground" on:click={() => dispatch("close")}>
Close
</Button>
</span>
</Modal>
{/if}