├── .env.example ├── .eslintignore ├── .eslintrc.cjs ├── .gitignore ├── .npmrc ├── .prettierignore ├── .prettierrc ├── README.md ├── package-lock.json ├── package.json ├── src ├── app.d.ts ├── app.html ├── lib │ ├── abi │ │ └── USDC.json │ ├── index.ts │ └── stores │ │ └── wagmi.ts └── routes │ └── +page.svelte ├── static └── favicon.png ├── svelte.config.js ├── tsconfig.json └── vite.config.ts /.env.example: -------------------------------------------------------------------------------- 1 | PUBLIC_ALCHEMY_ID= 2 | PUBLIC_WALLETCONNECT_ID= -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /build 4 | /.svelte-kit 5 | /package 6 | .env 7 | .env.* 8 | !.env.example 9 | 10 | # Ignore files for PNPM, NPM and YARN 11 | pnpm-lock.yaml 12 | package-lock.json 13 | yarn.lock 14 | -------------------------------------------------------------------------------- /.eslintrc.cjs: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | parser: '@typescript-eslint/parser', 4 | extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'prettier'], 5 | plugins: ['svelte3', '@typescript-eslint'], 6 | ignorePatterns: ['*.cjs'], 7 | overrides: [{ files: ['*.svelte'], processor: 'svelte3/svelte3' }], 8 | settings: { 9 | 'svelte3/typescript': () => require('typescript') 10 | }, 11 | parserOptions: { 12 | sourceType: 'module', 13 | ecmaVersion: 2020 14 | }, 15 | env: { 16 | browser: true, 17 | es2017: true, 18 | node: true 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /build 4 | /dist 5 | /.svelte-kit 6 | /package 7 | .env 8 | .env.* 9 | !.env.example 10 | vite.config.js.timestamp-* 11 | vite.config.ts.timestamp-* 12 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | engine-strict=true 2 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /build 4 | /.svelte-kit 5 | /package 6 | .env 7 | .env.* 8 | !.env.example 9 | 10 | # Ignore files for PNPM, NPM and YARN 11 | pnpm-lock.yaml 12 | package-lock.json 13 | yarn.lock 14 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "useTabs": true, 3 | "singleQuote": true, 4 | "trailingComma": "none", 5 | "printWidth": 100, 6 | "plugins": ["prettier-plugin-svelte"], 7 | "pluginSearchDirs": ["."], 8 | "overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }] 9 | } 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Svelte Wagmi 2 | 3 | Svelte Wagmi is a package that provides a collection of Svelte stores and functions for interacting with the Ethereum network. It utilizes the [@wagmi/core](https://wagmi.sh/core/getting-started) library for connecting to Ethereum networks and signing transactions. 4 | 5 | ## Installation 6 | 7 | To install the package and its peer dependencies, run the following command: 8 | 9 | ```bash 10 | npm install svelte-wagmi viem @wagmi/core @wagmi/connectors 11 | ``` 12 | 13 | ## Usages 14 | 15 | ### connected 16 | 17 | The `connected` store indicates whether the application is currently connected to an Ethereum provider. It is a boolean store that is set to true when the application is connected and false otherwise. 18 | 19 | Example usage: 20 | 21 | ```svelte 22 | 25 | 26 | {#if $connected} 27 |

Connected to Ethereum

28 | {:else} 29 |

Not connected to Ethereum

