Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Svelte kit #7

Draft
wants to merge 49 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
ceff822
Start with heavy refactor towards Svelte Kit
acuarica Dec 2, 2021
5699b46
Add wallet store to manage MetaMask
acuarica Dec 2, 2021
d351f71
Add MetaMask providers to use its type definition
acuarica Dec 3, 2021
d9d07e5
Enhance vite env typings and docs
acuarica Dec 3, 2021
d03f583
Fix type conversion
acuarica Dec 3, 2021
e2072b5
Export chain id
acuarica Dec 3, 2021
e977bc8
Improve wallet reactivity
acuarica Dec 3, 2021
c88f871
Improve warning look&feel
acuarica Dec 3, 2021
9bdef40
Move inside `pools/` route
acuarica Dec 5, 2021
42461e5
Set up vite mode
acuarica Dec 5, 2021
962a453
Add new info endpoints
acuarica Dec 5, 2021
d93615a
Add new env types
acuarica Dec 5, 2021
257c58e
Add storybook
acuarica Dec 5, 2021
40adce8
Refactor to decouple from store
acuarica Dec 5, 2021
0463352
Add network info component
acuarica Dec 5, 2021
1e56b21
Fix deposit/withdraw routes
acuarica Dec 5, 2021
f3d64e1
Import without $lib
acuarica Dec 5, 2021
4efebce
Import without $lib
acuarica Dec 5, 2021
48a623b
Use RenPool address from env
acuarica Dec 5, 2021
04385a0
Add component stories
acuarica Dec 5, 2021
b32578c
Improve error handling
acuarica Dec 5, 2021
2fd15b4
Add some jsdoc
acuarica Dec 5, 2021
2d90d67
Remove comments
acuarica Dec 5, 2021
12a71dc
Add ren allowance PoC
acuarica Dec 5, 2021
68e2fd3
Remove hardhat
acuarica Dec 6, 2021
34909b1
Decouple components from env
acuarica Dec 7, 2021
fa60d50
Refactor main layout into component
acuarica Dec 7, 2021
6c0bf43
Rename to support jest testing
acuarica Dec 8, 2021
af98ce2
Add jest packages and config
acuarica Dec 8, 2021
3fdcb0e
Allows ide support when import .svelte files
acuarica Dec 8, 2021
ac34993
Add some tests
acuarica Dec 8, 2021
d00f886
Separate constants from config
acuarica Dec 9, 2021
0664fda
Fix config/bond imports
acuarica Dec 9, 2021
4e03eb8
Add address type alias
acuarica Dec 9, 2021
8de489c
Refactor to use types
acuarica Dec 9, 2021
9a06c0f
Use context instead of module variable
acuarica Dec 9, 2021
17a4bab
Showcase multiple Howtos in one page
acuarica Dec 9, 2021
8cb4534
Use context for EtherscanLink
acuarica Dec 9, 2021
70ef0a2
Use prop instead of module variable
acuarica Dec 9, 2021
e106237
Unify contract usage
acuarica Dec 9, 2021
3185cdd
moving h1 and h2 tags to global theme (#10)
fede-rodes Dec 11, 2021
02f1c9f
introducing Chip component (#11)
fede-rodes Dec 13, 2021
7baa4cf
moving Chip to lib folder
fede-rodes Dec 13, 2021
23b5160
Feat/format (#12)
fede-rodes Dec 13, 2021
dc47301
adding missing dep (#13)
fede-rodes Dec 14, 2021
b7c81fb
Feat/format2 (#14)
fede-rodes Dec 15, 2021
a7bcbae
fix import
fede-rodes Dec 23, 2021
56f75f9
update storybook config to only process svelte files
fede-rodes Dec 23, 2021
54bdffb
Feat/comp lib (#15)
fede-rodes Dec 27, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.kovan-fork
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
VITE_CHAIN_ID=42
VITE_JSON_RPC_PROVIDER_URL=http://127.0.0.1:8545
VITE_REN_POOL=0xbF115A5538290D234fA31e917241a58A20a847Bc
3 changes: 3 additions & 0 deletions .env.mainnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
VITE_CHAIN_ID=1
VITE_JSON_RPC_PROVIDER_URL=https://eth-mainnet.alchemyapi.io/v2/4qQo8naBUQPUU4UWex6jJupYRxlfIdxl
VITE_REN_POOL=0xf1e98770ab8ed1364371B8c7DBdA56633F7cB6a9
3 changes: 3 additions & 0 deletions .env.mainnet-fork
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
VITE_CHAIN_ID=1
VITE_JSON_RPC_PROVIDER_URL=http://localhost:8545
VITE_REN_POOL=0xf1e98770ab8ed1364371B8c7DBdA56633F7cB6a9
31 changes: 16 additions & 15 deletions .storybook/main.cjs
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@

const preprocess = require('svelte-preprocess');

module.exports = {
// export default {
core: {
builder: "storybook-builder-vite",
},

async viteFinal(config, { configType }) {
// customize the Vite config here
config.resolve.alias.foo = 'bar';
config.mode = 'kovan'

// return the customized config
return config;
},

"stories": [
"../src/**/*.stories.mdx",
Expand All @@ -23,7 +9,22 @@ module.exports = {
"addons": [
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/addon-svelte-csf"
"@storybook/addon-svelte-csf",
{
name: '@storybook/addon-postcss',
options: {
cssLoaderOptions: {
// When you have splitted your css over multiple files
// and use @import('./other-styles.css')
importLoaders: 1,
},
postcssLoaderOptions: {
// When using postCSS 8
implementation: require('postcss'),
},
}
},
"@storybook/addon-interactions",
],
"framework": "@storybook/svelte",
"svelteOptions": {
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
"@metamask/providers": "^8.1.1",
"@storybook/addon-actions": "^6.4.7",
"@storybook/addon-essentials": "^6.4.7",
"@storybook/addon-interactions": "^6.4.8",
"@storybook/addon-links": "^6.4.7",
"@storybook/addon-postcss": "^2.0.0",
"@storybook/addon-svelte-csf": "^1.1.0",
"@storybook/svelte": "^6.4.7",
"@storybook/testing-library": "^0.0.7",
"@sveltejs/adapter-auto": "next",
"@sveltejs/kit": "next",
"@typescript-eslint/eslint-plugin": "^4.31.1",
Expand All @@ -32,11 +35,10 @@
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-svelte3": "^3.2.1",
"hardhat": "^2.7.0",
"postcss": "^8.4.4",
"storybook-builder-vite": "^0.1.10",
"svelte": "^3.44.2",
"svelte-check": "^2.2.6",
"svelte-loader": "^3.1.2",
"svelte-preprocess": "^4.9.4",
"tailwindcss": "^2.2.19",
"tslib": "^2.3.1",
Expand All @@ -47,4 +49,4 @@
"ethers": "^5.5.1",
"renpool-contracts": "https://github.com/renpool/renpool-contracts"
}
}
}
2 changes: 1 addition & 1 deletion src/context/RenTokenProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { FC, useState, useEffect, createContext } from 'react'
import { Contract } from '@ethersproject/contracts'
import { BigNumber } from '@ethersproject/bignumber'
import { ContractNames, CONTRACT_ADDRESSES } from '../lib/config'
import { ContractNames, CONTRACT_ADDRESSES } from '../lib/net/confignfig'
import { IERC20Standard as artifact } from 'renpool-contracts'
// import map from '../artifacts/deployments/map.json'
import { useActiveWeb3React } from '../hooks/useActiveWeb3React'
Expand Down
2 changes: 1 addition & 1 deletion src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ interface ImportMetaEnv {
* ```
*
*/
readonly VITE_CHAIN_ID: `${keyof typeof import('$lib/config').NETWORKS}`;
readonly VITE_CHAIN_ID: `${keyof typeof import('$lib/net/config').NETWORKS}`;
readonly VITE_JSON_RPC_PROVIDER_URL: string;
readonly VITE_REN_POOL: string;
}
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useActiveWeb3React.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useWeb3React } from '@web3-react/core'
import { Web3ReactContextInterface } from '@web3-react/core/dist/types'
import { Web3Provider } from '@ethersproject/providers'
import { NETWORK_CONTEXT_NAME } from '../lib/config'
import { NETWORK_CONTEXT_NAME } from '../lib/net/confignfig'

export const useActiveWeb3React = (): Web3ReactContextInterface<Web3Provider> => {
const context = useWeb3React<Web3Provider>()
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useContract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useEffect } from 'react'
import { Contract, ContractInterface } from '@ethersproject/contracts'
import { injected } from '../lib/connectors'
import { injected } from '../lib/net/connectorstors'
import { useActiveWeb3React } from './useActiveWeb3React'

const CHAIN_ID = process.env.REACT_APP_CHAIN_ID
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useEagerConnect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useWeb3React } from '@web3-react/core'
import { useEffect, useState } from 'react'
import { isMobile } from 'react-device-detect'
import { injected } from '../lib/connectors'
import { injected } from '../lib/net/connectorstors'

export const useEagerConnect = (): boolean => {
const { activate, active } = useWeb3React() // specifically using useWeb3React because of what this hook does
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useInactiveListener.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useWeb3React } from '@web3-react/core'
import { useEffect } from 'react'
import { injected } from '../lib/connectors'
import { injected } from '../lib/net/connectorstors'

/**
* Use for network and injected - logs user in
Expand Down
4 changes: 4 additions & 0 deletions src/lib/Addresses.stories.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<script context="module">
import { config } from "./EtherscanLink.svelte";
config.etherscan = "https://mainnet.etherscan.io/address/";
</script>
<script>
import { Meta, Template, Story } from "@storybook/addon-svelte-csf";
import Addresses from "./Addresses.svelte";
Expand Down
11 changes: 6 additions & 5 deletions src/lib/Addresses.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<script context="module" lang="ts">
export const config = { renToken: "", renPool: "" };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this component should depend on the context, maybe we could just remove the context and expose a prop called addresses:

export let addresses = {[key: string]: string}

Then, from the parent component you'd pass:

addresses = {
  owner: '0x',
  nodeOperator: '0x',
  renToken: '0x',
  renPool: '0x',
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap, we can do that.

</script>

<script lang="ts">
import { NETWORK } from "./config";
import EtherscanLink from "./EtherscanLink.svelte";

/**
Expand Down Expand Up @@ -33,13 +36,12 @@
<tr>
<td class="key">RenToken</td>
<td class="value"
><EtherscanLink address={NETWORK.contracts.REN_TOKEN} /></td
><EtherscanLink address={config.renToken} /></td
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in order to avoid repeating this <tr><td> maybe we could pass an array of objects or an object as prop and then do a for loop like this:

{#each addresses as {key, value}}
           <tr>
                <td class="key">{key}</td>
                <td class="value"><EtherscanLink address={value} />
            </td>
{/each}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap, we can do this as well.

>
</tr>
<tr>
<td class="key">RenPool</td>
<td class="value"
><EtherscanLink address={NETWORK.contracts.REN_POOL} /></td
<td class="value"><EtherscanLink address={config.renPool} /></td
>
</tr>
<tr>
Expand All @@ -52,7 +54,6 @@
</tr>
</tbody>
</table>

</section>

<style lang="postcss">
Expand Down
6 changes: 6 additions & 0 deletions src/lib/EtherscanLink.stories.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<script context="module">
import { config } from "./EtherscanLink.svelte";
config.etherscan = "https://kovan.etherscan.io/address/";
</script>

<script>
import { Meta, Template, Story } from "@storybook/addon-svelte-csf";
import EtherscanLink from "./EtherscanLink.svelte";
Expand All @@ -6,6 +11,7 @@
<Meta title="$lib/EtherscanLink" component={EtherscanLink} argTypes={{}} />

<Template let:args>
<p>Using config: {JSON.stringify(config)}</p>
<EtherscanLink {...args} />
</Template>

Expand Down
10 changes: 7 additions & 3 deletions src/lib/EtherscanLink.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<script lang="ts">
import { NETWORK } from "./config";
<script context="module" lang="ts">
export const config: {
etherscan: string | null;
Copy link
Contributor

@fede-rodes fede-rodes Dec 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, I think this component should not depend on the context. Probably the only component that should depend on the context is the page or the section component and then pass all the context values down to the child components as props.

export let etherscanPrefix: string;
export let address: string;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not here. The I wouldn't use etherscanPrefix as a component prop, because this should be the same for every instance. That's why it's at the module level.

} = { etherscan: null };
</script>

<script lang="ts">
export let address: string;
</script>

<a href="{NETWORK.etherscan}{address}" target="_blank" rel="noreferrer">
<a href="{config.etherscan}{address}" target="_blank" rel="noreferrer">
{address}
</a>
2 changes: 1 addition & 1 deletion src/lib/Footer.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
import Logo from './Logo.svelte';
import Logo from './img/Logo.svelte';
</script>

<footer class="text-gray-600 body-font">
Expand Down
11 changes: 7 additions & 4 deletions src/lib/Header.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<script context="module" lang="ts">
export const config: { renPool: string | null } = { renPool: null };
</script>

<script>
import Logo from "./Logo.svelte";
import { NETWORK } from "./config";
import Logo from "./img/Logo.svelte";
</script>

<header class="text-gray-600 bg-purple-100">
Expand All @@ -18,8 +21,8 @@
class="md:ml-auto flex flex-wrap items-center text-base justify-center"
>
<a href="/">Home</a>
<a href="/pools/{NETWORK.contracts.REN_POOL}/deposit">Deposit</a>
<a href="/pools/{NETWORK.contracts.REN_POOL}/withdraw">Withdraw</a>
<a href="/pools/{config.renPool}/deposit">Deposit</a>
<a href="/pools/{config.renPool}/withdraw">Withdraw</a>
<a href="/howto">How To</a>
</nav>
<slot />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<script>
import { Meta, Template, Story } from "@storybook/addon-svelte-csf";
import Howto from "./Howto.svelte";
import Step from "./Step.svelte";
import Step from "./HowtoStep.svelte";
</script>

<Meta title="$lib/howto/Howto" component={Howto} argTypes={{}} />
<Meta title="$lib/Howto" component={Howto} argTypes={{}} />

<Template let:args>
<Howto {...args} />
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<script>
import { Meta, Template, Story } from "@storybook/addon-svelte-csf";
import { setContext } from "svelte";
import Step from "./Step.svelte";
import Step from "./HowtoStep.svelte";

setContext("step", { count: 99 });
</script>

<Meta title="$lib/howto/Step" component={Step} argTypes={{}} />
<Meta title="$lib/HowtoStep" component={Step} argTypes={{}} />

<Template let:args>
<Step {...args} />
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions src/lib/Wallet.stories.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script>
import { Meta, Template, Story } from "@storybook/addon-svelte-csf";

import Wallet from "./Wallet.svelte";
</script>

Expand Down
10 changes: 7 additions & 3 deletions src/lib/Wallet.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<script lang="ts">
import { shortAccount } from "./shortAccount";
import { shortAccount } from "./net/shortAccount";

/**
* Indicates whether the MetaMask extension is installed in the browser.
* A `null` value should be used when loading the component.
*/
export let hasMetaMask: boolean | null = null;

export let chainId: number | null = null;
Expand All @@ -17,11 +21,11 @@
{chainId}

{#if hasMetaMask === null}
<div />
<span>loading...</span>
{:else if !hasMetaMask}
<a target="_blank" href="https://metamask.io/download">Install MetaMask</a>
{:else if selectedAddress === null}
<button on:click={connectToMetaMask}> Connect to MetaMask </button>
<button on:click={connectToMetaMask}>Connect to MetaMask</button>
{:else}
<a
href="/"
Expand Down
26 changes: 20 additions & 6 deletions src/lib/Warn.stories.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<script>
import { Meta, Template, Story } from "@storybook/addon-svelte-csf";
import { within, userEvent, waitFor } from "@storybook/testing-library";

import Warn from "./Warn.svelte";
</script>

Expand All @@ -25,11 +27,23 @@
}}
/>

<Story name="Content" args={{}}
><Warn title="Your Warning Message">Something went south</Warn></Story
>
<Story name="Content" args={{}}>
<Warn title="Your Warning Message">Something went south</Warn>
</Story>

<Story name="Long Content" args={{}}>
<Warn title="Your Warning Message">{"Something went south ".repeat(5)}</Warn>
</Story>

<Story name="Long Content" args={{}}
><Warn title="Your Warning Message">{"Something went south ".repeat(5)}</Warn
></Story
<Story
name="Dismiss Content"
args={{}}
play={async ({ canvasElement }) => {
const canvas = within(canvasElement);
await new Promise((r) => setTimeout(r, 2000));
userEvent.click(canvas.getByRole("button"));
}}
>
<Warn title="Your Warning Message">{"Something went south ".repeat(5)}</Warn>
<p>Warn message should be dismissed after 2s.</p>
</Story>
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/lib/connectors.ts → src/lib/net/connectors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InjectedConnector } from '@web3-react/injected-connector'
import { NetworkConnector } from '@web3-react/network-connector'
import { SUPPORTED_CHAIN_IDS } from './config'
import { SUPPORTED_CHAIN_IDS } from './net/confignfig'

const INFURA_PROJECT_ID = process.env.REACT_APP_INFURA_PROJECT_ID
const CHAIN_ID = process.env.REACT_APP_CHAIN_ID
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/lib/wallet.ts → src/lib/net/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { writable } from "svelte/store";
import { Contract, ethers } from "ethers";
import { deployments, IERC20Standard } from "renpool-contracts";
import { DECIMALS } from "$lib/config";
import { DECIMALS } from "$lib/net/config";
import { browser } from '$app/env';

const CHAIN_ID = parseInt(import.meta.env.VITE_CHAIN_ID);
Expand Down
8 changes: 5 additions & 3 deletions src/routes/__layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@
<script lang="ts">
import "../app.css";

import { wallet } from "$lib/wallet";
import Header from "$lib/Header.svelte";
import { wallet } from "$lib/net/wallet";
import Header, { config } from "$lib/Header.svelte";
import Footer from "$lib/Footer.svelte";
import Wallet from "$lib/Wallet.svelte";
import Addresses from "$lib/Addresses.svelte";
import Stats from "$lib/Stats.svelte";
import Warn from "$lib/Warn.svelte";
import { NETWORK } from "$lib/config";
import { NETWORK } from "$lib/net/config";
import Network from "$lib/Network.svelte";

config.renPool = NETWORK.contracts.REN_POOL;

type PropsOf<
Component extends new (options: { target: any; props: any }) => any
> = NonNullable<ConstructorParameters<Component>[0]["props"]> | null;
Expand Down
2 changes: 1 addition & 1 deletion src/routes/api/renpool.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NETWORK } from '$lib/config';
import { NETWORK } from '$lib/net/config';
import { Contract, providers } from "ethers";
import { RenPool } from "renpool-contracts";

Expand Down
Loading