Eth Hook for Svelte
useEth hook returns relevant stores and derived storesconnect: async (chainId?: number) => void connect to metamask, with desired side effects (connected = true, provider = injected....)connected: Writable<Bool> whether Metamask is connected provider: Writable<sProvider = ethers.providers.JrpcProvider | ethers.providers.Web3Provider> and the derived storesaccount: Readable<string>signer: Readable<ethers.Signer>useEth Hookimport { useEth, IStore } from 'sveth'
export default const stores: IStore = useEth()
export const {provider, signer, address, chainId, getBalanceStore...} = stores
const balanceStore = getBalanceStore("0xabc123")
<button on:click={() => connect()}> Connect Wallet </button>
<p>connected: {$connected}</p>
{#if $connected}
<p>chainId: {$chainId}</p>
<p>Account: {$account}</p>
<p>Balance: {balance}</p>
<p>Balance Hook: {$balanceStore}</p>
{/if}
$store to get value from stores<ContractCard/> Componentimport ContractCard from "sveth/src/components/ContractCard.svelte"
import { Contract } from "ethers"
import { Interface } from "@ethersproject/abi"
import ERC20ABI from "../abis/ERC20.json"
import store from "../stores/eth"
let contract = new Contract("0xabc123...", new Interface(ERC20ABI))
let contractChainId = 5
<ContractCard {contract} {store} {contractChainId} />
example/ folderexample/<Contract {contract} {store} {chainId}> by prompting users to switch chain when chain is not matchedsrc/components/utils.ts isFunction by not using instance of which is not ideal according to the discussion in ethersbalance hooks and others by listening to blocks/events/txsnewblock store may be a good ideascontractChainIds to other related stores like balance<Contract {contract} {store}>const {reactive_contract} = useContract(address, abi, $signer)