NetworkForm.svelte (6169B)
1 <script lang="ts"> 2 import { numberToHex } from 'viem' 3 import type { Chain } from '@wagmi/chains' 4 import { configuredChain, votingConfig } from '../store' 5 $configuredChain 6 $votingConfig 7 8 type ConfigFormData = { 9 rpcEndpoint: string 10 chainName: string 11 currencyName: string 12 currencySymbol: string 13 chainId: number 14 decimals: number 15 contractAddress: string 16 erc20ContractAddress: string 17 } 18 19 let configData: ConfigFormData = { 20 rpcEndpoint: $configuredChain.rpcUrls.default.http[0], 21 chainName: $configuredChain.name, 22 currencyName: $configuredChain.nativeCurrency.name, 23 currencySymbol: $configuredChain.nativeCurrency.symbol, 24 chainId: $configuredChain.id, 25 decimals: $configuredChain.nativeCurrency.decimals, 26 contractAddress: $votingConfig.contractAddress, 27 erc20ContractAddress: $votingConfig.erc20ContractAddress 28 } 29 30 const onSubmit = async () => { 31 await addNetworkToWallet(configData) 32 } 33 34 async function addNetworkToWallet(configData: ConfigFormData) { 35 if (!window.ethereum) alert('Please install a wallet to continue') 36 try { 37 updateConfiguredChain() 38 votingConfig.updateVotingConfig({ 39 contractAddress: configData.contractAddress, 40 erc20ContractAddress: configData.erc20ContractAddress 41 }) 42 43 await window.ethereum.request({ 44 method: 'wallet_addEthereumChain', 45 params: [ 46 { 47 chainId: numberToHex(configData.chainId), 48 rpcUrls: [configData.rpcEndpoint], 49 chainName: configData.chainName, 50 nativeCurrency: { 51 name: configData.currencyName, 52 symbol: configData.currencySymbol, 53 decimals: configData.decimals 54 }, 55 blockExplorerUrls: ['https://example.com'] 56 } 57 ] 58 }) 59 } catch (error) { 60 console.log(error) 61 } 62 } 63 64 const updateConfiguredChain = () => { 65 const updatedChain: Chain = { 66 id: configData.chainId, 67 name: configData.chainName, 68 rpcUrls: { 69 default: { 70 http: [configData.rpcEndpoint], 71 webSocket: undefined 72 }, 73 public: { 74 http: [], 75 webSocket: undefined 76 } 77 }, 78 nativeCurrency: { 79 name: configData.currencyName, 80 symbol: configData.currencySymbol, 81 decimals: configData.decimals 82 }, 83 network: $configuredChain.name 84 } 85 configuredChain.updateChain(updatedChain) 86 } 87 </script> 88 89 <div class="flex justify-center items-center w-full py-10"> 90 <div class="flex w-full items-center justify-center"> 91 <div class="card w-full sm:mx-2 md:w-4/5 lg:w-3/5 xl:w-2/5 m-2 md:m-0 shadow-xl bg-white"> 92 <div class="card-body"> 93 <h2 class="text-gray-900 text-center w-full font-semibold text-xl">Network Details</h2> 94 <form 95 on:submit|preventDefault={onSubmit} 96 class="flex flex-col justify-center w-full flex-1" 97 > 98 <div class="form-control w-full"> 99 <label for="rpcUrl" class="label"> 100 <span class="label-text text-gray-800">RPC endpoint</span> 101 </label> 102 <input 103 name="rpcUrl" 104 type="url" 105 placeholder="Enter RPC endpoint" 106 class="input input-bordered w-full" 107 bind:value={configData.rpcEndpoint} 108 /> 109 </div> 110 111 <div class="flex flex-col md:flex-row w-full gap-3 mt-3"> 112 <div class="form-control w-full"> 113 <label for="chainName" class="label"> 114 <span class="label-text text-gray-800">Chain Name</span> 115 </label> 116 <input 117 name="chainName" 118 type="text" 119 placeholder="Enter chain name" 120 class="input input-bordered w-full" 121 bind:value={configData.chainName} 122 /> 123 </div> 124 125 <div class="form-control w-full"> 126 <label for="currencyName" class="label"> 127 <span class="label-text text-gray-800">Currency Name</span> 128 </label> 129 <input 130 name="currencyName" 131 type="text" 132 placeholder="Enter currency name" 133 class="input input-bordered w-full" 134 bind:value={configData.currencyName} 135 /> 136 </div> 137 </div> 138 139 <div class="flex flex-col md:flex-row w-full gap-3 mt-3"> 140 <div class="form-control w-full"> 141 <label for="currencySymbol" class="label"> 142 <span class="label-text text-gray-800">Currency symbol</span> 143 </label> 144 <input 145 name="currencySymbol" 146 type="text" 147 placeholder="Enter currency symbol(2-6 characters)" 148 class="input input-bordered w-full" 149 minlength="2" 150 maxlength="6" 151 bind:value={configData.currencySymbol} 152 /> 153 </div> 154 155 <div class="form-control w-full"> 156 <label for="chainId" class="label"> 157 <span class="label-text text-gray-800">Chain ID</span> 158 </label> 159 <input 160 name="chainId" 161 type="number" 162 placeholder="Enter chainId" 163 class="input input-bordered w-full" 164 bind:value={configData.chainId} 165 /> 166 </div> 167 <div class="form-control w-full"> 168 <label for="chainId" class="label"> 169 <span class="label-text text-gray-800">Decimals</span> 170 </label> 171 <input 172 name="chainId" 173 type="number" 174 placeholder="Enter decimals" 175 class="input input-bordered w-full" 176 bind:value={configData.decimals} 177 /> 178 </div> 179 </div> 180 181 <div class="form-control w-full mt-3"> 182 <label for="contractAddress" class="label"> 183 <span class="label-text text-gray-800">Contract address</span> 184 </label> 185 <input 186 name="contractAddress" 187 type="text" 188 placeholder="Enter voting contract address" 189 class="input input-bordered w-full" 190 bind:value={configData.contractAddress} 191 /> 192 </div> 193 194 <div class="form-control w-full mt-3"> 195 <label for="erc20ContractAddress" class="label"> 196 <span class="label-text text-gray-800">ERC-20 Contract address</span> 197 </label> 198 <input 199 name="erc20ContractAddress" 200 type="text" 201 placeholder="Enter ERC-20 contract address" 202 class="input input-bordered w-full" 203 bind:value={configData.erc20ContractAddress} 204 /> 205 </div> 206 <button type="submit" class="btn btn-primary w-40 self-center mt-6 normal-case text-white" 207 >Add to wallet</button 208 > 209 </form> 210 </div> 211 </div> 212 </div> 213 </div>