diff --git a/src/blockies.ts b/src/blockies.ts index bd8ec72a9..046b302d0 100644 --- a/src/blockies.ts +++ b/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; diff --git a/src/error.ts b/src/error.ts index f297cbc04..a98428f28 100644 --- a/src/error.ts +++ b/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}`); } } diff --git a/src/project.ts b/src/project.ts index 1be5e6d8c..538ac42ad 100644 --- a/src/project.ts +++ b/src/project.ts @@ -65,8 +65,9 @@ export interface Tree { } export async function getMetadata(urn: string, config: Config): Promise { - 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', diff --git a/src/session.ts b/src/session.ts index 31ee59452..08eb0d9b6 100644 --- a/src/session.ts +++ b/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 { + connect(config: Config): Promise; + updateBalance(n: BigNumber): void; + refreshBalance(config: Config): Promise; + + setTxSigning(): void; + setTxPending(tx: TransactionResponse): void; + setTxConfirmed(tx: TransactionReceipt): void; + setChangedAccount([address]: string[]): void; +} + +export const loadState = (initial: State): Store => { const store = writable(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 { 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(); } diff --git a/src/utils.ts b/src/utils.ts index c589f865f..d81b0a88c 100644 --- a/src/utils.ts +++ b/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> { - const result = await config.provider.send("alchemy_getTokenBalances", [address, config.tokens]); + await config.provider.send("alchemy_getTokenBalances", [address, config.tokens]); // TODO return [];