Eth Hook for Svelte
useEth
hook returns relevant store
s and derived store
sconnect: 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 store
saccount: 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 store
s<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 ideascontractChainId
s to other related stores like balance
<Contract {contract} {store}>
const {reactive_contract} = useContract(address, abi, $signer)