[Project] Basic and Advanced Web Examples (#288)
* Chore: init * chore: init * feat: added basic dapps and improved README * fix: Unified env.example and added comments with instructions * fix: better usage instruction in readme * fix: removed advanced section * fix: added dapps and wallets * fix: added web3inbox * Update basic/dapps/web3inbox/README.md Co-authored-by: Ben Kremer <ben@walletconnect.com> * Update basic/dapps/ethereum-provider/src/App.tsx Co-authored-by: Gancho Radkov <43912948+ganchoradkov@users.noreply.github.com> * chore: added detailed README for basic section * chore(deps): updated deps for universal-provider-solana * fix: multiple suggestions --------- Co-authored-by: Boidushya Bhattacharya <boidushyabhattacharya@gmail.com> Co-authored-by: Ben Kremer <ben@walletconnect.com> Co-authored-by: Gancho Radkov <43912948+ganchoradkov@users.noreply.github.com>
This commit is contained in:
parent
c3af6c0ec0
commit
8b22738217
1
.gitignore
vendored
1
.gitignore
vendored
@ -27,6 +27,7 @@ yarn-debug.log*
|
||||
yarn-error.log*
|
||||
|
||||
# local env files
|
||||
.env
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
|
20
basic/README.md
Normal file
20
basic/README.md
Normal file
@ -0,0 +1,20 @@
|
||||
# Basic Web Examples
|
||||
|
||||
Basic examples/templates for using WalletConnect SDKs
|
||||
|
||||
## Table Of Contents:
|
||||
|
||||
```bash
|
||||
.
|
||||
├── dapps/
|
||||
│ ├── web3modal
|
||||
│ ├── web3inbox(TODO)
|
||||
│ ├── ethereum-provider
|
||||
│ ├── universal-provider-solana
|
||||
│ └── universal-provider-cosmos(TODO)
|
||||
└── wallets/
|
||||
├── web3wallet(TODO)
|
||||
├── web3wallet-ethers(TODO)
|
||||
├── web3wallet-viem(TODO)
|
||||
└── web3wallet-wagmi(TODO)
|
||||
```
|
1
basic/dapps/ethereum-provider/.codesandbox/Dockerfile
Normal file
1
basic/dapps/ethereum-provider/.codesandbox/Dockerfile
Normal file
@ -0,0 +1 @@
|
||||
FROM node:18-bullseye
|
31
basic/dapps/ethereum-provider/.codesandbox/tasks.json
Normal file
31
basic/dapps/ethereum-provider/.codesandbox/tasks.json
Normal file
@ -0,0 +1,31 @@
|
||||
{
|
||||
// These tasks will run in order when initializing your CodeSandbox project.
|
||||
"setupTasks": [
|
||||
{
|
||||
"name": "Install Dependencies",
|
||||
"command": "yarn install"
|
||||
}
|
||||
],
|
||||
|
||||
// These tasks can be run from CodeSandbox. Running one will open a log in the app.
|
||||
"tasks": {
|
||||
"dev": {
|
||||
"name": "dev",
|
||||
"command": "yarn dev",
|
||||
"runAtStart": true,
|
||||
"preview": {
|
||||
"port": 5173
|
||||
}
|
||||
},
|
||||
"build": {
|
||||
"name": "build",
|
||||
"command": "yarn build",
|
||||
"runAtStart": false
|
||||
},
|
||||
"preview": {
|
||||
"name": "preview",
|
||||
"command": "yarn preview",
|
||||
"runAtStart": false
|
||||
}
|
||||
}
|
||||
}
|
1
basic/dapps/ethereum-provider/.env.example
Normal file
1
basic/dapps/ethereum-provider/.env.example
Normal file
@ -0,0 +1 @@
|
||||
VITE_PROJECT_ID=YOUR_PROJECT_ID
|
24
basic/dapps/ethereum-provider/.gitignore
vendored
Normal file
24
basic/dapps/ethereum-provider/.gitignore
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
*.local
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
.DS_Store
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
22
basic/dapps/ethereum-provider/README.md
Normal file
22
basic/dapps/ethereum-provider/README.md
Normal file
@ -0,0 +1,22 @@
|
||||
# Vite + React
|
||||
|
||||
This is a [Vite](https://vitejs.dev) project together with React.
|
||||
|
||||
[![Edit in CodeSandbox](https://assets.codesandbox.io/github/button-edit-lime.svg)](https://codesandbox.io/p/github/codesandbox/codesandbox-template-vite-react/main)
|
||||
|
||||
[Configuration](https://codesandbox.io/docs/projects/learn/setting-up/tasks) has been added to optimize it for [CodeSandbox](https://codesandbox.io/dashboard).
|
||||
|
||||
## Usage
|
||||
|
||||
1. Go to [WalletConnect Cloud](https://cloud.walletconnect.com) and create a new project.
|
||||
2. Copy your `Project ID`
|
||||
3. Rename `.env.example` to `.env` and paste your `Project ID` as the value for `VITE_PROJECT_ID`
|
||||
4. Run `yarn` to install dependencies
|
||||
5. Run `yarn dev` to start the development server
|
||||
|
||||
## Resources
|
||||
|
||||
- [CodeSandbox — Docs](https://codesandbox.io/docs/projects)
|
||||
- [CodeSandbox — Discord](https://discord.gg/Ggarp3pX5H)
|
||||
- [Vite — GitHub](https://github.com/vitejs/vite)
|
||||
- [Vite — Docs](https://vitejs.dev/guide/)
|
13
basic/dapps/ethereum-provider/index.html
Normal file
13
basic/dapps/ethereum-provider/index.html
Normal file
@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>WalletConnect Ethereum Provider</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.tsx"></script>
|
||||
</body>
|
||||
</html>
|
25
basic/dapps/ethereum-provider/package.json
Normal file
25
basic/dapps/ethereum-provider/package.json
Normal file
@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "codesandbox-template-vite",
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "tsc && vite build",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"@walletconnect/ethereum-provider": "^2.9.1",
|
||||
"@walletconnect/modal": "^2.6.1",
|
||||
"ethers": "^5.7.1",
|
||||
"react": "^18.2.0",
|
||||
"react-dom": "^18.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "^18.0.15",
|
||||
"@types/react-dom": "^18.0.6",
|
||||
"@vitejs/plugin-react": "^2.0.0",
|
||||
"typescript": "^4.6.4",
|
||||
"vite": "^4.3.0"
|
||||
}
|
||||
}
|
1
basic/dapps/ethereum-provider/public/vite.svg
Normal file
1
basic/dapps/ethereum-provider/public/vite.svg
Normal file
@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
|
After Width: | Height: | Size: 1.5 KiB |
87
basic/dapps/ethereum-provider/src/App.css
Normal file
87
basic/dapps/ethereum-provider/src/App.css
Normal file
@ -0,0 +1,87 @@
|
||||
#root {
|
||||
max-width: 1280px;
|
||||
margin: 0 auto;
|
||||
padding: 2rem;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.logo {
|
||||
height: 6em;
|
||||
padding: 1.5em;
|
||||
will-change: filter;
|
||||
}
|
||||
.logo:hover {
|
||||
filter: drop-shadow(0 0 2em #646cffaa);
|
||||
}
|
||||
.logo.react:hover {
|
||||
filter: drop-shadow(0 0 2em #61dafbaa);
|
||||
}
|
||||
|
||||
@keyframes logo-spin {
|
||||
from {
|
||||
transform: rotate(0deg);
|
||||
}
|
||||
to {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: no-preference) {
|
||||
a:nth-of-type(1) .logo {
|
||||
animation: logo-spin infinite 20s linear;
|
||||
}
|
||||
}
|
||||
|
||||
.card {
|
||||
padding: 2em;
|
||||
}
|
||||
|
||||
.read-the-docs {
|
||||
color: #888;
|
||||
}
|
||||
#root {
|
||||
max-width: 100vw;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
flex-wrap: wrap;
|
||||
gap: 1rem;
|
||||
margin: 0 auto;
|
||||
padding: 2rem;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.logo {
|
||||
height: 6em;
|
||||
padding: 1.5em;
|
||||
will-change: filter;
|
||||
}
|
||||
.logo:hover {
|
||||
filter: drop-shadow(0 0 2em #646cffaa);
|
||||
}
|
||||
.logo.react:hover {
|
||||
filter: drop-shadow(0 0 2em #61dafbaa);
|
||||
}
|
||||
|
||||
@keyframes logo-spin {
|
||||
from {
|
||||
transform: rotate(0deg);
|
||||
}
|
||||
to {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: no-preference) {
|
||||
a:nth-of-type(1) .logo {
|
||||
animation: logo-spin infinite 20s linear;
|
||||
}
|
||||
}
|
||||
|
||||
.card {
|
||||
padding: 2em;
|
||||
}
|
||||
|
||||
.read-the-docs {
|
||||
color: #888;
|
||||
}
|
68
basic/dapps/ethereum-provider/src/App.tsx
Normal file
68
basic/dapps/ethereum-provider/src/App.tsx
Normal file
@ -0,0 +1,68 @@
|
||||
import "./App.css";
|
||||
|
||||
import { EthereumProvider } from "@walletconnect/ethereum-provider";
|
||||
import { ethers } from "ethers";
|
||||
import React, { useState } from "react";
|
||||
|
||||
const projectId = import.meta.env.VITE_PROJECT_ID as string;
|
||||
|
||||
// 1. Create a new EthereumProvider instance
|
||||
const provider = await EthereumProvider.init({
|
||||
projectId,
|
||||
chains: [1],
|
||||
methods: ["personal_sign", "eth_sendTransaction"],
|
||||
showQrModal: true,
|
||||
qrModalOptions: {
|
||||
themeMode: "light",
|
||||
},
|
||||
});
|
||||
|
||||
provider.on("display_uri", (uri) => {
|
||||
console.log("display_uri", uri);
|
||||
});
|
||||
|
||||
// 2. Pass the provider to ethers.js
|
||||
const ethersWeb3Provider = new ethers.providers.Web3Provider(provider);
|
||||
|
||||
function App() {
|
||||
// 3. Handle Connect
|
||||
const connect = () => {
|
||||
provider.connect().then(() => {
|
||||
setConnected(true);
|
||||
});
|
||||
};
|
||||
|
||||
const [connected, setConnected] = useState(false);
|
||||
const [balance, setBalance] = useState<string | null>(null);
|
||||
|
||||
// 4. Fetch Balance on click with ethers.js
|
||||
const getBalance = async () => {
|
||||
const balanceFromEthers = await ethersWeb3Provider
|
||||
.getSigner(provider.accounts[0])
|
||||
.getBalance();
|
||||
const remainder = balanceFromEthers.mod(1e14);
|
||||
setBalance(ethers.utils.formatEther(balanceFromEthers.sub(remainder)));
|
||||
};
|
||||
|
||||
// 5. Handle Disconnect
|
||||
const refresh = () => {
|
||||
provider.disconnect();
|
||||
window.localStorage.clear();
|
||||
setConnected(false);
|
||||
};
|
||||
|
||||
if (connected) {
|
||||
return (
|
||||
<>
|
||||
<button onClick={getBalance}>Balance</button>
|
||||
<button onClick={refresh}>Refresh</button>
|
||||
<p>
|
||||
balance: {balance ? `${balance} ETH` : `click "Balance" to fetch`}
|
||||
</p>
|
||||
</>
|
||||
);
|
||||
}
|
||||
return <button onClick={connect}>Connect with ethereum-provider</button>;
|
||||
}
|
||||
|
||||
export default App;
|
1
basic/dapps/ethereum-provider/src/assets/react.svg
Normal file
1
basic/dapps/ethereum-provider/src/assets/react.svg
Normal file
@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="35.93" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 228"><path fill="#00D8FF" d="M210.483 73.824a171.49 171.49 0 0 0-8.24-2.597c.465-1.9.893-3.777 1.273-5.621c6.238-30.281 2.16-54.676-11.769-62.708c-13.355-7.7-35.196.329-57.254 19.526a171.23 171.23 0 0 0-6.375 5.848a155.866 155.866 0 0 0-4.241-3.917C100.759 3.829 77.587-4.822 63.673 3.233C50.33 10.957 46.379 33.89 51.995 62.588a170.974 170.974 0 0 0 1.892 8.48c-3.28.932-6.445 1.924-9.474 2.98C17.309 83.498 0 98.307 0 113.668c0 15.865 18.582 31.778 46.812 41.427a145.52 145.52 0 0 0 6.921 2.165a167.467 167.467 0 0 0-2.01 9.138c-5.354 28.2-1.173 50.591 12.134 58.266c13.744 7.926 36.812-.22 59.273-19.855a145.567 145.567 0 0 0 5.342-4.923a168.064 168.064 0 0 0 6.92 6.314c21.758 18.722 43.246 26.282 56.54 18.586c13.731-7.949 18.194-32.003 12.4-61.268a145.016 145.016 0 0 0-1.535-6.842c1.62-.48 3.21-.974 4.76-1.488c29.348-9.723 48.443-25.443 48.443-41.52c0-15.417-17.868-30.326-45.517-39.844Zm-6.365 70.984c-1.4.463-2.836.91-4.3 1.345c-3.24-10.257-7.612-21.163-12.963-32.432c5.106-11 9.31-21.767 12.459-31.957c2.619.758 5.16 1.557 7.61 2.4c23.69 8.156 38.14 20.213 38.14 29.504c0 9.896-15.606 22.743-40.946 31.14Zm-10.514 20.834c2.562 12.94 2.927 24.64 1.23 33.787c-1.524 8.219-4.59 13.698-8.382 15.893c-8.067 4.67-25.32-1.4-43.927-17.412a156.726 156.726 0 0 1-6.437-5.87c7.214-7.889 14.423-17.06 21.459-27.246c12.376-1.098 24.068-2.894 34.671-5.345a134.17 134.17 0 0 1 1.386 6.193ZM87.276 214.515c-7.882 2.783-14.16 2.863-17.955.675c-8.075-4.657-11.432-22.636-6.853-46.752a156.923 156.923 0 0 1 1.869-8.499c10.486 2.32 22.093 3.988 34.498 4.994c7.084 9.967 14.501 19.128 21.976 27.15a134.668 134.668 0 0 1-4.877 4.492c-9.933 8.682-19.886 14.842-28.658 17.94ZM50.35 144.747c-12.483-4.267-22.792-9.812-29.858-15.863c-6.35-5.437-9.555-10.836-9.555-15.216c0-9.322 13.897-21.212 37.076-29.293c2.813-.98 5.757-1.905 8.812-2.773c3.204 10.42 7.406 21.315 12.477 32.332c-5.137 11.18-9.399 22.249-12.634 32.792a134.718 134.718 0 0 1-6.318-1.979Zm12.378-84.26c-4.811-24.587-1.616-43.134 6.425-47.789c8.564-4.958 27.502 2.111 47.463 19.835a144.318 144.318 0 0 1 3.841 3.545c-7.438 7.987-14.787 17.08-21.808 26.988c-12.04 1.116-23.565 2.908-34.161 5.309a160.342 160.342 0 0 1-1.76-7.887Zm110.427 27.268a347.8 347.8 0 0 0-7.785-12.803c8.168 1.033 15.994 2.404 23.343 4.08c-2.206 7.072-4.956 14.465-8.193 22.045a381.151 381.151 0 0 0-7.365-13.322Zm-45.032-43.861c5.044 5.465 10.096 11.566 15.065 18.186a322.04 322.04 0 0 0-30.257-.006c4.974-6.559 10.069-12.652 15.192-18.18ZM82.802 87.83a323.167 323.167 0 0 0-7.227 13.238c-3.184-7.553-5.909-14.98-8.134-22.152c7.304-1.634 15.093-2.97 23.209-3.984a321.524 321.524 0 0 0-7.848 12.897Zm8.081 65.352c-8.385-.936-16.291-2.203-23.593-3.793c2.26-7.3 5.045-14.885 8.298-22.6a321.187 321.187 0 0 0 7.257 13.246c2.594 4.48 5.28 8.868 8.038 13.147Zm37.542 31.03c-5.184-5.592-10.354-11.779-15.403-18.433c4.902.192 9.899.29 14.978.29c5.218 0 10.376-.117 15.453-.343c-4.985 6.774-10.018 12.97-15.028 18.486Zm52.198-57.817c3.422 7.8 6.306 15.345 8.596 22.52c-7.422 1.694-15.436 3.058-23.88 4.071a382.417 382.417 0 0 0 7.859-13.026a347.403 347.403 0 0 0 7.425-13.565Zm-16.898 8.101a358.557 358.557 0 0 1-12.281 19.815a329.4 329.4 0 0 1-23.444.823c-7.967 0-15.716-.248-23.178-.732a310.202 310.202 0 0 1-12.513-19.846h.001a307.41 307.41 0 0 1-10.923-20.627a310.278 310.278 0 0 1 10.89-20.637l-.001.001a307.318 307.318 0 0 1 12.413-19.761c7.613-.576 15.42-.876 23.31-.876H128c7.926 0 15.743.303 23.354.883a329.357 329.357 0 0 1 12.335 19.695a358.489 358.489 0 0 1 11.036 20.54a329.472 329.472 0 0 1-11 20.722Zm22.56-122.124c8.572 4.944 11.906 24.881 6.52 51.026c-.344 1.668-.73 3.367-1.15 5.09c-10.622-2.452-22.155-4.275-34.23-5.408c-7.034-10.017-14.323-19.124-21.64-27.008a160.789 160.789 0 0 1 5.888-5.4c18.9-16.447 36.564-22.941 44.612-18.3ZM128 90.808c12.625 0 22.86 10.235 22.86 22.86s-10.235 22.86-22.86 22.86s-22.86-10.235-22.86-22.86s10.235-22.86 22.86-22.86Z"></path></svg>
|
After Width: | Height: | Size: 4.0 KiB |
70
basic/dapps/ethereum-provider/src/index.css
Normal file
70
basic/dapps/ethereum-provider/src/index.css
Normal file
@ -0,0 +1,70 @@
|
||||
:root {
|
||||
font-family: Inter, Avenir, Helvetica, Arial, sans-serif;
|
||||
font-size: 16px;
|
||||
line-height: 24px;
|
||||
font-weight: 400;
|
||||
|
||||
color-scheme: light dark;
|
||||
color: rgba(255, 255, 255, 0.87);
|
||||
background-color: #242424;
|
||||
|
||||
font-synthesis: none;
|
||||
text-rendering: optimizeLegibility;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
-webkit-text-size-adjust: 100%;
|
||||
}
|
||||
|
||||
a {
|
||||
font-weight: 500;
|
||||
color: #646cff;
|
||||
text-decoration: inherit;
|
||||
}
|
||||
a:hover {
|
||||
color: #535bf2;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
display: flex;
|
||||
place-items: center;
|
||||
min-width: 320px;
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 3.2em;
|
||||
line-height: 1.1;
|
||||
}
|
||||
|
||||
button {
|
||||
border-radius: 8px;
|
||||
border: 1px solid transparent;
|
||||
padding: 0.6em 1.2em;
|
||||
font-size: 1em;
|
||||
font-weight: 500;
|
||||
font-family: inherit;
|
||||
background-color: #1a1a1a;
|
||||
cursor: pointer;
|
||||
transition: border-color 0.25s;
|
||||
}
|
||||
button:hover {
|
||||
border-color: #646cff;
|
||||
}
|
||||
button:focus,
|
||||
button:focus-visible {
|
||||
outline: 4px auto -webkit-focus-ring-color;
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: light) {
|
||||
:root {
|
||||
color: #213547;
|
||||
background-color: #ffffff;
|
||||
}
|
||||
a:hover {
|
||||
color: #747bff;
|
||||
}
|
||||
button {
|
||||
background-color: #f9f9f9;
|
||||
}
|
||||
}
|
10
basic/dapps/ethereum-provider/src/main.tsx
Normal file
10
basic/dapps/ethereum-provider/src/main.tsx
Normal file
@ -0,0 +1,10 @@
|
||||
import React from "react";
|
||||
import ReactDOM from "react-dom/client";
|
||||
import App from "./App";
|
||||
import "./index.css";
|
||||
|
||||
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
|
||||
<React.StrictMode>
|
||||
<App />
|
||||
</React.StrictMode>
|
||||
);
|
1
basic/dapps/ethereum-provider/src/vite-env.d.ts
vendored
Normal file
1
basic/dapps/ethereum-provider/src/vite-env.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
/// <reference types="vite/client" />
|
31
basic/dapps/ethereum-provider/tsconfig.json
Normal file
31
basic/dapps/ethereum-provider/tsconfig.json
Normal file
@ -0,0 +1,31 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ESNext",
|
||||
"useDefineForClassFields": true,
|
||||
"lib": [
|
||||
"DOM",
|
||||
"DOM.Iterable",
|
||||
"ESNext"
|
||||
],
|
||||
"allowJs": false,
|
||||
"skipLibCheck": true,
|
||||
"esModuleInterop": false,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"strict": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "Node",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"noEmit": true,
|
||||
"jsx": "react-jsx"
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"path": "./tsconfig.node.json"
|
||||
}
|
||||
]
|
||||
}
|
11
basic/dapps/ethereum-provider/tsconfig.node.json
Normal file
11
basic/dapps/ethereum-provider/tsconfig.node.json
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "Node",
|
||||
"allowSyntheticDefaultImports": true
|
||||
},
|
||||
"include": [
|
||||
"vite.config.ts"
|
||||
]
|
||||
}
|
7
basic/dapps/ethereum-provider/vite.config.ts
Normal file
7
basic/dapps/ethereum-provider/vite.config.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { defineConfig } from "vite";
|
||||
import react from "@vitejs/plugin-react";
|
||||
|
||||
// https://vitejs.dev/config/
|
||||
export default defineConfig({
|
||||
plugins: [react()]
|
||||
});
|
2154
basic/dapps/ethereum-provider/yarn.lock
Normal file
2154
basic/dapps/ethereum-provider/yarn.lock
Normal file
File diff suppressed because it is too large
Load Diff
@ -0,0 +1 @@
|
||||
FROM node:20-bullseye
|
BIN
basic/dapps/universal-provider-solana/.codesandbox/icon.png
Normal file
BIN
basic/dapps/universal-provider-solana/.codesandbox/icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 5.6 KiB |
@ -0,0 +1,31 @@
|
||||
{
|
||||
// These tasks will run in order when initializing your CodeSandbox project.
|
||||
"setupTasks": [
|
||||
{
|
||||
"name": "Install Dependencies",
|
||||
"command": "yarn install"
|
||||
}
|
||||
],
|
||||
|
||||
// These tasks can be run from CodeSandbox. Running one will open a log in the app.
|
||||
"tasks": {
|
||||
"dev": {
|
||||
"name": "dev",
|
||||
"command": "yarn dev",
|
||||
"runAtStart": true,
|
||||
"preview": {
|
||||
"port": 5173
|
||||
}
|
||||
},
|
||||
"build": {
|
||||
"name": "build",
|
||||
"command": "yarn build",
|
||||
"runAtStart": false
|
||||
},
|
||||
"preview": {
|
||||
"name": "preview",
|
||||
"command": "yarn preview",
|
||||
"runAtStart": false
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
{
|
||||
"title": "React (Vite + TS)",
|
||||
"description": "React running from the Vite dev server!",
|
||||
"iconUrl": "https://github.com/codesandbox/sandbox-templates/blob/main/react-vite-ts/.codesandbox/icon.png?raw=true",
|
||||
"tags": [
|
||||
"react",
|
||||
"vite",
|
||||
"javascript",
|
||||
"typescript"
|
||||
],
|
||||
"published": true
|
||||
}
|
1
basic/dapps/universal-provider-solana/.env.example
Normal file
1
basic/dapps/universal-provider-solana/.env.example
Normal file
@ -0,0 +1 @@
|
||||
VITE_PROJECT_ID=YOUR_PROJECT_ID
|
36
basic/dapps/universal-provider-solana/.eslintrc.cjs
Normal file
36
basic/dapps/universal-provider-solana/.eslintrc.cjs
Normal file
@ -0,0 +1,36 @@
|
||||
module.exports = {
|
||||
"env": {
|
||||
"browser": true,
|
||||
"es2021": true
|
||||
},
|
||||
"extends": [
|
||||
"eslint:recommended",
|
||||
"plugin:@typescript-eslint/recommended",
|
||||
"plugin:react/recommended"
|
||||
],
|
||||
"overrides": [
|
||||
{
|
||||
"env": {
|
||||
"node": true
|
||||
},
|
||||
"files": [
|
||||
".eslintrc.{js,cjs}"
|
||||
],
|
||||
"parserOptions": {
|
||||
"sourceType": "script"
|
||||
}
|
||||
}
|
||||
],
|
||||
"parser": "@typescript-eslint/parser",
|
||||
"parserOptions": {
|
||||
"ecmaVersion": "latest",
|
||||
"sourceType": "module"
|
||||
},
|
||||
"plugins": [
|
||||
"@typescript-eslint",
|
||||
"react"
|
||||
],
|
||||
"rules": {
|
||||
"react/react-in-jsx-scope": "off"
|
||||
}
|
||||
}
|
24
basic/dapps/universal-provider-solana/.gitignore
vendored
Normal file
24
basic/dapps/universal-provider-solana/.gitignore
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
*.local
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
.DS_Store
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
22
basic/dapps/universal-provider-solana/README.md
Normal file
22
basic/dapps/universal-provider-solana/README.md
Normal file
@ -0,0 +1,22 @@
|
||||
# Vite + React
|
||||
|
||||
This is a [Vite](https://vitejs.dev) project together with React.
|
||||
|
||||
[![Edit in CodeSandbox](https://assets.codesandbox.io/github/button-edit-lime.svg)](https://codesandbox.io/p/github/codesandbox/codesandbox-template-vite-react/main)
|
||||
|
||||
[Configuration](https://codesandbox.io/docs/projects/learn/setting-up/tasks) `.codesandbox/tasks.json` has been added to optimize it for [CodeSandbox](https://codesandbox.io/dashboard).
|
||||
|
||||
## Usage
|
||||
|
||||
1. Go to [WalletConnect Cloud](https://cloud.walletconnect.com) and create a new project.
|
||||
2. Copy your `Project ID`
|
||||
3. Rename `.env.example` to `.env` and paste your `Project ID` as the value for `VITE_PROJECT_ID`
|
||||
4. Run `yarn` to install dependencies
|
||||
5. Run `yarn dev` to start the development server
|
||||
|
||||
## Resources
|
||||
|
||||
- [CodeSandbox — Docs](https://codesandbox.io/docs/learn)
|
||||
- [CodeSandbox — Discord](https://discord.gg/Ggarp3pX5H)
|
||||
- [Vite — GitHub](https://github.com/vitejs/vite)
|
||||
- [Vite — Docs](https://vitejs.dev/guide/)
|
13
basic/dapps/universal-provider-solana/index.html
Normal file
13
basic/dapps/universal-provider-solana/index.html
Normal file
@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Vite + React + TS</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.tsx"></script>
|
||||
</body>
|
||||
</html>
|
35
basic/dapps/universal-provider-solana/package.json
Normal file
35
basic/dapps/universal-provider-solana/package.json
Normal file
@ -0,0 +1,35 @@
|
||||
{
|
||||
"name": "sandbox",
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "tsc && vite build",
|
||||
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
|
||||
"@solana/web3.js": "^1.78.4",
|
||||
"@walletconnect/modal": "^2.6.2",
|
||||
"@walletconnect/universal-provider": "^2.10.1",
|
||||
"bs58": "^5.0.0",
|
||||
"react": "^18.2.0",
|
||||
"react-dom": "^18.2.0",
|
||||
"tweetnacl": "^1.0.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "^18.2.21",
|
||||
"@types/react-dom": "^18.2.7",
|
||||
"@typescript-eslint/eslint-plugin": "^6.4.1",
|
||||
"@typescript-eslint/parser": "^6.4.1",
|
||||
"@vitejs/plugin-react-swc": "^3.3.2",
|
||||
"eslint": "^8.48.0",
|
||||
"eslint-plugin-react": "^7.33.2",
|
||||
"eslint-plugin-react-hooks": "^4.6.0",
|
||||
"eslint-plugin-react-refresh": "^0.4.3",
|
||||
"typescript": "^5.2.2",
|
||||
"vite": "^4.4.9"
|
||||
}
|
||||
}
|
1
basic/dapps/universal-provider-solana/public/vite.svg
Normal file
1
basic/dapps/universal-provider-solana/public/vite.svg
Normal file
@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
|
After Width: | Height: | Size: 1.5 KiB |
111
basic/dapps/universal-provider-solana/src/App.tsx
Normal file
111
basic/dapps/universal-provider-solana/src/App.tsx
Normal file
@ -0,0 +1,111 @@
|
||||
import UniversalProvider from "@walletconnect/universal-provider";
|
||||
import { WalletConnectModal } from "@walletconnect/modal";
|
||||
import { useState } from "react";
|
||||
import { signMessage, sendTransaction, SolanaChains } from "./utils/helpers";
|
||||
|
||||
const projectId = import.meta.env.VITE_PROJECT_ID;
|
||||
|
||||
const events: string[] = [];
|
||||
|
||||
// 1. select chains (solana)
|
||||
const chains = [`solana:${SolanaChains.MainnetBeta}`];
|
||||
|
||||
// 2. select methods (solana)
|
||||
const methods = ["solana_signMessage", "solana_signTransaction"];
|
||||
|
||||
// 3. create modal instance
|
||||
const modal = new WalletConnectModal({
|
||||
projectId,
|
||||
chains,
|
||||
});
|
||||
|
||||
// 4. create provider instance
|
||||
const provider = await UniversalProvider.init({
|
||||
logger: "error",
|
||||
projectId: projectId,
|
||||
metadata: {
|
||||
name: "WalletConnect x Solana",
|
||||
description: "Solana integration with WalletConnect's Universal Provider",
|
||||
url: "https://walletconnect.com/",
|
||||
icons: ["https://avatars.githubusercontent.com/u/37784886"],
|
||||
},
|
||||
});
|
||||
|
||||
const App = () => {
|
||||
const [isConnected, setIsConnected] = useState(false);
|
||||
|
||||
// 5. get address once loaded
|
||||
const address =
|
||||
provider.session?.namespaces.solana?.accounts[0].split(":")[2];
|
||||
|
||||
// 6. handle display_uri event and open modal
|
||||
provider.on("display_uri", async (uri: string) => {
|
||||
console.log("uri", uri);
|
||||
await modal.openModal({
|
||||
uri,
|
||||
});
|
||||
});
|
||||
|
||||
// 7. handle connect event
|
||||
const connect = async () => {
|
||||
try {
|
||||
await provider.connect({
|
||||
namespaces: {
|
||||
solana: {
|
||||
methods,
|
||||
chains,
|
||||
events,
|
||||
},
|
||||
},
|
||||
});
|
||||
setIsConnected(true);
|
||||
console.log("session", provider.session);
|
||||
} catch {
|
||||
console.log("Something went wrong, request cancelled");
|
||||
}
|
||||
modal.closeModal();
|
||||
};
|
||||
|
||||
// 8. handle disconnect event
|
||||
const disconnect = async () => {
|
||||
await provider.disconnect();
|
||||
setIsConnected(false);
|
||||
};
|
||||
|
||||
// 9. handle signMessage and sendTransaction
|
||||
const handleSign = async () => {
|
||||
const res = await signMessage(
|
||||
`Can i have authorize this request pls bossman - ${Date.now()}`,
|
||||
provider,
|
||||
address!
|
||||
);
|
||||
console.log(res);
|
||||
};
|
||||
|
||||
const handleSend = async () => {
|
||||
const res = await sendTransaction(address!, 1000, provider, address!);
|
||||
console.log(res);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="App">
|
||||
{isConnected ? (
|
||||
<>
|
||||
<p>
|
||||
<b>Public Key: </b>
|
||||
{address}
|
||||
</p>
|
||||
<div className="btn-container">
|
||||
<button onClick={handleSign}>Sign</button>
|
||||
<button onClick={handleSend}>Send</button>
|
||||
<button onClick={disconnect}>Disconnect</button>
|
||||
</div>
|
||||
</>
|
||||
) : (
|
||||
<button onClick={connect}>Connect</button>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default App;
|
10
basic/dapps/universal-provider-solana/src/main.tsx
Normal file
10
basic/dapps/universal-provider-solana/src/main.tsx
Normal file
@ -0,0 +1,10 @@
|
||||
import React from "react";
|
||||
import ReactDOM from "react-dom/client";
|
||||
import App from "./App";
|
||||
import "./styles/index.css";
|
||||
|
||||
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
|
||||
<React.StrictMode>
|
||||
<App />
|
||||
</React.StrictMode>
|
||||
);
|
83
basic/dapps/universal-provider-solana/src/styles/index.css
Normal file
83
basic/dapps/universal-provider-solana/src/styles/index.css
Normal file
@ -0,0 +1,83 @@
|
||||
:root {
|
||||
font-family: Inter, Avenir, Helvetica, Arial, sans-serif;
|
||||
font-size: 16px;
|
||||
line-height: 24px;
|
||||
font-weight: 400;
|
||||
|
||||
color-scheme: light dark;
|
||||
color: rgba(255, 255, 255, 0.87);
|
||||
background-color: #242424;
|
||||
|
||||
font-synthesis: none;
|
||||
text-rendering: optimizeLegibility;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
-webkit-text-size-adjust: 100%;
|
||||
}
|
||||
|
||||
a {
|
||||
font-weight: 500;
|
||||
color: #646cff;
|
||||
text-decoration: inherit;
|
||||
}
|
||||
a:hover {
|
||||
color: #535bf2;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
display: flex;
|
||||
place-items: center;
|
||||
min-width: 320px;
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
#root {
|
||||
flex: 1;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 3.2em;
|
||||
line-height: 1.1;
|
||||
}
|
||||
|
||||
button {
|
||||
border-radius: 8px;
|
||||
border: 1px solid transparent;
|
||||
padding: 0.6em 1.2em;
|
||||
font-size: 1em;
|
||||
font-weight: 500;
|
||||
font-family: inherit;
|
||||
background-color: #1a1a1a;
|
||||
cursor: pointer;
|
||||
transition: border-color 0.25s;
|
||||
}
|
||||
button:hover {
|
||||
border-color: #646cff;
|
||||
}
|
||||
button:focus,
|
||||
button:focus-visible {
|
||||
outline: 4px auto -webkit-focus-ring-color;
|
||||
}
|
||||
|
||||
.btn-container {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: light) {
|
||||
:root {
|
||||
color: #213547;
|
||||
background-color: #ffffff;
|
||||
}
|
||||
a:hover {
|
||||
color: #747bff;
|
||||
}
|
||||
button {
|
||||
background-color: #f9f9f9;
|
||||
}
|
||||
}
|
175
basic/dapps/universal-provider-solana/src/utils/helpers.ts
Normal file
175
basic/dapps/universal-provider-solana/src/utils/helpers.ts
Normal file
@ -0,0 +1,175 @@
|
||||
import {
|
||||
Connection,
|
||||
PublicKey,
|
||||
SystemProgram,
|
||||
Transaction,
|
||||
VersionedTransaction,
|
||||
clusterApiUrl,
|
||||
} from "@solana/web3.js";
|
||||
import UniversalProvider from "@walletconnect/universal-provider/dist/types/UniversalProvider";
|
||||
import bs58 from "bs58";
|
||||
import nacl from "tweetnacl";
|
||||
|
||||
export enum SolanaChains {
|
||||
MainnetBeta = "4sGjMW1sUnHzSxGspuhpqLDx6wiyjNtZ",
|
||||
Devnet = "8E9rvCKLFQia2Y35HXjjpWzj8weVo44K",
|
||||
}
|
||||
|
||||
export function verifyTransactionSignature(
|
||||
address: string,
|
||||
signature: string,
|
||||
tx: Transaction
|
||||
) {
|
||||
return nacl.sign.detached.verify(
|
||||
tx.serializeMessage(),
|
||||
bs58.decode(signature),
|
||||
bs58.decode(address)
|
||||
);
|
||||
}
|
||||
|
||||
export function verifyMessageSignature(
|
||||
address: string,
|
||||
signature: string,
|
||||
message: string
|
||||
) {
|
||||
return nacl.sign.detached.verify(
|
||||
bs58.decode(message),
|
||||
bs58.decode(signature),
|
||||
bs58.decode(address)
|
||||
);
|
||||
}
|
||||
|
||||
const isVersionedTransaction = (
|
||||
transaction: Transaction | VersionedTransaction
|
||||
): transaction is VersionedTransaction => "version" in transaction;
|
||||
|
||||
|
||||
export const getProviderUrl = (chainId: string) => {
|
||||
return `https://rpc.walletconnect.com/v1/?chainId=${chainId}&projectId=${
|
||||
import.meta.env.VITE_PROJECT_ID
|
||||
}`;
|
||||
};
|
||||
|
||||
export const signMessage = async (
|
||||
msg: string,
|
||||
provider: UniversalProvider,
|
||||
address: string
|
||||
) => {
|
||||
const senderPublicKey = new PublicKey(address);
|
||||
|
||||
const message = bs58.encode(new TextEncoder().encode(msg));
|
||||
|
||||
try {
|
||||
const result = await provider!.request<{ signature: string }>({
|
||||
method: "solana_signMessage",
|
||||
params: {
|
||||
pubkey: senderPublicKey.toBase58(),
|
||||
message,
|
||||
},
|
||||
});
|
||||
|
||||
const valid = verifyMessageSignature(
|
||||
senderPublicKey.toBase58(),
|
||||
result.signature,
|
||||
message
|
||||
);
|
||||
|
||||
return {
|
||||
method: "solana_signMessage",
|
||||
address,
|
||||
valid,
|
||||
result: result.signature,
|
||||
};
|
||||
//eslint-disable-next-line
|
||||
} catch (error: any) {
|
||||
throw new Error(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const sendTransaction = async (
|
||||
to: string,
|
||||
amount: number,
|
||||
provider: UniversalProvider,
|
||||
address: string
|
||||
) => {
|
||||
const isTestnet = provider.session!.namespaces.solana.chains?.includes(
|
||||
`solana:${SolanaChains.Devnet}`
|
||||
);
|
||||
|
||||
const senderPublicKey = new PublicKey(address);
|
||||
|
||||
const connection = new Connection(
|
||||
isTestnet
|
||||
? clusterApiUrl("testnet")
|
||||
: getProviderUrl(`solana:${SolanaChains.MainnetBeta}`)
|
||||
);
|
||||
|
||||
const { blockhash } = await connection.getLatestBlockhash();
|
||||
|
||||
const transaction:Transaction | VersionedTransaction = new Transaction({
|
||||
feePayer: senderPublicKey,
|
||||
recentBlockhash: blockhash,
|
||||
}).add(
|
||||
SystemProgram.transfer({
|
||||
fromPubkey: senderPublicKey,
|
||||
toPubkey: new PublicKey(to),
|
||||
lamports: amount,
|
||||
})
|
||||
);
|
||||
|
||||
let rawTransaction: string;
|
||||
let legacyTransaction: Transaction | VersionedTransaction | undefined;
|
||||
|
||||
if (isVersionedTransaction(transaction)) {
|
||||
// V0 transactions are serialized and passed in the `transaction` property
|
||||
rawTransaction = Buffer.from(transaction.serialize()).toString(
|
||||
"base64"
|
||||
);
|
||||
|
||||
if (transaction.version === "legacy") {
|
||||
// For backwards-compatible, legacy transactions are spread in the params
|
||||
legacyTransaction = Transaction.from(transaction.serialize());
|
||||
}
|
||||
} else {
|
||||
rawTransaction = transaction
|
||||
.serialize({
|
||||
requireAllSignatures: false,
|
||||
verifySignatures: false,
|
||||
})
|
||||
.toString("base64");
|
||||
legacyTransaction = transaction;
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await provider!.request<{ signature: string }>({
|
||||
method: "solana_signTransaction",
|
||||
params: {
|
||||
// Passing ...legacyTransaction is deprecated.
|
||||
// All new clients should rely on the `transaction` parameter.
|
||||
// The future versions will stop passing ...legacyTransaction.
|
||||
...legacyTransaction,
|
||||
// New base64-encoded serialized transaction request parameter
|
||||
transaction: rawTransaction,
|
||||
},
|
||||
});
|
||||
|
||||
// We only need `Buffer.from` here to satisfy the `Buffer` param type for `addSignature`.
|
||||
// The resulting `UInt8Array` is equivalent to just `bs58.decode(...)`.
|
||||
transaction.addSignature(
|
||||
senderPublicKey,
|
||||
Buffer.from(bs58.decode(result.signature))
|
||||
);
|
||||
|
||||
const valid = transaction.verifySignatures();
|
||||
|
||||
return {
|
||||
method: "solana_signTransaction",
|
||||
address,
|
||||
valid,
|
||||
result: result.signature,
|
||||
};
|
||||
// eslint-disable-next-line
|
||||
} catch (error: any) {
|
||||
throw new Error(error);
|
||||
}
|
||||
};
|
1
basic/dapps/universal-provider-solana/src/vite-env.d.ts
vendored
Normal file
1
basic/dapps/universal-provider-solana/src/vite-env.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
/// <reference types="vite/client" />
|
31
basic/dapps/universal-provider-solana/tsconfig.json
Normal file
31
basic/dapps/universal-provider-solana/tsconfig.json
Normal file
@ -0,0 +1,31 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ESNext",
|
||||
"useDefineForClassFields": true,
|
||||
"lib": [
|
||||
"DOM",
|
||||
"DOM.Iterable",
|
||||
"ESNext"
|
||||
],
|
||||
"allowJs": false,
|
||||
"skipLibCheck": true,
|
||||
"esModuleInterop": false,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"strict": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "Node",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"noEmit": true,
|
||||
"jsx": "react-jsx"
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"path": "./tsconfig.node.json"
|
||||
}
|
||||
]
|
||||
}
|
11
basic/dapps/universal-provider-solana/tsconfig.node.json
Normal file
11
basic/dapps/universal-provider-solana/tsconfig.node.json
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "Node",
|
||||
"allowSyntheticDefaultImports": true
|
||||
},
|
||||
"include": [
|
||||
"vite.config.ts"
|
||||
]
|
||||
}
|
22
basic/dapps/universal-provider-solana/vite.config.ts
Normal file
22
basic/dapps/universal-provider-solana/vite.config.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { defineConfig } from "vite";
|
||||
import react from "@vitejs/plugin-react-swc";
|
||||
import { NodeGlobalsPolyfillPlugin } from "@esbuild-plugins/node-globals-polyfill";
|
||||
|
||||
// https://vitejs.dev/config/
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
optimizeDeps: {
|
||||
esbuildOptions: {
|
||||
// Node.js global to browser globalThis
|
||||
define: {
|
||||
global: "globalThis",
|
||||
},
|
||||
// Enable esbuild polyfill plugins
|
||||
plugins: [
|
||||
NodeGlobalsPolyfillPlugin({
|
||||
buffer: true,
|
||||
}),
|
||||
],
|
||||
},
|
||||
},
|
||||
});
|
3268
basic/dapps/universal-provider-solana/yarn.lock
Normal file
3268
basic/dapps/universal-provider-solana/yarn.lock
Normal file
File diff suppressed because it is too large
Load Diff
3
basic/dapps/web3inbox/README.md
Normal file
3
basic/dapps/web3inbox/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Web3Inbox Example
|
||||
|
||||
Head over to the [GM Hackers Repository](https://github.com/WalletConnect/gm-hackers) for more information.
|
1
basic/dapps/web3modal/.codesandbox/Dockerfile
Normal file
1
basic/dapps/web3modal/.codesandbox/Dockerfile
Normal file
@ -0,0 +1 @@
|
||||
FROM node:18-bullseye
|
31
basic/dapps/web3modal/.codesandbox/tasks.json
Normal file
31
basic/dapps/web3modal/.codesandbox/tasks.json
Normal file
@ -0,0 +1,31 @@
|
||||
{
|
||||
// These tasks will run in order when initializing your CodeSandbox project.
|
||||
"setupTasks": [
|
||||
{
|
||||
"name": "Install Dependencies",
|
||||
"command": "yarn install"
|
||||
}
|
||||
],
|
||||
|
||||
// These tasks can be run from CodeSandbox. Running one will open a log in the app.
|
||||
"tasks": {
|
||||
"dev": {
|
||||
"name": "dev",
|
||||
"command": "yarn dev",
|
||||
"runAtStart": true,
|
||||
"preview": {
|
||||
"port": 5173
|
||||
}
|
||||
},
|
||||
"build": {
|
||||
"name": "build",
|
||||
"command": "yarn build",
|
||||
"runAtStart": false
|
||||
},
|
||||
"preview": {
|
||||
"name": "preview",
|
||||
"command": "yarn preview",
|
||||
"runAtStart": false
|
||||
}
|
||||
}
|
||||
}
|
1
basic/dapps/web3modal/.env.example
Normal file
1
basic/dapps/web3modal/.env.example
Normal file
@ -0,0 +1 @@
|
||||
VITE_PROJECT_ID=YOUR_PROJECT_ID
|
36
basic/dapps/web3modal/.eslintrc.cjs
Normal file
36
basic/dapps/web3modal/.eslintrc.cjs
Normal file
@ -0,0 +1,36 @@
|
||||
module.exports = {
|
||||
"env": {
|
||||
"browser": true,
|
||||
"es2021": true
|
||||
},
|
||||
"extends": [
|
||||
"eslint:recommended",
|
||||
"plugin:@typescript-eslint/recommended",
|
||||
"plugin:react/recommended"
|
||||
],
|
||||
"overrides": [
|
||||
{
|
||||
"env": {
|
||||
"node": true
|
||||
},
|
||||
"files": [
|
||||
".eslintrc.{js,cjs}"
|
||||
],
|
||||
"parserOptions": {
|
||||
"sourceType": "script"
|
||||
}
|
||||
}
|
||||
],
|
||||
"parser": "@typescript-eslint/parser",
|
||||
"parserOptions": {
|
||||
"ecmaVersion": "latest",
|
||||
"sourceType": "module"
|
||||
},
|
||||
"plugins": [
|
||||
"@typescript-eslint",
|
||||
"react"
|
||||
],
|
||||
"rules": {
|
||||
"react/react-in-jsx-scope": "off"
|
||||
}
|
||||
}
|
24
basic/dapps/web3modal/.gitignore
vendored
Normal file
24
basic/dapps/web3modal/.gitignore
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
*.local
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
.DS_Store
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
22
basic/dapps/web3modal/README.md
Normal file
22
basic/dapps/web3modal/README.md
Normal file
@ -0,0 +1,22 @@
|
||||
# Vite + React
|
||||
|
||||
This is a [Vite](https://vitejs.dev) project together with React.
|
||||
|
||||
[![Edit in CodeSandbox](https://assets.codesandbox.io/github/button-edit-lime.svg)](https://codesandbox.io/p/github/codesandbox/codesandbox-template-vite-react/main)
|
||||
|
||||
[Configuration](https://codesandbox.io/docs/projects/learn/setting-up/tasks) `.codesandbox/tasks.json` has been added to optimize it for [CodeSandbox](https://codesandbox.io/dashboard).
|
||||
|
||||
## Usage
|
||||
|
||||
1. Go to [WalletConnect Cloud](https://cloud.walletconnect.com) and create a new project.
|
||||
2. Copy your `Project ID`
|
||||
3. Rename `.env.example` to `.env` and paste your `Project ID` as the value for `VITE_PROJECT_ID`
|
||||
4. Run `yarn` to install dependencies
|
||||
5. Run `yarn dev` to start the development server
|
||||
|
||||
## Resources
|
||||
|
||||
- [CodeSandbox — Docs](https://codesandbox.io/docs/projects)
|
||||
- [CodeSandbox — Discord](https://discord.gg/Ggarp3pX5H)
|
||||
- [Vite — GitHub](https://github.com/vitejs/vite)
|
||||
- [Vite — Docs](https://vitejs.dev/guide/)
|
13
basic/dapps/web3modal/index.html
Normal file
13
basic/dapps/web3modal/index.html
Normal file
@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Vite + React + TS</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.tsx"></script>
|
||||
</body>
|
||||
</html>
|
33
basic/dapps/web3modal/package.json
Normal file
33
basic/dapps/web3modal/package.json
Normal file
@ -0,0 +1,33 @@
|
||||
{
|
||||
"name": "sandbox",
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "tsc && vite build",
|
||||
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"@web3modal/ethereum": "^2.7.1",
|
||||
"@web3modal/react": "^2.7.1",
|
||||
"react": "^18.2.0",
|
||||
"react-dom": "^18.2.0",
|
||||
"viem": "^1.4.1",
|
||||
"wagmi": "^1.3.9"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "^18.2.14",
|
||||
"@types/react-dom": "^18.2.6",
|
||||
"@typescript-eslint/eslint-plugin": "^5.61.0",
|
||||
"@typescript-eslint/parser": "^5.61.0",
|
||||
"@vitejs/plugin-react-swc": "^3.3.2",
|
||||
"eslint": "^8.44.0",
|
||||
"eslint-plugin-react": "^7.32.2",
|
||||
"eslint-plugin-react-hooks": "^4.6.0",
|
||||
"eslint-plugin-react-refresh": "^0.4.1",
|
||||
"typescript": "^5.0.2",
|
||||
"vite": "^4.4.0"
|
||||
}
|
||||
}
|
1
basic/dapps/web3modal/public/vite.svg
Normal file
1
basic/dapps/web3modal/public/vite.svg
Normal file
@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
|
After Width: | Height: | Size: 1.5 KiB |
34
basic/dapps/web3modal/src/main.tsx
Normal file
34
basic/dapps/web3modal/src/main.tsx
Normal file
@ -0,0 +1,34 @@
|
||||
import React from "react";
|
||||
import ReactDOM from "react-dom/client";
|
||||
import {
|
||||
EthereumClient,
|
||||
w3mConnectors,
|
||||
w3mProvider,
|
||||
} from "@web3modal/ethereum";
|
||||
import { Web3Modal, Web3Button } from "@web3modal/react";
|
||||
import { configureChains, createConfig, WagmiConfig } from "wagmi";
|
||||
import { arbitrum, mainnet, polygon } from "wagmi/chains";
|
||||
|
||||
// 1. select chains
|
||||
const chains = [arbitrum, mainnet, polygon];
|
||||
const projectId = import.meta.env.VITE_PROJECT_ID;
|
||||
|
||||
// 2. generate wagmiConfig with w3mConnectors
|
||||
const { publicClient } = configureChains(chains, [w3mProvider({ projectId })]);
|
||||
const wagmiConfig = createConfig({
|
||||
autoConnect: true,
|
||||
connectors: w3mConnectors({ projectId, chains }),
|
||||
publicClient,
|
||||
});
|
||||
|
||||
// 3. create ethereumClient
|
||||
const ethereumClient = new EthereumClient(wagmiConfig, chains);
|
||||
|
||||
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
|
||||
<React.StrictMode>
|
||||
<WagmiConfig config={wagmiConfig}>
|
||||
<Web3Button />
|
||||
</WagmiConfig>
|
||||
<Web3Modal projectId={projectId} ethereumClient={ethereumClient} />
|
||||
</React.StrictMode>
|
||||
);
|
1
basic/dapps/web3modal/src/vite-env.d.ts
vendored
Normal file
1
basic/dapps/web3modal/src/vite-env.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
/// <reference types="vite/client" />
|
31
basic/dapps/web3modal/tsconfig.json
Normal file
31
basic/dapps/web3modal/tsconfig.json
Normal file
@ -0,0 +1,31 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ESNext",
|
||||
"useDefineForClassFields": true,
|
||||
"lib": [
|
||||
"DOM",
|
||||
"DOM.Iterable",
|
||||
"ESNext"
|
||||
],
|
||||
"allowJs": false,
|
||||
"skipLibCheck": true,
|
||||
"esModuleInterop": false,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"strict": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "Node",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"noEmit": true,
|
||||
"jsx": "react-jsx"
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"path": "./tsconfig.node.json"
|
||||
}
|
||||
]
|
||||
}
|
11
basic/dapps/web3modal/tsconfig.node.json
Normal file
11
basic/dapps/web3modal/tsconfig.node.json
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "Node",
|
||||
"allowSyntheticDefaultImports": true
|
||||
},
|
||||
"include": [
|
||||
"vite.config.ts"
|
||||
]
|
||||
}
|
7
basic/dapps/web3modal/vite.config.ts
Normal file
7
basic/dapps/web3modal/vite.config.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { defineConfig } from "vite";
|
||||
import react from "@vitejs/plugin-react-swc";
|
||||
|
||||
// https://vitejs.dev/config/
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
});
|
3699
basic/dapps/web3modal/yarn.lock
Normal file
3699
basic/dapps/web3modal/yarn.lock
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user