Radish alpha
r
Radicle web interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
Make sure all exported functions are typed
Alexis Sellier committed 4 years ago
commit 97d3ae0bfd4d15a5b0639e891cb98afd710d012a
parent 34fdf88b84daf064937a73d3ba7ba1192e74726f
5 files changed +29 -17
modified src/blockies.ts
@@ -106,7 +106,7 @@ export interface Options {
  spotcolor?: string
}

-
export function createIcon(opts: Options) {
+
export function createIcon(opts: Options): HTMLCanvasElement {
  opts = opts || {};
  const size = opts.size || 8;
  const scale = opts.scale || 4;
modified src/error.ts
@@ -34,14 +34,14 @@ class AssertionError extends Error {
  }
}

-
export function assert(value: any, message?: string): asserts value {
+
export function assert(value: unknown, message?: string): asserts value {
  if (! value) {
    throw new AssertionError(message);
  }
}

-
export function assertEq(actual: any, expected: any, message?: string) {
+
export function assertEq(actual: unknown, expected: unknown, message?: string): void {
  if (actual !== expected) {
-
    throw new AssertionError(`assertion failed: expected '${expected}', got '${actual}'`);
+
    throw new AssertionError(`assertion failed: expected '${expected}', got '${actual}': ${message}`);
  }
}
modified src/project.ts
@@ -65,8 +65,9 @@ export interface Tree {
}

export async function getMetadata(urn: string, config: Config): Promise<Meta | null> {
-
  const api = import.meta.env.RADICLE_HTTP_API;
-
  const response = await fetch(`${api}/v1/projects/${urn}`, {
+
  if (! config.seed.api) return null;
+

+
  const response = await fetch(`${config.seed.api}/v1/projects/${urn}`, {
    method: 'GET',
    headers: {
      'Accept': 'application/json',
modified src/session.ts
@@ -1,4 +1,4 @@
-
import { get, writable, derived, Writable } from "svelte/store";
+
import { get, writable, derived, Readable } from "svelte/store";
import { ethers } from "ethers";
import type { BigNumber } from 'ethers';
import type { TransactionReceipt, TransactionResponse } from '@ethersproject/providers';
@@ -29,7 +29,18 @@ export interface Session {
  tx: TxState
}

-
export const loadState = (initial: State) => {
+
export interface Store extends Readable<State> {
+
  connect(config: Config): Promise<void>;
+
  updateBalance(n: BigNumber): void;
+
  refreshBalance(config: Config): Promise<void>;
+

+
  setTxSigning(): void;
+
  setTxPending(tx: TransactionResponse): void;
+
  setTxConfirmed(tx: TransactionReceipt): void;
+
  setChangedAccount([address]: string[]): void;
+
}
+

+
export const loadState = (initial: State): Store => {
  const store = writable<State>(initial);

  const session = window.localStorage.getItem("session");
@@ -45,7 +56,7 @@ export const loadState = (initial: State) => {

      // TODO: This hangs on Brave, if you have to unlock your wallet..
      try {
-
        const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
+
        await window.ethereum.request({ method: 'eth_requestAccounts' });
      } catch (e) {
        console.error(e);
      }
@@ -154,7 +165,7 @@ export const loadState = (initial: State) => {
            // To prevent out of sync state, the wallet gets disconnected.
            if (address === undefined) disconnectWallet();
            else {
-
              s.session.address = address; 
+
              s.session.address = address;
              window.localStorage.setItem("session", JSON.stringify({ address, tokenBalance: s.session.tokenBalance, tx: s.session.tx }));
            }
            return s;
@@ -183,7 +194,7 @@ state.subscribe(s => {
  console.log("session.state", s);
});

-
export async function approveSpender(spender: string, amount: BigNumber, config: Config) {
+
export async function approveSpender(spender: string, amount: BigNumber, config: Config): Promise<void> {
  const token = new ethers.Contract(config.radToken.address, config.abi.token, config.provider);
  const signer = config.provider.getSigner();
  const addr = await signer.getAddress();
@@ -200,7 +211,7 @@ export function token(config: Config): ethers.Contract {
  return new ethers.Contract(config.radToken.address, config.abi.token, config.provider);
}

-
export function disconnectWallet() {
+
export function disconnectWallet(): void {
  window.localStorage.removeItem("session");
  location.reload();
}
modified src/utils.ts
@@ -17,21 +17,21 @@ export interface Safe {
  threshold: number
}

-
export function formatBalance(n: BigNumber) {
+
export function formatBalance(n: BigNumber): string {
  return ethers.utils.commify(parseFloat(ethers.utils.formatUnits(n)).toFixed(2));
}

-
export function formatAddress(addr: string) {
+
export function formatAddress(addr: string): string {
  return formatHash(addr);
}

-
export function formatHash(hash: string) {
+
export function formatHash(hash: string): string {
  return hash.substring(0, 6)
    + '...'
    + hash.substring(hash.length - 4, hash.length);
}

-
export function capitalize(s: string) {
+
export function capitalize(s: string): string {
  if (s === "") return s;
  return s[0].toUpperCase() + s.substr(1);
}
@@ -191,7 +191,7 @@ export async function getSafe(address: string, config: Config): Promise<Safe | n
export async function getTokens(address: string, config: Config):
  Promise<Array<{ tokenName: string, tokenLogo: string }>>
{
-
  const result = await config.provider.send("alchemy_getTokenBalances", [address, config.tokens]);
+
  await config.provider.send("alchemy_getTokenBalances", [address, config.tokens]);

  // TODO
  return [];