Switch to TypeScript

This commit is contained in:
Alexis Sellier
2021-04-27 15:22:22 +02:00
parent 52522d242f
commit a34b26384c
19 changed files with 1126 additions and 408 deletions
+971 -308
View File
File diff suppressed because it is too large Load Diff
+7 -1
View File
@@ -2,13 +2,19 @@
"scripts": {
"start": "snowpack dev",
"build": "snowpack build",
"check": "svelte-check",
"test": "echo \"This template does not include a test runner by default.\" && exit 1"
},
"devDependencies": {
"@snowpack/plugin-svelte": "^3.5.2",
"snowpack": "^3.0.1"
"@tsconfig/svelte": "^1.0.10",
"snowpack": "^3.0.1",
"svelte-check": "^1.5.2",
"svelte-preprocess": "^4.7.2",
"typescript": "^4.2.4"
},
"dependencies": {
"@snowpack/plugin-typescript": "^1.2.1",
"ethers": "^5.0.31",
"svelte": "^3.32.3",
"svelte-routing": "^1.5.0"
+2 -1
View File
@@ -8,7 +8,8 @@ module.exports = {
'@app': './src',
},
plugins: [
'@snowpack/plugin-svelte'
'@snowpack/plugin-svelte',
'@snowpack/plugin-typescript',
],
routes: [
/* Enable an SPA Fallback in development: */
+4 -8
View File
@@ -1,10 +1,10 @@
<script lang="javascript">
<script lang="typescript">
// TODO: Navigating directly to /vesting doesn't work.
import { ethers } from 'ethers';
import { get } from 'svelte/store';
import { Router, Link, Route } from "svelte-routing";
import { getConfig } from '@app/config.js';
import { session } from '@app/session.js';
import { getConfig } from '@app/config';
import { session } from '@app/session';
import Vesting from '@app/base/vesting/Vesting.svelte';
import Register from '@app/base/register/Register.svelte';
@@ -16,16 +16,12 @@
function handleKeydown(event) {
if (event.key === 'Enter') {
document.querySelector('button.primary').click();
(document.querySelector('button.primary') as HTMLElement).click();
}
}
</script>
<style>
main {
padding-top: 2rem;
align-self: center;
}
.wrapper {
display: flex;
align-items: center;
+4 -4
View File
@@ -1,6 +1,6 @@
<script lang="javascript">
<script lang="typescript">
import { derived } from "svelte/store";
import { CONNECTION, session, connectWallet } from "./session.js";
import { Connection, session, connectWallet } from "./session";
export let caption = "Connect";
export let className = "";
@@ -8,8 +8,8 @@
let walletUnavailable = !window.ethereum;
const connecting = derived(session, $s => {
return $s.connection === CONNECTION.CONNECTING;
const connecting = derived(session, ($s) => {
return $s.connection === Connection.Connecting;
});
</script>
+3 -7
View File
@@ -1,12 +1,12 @@
<script lang="javascript">
<script lang="typescript">
// TODO: Shorten tx hash
// TODO: Link to correct network on etherscan
import { derived } from "svelte/store";
import { ethers } from "ethers";
import { link } from "svelte-routing";
import { formatBalance } from "@app/utils";
import { CONNECTION, session, connectWallet, disconnectWallet, shortAddress } from "./session.js";
import { error, ERROR } from './error.js';
import { session, disconnectWallet, shortAddress } from "./session";
import { error, ERROR } from './error';
import Logo from './Logo.svelte';
import Connect from './Connect.svelte';
@@ -59,10 +59,6 @@
margin-left: 2rem;
width: 9.75rem;
}
.indicator {
font-size: 0.5rem;
margin-left: 0.5rem;
}
</style>
{#if $error}
+1 -1
View File
@@ -1,4 +1,4 @@
<script>
<script lang="typescript">
export let style = "";
</script>
+23 -19
View File
@@ -1,16 +1,22 @@
<script lang="javascript">
import { ethers } from 'ethers';
<script lang="typescript">
import { onMount } from 'svelte';
import { get } from 'svelte/store';
import { error } from '@app/error.js';
import { session } from '@app/session.js';
import { registrar, registerName, registrationFee } from './registrar.js';
import { STATE, state } from './state.js';
import { ethers } from 'ethers';
import { error } from '@app/error';
import { session } from '@app/session';
import { registrar, registerName, registrationFee } from './registrar';
import { State, state } from './state';
import RegisterButton from './RegisterButton.svelte';
export let config;
let subdomain = "";
let input;
onMount(() => {
input.focus();
});
async function getFee(cfg) {
let fee = await registrationFee(cfg);
@@ -25,8 +31,8 @@
}
input.subdomain {
margin-right: 0;
border-radius-top-right: 0;
border-radius-bottom-right: 0;
border-top-right-radius: 0;
border-bottom-right-radius: 0;
border-radius: var(--border-radius) 0 0 var(--border-radius);
border-right: none;
}
@@ -77,13 +83,10 @@
border: 1px solid var(--color-secondary);
border-left: none;
}
.register {
margin: 1rem;
}
</style>
<main>
{#if $state === STATE.IDLE || $state === STATE.CHECKING_AVAILABILITY}
{#if $state === State.Idle || $state === State.CheckingAvailability}
<div class="input-caption">
Register a <strong>radicle.eth</strong> name
</div>
@@ -91,11 +94,12 @@
<span class="name">
<div>
<input
autofocus
bind:this={input}
bind:value={subdomain}
placeholder=""
class="subdomain"
disabled={$state === STATE.CHECKING_AVAILABILITY}
type="text" bind:value={subdomain}
disabled={$state === State.CheckingAvailability}
type="text"
/>
<span class="root">.radicle.eth</span>
</div>
@@ -107,11 +111,11 @@
<div class="modal-title">
{subdomain}.radicle.eth
</div>
{#if $state === STATE.REGISTERED}
{#if $state === State.Registered}
<div class="available">The name <span class="domain">{subdomain}</span> has been successfully registered to {$session.address}.</div>
{:else if $state === STATE.NAME_AVAILABLE}
{:else if $state === State.NameAvailable}
<div class="available">The name <span class="domain">{subdomain}</span> is available for registration.</div>
{:else if $state === STATE.APPROVING}
{:else if $state === State.Approving}
<div class="available">
Approving Radicle for {#await getFee(config)}
?
@@ -119,7 +123,7 @@
{fee}
{/await} <strong>RAD</strong>...
</div>
{:else if $state == STATE.NAME_UNAVAILABLE}
{:else if $state == State.NameUnavailable}
<div class="available">The name <span class="domain">{subdomain}</span> is not available for registration.</div>
{/if}
<div class="modal-actions">
+20 -20
View File
@@ -1,9 +1,9 @@
<script lang="javascript">
<script lang="typescript">
import { get } from 'svelte/store';
import { error } from '@app/error.js';
import { session } from '@app/session.js';
import { STATE, state } from './state.js';
import { registrar, registerName } from './registrar.js';
import { error } from '@app/error';
import { session } from '@app/session';
import { State, state } from './state';
import { registrar, registerName } from './registrar';
import Connect from '@app/Connect.svelte';
@@ -27,16 +27,16 @@
async function checkAvailability() {
registrar(config).available(subdomain).then(isAvailable => {
if (isAvailable) {
state.set(STATE.NAME_AVAILABLE);
state.set(State.NameAvailable);
} else {
state.set(STATE.NAME_UNAVAILABLE);
state.set(State.NameUnavailable);
}
});
state.set(STATE.CHECKING_AVAILABILITY);
state.set(State.CheckingAvailability);
}
function cancel() {
state.set(STATE.IDLE);
state.set(State.Idle);
error.set(null);
}
</script>
@@ -47,16 +47,16 @@
}
</style>
{#if $state >= STATE.NAME_AVAILABLE && $state < STATE.REGISTERED}
{#if $state >= State.NameAvailable && $state < State.Registered}
{#if $session.address}
<button on:click={register} disabled={$state > STATE.NAME_AVAILABLE} class="primary register">
{#if $state === STATE.APPROVING}
<button on:click={register} disabled={$state > State.NameAvailable} class="primary register">
{#if $state === State.Approving}
Approving...
{:else if $state === STATE.COMMITTING}
{:else if $state === State.Committing}
Committing...
{:else if $state === STATE.WAITING_TO_REGISTER}
{:else if $state === State.WaitingToRegister}
Waiting...
{:else if $state === STATE.REGISTERING}
{:else if $state === State.Registering}
Registering...
{:else}
Begin registration &rarr;
@@ -68,19 +68,19 @@
<button on:click={cancel} class="cancel text">
Cancel
</button>
{:else if $state === STATE.REGISTERED}
<button on:click={() => state.set(STATE.IDLE)}>
{:else if $state === State.Registered}
<button on:click={() => state.set(State.Idle)}>
Done
</button>
{:else if $state === STATE.NAME_UNAVAILABLE}
<button on:click={() => state.set(STATE.IDLE)}>
{:else if $state === State.NameUnavailable}
<button on:click={() => state.set(State.Idle)}>
Back
</button>
{:else if subdomain == ""}
<button disabled class="primary register">
Check
</button>
{:else if $state === STATE.CHECKING_AVAILABILITY}
{:else if $state === State.CheckingAvailability}
<button disabled class="primary register" data-waiting>
Check
</button>
@@ -1,9 +1,9 @@
// TODO: Show "look at your wallet" / "confirm tx" before state change.
// TODO: Two registration actions with same label
import { ethers } from "ethers";
import { STATE, state } from './state.js';
import { approveSpender, updateBalance } from '@app/session.js';
import { ERROR } from '@app/error.js';
import { State, state } from './state';
import { approveSpender, updateBalance } from '@app/session';
import { ERROR } from '@app/error';
const registrarAbi = [
{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bytes32","name":"commitment","type":"bytes32"},{"indexed":false,"internalType":"uint256","name":"blockNumber","type":"uint256"}],"name":"CommitmentMade","type":"event"},
@@ -44,7 +44,7 @@ export async function registerName(name, owner, config) {
}
async function approveRegistrar(owner, config) {
state.set(STATE.APPROVING);
state.set(State.Approving);
const amount = await registrationFee(config);
await approveSpender(config.registrar.address, amount, config);
@@ -68,7 +68,7 @@ async function commitAndRegister(name, owner, config) {
}
async function commit(commitment, fee, minAge, config) {
state.set(STATE.COMMITTING);
state.set(State.Committing);
const signer = config.provider.getSigner();
const tx = await registrar(config)
@@ -80,12 +80,12 @@ async function commit(commitment, fee, minAge, config) {
updateBalance(fee.mul(-1));
// TODO: Getting "commitment too new"
state.set(STATE.WAITING_TO_REGISTER);
state.set(State.WaitingToRegister);
await tx.wait(minAge + 1);
}
async function register(name, owner, salt, config) {
state.set(STATE.REGISTERING);
state.set(State.Registering);
const signer = config.provider.getSigner();
const tx = await registrar(config).connect(signer).register(
@@ -96,7 +96,7 @@ async function register(name, owner, salt, config) {
try {
await tx.wait();
window.localStorage.clear();
state.set(STATE.REGISTERED);
state.set(State.Registered);
} catch (e) {
throw { type: ERROR.TRANSACTION_FAILED, hash: tx.hash };
}
@@ -106,7 +106,7 @@ function makeCommitment(name, owner, salt) {
let bytes = ethers.utils.concat([
ethers.utils.toUtf8Bytes(name),
ethers.utils.getAddress(owner),
ethers.BigNumber.from(salt),
ethers.BigNumber.from(salt).toHexString(),
]);
return ethers.utils.keccak256(bytes);
}
-16
View File
@@ -1,16 +0,0 @@
import { derived, writable } from "svelte/store";
export const STATE = {
ERROR: -1,
IDLE: 0,
CHECKING_AVAILABILITY: 1,
NAME_UNAVAILABLE: 2,
NAME_AVAILABLE: 3,
APPROVING: 4,
COMMITTING: 5,
WAITING_TO_REGISTER: 6,
REGISTERING: 7,
REGISTERED: 8,
};
export const state = writable(STATE.IDLE);
+16
View File
@@ -0,0 +1,16 @@
import { derived, writable } from "svelte/store";
export enum State {
Error = -1,
Idle,
CheckingAvailability,
NameUnavailable,
NameAvailable,
Approving,
Committing,
WaitingToRegister,
Registering,
Registered,
}
export const state = writable(State.Idle);
+12 -4
View File
@@ -1,10 +1,17 @@
<script lang="javascript">
import { ethers } from 'ethers';
<script lang="typescript">
import { onMount } from 'svelte';
import { get, derived, writable } from 'svelte/store';
import { ethers } from 'ethers';
import { session, shortAddress } from '@app/session.js';
import { STATE, state } from './state.js';
import { getInfo, withdrawVested } from './vesting.js';
let input;
onMount(() => {
input.focus();
});
export let config = null;
let contractAddress = "";
@@ -91,12 +98,13 @@
<span class="name">
<div>
<input
autofocus
size="40"
placeholder=""
class="subdomain"
disabled={$state === STATE.LOADING}
type="text" bind:value={contractAddress}
type="text"
bind:this={input}
bind:value={contractAddress}
/>
</div>
</span>
+6
View File
@@ -1,5 +1,11 @@
import { ethers } from "ethers";
declare global {
interface Window {
ethereum: any;
}
}
const config = {
homestead: {
registrar: {
View File
+16 -10
View File
@@ -1,15 +1,21 @@
import { get, writable } from "svelte/store";
import { get, writable, Writable } from "svelte/store";
import { ethers } from "ethers";
import { getConfig } from "./config.js";
import { getConfig } from "./config";
export const CONNECTION = {
DISCONNECTED: 0,
CONNECTING: 1,
CONNECTED: 2
export enum Connection {
Disconnected,
Connecting,
Connected
}
export type Session = {
connection: Connection
address?: string
tokenBalance?: any
};
export const session = writable({
connection: CONNECTION.DISCONNECTED,
export const session: Writable<Session> = writable({
connection: Connection.Disconnected,
});
session.subscribe(s => {
@@ -23,7 +29,7 @@ const tokenAbi = [
];
export async function connectWallet() {
session.set({ connection: CONNECTION.CONNECTING });
session.set({ connection: Connection.Connecting });
// TODO: This hangs on Brave, if you have to unlock your wallet..
try {
@@ -44,7 +50,7 @@ export async function connectWallet() {
session.set({
address: addr,
tokenBalance: tokenBalance,
connection: CONNECTION.CONNECTED
connection: Connection.Connected
});
} catch (e) {
console.error(e);
View File
+9
View File
@@ -0,0 +1,9 @@
const autoPreprocess = require('svelte-preprocess');
module.exports = {
preprocess: autoPreprocess({
defaults: {
script: 'typescript',
},
}),
};
+23
View File
@@ -0,0 +1,23 @@
{
"extends": "@tsconfig/svelte/tsconfig.json",
"include": [
"src",
"types"
],
"exclude": [
"node_modules/*"
],
"compilerOptions": {
"target": "es2019",
"types": [
"svelte"
],
"moduleResolution": "node",
"paths": {
"@app/*": ["./src/*"]
}
},
"strict": true,
"skipLibCheck": true,
"allowSyntheticDefaultImports": true
}