30 | {/if} 31 | ``` 32 | 33 | ### wagmiLoaded 34 | 35 | The `wagmiLoaded` store indicates whether the @wagmi/core library has been loaded and initialized. It is a boolean store that is set to true when the library is loaded and false otherwise. 36 | 37 | Example usage: 38 | 39 | ```svelte 40 | 43 | 44 | {#if $wagmiLoaded} 45 |

@wagmi/core is loaded and initialized

46 | {:else} 47 |

@wagmi/core is not yet loaded

48 | {/if} 49 | ``` 50 | 51 | ### chainId 52 | 53 | The `chainId` store contains the current chain ID of the connected Ethereum provider. It is a number store that is set to null if the provider is not connected or if the chain ID is unknown. 54 | 55 | Example usage: 56 | 57 | ```svelte 58 | 61 | 62 | {#if $chainId} 63 |

Current chain ID: {$chainId}

64 | {:else} 65 |

Chain ID not yet available

66 | {/if} 67 | ``` 68 | 69 | ### signerAddress 70 | 71 | The `signerAddress` store contains the current signer address of the connected Ethereum provider. It is a string store that is set to null if the provider is not connected or if the signer address is unknown. 72 | 73 | Example usage: 74 | 75 | ```svelte 76 | 79 | 80 | {#if $signerAddress} 81 |

Current signer address: {$signerAddress}

82 | {:else} 83 |

Signer address not yet available

84 | {/if} 85 | ``` 86 | 87 | ### loading 88 | 89 | The `loading` store indicates whether the application is currently loading data from the Ethereum provider. It is a boolean store that is set to `true` when the application is loading and `false` otherwise. 90 | 91 | Example usage: 92 | 93 | ```svelte 94 | 97 | 98 | {#if $loading} 99 |

Loading data...

100 | {:else} 101 |

Data loaded

102 | {/if} 103 | ``` 104 | 105 | ### web3Modal 106 | 107 | The `web3Modal` store contains an instance of the `Web3Modal` class from the `@web3modal/html` package. It is used to display a modal for connecting to an Ethereum provider using WalletConnect or other methods. 108 | Example usage: 109 | 110 | ```svelte 111 | 114 | 115 | {#if $web3Modal} 116 | 117 | {:else} 118 |

Web3Modal not yet available

119 | {/if} 120 | ``` 121 | 122 | ### defaultConfig 123 | 124 | The `defaultConfig` function is used to configure the `@wagmi/core` library and initialize the Svelte stores. It takes an optional options object that can be used to configure the behavior of the function. 125 | 126 | - `autoConnect` (boolean, default: `true`): Specifies whether the Ethereum client should automatically connect to a provider upon initialization. If set to `true`, the client will attempt to connect automatically. 127 | - `appName` (string, default: `'Erc.Kit'`): Specifies the name of the application using the Ethereum client. 128 | - `chains` (array, default: `defaultChains`): An array of chain configurations to connect with. If not provided, the function will use default chain configurations. 129 | - `alchemyId` (optional) (string): The API key for the Alchemy provider, used for connecting to the Alchemy service. 130 | - `connectors` (object): An object containing customized connector configurations for the Ethereum client. 131 | - `walletConnectProjectId` (string): The project ID used for the WalletConnect integration. 132 | 133 | ### Return Value: 134 | 135 | The `defaultConfig` function returns an object with the following properties: 136 | 137 | - `init`: A function that can be called to initialize the Ethereum client and set up the connections based on the provided configurations. 138 | 139 | Example usage: 140 | 141 | ```svelte 142 | 159 | ``` 160 | 161 | ### configuredConnectors 162 | 163 | The `configuredConnectors` are store value array 164 | 165 | Example usage: 166 | 167 | ```svelte 168 | 175 | 176 | 177 | ``` 178 | 179 | ### WC 180 | 181 | The `WC` function is used to connect to an Ethereum provider using WalletConnect. It takes one parameter: 182 | 183 | - `statement` (optional): A string that specifies the statement to be signed by the user when logging in. 184 | 185 | Example usage: 186 | 187 | ```svelte 188 | 195 | 196 | 197 | ``` 198 | 199 | ### disconnectWagmi 200 | 201 | The `disconnectWagmi` function is used to disconnect from the Ethereum provider and clear the Svelte stores. Example usage: 202 | 203 | ```svelte 204 | 211 | 212 | 213 | ``` 214 | 215 | ### Svelte stores 216 | 217 | The `svelte-wagmi` library also provides several Svelte stores that can be used to retrieve information about the user's Ethereum connection: 218 | 219 | - `connected`: A boolean that indicates whether the user is connected to an Ethereum provider. 220 | - `wagmiLoaded`: A boolean that indicates whether the `@wagmi/core` library has been loaded. 221 | - `chainId`: A number that indicates the chain ID of the user's Ethereum connection. 222 | - `signerAddress`: A string that contains the Ethereum address of the user's account. 223 | - `loading`: A boolean that indicates whether the library is currently loading. 224 | Example usage: 225 | 226 | ```svelte 227 | 234 | 235 |

Connected: {$connected ? 'Yes' : 'No'}

236 |

Chain ID: {$chainId}

237 |

Signer address: {$signerAddress}

238 | ``` 239 | 240 | ### Using @wagmi/core 241 | 242 | You can use any wagmi/core functions by passing $wagmiConfig from svelte-wagmi 243 | 244 | ```svelte 245 | 255 | ``` 256 | 257 | ### Note 258 | 259 | changing network using `@wagmi/core` will also chage the `svelte-wagmi`: chainId store 260 | 261 | `$signerAddress` and `getAccount()` are the same ETH address. 262 | 263 | If you experience an error from @Web3Modal/connectors that "process is not defined", a workaround is to defined process.env explicitly in your vite.config.ts 264 | 265 | ```ts 266 | export default defineConfig({ 267 | // ... rest of your config 268 | define: { 269 | 'process.env': {} 270 | } 271 | }); 272 | ``` 273 | 274 | ## Roadmap 275 | 276 | this is a basic package I made for projects and can add more features if people like this. 277 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "svelte-wagmi", 3 | "author": "@softwarecurator", 4 | "version": "1.0.7", 5 | "license": "MIT", 6 | "repository": { 7 | "type": "git", 8 | "url": "git+https://github.com/softwarecurator/svelte-wagmi.git" 9 | }, 10 | "keywords": [ 11 | "wagmi", 12 | "svelte-wagmi", 13 | "wagmi/core", 14 | "web3", 15 | "ethereum", 16 | "wallet", 17 | "web3modal", 18 | "web3-react", 19 | "web3.js", 20 | "ethers.js", 21 | "ethers", 22 | "ethers-react", 23 | "svelte", 24 | "sveltekit", 25 | "metamask" 26 | ], 27 | "scripts": { 28 | "dev": "vite dev", 29 | "build": "vite build && npm run package", 30 | "preview": "vite preview", 31 | "package": "svelte-kit sync && svelte-package && publint", 32 | "prepublishOnly": "npm run package", 33 | "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", 34 | "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch", 35 | "lint": "prettier --plugin-search-dir . --check . && eslint .", 36 | "format": "prettier --plugin-search-dir . --write ." 37 | }, 38 | "exports": { 39 | ".": { 40 | "types": "./dist/index.d.ts", 41 | "svelte": "./dist/index.js" 42 | } 43 | }, 44 | "files": [ 45 | "dist" 46 | ], 47 | "peerDependencies": { 48 | "@wagmi/connectors": "^5.0.14", 49 | "@wagmi/core": "^2.0.0", 50 | "svelte": "^4.0.0 || ^5.0.0", 51 | "vite": "^4.0.0 || ^5.0.0" 52 | }, 53 | "devDependencies": { 54 | "@originjs/vite-plugin-commonjs": "^1.0.3", 55 | "@sveltejs/adapter-auto": "^3.2.0", 56 | "@sveltejs/kit": "^2.5.5", 57 | "@sveltejs/package": "^2.0.0", 58 | "@typescript-eslint/eslint-plugin": "^7.6.0", 59 | "@typescript-eslint/parser": "^7.6.0", 60 | "@wagmi/core": "^2.11.2", 61 | "eslint": "^9.4.0", 62 | "eslint-config-prettier": "^9.0.0", 63 | "prettier": "^3.0.0", 64 | "prettier-plugin-svelte": "^3.0.0", 65 | "publint": "^0.2.2", 66 | "svelte": "^4.0.0", 67 | "svelte-check": "^3.4.3", 68 | "tslib": "^2.4.1", 69 | "typescript": "^5.0.0", 70 | "viem": "^2.9.16", 71 | "vite": "^5.2.8", 72 | "vite-plugin-node-polyfills": "^0.22.0" 73 | }, 74 | "svelte": "./dist/index.js", 75 | "types": "./dist/index.d.ts", 76 | "type": "module", 77 | "dependencies": { 78 | "@web3modal/core": "^5.0.1", 79 | "@web3modal/ui": "^5.0.1", 80 | "@web3modal/wagmi": "^5.0.1" 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/app.d.ts: -------------------------------------------------------------------------------- 1 | // See https://kit.svelte.dev/docs/types#app 2 | // for information about these interfaces 3 | declare global { 4 | namespace App { 5 | // interface Error {} 6 | // interface Locals {} 7 | // interface PageData {} 8 | // interface Platform {} 9 | } 10 | } 11 | 12 | export {}; 13 | -------------------------------------------------------------------------------- /src/app.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %sveltekit.head% 8 | 9 | 10 |
%sveltekit.body%
11 | 12 | 13 | -------------------------------------------------------------------------------- /src/lib/abi/USDC.json: -------------------------------------------------------------------------------- 1 | [ 2 | { "inputs": [], "stateMutability": "nonpayable", "type": "constructor" }, 3 | { 4 | "anonymous": false, 5 | "inputs": [ 6 | { 7 | "indexed": true, 8 | "internalType": "address", 9 | "name": "owner", 10 | "type": "address" 11 | }, 12 | { 13 | "indexed": true, 14 | "internalType": "address", 15 | "name": "spender", 16 | "type": "address" 17 | }, 18 | { 19 | "indexed": false, 20 | "internalType": "uint256", 21 | "name": "value", 22 | "type": "uint256" 23 | } 24 | ], 25 | "name": "Approval", 26 | "type": "event" 27 | }, 28 | { 29 | "anonymous": false, 30 | "inputs": [ 31 | { 32 | "indexed": true, 33 | "internalType": "address", 34 | "name": "from", 35 | "type": "address" 36 | }, 37 | { 38 | "indexed": true, 39 | "internalType": "address", 40 | "name": "to", 41 | "type": "address" 42 | }, 43 | { 44 | "indexed": false, 45 | "internalType": "uint256", 46 | "name": "value", 47 | "type": "uint256" 48 | } 49 | ], 50 | "name": "Transfer", 51 | "type": "event" 52 | }, 53 | { 54 | "inputs": [ 55 | { "internalType": "address", "name": "owner", "type": "address" }, 56 | { "internalType": "address", "name": "spender", "type": "address" } 57 | ], 58 | "name": "allowance", 59 | "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], 60 | "stateMutability": "view", 61 | "type": "function" 62 | }, 63 | { 64 | "inputs": [ 65 | { "internalType": "address", "name": "spender", "type": "address" }, 66 | { "internalType": "uint256", "name": "amount", "type": "uint256" } 67 | ], 68 | "name": "approve", 69 | "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }], 70 | "stateMutability": "nonpayable", 71 | "type": "function" 72 | }, 73 | { 74 | "inputs": [{ "internalType": "address", "name": "account", "type": "address" }], 75 | "name": "balanceOf", 76 | "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], 77 | "stateMutability": "view", 78 | "type": "function" 79 | }, 80 | { 81 | "inputs": [], 82 | "name": "decimals", 83 | "outputs": [{ "internalType": "uint8", "name": "", "type": "uint8" }], 84 | "stateMutability": "view", 85 | "type": "function" 86 | }, 87 | { 88 | "inputs": [ 89 | { "internalType": "address", "name": "spender", "type": "address" }, 90 | { 91 | "internalType": "uint256", 92 | "name": "subtractedValue", 93 | "type": "uint256" 94 | } 95 | ], 96 | "name": "decreaseAllowance", 97 | "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }], 98 | "stateMutability": "nonpayable", 99 | "type": "function" 100 | }, 101 | { 102 | "inputs": [ 103 | { "internalType": "address", "name": "spender", "type": "address" }, 104 | { "internalType": "uint256", "name": "addedValue", "type": "uint256" } 105 | ], 106 | "name": "increaseAllowance", 107 | "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }], 108 | "stateMutability": "nonpayable", 109 | "type": "function" 110 | }, 111 | { 112 | "inputs": [ 113 | { "internalType": "address", "name": "account_", "type": "address" }, 114 | { "internalType": "uint256", "name": "amount_", "type": "uint256" } 115 | ], 116 | "name": "mint", 117 | "outputs": [], 118 | "stateMutability": "nonpayable", 119 | "type": "function" 120 | }, 121 | { 122 | "inputs": [], 123 | "name": "name", 124 | "outputs": [{ "internalType": "string", "name": "", "type": "string" }], 125 | "stateMutability": "view", 126 | "type": "function" 127 | }, 128 | { 129 | "inputs": [], 130 | "name": "symbol", 131 | "outputs": [{ "internalType": "string", "name": "", "type": "string" }], 132 | "stateMutability": "view", 133 | "type": "function" 134 | }, 135 | { 136 | "inputs": [], 137 | "name": "totalSupply", 138 | "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], 139 | "stateMutability": "view", 140 | "type": "function" 141 | }, 142 | { 143 | "inputs": [ 144 | { "internalType": "address", "name": "to", "type": "address" }, 145 | { "internalType": "uint256", "name": "amount", "type": "uint256" } 146 | ], 147 | "name": "transfer", 148 | "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }], 149 | "stateMutability": "nonpayable", 150 | "type": "function" 151 | }, 152 | { 153 | "inputs": [ 154 | { "internalType": "address", "name": "from", "type": "address" }, 155 | { "internalType": "address", "name": "to", "type": "address" }, 156 | { "internalType": "uint256", "name": "amount", "type": "uint256" } 157 | ], 158 | "name": "transferFrom", 159 | "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }], 160 | "stateMutability": "nonpayable", 161 | "type": "function" 162 | } 163 | ] 164 | -------------------------------------------------------------------------------- /src/lib/index.ts: -------------------------------------------------------------------------------- 1 | // Reexport your entry components here 2 | import { 3 | connected, 4 | chainId, 5 | signerAddress, 6 | loading, 7 | web3Modal, 8 | wagmiLoaded, 9 | wagmiConfig, 10 | init, 11 | WC, 12 | disconnectWagmi, 13 | defaultConfig, 14 | configuredConnectors 15 | } from './stores/wagmi'; 16 | 17 | export { 18 | defaultConfig, 19 | configuredConnectors, 20 | wagmiLoaded, 21 | connected, 22 | chainId, 23 | init, 24 | wagmiConfig, 25 | web3Modal, 26 | signerAddress, 27 | loading, 28 | WC, 29 | disconnectWagmi 30 | }; 31 | -------------------------------------------------------------------------------- /src/lib/stores/wagmi.ts: -------------------------------------------------------------------------------- 1 | import { writable, get } from 'svelte/store'; 2 | import { 3 | createConfig, 4 | getAccount, 5 | disconnect, 6 | watchAccount, 7 | reconnect, 8 | type CreateConnectorFn, 9 | type GetAccountReturnType, 10 | type Config, 11 | http 12 | } from '@wagmi/core'; 13 | import { mainnet, polygon, optimism, arbitrum, type Chain } from '@wagmi/core/chains'; 14 | import { createWeb3Modal, type Web3Modal } from '@web3modal/wagmi'; 15 | 16 | export const connected = writable(false); 17 | export const wagmiLoaded = writable(false); 18 | export const chainId = writable(null); 19 | export const signerAddress = writable(null); 20 | export const configuredConnectors = writable([]); 21 | export const loading = writable(true); 22 | export const web3Modal = writable(); 23 | export const wagmiConfig = writable(); 24 | 25 | type DefaultConfigProps = { 26 | appName: string; 27 | appIcon?: string | null; 28 | appDescription?: string | null; 29 | appUrl?: string | null; 30 | autoConnect?: boolean; 31 | alchemyId?: string | null; 32 | chains?: Chain[] | null; 33 | connectors: CreateConnectorFn[]; 34 | walletConnectProjectId: string; 35 | }; 36 | const defaultChains = [mainnet, polygon, optimism, arbitrum]; 37 | 38 | export const defaultConfig = ({ 39 | autoConnect = true, 40 | chains = defaultChains, 41 | alchemyId, 42 | connectors, 43 | walletConnectProjectId 44 | }: DefaultConfigProps) => { 45 | if (connectors) configuredConnectors.set(connectors); 46 | 47 | //add email connector 48 | configuredConnectors.update((connectors) => [...connectors]); 49 | 50 | const url = alchemyId ? http(`https://eth-mainnet.g.alchemy.com/v2/${alchemyId}`) : http(); 51 | 52 | const chainsToUse = chains ? chains.map((chain) => chain) : []; 53 | const transports = chains 54 | ? chains.reduce( 55 | (acc, chain) => ({ 56 | ...acc, 57 | [chain.id]: url 58 | }), 59 | {} 60 | ) 61 | : {}; 62 | 63 | const config = createConfig({ 64 | chains: chainsToUse as [Chain, ...Chain[]], 65 | transports, 66 | connectors: get(configuredConnectors) 67 | }); 68 | 69 | wagmiConfig.set(config); 70 | 71 | if (autoConnect) reconnect(config); 72 | 73 | const modal = createWeb3Modal({ 74 | wagmiConfig: config, 75 | projectId: walletConnectProjectId, 76 | enableAnalytics: true, // Optional - defaults to your Cloud configuration 77 | enableOnramp: true // Optional - false as default 78 | }); 79 | 80 | web3Modal.set(modal); 81 | wagmiLoaded.set(true); 82 | 83 | return { init }; 84 | }; 85 | 86 | export const init = async () => { 87 | try { 88 | setupListeners(); 89 | const account = await waitForConnection(); 90 | if (account.address) { 91 | const chain = get(wagmiConfig).chains.find((chain) => chain.id === account.chainId); 92 | if (chain) chainId.set(chain.id); 93 | connected.set(true); 94 | signerAddress.set(account.address); 95 | } 96 | loading.set(false); 97 | } catch (err) { 98 | loading.set(false); 99 | } 100 | }; 101 | 102 | const setupListeners = () => { 103 | watchAccount(get(wagmiConfig), { 104 | onChange(data) { 105 | handleAccountChange(data); 106 | } 107 | }); 108 | }; 109 | 110 | const handleAccountChange = (data: GetAccountReturnType) => { 111 | // Wrap the original async logic in an immediately invoked function expression (IIFE) 112 | return (async () => { 113 | if (get(wagmiLoaded) && data.address) { 114 | const chain = get(wagmiConfig).chains.find((chain) => chain.id === data.chainId); 115 | 116 | if (chain) chainId.set(chain.id); 117 | connected.set(true); 118 | loading.set(false); 119 | signerAddress.set(data.address); 120 | } else if (data.isDisconnected && get(connected)) { 121 | loading.set(false); 122 | await disconnectWagmi(); // Handle async operation inside 123 | } 124 | })(); 125 | }; 126 | 127 | export const WC = async () => { 128 | try { 129 | get(web3Modal).open(); 130 | await waitForAccount(); 131 | 132 | return { succcess: true }; 133 | } catch (err) { 134 | return { success: false }; 135 | } 136 | }; 137 | 138 | export const disconnectWagmi = async () => { 139 | await disconnect(get(wagmiConfig)); 140 | connected.set(false); 141 | chainId.set(null); 142 | signerAddress.set(null); 143 | loading.set(false); 144 | }; 145 | 146 | const waitForAccount = () => { 147 | return new Promise((resolve, reject) => { 148 | const unsub1 = get(web3Modal).subscribeEvents((newState) => { 149 | if (newState.data.event === 'MODAL_CLOSE') { 150 | reject('modal closed'); 151 | unsub1(); 152 | } 153 | }); 154 | const unsub = watchAccount(get(wagmiConfig), { 155 | onChange(data) { 156 | if (data?.isConnected) { 157 | // Gottem, resolve the promise w/user's selected & connected Acc. 158 | resolve(data); 159 | unsub(); 160 | } else { 161 | console.warn('🔃 - No Account Connected Yet...'); 162 | } 163 | } 164 | }); 165 | }); 166 | }; 167 | 168 | const waitForConnection = (): Promise => 169 | new Promise((resolve, reject) => { 170 | const attemptToGetAccount = () => { 171 | const account = getAccount(get(wagmiConfig)); 172 | if (account.isDisconnected) reject('account is disconnected'); 173 | if (account.isConnecting) { 174 | setTimeout(attemptToGetAccount, 250); 175 | } else { 176 | resolve(account); 177 | } 178 | }; 179 | 180 | attemptToGetAccount(); 181 | }); 182 | -------------------------------------------------------------------------------- /src/routes/+page.svelte: -------------------------------------------------------------------------------- 1 | 49 | 50 |
51 | {#if wagmiLoaded} 52 |

Svelte Wagmi

53 |

54 | Svelte Wagmi is a package that provides a collection of Svelte stores and functions for 55 | interacting with the Ethereum network. It utilizes the @wagmi/core library for connecting to 56 | Ethereum networks and signing transactions. 57 |

58 | {#if $loading} 59 |
60 | Waiting... 61 |
62 | {:else if $connected} 63 |

{$signerAddress}

64 |

chain ID: {$chainId}

65 | 66 | 67 | {:else} 68 |

not connected

69 |

Connect With walletconnect

70 | 83 | 84 |

Connect With InjectedConnector

85 | 100 | {/if} 101 | {:else} 102 |

Svelte Wagmi Not Configured

103 | {/if} 104 |
105 | 106 | 159 | -------------------------------------------------------------------------------- /static/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/softwarecurator/svelte-wagmi/37fa093c38e7ea857c07748dbdf309f9340c5821/static/favicon.png -------------------------------------------------------------------------------- /svelte.config.js: -------------------------------------------------------------------------------- 1 | import adapter from '@sveltejs/adapter-auto'; 2 | import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'; 3 | 4 | /** @type {import('@sveltejs/kit').Config} */ 5 | const config = { 6 | // Consult https://kit.svelte.dev/docs/integrations#preprocessors 7 | // for more information about preprocessors 8 | preprocess: vitePreprocess(), 9 | commonjsOptions: { 10 | transformMixedEsModules: true 11 | }, 12 | kit: { 13 | // adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list. 14 | // If your environment is not supported or you settled on a specific environment, switch out the adapter. 15 | // See https://kit.svelte.dev/docs/adapters for more information about adapters. 16 | adapter: adapter() 17 | } 18 | }; 19 | 20 | export default config; 21 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./.svelte-kit/tsconfig.json", 3 | "compilerOptions": { 4 | "allowJs": true, 5 | "checkJs": true, 6 | "esModuleInterop": true, 7 | "forceConsistentCasingInFileNames": true, 8 | "resolveJsonModule": true, 9 | "skipLibCheck": true, 10 | "sourceMap": true, 11 | "strict": true 12 | } 13 | // Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias 14 | // 15 | // If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes 16 | // from the referenced tsconfig.json - TypeScript does not merge them in 17 | } 18 | -------------------------------------------------------------------------------- /vite.config.ts: -------------------------------------------------------------------------------- 1 | import { sveltekit } from '@sveltejs/kit/vite'; 2 | import { nodePolyfills } from 'vite-plugin-node-polyfills'; 3 | import { defineConfig } from 'vite'; 4 | import { esbuildCommonjs } from '@originjs/vite-plugin-commonjs'; 5 | 6 | export default defineConfig({ 7 | plugins: [ 8 | nodePolyfills({ 9 | exclude: ['fs'], 10 | globals: { 11 | Buffer: true, 12 | global: true, 13 | process: true 14 | }, 15 | protocolImports: true 16 | }), 17 | sveltekit() 18 | ], 19 | build: { 20 | commonjsOptions: { 21 | transformMixedEsModules: true 22 | } 23 | }, 24 | optimizeDeps: { 25 | esbuildOptions: { 26 | plugins: [esbuildCommonjs(['@wagmi/core'])] 27 | } 28 | } 29 | }); 30 | --------------------------------------------------------------------------------