@svelte-on-solana/wallet-adapter-anchor
AnchorConnectionProvider
component and workSpace
for Solana wallets using Svelte
Installing
You have already installed the core package to run the wallet Svelte Store @svelte-on-solana/wallet-adapter-core and the UI components to use the wallet @svelte-on-solana/wallet-adapter-ui. Then install the AnchorConnectionProvider
component and workSpace
file contained in this package.
npm i @svelte-on-solana/wallet-adapter-anchor
Set Up
The AnchorConnectionProvider
for Anchor Dapps accepts the next props.
prop | type | default |
---|
network | string | |
idl | Idl | |
It is automatically connected to the workSpace
defining all the parameters to share among the components in your Anchor Dapp (baseAccount, connection, provider, program, systemProgram and network).
SvelteKit
In the __layout.svelte component you can import the wallets and setup the UI components.
<script lang="ts">
import { onMount } from 'svelte';
import { walletStore } from '@svelte-on-solana/wallet-adapter-core';
import { WalletProvider, WalletMultiButton } from '@svelte-on-solana/wallet-adapter-ui';
import { AnchorConnectionProvider, workSpace } from '@svelte-on-solana/wallet-adapter-anchor';
import { clusterApiUrl } from '@solana/web3.js';
import idl from '../../../target/idl/<my-anchor-project>.json';
import { PhantomWalletAdapter, SolflareWalletAdapter } from '@solana/wallet-adapter-wallets';
const localStorageKey = 'walletAdapter';
const network = clusterApiUrl('devnet');
let wallets;
onMount(async () => {
const { PhantomWalletAdapter, SolflareWalletAdapter } = await import(
'@solana/wallet-adapter-wallets'
);
wallets = [new PhantomWalletAdapter(), new SolflareWalletAdapter()];
});
</script>
<WalletProvider {localStorageKey} {wallets} autoConnect />
<AnchorConnectionProvider {network} {idl} />
<div>
<slot />
</div>
<WalletMultiButton />
Svelte Template
In App.svelte
or the entry point of your SPA, you can setup the wallet and components like this.
<script lang="ts">
import { walletStore } from '@svelte-on-solana/wallet-adapter-core';
import { WalletProvider, WalletMultiButton } from '@svelte-on-solana/wallet-adapter-ui';
import { AnchorConnectionProvider, workSpace } from '@svelte-on-solana/wallet-adapter-anchor';
import { clusterApiUrl } from '@solana/web3.js';
import idl from '../../../target/idl/<my-anchor-project>.json';
import { PhantomWalletAdapter, SolflareWalletAdapter } from '@solana/wallet-adapter-wallets';
const localStorageKey = 'walletAdapter';
const network = clusterApiUrl('devnet');
let wallets = [new PhantomWalletAdapter(), new SolflareWalletAdapter()];
</script>
<WalletProvider {localStorageKey} {wallets} autoConnect />
<AnchorConnectionProvider {network} {idl} />
<WalletMultiButton />
{#if $walletStore?.connected}
<div>My wallet is connected</div>
{/if}
Example Implementation
See example implementations of the @svelte-on-solana/wallet-adapter-ui
library.