mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-16 03:40:37 +08:00
Get everything properly typed
This commit is contained in:
+3
-2
@@ -12,11 +12,12 @@
|
||||
import type { Session } from '@app/session';
|
||||
import Logo from './Logo.svelte';
|
||||
import Connect from './Connect.svelte';
|
||||
import type { Config } from '@app/config';
|
||||
|
||||
export let session: Session | null;
|
||||
export let config;
|
||||
export let config: Config;
|
||||
|
||||
let sessionButton = null;
|
||||
let sessionButton: HTMLElement | null = null;
|
||||
let sessionButtonHover = false;
|
||||
|
||||
$: address = session && session.address;
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
import { createEventDispatcher } from 'svelte';
|
||||
|
||||
export let floating = false;
|
||||
export let error = null;
|
||||
export let error: Error | null = null;
|
||||
|
||||
let dispatch = createEventDispatcher();
|
||||
|
||||
|
||||
@@ -15,8 +15,8 @@
|
||||
}
|
||||
|
||||
let state = State.Idle;
|
||||
let error = null;
|
||||
let org = null;
|
||||
let error: Error | null = null;
|
||||
let org: Org | null = null;
|
||||
|
||||
const dispatch = createEventDispatcher();
|
||||
const createOrg = async () => {
|
||||
|
||||
@@ -20,12 +20,16 @@ export class Org {
|
||||
this.safe = safe;
|
||||
}
|
||||
|
||||
static fromReceipt(receipt: ContractReceipt): Org {
|
||||
let event = receipt.events.find(e => e.event === 'OrgCreated');
|
||||
let address = event.args[0];
|
||||
let safe = event.args[1];
|
||||
static fromReceipt(receipt: ContractReceipt): Org | null {
|
||||
let event = receipt.events?.find(e => e.event === 'OrgCreated');
|
||||
|
||||
return new Org(address, safe);
|
||||
if (event && event.args) {
|
||||
let address = event.args[0];
|
||||
let safe = event.args[1];
|
||||
|
||||
return new Org(address, safe);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static async create(
|
||||
|
||||
@@ -6,12 +6,13 @@
|
||||
import { error } from '@app/error';
|
||||
import { session } from '@app/session';
|
||||
import CreateOrg from '@app/base/orgs/CreateOrg.svelte';
|
||||
import type { Config } from '@app/config';
|
||||
|
||||
enum State {
|
||||
Idle,
|
||||
}
|
||||
|
||||
export let config;
|
||||
export let config: Config;
|
||||
export const query = {};
|
||||
|
||||
let modal = null;
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
<script lang="typescript">
|
||||
import { Route } from "svelte-routing";
|
||||
import Orgs from '@app/base/orgs/Orgs.svelte';
|
||||
import type { Config } from '@app/config';
|
||||
|
||||
export let config;
|
||||
export let config: Config;
|
||||
export let query;
|
||||
</script>
|
||||
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
// TODO: Show "look at your wallet" / "confirm tx" before state change.
|
||||
// TODO: Two registration actions with same label
|
||||
import { ethers } from "ethers";
|
||||
import { ethers } from 'ethers';
|
||||
import type { BigNumber } from 'ethers';
|
||||
import { State, state } from './state';
|
||||
import * as session from '@app/session';
|
||||
import { Failure } from '@app/error';
|
||||
import type { Config } from '@app/config';
|
||||
|
||||
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"},
|
||||
@@ -20,15 +22,15 @@ const registrarAbi = [
|
||||
{"inputs":[{"internalType":"string","name":"name","type":"string"}],"name":"valid","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"pure","type":"function"}
|
||||
];
|
||||
|
||||
export function registrar(config) {
|
||||
export function registrar(config: Config) {
|
||||
return new ethers.Contract(config.registrar.address, registrarAbi, config.provider);
|
||||
}
|
||||
|
||||
export async function registrationFee(config) {
|
||||
export async function registrationFee(config: Config) {
|
||||
return await registrar(config).registrationFeeRad();
|
||||
}
|
||||
|
||||
export async function registerName(name, owner, config) {
|
||||
export async function registerName(name: string, owner: string, config: Config) {
|
||||
if (! name) return;
|
||||
|
||||
let commitmentJson = window.localStorage.getItem('commitment');
|
||||
@@ -47,14 +49,14 @@ export async function registerName(name, owner, config) {
|
||||
}
|
||||
}
|
||||
|
||||
async function approveRegistrar(owner, config) {
|
||||
async function approveRegistrar(owner: string, config: Config) {
|
||||
state.set(State.Approving);
|
||||
|
||||
const amount = await registrationFee(config);
|
||||
await session.approveSpender(config.registrar.address, amount, config);
|
||||
}
|
||||
|
||||
async function commitAndRegister(name, owner, config) {
|
||||
async function commitAndRegister(name: string, owner: string, config: Config) {
|
||||
let salt = ethers.utils.randomBytes(32);
|
||||
let minAge = (await registrar(config).minCommitmentAge()).toNumber();
|
||||
let fee = await registrationFee(config);
|
||||
@@ -71,14 +73,14 @@ async function commitAndRegister(name, owner, config) {
|
||||
await register(name, owner, salt, config);
|
||||
}
|
||||
|
||||
async function commit(commitment, fee, minAge, config) {
|
||||
async function commit(commitment: string, fee: BigNumber, minAge: number, config: Config) {
|
||||
state.set(State.Committing);
|
||||
|
||||
const signer = config.provider.getSigner();
|
||||
const tx = await registrar(config)
|
||||
.connect(signer)
|
||||
.commit(commitment, { gasLimit: 150000 })
|
||||
.catch(e => console.error(e));
|
||||
.catch((e: Error) => console.error(e));
|
||||
|
||||
await tx.wait(1);
|
||||
session.state.updateBalance(fee.mul(-1));
|
||||
@@ -88,7 +90,7 @@ async function commit(commitment, fee, minAge, config) {
|
||||
await tx.wait(minAge + 1);
|
||||
}
|
||||
|
||||
async function register(name, owner, salt, config) {
|
||||
async function register(name: string, owner: string, salt: Uint8Array, config: Config) {
|
||||
state.set(State.Registering);
|
||||
|
||||
const signer = config.provider.getSigner();
|
||||
@@ -102,7 +104,7 @@ async function register(name, owner, salt, config) {
|
||||
state.set(State.Registered);
|
||||
}
|
||||
|
||||
function makeCommitment(name, owner, salt) {
|
||||
function makeCommitment(name: string, owner: string, salt: Uint8Array): string {
|
||||
let bytes = ethers.utils.concat([
|
||||
ethers.utils.toUtf8Bytes(name),
|
||||
ethers.utils.getAddress(owner),
|
||||
|
||||
@@ -2,6 +2,7 @@ import { ethers } from "ethers";
|
||||
import { formatBalance } from "@app/utils";
|
||||
import * as session from "@app/session";
|
||||
import { State, state } from "./state";
|
||||
import type { Config } from "@app/config";
|
||||
|
||||
const abi = [
|
||||
"function token() view returns (address)",
|
||||
@@ -20,7 +21,7 @@ const tokenAbi = [
|
||||
"function symbol() view returns (string)",
|
||||
];
|
||||
|
||||
export async function withdrawVested(address, config) {
|
||||
export async function withdrawVested(address: string, config: Config) {
|
||||
const contract = new ethers.Contract(address, abi, config.provider);
|
||||
const signer = config.provider.getSigner();
|
||||
|
||||
@@ -34,7 +35,7 @@ export async function withdrawVested(address, config) {
|
||||
state.set(State.Withdrawn);
|
||||
}
|
||||
|
||||
export async function getInfo(address, config) {
|
||||
export async function getInfo(address: string, config: Config) {
|
||||
const contract = new ethers.Contract(address, abi, config.provider);
|
||||
const signer = config.provider.getSigner();
|
||||
|
||||
|
||||
+8
-3
@@ -1,6 +1,7 @@
|
||||
// TODO: Handle wallet account change.
|
||||
import { get, writable, derived, Writable } from "svelte/store";
|
||||
import { ethers } from "ethers";
|
||||
import type { BigNumber } from 'ethers';
|
||||
import type { TransactionReceipt, TransactionResponse } from '@ethersproject/providers';
|
||||
import type { Config } from "@app/config";
|
||||
import { Unreachable, assert, assertEq } from "@app/error";
|
||||
@@ -62,7 +63,7 @@ export const createState = (initial: State) => {
|
||||
}
|
||||
},
|
||||
|
||||
updateBalance: n => {
|
||||
updateBalance: (n: BigNumber) => {
|
||||
store.update((s) => {
|
||||
assert(s.connection === Connection.Connected);
|
||||
s.session.tokenBalance = s.session.tokenBalance.add(n);
|
||||
@@ -70,7 +71,7 @@ export const createState = (initial: State) => {
|
||||
});
|
||||
},
|
||||
|
||||
refreshBalance: async (config) => {
|
||||
refreshBalance: async (config: Config) => {
|
||||
let state = get(store);
|
||||
assert(state.connection === Connection.Connected);
|
||||
const addr = state.session.address;
|
||||
@@ -102,6 +103,9 @@ export const createState = (initial: State) => {
|
||||
store.update(s => {
|
||||
switch (s.connection) {
|
||||
case Connection.Connected:
|
||||
assert(s.session.tx !== null);
|
||||
assert(s.session.tx.state === 'signing');
|
||||
|
||||
s.session.tx = { state: 'pending', hash: tx.hash };
|
||||
return s;
|
||||
default:
|
||||
@@ -114,6 +118,7 @@ export const createState = (initial: State) => {
|
||||
store.update(s => {
|
||||
switch (s.connection) {
|
||||
case Connection.Connected:
|
||||
assert(s.session.tx !== null);
|
||||
assert(s.session.tx.state === 'pending');
|
||||
|
||||
if (tx.status === 1) {
|
||||
@@ -159,7 +164,7 @@ const tokenAbi = [
|
||||
"function allowance(address, address) view returns (uint256)",
|
||||
];
|
||||
|
||||
export async function approveSpender(spender, amount, config) {
|
||||
export async function approveSpender(spender: string, amount: BigNumber, config: Config) {
|
||||
const token = new ethers.Contract(config.radToken.address, tokenAbi, config.provider);
|
||||
const signer = config.provider.getSigner();
|
||||
const addr = await signer.getAddress();
|
||||
|
||||
+3
-2
@@ -1,10 +1,11 @@
|
||||
import { ethers } from "ethers";
|
||||
import type { BigNumber } from "ethers";
|
||||
|
||||
export function formatBalance(n) {
|
||||
export function formatBalance(n: BigNumber) {
|
||||
return ethers.utils.commify(parseFloat(ethers.utils.formatUnits(n)).toFixed(2));
|
||||
}
|
||||
|
||||
export function formatAddress(addr) {
|
||||
export function formatAddress(addr: string) {
|
||||
return addr.substring(0, 6)
|
||||
+ '...'
|
||||
+ addr.substring(addr.length - 4, addr.length);
|
||||
|
||||
Reference in New Issue
Block a user