|
| 1 | +--- |
| 2 | +sidebar_position: 1 |
| 3 | +sidebar_label: 'Getting started' |
| 4 | +--- |
| 5 | + |
| 6 | +# Getting Started |
| 7 | + |
| 8 | +Welcome to the Web3modal📱 Guide. |
| 9 | + |
| 10 | +The Web3Modal SDK allows you to easily connect your Web3 app with wallets. It provides a simple and intuitive interface for requesting actions such as signing transactions and interacting with smart contracts on the blockchain. |
| 11 | + |
| 12 | +In this guide, you will learn how to set up Walletconnect with web3js. |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +## Preview |
| 17 | +:::info |
| 18 | +Switch your browsers if the preview doesn't load. |
| 19 | +::: |
| 20 | +<iframe width="100%" height="700px" src="https://stackblitz.com/edit/vitejs-vite-cg7ctd?embed=1&file=src%2FApp.tsx"></iframe> |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +```bash |
| 27 | +npm install web3modal-web3js web3js |
| 28 | +``` |
| 29 | + |
| 30 | +## Implementation |
| 31 | + |
| 32 | +```typescript |
| 33 | + |
| 34 | +import { createWeb3Modal, defaultConfig } from 'web3modal-web3/react' |
| 35 | + |
| 36 | +// 1. Get projectId, Your Project ID can be obtained from walletconnect.com |
| 37 | +const projectId = 'YOUR_PROJECT_ID' |
| 38 | + |
| 39 | +// 2. Set chains |
| 40 | +const mainnet = { |
| 41 | + chainId: 1, |
| 42 | + name: 'Ethereum', |
| 43 | + currency: 'ETH', |
| 44 | + explorerUrl: 'https://etherscan.io', |
| 45 | + rpcUrl: 'https://cloudflare-eth.com' |
| 46 | +} |
| 47 | + |
| 48 | +// 3. Create a metadata object |
| 49 | +const metadata = { |
| 50 | + name: 'My Website', |
| 51 | + description: 'My Website description', |
| 52 | + url: 'https://mywebsite.com', // origin must match your domain & subdomain |
| 53 | + icons: ['https://avatars.mywebsite.com/'] |
| 54 | +} |
| 55 | + |
| 56 | +// 4. Create web3 config |
| 57 | +const web3Config = defaultConfig({ |
| 58 | + /*Required*/ |
| 59 | + metadata, |
| 60 | + |
| 61 | + /*Optional*/ |
| 62 | + enableEIP6963: true, // true by default |
| 63 | + enableInjected: true, // true by default |
| 64 | + enableCoinbase: true, // true by default |
| 65 | + rpcUrl: '...', // used for the Coinbase SDK |
| 66 | + defaultChainId: 1, // used for the Coinbase SDK |
| 67 | +}) |
| 68 | + |
| 69 | +// 5. Create a Web3Modal instance |
| 70 | +createWeb3Modal({ |
| 71 | + ethersConfig, |
| 72 | + chains: [mainnet], |
| 73 | + projectId, |
| 74 | + enableAnalytics: true // Optional - defaults to your Cloud configuration |
| 75 | +}) |
| 76 | + |
| 77 | +export default function App() { |
| 78 | + return <YourApp /> |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +## Triggering the modal |
| 83 | + |
| 84 | +```Typescript |
| 85 | + |
| 86 | +export default function ConnectButton() { |
| 87 | + return <w3m-button/> |
| 88 | +} |
| 89 | + |
| 90 | +``` |
| 91 | +
|
| 92 | +## Smart Contract Interaction |
| 93 | +
|
| 94 | +<iframe width="100%" height="700px" src="https://stackblitz.com/edit/vitejs-vite-itfrzf?embed=1&file=src%2FApp.tsx"></iframe> |
| 95 | +
|
| 96 | +Web3js can help us interact with wallets and smart contracts: |
| 97 | +
|
| 98 | +```Typescript |
| 99 | +import Web3 from 'web3'; |
| 100 | +import { ERC20ABI } from './contracts/ERC20'; |
| 101 | + |
| 102 | +const USDTAddress = '0xdac17f958d2ee523a2206206994597c13d831ec7'; |
| 103 | + |
| 104 | +function Components() { |
| 105 | + const { isConnected } = useWeb3ModalAccount() |
| 106 | + const { walletProvider } = useWeb3ModalProvider() |
| 107 | + const [USDTBalance, setUSDTBalance] = useState(0); |
| 108 | + const [smartContractName, setSmartContractName] = useState(''); |
| 109 | + |
| 110 | + async function getContractInfo() { |
| 111 | + if (!isConnected) throw Error('not connected'); |
| 112 | + const web3 = new Web3({ |
| 113 | + provider: walletProvider, |
| 114 | + config: { defaultNetworkId: chainId }, |
| 115 | + }); |
| 116 | + const contract = new web3.eth.Contract(ERC20ABI, USDTAddress); |
| 117 | + const balance = await contract.methods.balanceOf(address).call(); |
| 118 | + const name = (await contract.methods.name().call()) as string; |
| 119 | + setUSDTBalance(Number(balance)); |
| 120 | + setSmartContractName(name); |
| 121 | + } |
| 122 | + |
| 123 | + return <> <button onClick={getContractInfo}>Get User Balance and Contract name</button> <p> Balance: {USDTBalance} smartContractName: {smartContractName}</p></> |
| 124 | +} |
| 125 | + |
| 126 | +``` |
| 127 | +
|
| 128 | +:::info |
| 129 | +- To learn how to set up Web3modal with vue, click [here](/guides/web3_modal_guide/vue). |
| 130 | +::: |
0 commit comments