Skip to content

Commit

Permalink
web3modal documentation (#7041)
Browse files Browse the repository at this point in the history
* add web3modal guide

* update docs

* update link

* update link

* update examples

* update docs positions

* update emoji
  • Loading branch information
Alex authored May 16, 2024
1 parent 866469d commit 2f73aa5
Show file tree
Hide file tree
Showing 7 changed files with 261 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/docs/guides/advanced/_category_.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ label: '🧠 Advanced'
collapsible: true
collapsed: true
link: null
position: 14
position: 15
2 changes: 1 addition & 1 deletion docs/docs/guides/feedback/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 17
sidebar_position: 18
sidebar_label: '🗣️ Feedback'
---

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/guides/resources_and_troubleshooting/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 16
sidebar_position: 17
sidebar_label: '📚 Resources & Troubleshooting'
---
# Resources & Troubleshooting
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/guides/web3_config/_category_.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ label: '⚙️ Web3 config'
collapsible: true
collapsed: true
link: null
position: 15
position: 16
5 changes: 5 additions & 0 deletions docs/docs/guides/web3_modal_guide/_category_.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
label: '📱 WalletConnect Tutorial'
collapsible: true
collapsed: true
link: null
position: 14
130 changes: 130 additions & 0 deletions docs/docs/guides/web3_modal_guide/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
---
sidebar_position: 1
sidebar_label: 'Getting started'
---

# Getting Started

Welcome to the Web3modal📱 Guide.

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.

In this guide, you will learn how to set up Walletconnect with web3js.



## Preview
:::info
Switch your browsers if the preview doesn't load.
:::
<iframe width="100%" height="700px" src="https://stackblitz.com/edit/vitejs-vite-cg7ctd?embed=1&file=src%2FApp.tsx"></iframe>



## Installation

```bash
npm install web3modal-web3js web3js
```

## Implementation

```typescript

import { createWeb3Modal, defaultConfig } from 'web3modal-web3/react'

// 1. Get projectId, Your Project ID can be obtained from walletconnect.com
const projectId = 'YOUR_PROJECT_ID'

// 2. Set chains
const mainnet = {
chainId: 1,
name: 'Ethereum',
currency: 'ETH',
explorerUrl: 'https://etherscan.io',
rpcUrl: 'https://cloudflare-eth.com'
}

// 3. Create a metadata object
const metadata = {
name: 'My Website',
description: 'My Website description',
url: 'https://mywebsite.com', // origin must match your domain & subdomain
icons: ['https://avatars.mywebsite.com/']
}

// 4. Create web3 config
const web3Config = defaultConfig({
/*Required*/
metadata,

/*Optional*/
enableEIP6963: true, // true by default
enableInjected: true, // true by default
enableCoinbase: true, // true by default
rpcUrl: '...', // used for the Coinbase SDK
defaultChainId: 1, // used for the Coinbase SDK
})

// 5. Create a Web3Modal instance
createWeb3Modal({
ethersConfig,
chains: [mainnet],
projectId,
enableAnalytics: true // Optional - defaults to your Cloud configuration
})

export default function App() {
return <YourApp />
}
```

## Triggering the modal

```Typescript

export default function ConnectButton() {
return <w3m-button/>
}

```
## Smart Contract Interaction
<iframe width="100%" height="700px" src="https://stackblitz.com/edit/vitejs-vite-itfrzf?embed=1&file=src%2FApp.tsx"></iframe>
Web3js can help us interact with wallets and smart contracts:
```Typescript
import Web3 from 'web3';
import { ERC20ABI } from './contracts/ERC20';

const USDTAddress = '0xdac17f958d2ee523a2206206994597c13d831ec7';

function Components() {
const { isConnected } = useWeb3ModalAccount()
const { walletProvider } = useWeb3ModalProvider()
const [USDTBalance, setUSDTBalance] = useState(0);
const [smartContractName, setSmartContractName] = useState('');

async function getContractInfo() {
if (!isConnected) throw Error('not connected');
const web3 = new Web3({
provider: walletProvider,
config: { defaultNetworkId: chainId },
});
const contract = new web3.eth.Contract(ERC20ABI, USDTAddress);
const balance = await contract.methods.balanceOf(address).call();
const name = (await contract.methods.name().call()) as string;
setUSDTBalance(Number(balance));
setSmartContractName(name);
}

return <> <button onClick={getContractInfo}>Get User Balance and Contract name</button> <p> Balance: {USDTBalance} smartContractName: {smartContractName}</p></>
}

```
:::info
- To learn how to set up Web3modal with vue, click [here](/guides/web3_modal_guide/vue).
:::
122 changes: 122 additions & 0 deletions docs/docs/guides/web3_modal_guide/vue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
---
sidebar_position: 1
sidebar_label: 'Web3Modal with Vue'
---

# Web3Modal with Vue and web3js

## Live code editor

<iframe width="100%" height="700px" src="https://stackblitz.com/edit/vitejs-vite-cg7ctd?embed=1&file=src%2FApp.tsx"></iframe>



## Installation

For this guide we will be creating a new project will need to install dependancies. We will be using vite to locally host the app, React and web3modal-web3js

```bash
npm install web3modal-web3js react react-dom
npm install --save-dev vite @vitejs/plugin-react
```

## Implementation

Within the root of the project create `index.html`
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>React Web3 example</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
```

Now we will add the Web3modal code within `src/Web3modal.tsx`
```typescript

import { createWeb3Modal, defaultConfig } from 'web3modal-web3/react'

// 1. Get projectId, Your Project ID can be obtained from walletconnect.com
const projectId = 'YOUR_PROJECT_ID'

// 2. Set chains
const mainnet = {
chainId: 1,
name: 'Ethereum',
currency: 'ETH',
explorerUrl: 'https://etherscan.io',
rpcUrl: 'https://cloudflare-eth.com'
}

// 3. Create a metadata object
const metadata = {
name: 'My Website',
description: 'My Website description',
url: 'https://mywebsite.com', // origin must match your domain & subdomain
icons: ['https://avatars.mywebsite.com/']
}

// 4. Create web3 config
const web3Config = defaultConfig({
/*Required*/
metadata,

/*Optional*/
enableEIP6963: true, // true by default
enableInjected: true, // true by default
enableCoinbase: true, // true by default
rpcUrl: '...', // used for the Coinbase SDK
defaultChainId: 1, // used for the Coinbase SDK
})

// 5. Create a Web3Modal instance
createWeb3Modal({
ethersConfig,
chains: [mainnet],
projectId,
enableAnalytics: true // Optional - defaults to your Cloud configuration
})

export default function App() {
return <YourApp />
}
```

Set up vite configs within root `vite.config.js`
```javascript
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'

export default defineConfig({
plugins: [react()]
})
```
And finally add react to the app `src/main.tsx`
```typescript
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.js'

ReactDOM.createRoot(document.getElementById('app')!).render(
<React.StrictMode>
<App />
</React.StrictMode>
)
```
You are finished and have successfully created Web3modal with Vue!
:::info
- For additional information take a look into the interactive code editor above.
- You can view different examples of setting up walletconnect with web3.js [here](https://github.com/ChainSafe/web3modal/tree/add-examples/examples/vue-web3)
- Learn more about Web3modal [here](https://docs.walletconnect.com/web3modal/about)
:::

1 comment on commit 2f73aa5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 2f73aa5 Previous: 866469d Ratio
processingTx 9204 ops/sec (±3.98%) 8892 ops/sec (±3.98%) 0.97
processingContractDeploy 38039 ops/sec (±7.10%) 38535 ops/sec (±6.62%) 1.01
processingContractMethodSend 19205 ops/sec (±6.35%) 18875 ops/sec (±7.64%) 0.98
processingContractMethodCall 40094 ops/sec (±5.17%) 40251 ops/sec (±5.97%) 1.00
abiEncode 43919 ops/sec (±6.46%) 42727 ops/sec (±6.75%) 0.97
abiDecode 29754 ops/sec (±7.79%) 29052 ops/sec (±7.20%) 0.98
sign 1568 ops/sec (±0.52%) 1560 ops/sec (±0.80%) 0.99
verify 372 ops/sec (±0.68%) 371 ops/sec (±0.49%) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.