)
```
### Using the stores
After a connection has been established, you may import the stores
anywhere in your application. Most of the time, you should use the `$`
prefix Svelte notation to access the stores values.
```html
{#if !$connected}
My application is not yet connected
{:else}
Connected to chain with id {$chainId}
{/if}
```
### Using the Web3 API
Likewise use the `$` prefix Svelte notation to access its instance and
use the full Web3.js API. (beware, in the Web3.js library
documentation, instances are always noted as `web3`, without `$`, but
in the context of `svelte-web3`, `web3` is the Svelte store itself,
not it's value).
```js
import { web3, selectedAccount } from 'svelte-web3'
// ...
const { name, chainId } = await $web3.eth.getChainId()
const balance = await $web3.eth.getBalance('0x0000000000000000000000000000000000000000') : ''
```
### Using the contracts store for reactive contract calls
To enjoy the same reactivity as using `$web3` but with a `web3.eth.Contract`
contract instance, you first need to declare its address and interface. To
differentiate each `eth.Contract` instance, you also need to define a logical
name. That's the function `attachContract`:
```html
```
`attachContract` only needs to be called once and can be called before
connection since `web3.eth.Contract` instances will only be created when
a connection becomes available. You may want to reattach new contract
definition or abi for example when you the current network change. For
the old definition will be overwritten and instance updated in the
`contracts` store, simply use the same logical name.
After a contract as be declared, you can use its instance anywhere
using the `$` notation and the logical name :
```html
{#await $contracts.myContract.methods.totalSupply().call()}
waiting...
{:then value}
result of contract call totalSupply on my contract : { value }
{/await}
```
By default, `svelte-web3` build contract instances using the current connection
options. You may want to overwrite options by passing them as fourth argument.
```js
defaultEvmStores.attachContract('myContract', , , { from: , ... })
```
### Reading stores outside of Svelte files
The `$` prefix Svelte notation to access store values is only
available inside Svelte files. To directly access the instantiated
values in pure javascript library without subscribing to the store,
you can use a special getter on the library abstract helper:
```js
// this is not a Svelte file but a standard JavaScript file
import { defaultEvmStores } from 'svelte-web3'
if (defaultEvmStores.$selectedAccount) {
// do something if store selectedAccount is non null
}
```
### Forcing a disconnect (and removing all listeners)
Simply call the function `disconnect` directly on the on the library
abstract helper:
```js
defaultEvmStores.disconnect()
```
## Human readable chain CAIP-2 information
`chainData` is a store returning the current JavaScript [CAIP-2 representation](https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-2.md) object.
### Example
The information returned by the `chainData` store depends (like all
other web3 stores) on which chain the current provider is
connected. If the store has not yet been connected (with
`setProvider`), the store value will be `undefined`.
This object is extremely useful to build components that reactively
update all variables elements that depends on the current active chain
or account.
Below is the CAIP-2 formatted information when the default store is
connected with the Ethereum Mainnet :
```json
{
"name": "Ethereum Mainnet",
"chain": "ETH",
"icon": "ethereum",
"rpc": [
"https://mainnet.infura.io/v3/${INFURA_API_KEY}",
"https://api.mycryptoapi.com/eth"
],
"faucets": [],
"nativeCurrency": {
"name": "Ether",
"symbol": "ETH",
"decimals": 18
},
"infoURL": "https://ethereum.org",
"shortName": "eth",
"chainId": 1,
"networkId": 1,
"slip44": 60,
"ens": { "registry": "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e" },
"explorers": [
{
"name": "etherscan",
"url": "https://etherscan.io",
"standard": "EIP3091"
}
]
}
```
You might want to access all chains CAIP-2 data directly without using the
`chainData` store. In this case, use the getter `allChainsData`, it returns
the list of all CAIP-2 data available.
```js
import { allChainsData } from 'svelte-web3'
console.log(allChainsData)
```
Another solution is to use the helper function `getChainDataByChainId`
that takes the chainId as argument and returns
the CAIP-2 data or an empty object if not found.
```js
import { getChainDataByChainId } from 'svelte-web3'
console.log(getChainDataByChainId(5))
```
## Web3 Svelte components
`web3-svelte` includes several basic Svelte components designed to simplify
common tasks when building Ethereum-based applications. These components
demonstrate how to use the `web3-svelte` library effectively and serve as
reusable and composable best practices components. Currently, the library
includes `Balance`, `Identicon`, and `Jazzicon` components. We encourage
community members to contribute and help develop additional components by
joining our discussions in our [Discord](https://discord.gg/7yXuwDwaHF).
You can find example usage of these components in the `components` route in the
examples/sveltekit-app-template-web3 directory.
### Balance Component
The `Balance` component displays the balance of a specified Ethereum address. To
use the `Balance` component, import it and pass the address as a prop:
```html
Balance:
```
### Identicon Component
The `Identicon` component generates a unique identicon (a visual representation
of an Ethereum address) using the `ethereum-blockies-base64` library. To use the
`Identicon` component, import it and pass the address as a prop:
```html
```
### Jazzicon Component
The `Jazzicon` component generates a unique and colorful identicon using the
`@metamask/jazzicon` library. To use the `Jazzicon` component, import it and
pass the address and size (optional) as props:
```html
```
These components serve as a starting point for building your Ethereum-based
applications using `web3-svelte`. You can further customize them and create
additional components according to your application's requirements. By
leveraging the reactivity and simplicity of Svelte along with the powerful
features of web3.js, you can create a seamless and efficient user experience
for your decentralized applications.
## Simultaneous multi chain usage
You can also using the library to create several stores, each
connected to different providers. For example, you may want a
connection to the same chain througth the browser wallet and
simultaneously with Infura; or many stores each connected to a
different chains at the same time.
In this case, use the `makeEvmStores` factory function as below :
```js
import { makeEvmStores } from 'svelte-web3'
let evmStores, web3, connected, selectedAccount, chainId, chainData
;({ web3, connected, selectedAccount, chainId, chainData, ...evmStores } =
makeEvmStores(''))
evmStores.setProvider('https://rpc.slock.it/goerli')
```
`` can be an arbitrary name to be able to retrieve the store with the function `getChainStore`
without reinitializing the conection:
```js
import { getChainStore } from 'svelte-web3'
let evmStores, web3, connected, selectedAccount, chainId, chainData
;({ web3, connected, selectedAccount, chainId, chainData, ...evmStores } =
getChainStore(''))
```
The `web3` store and all other derived stores will work the same way as with the default store.
If you want to use the different chain stores in the same Svelte file
(not advised, it's better to use subcomponents), you may renamed the
stores this way :
```js
let evmStores_A, web3_A, evmStores_B, web3_B
;({ web3: web3_A, ...evmStores_A } = makeEvmStores(''))(
({ web3: web3_B, ...evmStores_B } = makeEvmStores(''))
)
```
## FAQ
### _What would be the most basic scaffolding to get Svelte + web3 running_
Assuming that you already have a working injected provider like MetaMask in your browser:
1. npm create vite@latest myapp -- --template svelte
2. cd myapp && npm install svelte-web3
3. add `web3.js` script loading tag inside the `index.html` header (see above)
4. add this minimal connection code in App.svelte:
```js
import { onMount } from 'svelte'
import { defaultEvmStores as evm, selectedAccount, chainId } from 'svelte-web3'
onMount(evm.setProvider)
```
5. use the stores in the HTML
```html
account: {$selectedAccount} / chainId: {$chainId}
```
6. `npm run dev`
### _X doesn't work after `await defaultEvmStores.setProvider()`_
It's not sufficient to `await` for the `setProvider` to be certain that
other derived stores have been populated. Instead you should subscribe
to the needed store and put your code inside the handler.
For example:
on
```js
import { defaultEvmStores as evm, web3, selectedAccount } from 'svelte-web3'
selectedAccount.subscribe(async ($selectedAccount) => {
if (!$selectedAccount) return
// do stuff here
// ...
})
onMount(() => {
evm.setProvider()
})
```
### _how to auto-connect on page load?_
It is out of scope of this package to implement this function but it
generally depends on the type of provider you are using and a way to
store connection information between page loads (for example by using
localStorage).
## Examples
If you are using `svelte-web3` to build an open source Dapp, let us know
if you want to be listed in this section.
### Svelte basic example (using Vite)
Please check [examples/svelte-vite-template-web3 on github](https://github.com/clbrge/svelte-web3/tree/master/examples/svelte-vite-template-web3).
### SvelteKit basic example
Please check [examples/sveltekit-app-template-web3 on github](https://github.com/clbrge/svelte-web3/tree/master/examples/sveltekit-app-template-web3).
### Svelte basic example (based on rollup template)
:exclamation: This is a legacy example and will be removed in future version of `svelte-web3`.
Please check [examples/svelte-app-template-web3 on github](https://github.com/clbrge/svelte-web3/tree/master/examples/svelte-app-template-web3).
Contains demos to use the default store and multi stores.
### Sapper basic example (based on webpack template)
:exclamation: This is a legacy example and will be removed in future version of `svelte-web3`.
Please check [examples/sapper-app-template-web3 on github](https://github.com/clbrge/svelte-web3/tree/master/examples/sapper-app-template-web3).
### tradingstrategy.ai presented at EthLisbon 2021
A website presented in EthLisbon 2021, used svelte-web3 (version 2) for building the frontend. :
- Tutorial: https://tradingstrategy.ai/blog/building-cryptocurrency-website