Unverified Commit 92db7f16 authored by Saurabh Burade's avatar Saurabh Burade Committed by GitHub

examples/evm-multichain-dapp (#39206)




## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm lint`
- [x] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)


The template repository can be found [here](https://github.com/saurabhburade/with-evm-chains)
Public reviews, and improvement suggestions are welcome!
## Supported Blockchains
1. ETHEREUM
2. ROPSTEN
3. RINKEBY
4. GÖRLI
5. KOVAN
6. MATIC
7. MATIC_TESTNET
8. FANTOM
9. FANTOM_TESTNET
10. XDAI
11. BSC
12. BSC_TESTNET
13. ARBITRUM
14. ARBITRUM_TESTNET
15. MOONBEAM_TESTNET
16. AVALANCHE
and more


### Note 
- This template has the flexibility to seamlessly extend to other EVM Blockchains.
- It has web3.js and wallet connection modules.
- It supports blockchain Read and Write functionalities.
Co-authored-by: 's avatarBalázs Orbán <18369201+balazsorban44@users.noreply.github.com>
parent f09c3bec
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# next.js
/.next/
/out/
# production
/build
# misc
.DS_Store
*.pem
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*
# local env files
.env*.local
# vercel
.vercel
# typescript
*.tsbuildinfo
next-env.d.ts
# EVM multichain nextjs template
Nextjs Public template for building Ethereum Virtual Machine Multichain Dapps
## Deploy your own
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example):
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/evm-multichain-dapp&project-name=evm-multichain-dapp&repository-name=evm-multichain-dapp)
## How to use
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example:
```bash
npx create-next-app --example evm-multichain-dapp evm-multichain-dapp-app
```
```bash
yarn create next-app --example evm-multichain-dapp evm-multichain-dapp-app
```
```bash
pnpm create next-app --example evm-multichain-dapp evm-multichain-dapp-app
```
Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
[
{
"inputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "spender",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "previousOwner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "OwnershipTransferred",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "from",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "to",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"constant": true,
"inputs": [],
"name": "_decimals",
"outputs": [{ "internalType": "uint8", "name": "", "type": "uint8" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "_name",
"outputs": [{ "internalType": "string", "name": "", "type": "string" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "_symbol",
"outputs": [{ "internalType": "string", "name": "", "type": "string" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{ "internalType": "address", "name": "owner", "type": "address" },
{ "internalType": "address", "name": "spender", "type": "address" }
],
"name": "allowance",
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "spender", "type": "address" },
{ "internalType": "uint256", "name": "amount", "type": "uint256" }
],
"name": "approve",
"outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{ "internalType": "address", "name": "account", "type": "address" }
],
"name": "balanceOf",
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "uint256", "name": "amount", "type": "uint256" }
],
"name": "burn",
"outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [{ "internalType": "uint8", "name": "", "type": "uint8" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "spender", "type": "address" },
{
"internalType": "uint256",
"name": "subtractedValue",
"type": "uint256"
}
],
"name": "decreaseAllowance",
"outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "getOwner",
"outputs": [{ "internalType": "address", "name": "", "type": "address" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "spender", "type": "address" },
{ "internalType": "uint256", "name": "addedValue", "type": "uint256" }
],
"name": "increaseAllowance",
"outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "uint256", "name": "amount", "type": "uint256" }
],
"name": "mint",
"outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [{ "internalType": "string", "name": "", "type": "string" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "owner",
"outputs": [{ "internalType": "address", "name": "", "type": "address" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "renounceOwnership",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [{ "internalType": "string", "name": "", "type": "string" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "recipient", "type": "address" },
{ "internalType": "uint256", "name": "amount", "type": "uint256" }
],
"name": "transfer",
"outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "sender", "type": "address" },
{ "internalType": "address", "name": "recipient", "type": "address" },
{ "internalType": "uint256", "name": "amount", "type": "uint256" }
],
"name": "transferFrom",
"outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "newOwner", "type": "address" }
],
"name": "transferOwnership",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
]
{
"1": {
"nativeCurrency": { "name": "Ether", "symbol": "ETH", "decimals": 18 },
"name": "Ethereum Mainnet"
},
"3": {
"nativeCurrency": {
"name": "Ropsten Ether",
"symbol": "ROP",
"decimals": 18
},
"name": "Ropsten"
},
"4": {
"nativeCurrency": {
"name": "Rinkeby Ether",
"symbol": "RIN",
"decimals": 18
},
"name": "Rinkeby"
},
"5": {
"nativeCurrency": {
"name": "Görli Ether",
"symbol": "GOR",
"decimals": 18
},
"name": "Görli"
},
"40": {
"nativeCurrency": { "name": "Telos", "symbol": "TLOS", "decimals": 18 },
"name": "Telos EVM Mainnet"
},
"42": {
"nativeCurrency": {
"name": "Kovan Ether",
"symbol": "KOV",
"decimals": 18
},
"name": "Kovan"
},
"56": {
"nativeCurrency": {
"name": "Binance Chain Native Token",
"symbol": "BNB",
"decimals": 18
},
"name": "Binance Smart Chain Mainnet"
},
"65": {
"nativeCurrency": {
"name": "OKExChain Global Utility Token in testnet",
"symbol": "OKT",
"decimals": 18
},
"name": "OKExChain Testnet"
},
"66": {
"nativeCurrency": {
"name": "OKXChain Global Utility Token",
"symbol": "OKT",
"decimals": 18
},
"name": "OKXChain Mainnet"
},
"97": {
"nativeCurrency": {
"name": "Binance Chain Native Token",
"symbol": "tBNB",
"decimals": 18
},
"name": "Binance Smart Chain Testnet"
},
"100": {
"nativeCurrency": { "name": "xDAI", "symbol": "xDAI", "decimals": 18 },
"name": "Gnosis Chain"
},
"122": {
"nativeCurrency": { "name": "Fuse", "symbol": "FUSE", "decimals": 18 },
"name": "Fuse Mainnet"
},
"128": {
"nativeCurrency": {
"name": "Huobi ECO Chain Native Token",
"symbol": "HT",
"decimals": 18
},
"name": "Huobi ECO Chain Mainnet"
},
"137": {
"nativeCurrency": { "name": "MATIC", "symbol": "MATIC", "decimals": 18 },
"name": "Polygon Mainnet"
},
"250": {
"nativeCurrency": { "name": "Fantom", "symbol": "FTM", "decimals": 18 },
"name": "Fantom Opera"
},
"256": {
"nativeCurrency": {
"name": "Huobi ECO Chain Test Native Token",
"symbol": "htt",
"decimals": 18
},
"name": "Huobi ECO Chain Testnet"
},
"1284": {
"nativeCurrency": { "name": "Glimmer", "symbol": "GLMR", "decimals": 18 },
"name": "Moonbeam"
},
"1285": {
"nativeCurrency": { "name": "Moonriver", "symbol": "MOVR", "decimals": 18 },
"name": "Moonriver"
},
"1287": {
"nativeCurrency": { "name": "Dev", "symbol": "DEV", "decimals": 18 },
"name": "Moonbase Alpha"
},
"4002": {
"nativeCurrency": { "name": "Fantom", "symbol": "FTM", "decimals": 18 },
"name": "Fantom Testnet"
},
"31337": {
"nativeCurrency": {
"name": "GoChain Coin",
"symbol": "GO",
"decimals": 18
},
"name": "GoChain Testnet"
},
"42161": {
"nativeCurrency": { "name": "Ether", "symbol": "ETH", "decimals": 18 },
"name": "Arbitrum One"
},
"42220": {
"nativeCurrency": { "name": "CELO", "symbol": "CELO", "decimals": 18 },
"name": "Celo Mainnet"
},
"43113": {
"nativeCurrency": { "name": "Avalanche", "symbol": "AVAX", "decimals": 18 },
"name": "Avalanche Fuji Testnet"
},
"43114": {
"nativeCurrency": { "name": "Avalanche", "symbol": "AVAX", "decimals": 18 },
"name": "Avalanche C-Chain"
},
"80001": {
"nativeCurrency": { "name": "MATIC", "symbol": "MATIC", "decimals": 18 },
"name": "Mumbai"
},
"1666600000": {
"nativeCurrency": { "name": "ONE", "symbol": "ONE", "decimals": 18 },
"name": "Harmony Mainnet Shard 0"
},
"1666700000": {
"nativeCurrency": { "name": "ONE", "symbol": "ONE", "decimals": 18 },
"name": "Harmony Testnet Shard 0"
},
"11297108109": {
"nativeCurrency": { "name": "PALM", "symbol": "PALM", "decimals": 18 },
"name": "Palm"
},
"11297108099": {
"nativeCurrency": { "name": "PALM", "symbol": "PALM", "decimals": 18 },
"name": "Palm Testnet"
}
}
export enum ChainId {
ETHEREUM = 1,
ROPSTEN = 3,
RINKEBY = 4,
GÖRLI = 5,
KOVAN = 42,
MATIC = 137,
MATIC_TESTNET = 80001,
FANTOM = 250,
FANTOM_TESTNET = 4002,
XDAI = 100,
BSC = 56,
BSC_TESTNET = 97,
ARBITRUM = 42161,
MOONBEAM_TESTNET = 1287,
AVALANCHE = 43114,
AVALANCHE_TESTNET = 43113,
HECO = 128,
HECO_TESTNET = 256,
HARMONY = 1666600000,
HARMONY_TESTNET = 1666700000,
OKEX = 66,
OKEX_TESTNET = 65,
CELO = 42220,
PALM = 11297108109,
PALM_TESTNET = 11297108099,
MOONRIVER = 1285,
FUSE = 122,
TELOS = 40,
HARDHAT = 31337,
MOONBEAM = 1284,
}
import { ChainId } from './chainIds'
const Arbitrum =
'https://raw.githubusercontent.com/sushiswap/icons/master/network/arbitrum.jpg'
const Avalanche =
'https://raw.githubusercontent.com/sushiswap/icons/master/network/avalanche.jpg'
const Bsc =
'https://raw.githubusercontent.com/sushiswap/icons/master/network/bsc.jpg'
const Fantom =
'https://raw.githubusercontent.com/sushiswap/icons/master/network/fantom.jpg'
const Goerli =
'https://raw.githubusercontent.com/sushiswap/icons/master/network/goerli.jpg'
const Harmony =
'https://raw.githubusercontent.com/sushiswap/icons/master/network/harmonyone.jpg'
const Heco =
'https://raw.githubusercontent.com/sushiswap/icons/master/network/heco.jpg'
const Kovan =
'https://raw.githubusercontent.com/sushiswap/icons/master/network/kovan.jpg'
const Mainnet =
'https://raw.githubusercontent.com/sushiswap/icons/master/network/mainnet.jpg'
const Matic =
'https://raw.githubusercontent.com/sushiswap/icons/master/network/polygon.jpg'
const Moonbeam =
'https://raw.githubusercontent.com/sushiswap/icons/master/network/moonbeam.jpg'
const OKEx =
'https://raw.githubusercontent.com/sushiswap/icons/master/network/okex.jpg'
const Polygon =
'https://raw.githubusercontent.com/sushiswap/icons/master/network/polygon.jpg'
const Rinkeby =
'https://raw.githubusercontent.com/sushiswap/icons/master/network/rinkeby.jpg'
const Ropsten =
'https://raw.githubusercontent.com/sushiswap/icons/master/network/ropsten.jpg'
const xDai =
'https://raw.githubusercontent.com/sushiswap/icons/master/network/xdai.jpg'
const Celo =
'https://raw.githubusercontent.com/sushiswap/icons/master/network/celo.jpg'
const Palm =
'https://raw.githubusercontent.com/sushiswap/icons/master/network/palm.jpg'
const Moonriver =
'https://raw.githubusercontent.com/sushiswap/icons/master/network/moonriver.jpg'
const Fuse =
'https://raw.githubusercontent.com/sushiswap/icons/master/token/fuse.jpg'
const Telos =
'https://raw.githubusercontent.com/sushiswap/logos/main/network/telos/0xD102cE6A4dB07D247fcc28F366A623Df0938CA9E.jpg'
export const NETWORK_ICON = {
[ChainId.ETHEREUM]: Mainnet,
[ChainId.ROPSTEN]: Ropsten,
[ChainId.RINKEBY]: Rinkeby,
[ChainId.GÖRLI]: Goerli,
[ChainId.KOVAN]: Kovan,
[ChainId.FANTOM]: Fantom,
[ChainId.FANTOM_TESTNET]: Fantom,
[ChainId.BSC]: Bsc,
[ChainId.BSC_TESTNET]: Bsc,
[ChainId.MATIC]: Polygon,
[ChainId.MATIC_TESTNET]: Matic,
[ChainId.XDAI]: xDai,
[ChainId.ARBITRUM]: Arbitrum,
[ChainId.MOONBEAM_TESTNET]: Moonbeam,
[ChainId.AVALANCHE]: Avalanche,
[ChainId.AVALANCHE_TESTNET]: Avalanche,
[ChainId.HECO]: Heco,
[ChainId.HECO_TESTNET]: Heco,
[ChainId.HARMONY]: Harmony,
[ChainId.HARMONY_TESTNET]: Harmony,
[ChainId.OKEX]: OKEx,
[ChainId.OKEX_TESTNET]: OKEx,
[ChainId.CELO]: Celo,
[ChainId.PALM]: Palm,
[ChainId.MOONRIVER]: Moonriver,
[ChainId.FUSE]: Fuse,
[ChainId.TELOS]: Telos,
}
export const NETWORK_LABEL: { [chainId in ChainId]?: string } = {
[ChainId.ETHEREUM]: 'Ethereum',
[ChainId.RINKEBY]: 'Rinkeby',
[ChainId.ROPSTEN]: 'Ropsten',
[ChainId.GÖRLI]: 'Görli',
[ChainId.KOVAN]: 'Kovan',
[ChainId.FANTOM]: 'Fantom',
[ChainId.FANTOM_TESTNET]: 'Fantom Testnet',
[ChainId.MATIC]: 'Polygon',
[ChainId.MATIC_TESTNET]: 'Polygon Testnet',
[ChainId.XDAI]: 'xDai',
[ChainId.ARBITRUM]: 'Arbitrum',
[ChainId.BSC]: 'BSC',
[ChainId.BSC_TESTNET]: 'BSC Testnet',
[ChainId.MOONBEAM_TESTNET]: 'Moonbase',
[ChainId.AVALANCHE]: 'Avalanche',
[ChainId.AVALANCHE_TESTNET]: 'Fuji',
[ChainId.HECO]: 'HECO',
[ChainId.HECO_TESTNET]: 'HECO Testnet',
[ChainId.HARMONY]: 'Harmony',
[ChainId.HARMONY_TESTNET]: 'Harmony Testnet',
[ChainId.OKEX]: 'OKEx',
[ChainId.OKEX_TESTNET]: 'OKEx',
[ChainId.CELO]: 'Celo',
[ChainId.PALM]: 'Palm',
[ChainId.MOONRIVER]: 'Moonriver',
[ChainId.FUSE]: 'Fuse',
[ChainId.TELOS]: 'Telos EVM',
}
import { ChainId } from './chainIds'
const RPC = {
[ChainId.ETHEREUM]: 'https://api.sushirelay.com/v1',
// [ChainId.ETHEREUM]: 'https://eth-mainnet.alchemyapi.io/v2/HNQXSfiUcPjfpDBQaWYXjqlhTr1cEY9c',
// [ChainId.MAINNET]: 'https://eth-mainnet.alchemyapi.io/v2/q1gSNoSMEzJms47Qn93f9-9Xg5clkmEC',
[ChainId.ROPSTEN]:
'https://eth-ropsten.alchemyapi.io/v2/cidKix2Xr-snU3f6f6Zjq_rYdalKKHmW',
[ChainId.RINKEBY]:
'https://eth-rinkeby.alchemyapi.io/v2/XVLwDlhGP6ApBXFz_lfv0aZ6VmurWhYD',
[ChainId.GÖRLI]:
'https://eth-goerli.alchemyapi.io/v2/Dkk5d02QjttYEoGmhZnJG37rKt8Yl3Im',
[ChainId.KOVAN]:
'https://eth-kovan.alchemyapi.io/v2/6OVAa_B_rypWWl9HqtiYK26IRxXiYqER',
[ChainId.FANTOM]: 'https://rpcapi.fantom.network',
[ChainId.FANTOM_TESTNET]: 'https://rpc.testnet.fantom.network',
[ChainId.MATIC]: 'https://polygon-rpc.com/',
[ChainId.MATIC_TESTNET]: 'https://rpc-mumbai.matic.today',
[ChainId.XDAI]: 'https://rpc.xdaichain.com',
[ChainId.BSC]: 'https://bsc-dataseed.binance.org/',
[ChainId.BSC_TESTNET]: 'https://data-seed-prebsc-2-s3.binance.org:8545',
[ChainId.MOONBEAM_TESTNET]: 'https://rpc.testnet.moonbeam.network',
[ChainId.AVALANCHE]: 'https://api.avax.network/ext/bc/C/rpc',
[ChainId.AVALANCHE_TESTNET]: 'https://api.avax-test.network/ext/bc/C/rpc',
[ChainId.HECO]: 'https://http-mainnet.hecochain.com',
[ChainId.HECO_TESTNET]: 'https://http-testnet.hecochain.com',
[ChainId.HARMONY]: 'https://api.harmony.one',
[ChainId.HARMONY_TESTNET]: 'https://api.s0.b.hmny.io',
[ChainId.OKEX]: 'https://exchainrpc.okex.org',
[ChainId.OKEX_TESTNET]: 'https://exchaintestrpc.okex.org',
[ChainId.ARBITRUM]: 'https://arb1.arbitrum.io/rpc',
[ChainId.PALM]:
'https://palm-mainnet.infura.io/v3/da5fbfafcca14b109e2665290681e267',
[ChainId.FUSE]: 'https://rpc.fuse.io',
[ChainId.CELO]: 'https://forno.celo.org',
[ChainId.MOONRIVER]: 'https://rpc.moonriver.moonbeam.network',
[ChainId.TELOS]: 'https://mainnet.telos.net/evm',
}
export default RPC
import { ChainId } from './chainIds'
import { InjectedConnector } from '@web3-react/injected-connector'
export const supportedChainIds = Object.values(ChainId) as number[]
export const injected = new InjectedConnector({
supportedChainIds,
})
export const wallets = [
{
name: 'Injected',
connector: injected,
logo: 'https://cdn.iconscout.com/icon/free/png-256/metamask-2728406-2261817.png',
},
]
import { useEffect, useState, useRef } from 'react'
import Web3 from 'web3'
import { useWeb3React } from '@web3-react/core'
import RPC from '../config/rpc'
/**
* Provides a web3 instance using the provider provided by useWallet
* with a fallback of an httpProver
* Recreate web3 instance only if the provider change
*/
const useWeb3 = () => {
const { library, chainId = 1 } = useWeb3React()
const refEth = useRef(library)
const [web3, setweb3] = useState(
library
? new Web3(library)
: // @ts-ignore TYPE NEEDS FIXING
new Web3(RPC[chainId])
)
useEffect(() => {
if (library !== refEth.current) {
setweb3(
library
? new Web3(library)
: // @ts-ignore TYPE NEEDS FIXING
new Web3(RPC[chainId])
)
refEth.current = library
}
}, [library, setweb3, chainId])
return { web3, setweb3 }
}
export default useWeb3
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
images: {
domains: ['raw.githubusercontent.com'],
},
}
module.exports = nextConfig
{
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"@web3-react/abstract-connector": "^6.0.7",
"@web3-react/core": "^6.1.9",
"@web3-react/injected-connector": "^6.0.7",
"@web3-react/walletconnect-connector": "^6.2.13",
"bignumber.js": "^9.0.2",
"next": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"web3": "^1.7.4"
},
"devDependencies": {
"@types/node": "^18.6.2",
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.6",
"typescript": "^4.7.4"
}
}
import '../styles/globals.css'
import type { AppProps } from 'next/app'
import { Web3ReactProvider } from '@web3-react/core'
function MyApp({ Component, pageProps }: AppProps) {
const getLibrary = (provider: any) => {
return provider
}
return (
<>
<Web3ReactProvider getLibrary={getLibrary}>
<Component {...pageProps} />
</Web3ReactProvider>
</>
)
}
export default MyApp
import { useWeb3React } from '@web3-react/core'
import type { NextPage } from 'next'
import Head from 'next/head'
import Image from 'next/image'
import { useEffect, useState } from 'react'
import blockchains from '../config/blockchains.json'
import { ChainId } from '../config/chainIds'
import { NETWORK_ICON, NETWORK_LABEL } from '../config/networks'
import { injected, supportedChainIds, wallets } from '../config/wallets'
import useWeb3 from '../hooks/useWeb3'
import styles from '../styles/Home.module.css'
const Home: NextPage = () => {
const { account, activate, chainId, deactivate } = useWeb3React()
const [nativeBalance, setNativeBalance] = useState(0)
const [sendAddress, setSendAddress] = useState('')
const [sendAmount, setSendAmount] = useState('0')
const { web3 } = useWeb3()
// @ts-ignore TYPE NEEDS FIXING
const chainData = blockchains[chainId]
useEffect(() => {
// @ts-ignore TYPE NEEDS FIXING
if (window?.ethereum) {
activate(injected)
} else {
alert('You need to install a crypto wallet to run this Dapp!!')
}
}, [activate])
const fetchBalanceData = async () => {
const eth = await web3.eth.getBalance(account as string)
if (Number(eth) > 0) {
const bal = web3.utils.fromWei(eth, 'ether')
console.log({ eth, bal })
setNativeBalance(Number(bal))
}
}
useEffect(() => {
if (account) {
fetchBalanceData()
}
})
const sendEth = async () => {
const isValidAddress = web3.utils.isAddress(sendAddress)
if (isValidAddress && Number(sendAmount) > 0 && account) {
const tx = await web3.eth.sendTransaction({
from: account,
to: sendAddress,
// @ts-ignore TYPE NEEDS FIXING
value: web3.utils.toWei(sendAmount, 'ether'),
})
if (tx.transactionHash) {
alert(`Transaction Success!! ${tx.transactionHash}`)
fetchBalanceData()
}
console.log({ tx })
} else {
alert('Invalid Transaction Data')
}
}
return (
<div className={styles.container}>
<Head>
<title>Create Next App</title>
<meta name="description" content="Generated by create next app" />
<link rel="icon" href="/favicon.ico" />
</Head>
<main>
{account ? (
<>
<div className="card">
<h4>
Connected to Network : {NETWORK_LABEL[chainId as ChainId]}
</h4>
<h4>
Native Balance : {nativeBalance || 0}{' '}
{chainData?.nativeCurrency?.symbol}
</h4>
<p>Account : {account}</p>
<button className="btn" onClick={() => deactivate()}>
Logout
</button>
</div>
<div className="card">
<h2>Send {chainData?.nativeCurrency?.symbol}</h2>
<div className="my-1">
<label htmlFor="sendAddress">Recipient Address</label>
<input
id="sendAddress"
className="mx-1"
type="text"
value={sendAddress}
onChange={(e) => setSendAddress(e?.target?.value)}
/>
</div>
<div className="my-1">
<label htmlFor="sendAmount">Recipient Amount</label>
<input
id="sendAmount"
className="mx-1"
type="number"
value={sendAmount}
onChange={(e) => setSendAmount(e?.target?.value)}
/>
</div>
<h4>
Native Balance : {nativeBalance || 0}{' '}
{chainData?.nativeCurrency?.symbol}
</h4>
<button className="btn" onClick={sendEth}>
Send {chainData?.nativeCurrency?.symbol}{' '}
</button>
</div>
</>
) : (
<div className="card">
<h1>Connect Wallet</h1>
{wallets?.map((wallet, idx) => {
return (
<button
key={wallet?.name}
className="btn"
onClick={() => activate(wallet?.connector)}
>
{wallet?.name}
</button>
)
})}
</div>
)}
<div className="card">
<h1>Supported Blockchains</h1>
<div className="flex flex-wrap">
{supportedChainIds?.map((chain, idx) =>
// @ts-ignore TYPE NEEDS FIXING
Number(chain) && NETWORK_ICON[Number(chain)] ? (
<div
key={Number(chain)}
className="flex items-center rounded-md"
style={{
background: 'black',
padding: '0.1em 0.25em',
margin: '1em 0.5em',
}}
>
<Image
// @ts-ignore TYPE NEEDS FIXING
src={NETWORK_ICON[Number(chain)]}
width={30}
height={30}
alt=""
className="rounded-md"
/>
<p
style={{
marginLeft: '0.5em',
fontSize: '12px',
padding: '0em 0.5em',
}}
>
{/* @ts-ignore TYPE NEEDS FIXING */}
{NETWORK_LABEL[Number(chain)]}
</p>
</div>
) : null
)}
<p>Note : The chains are extendable to any EVM based Blockchain.</p>
</div>
</div>
</main>
</div>
)
}
export default Home
<svg width="283" height="64" viewBox="0 0 283 64" fill="none"
xmlns="http://www.w3.org/2000/svg">
<path d="M141.04 16c-11.04 0-19 7.2-19 18s8.96 18 20 18c6.67 0 12.55-2.64 16.19-7.09l-7.65-4.42c-2.02 2.21-5.09 3.5-8.54 3.5-4.79 0-8.86-2.5-10.37-6.5h28.02c.22-1.12.35-2.28.35-3.5 0-10.79-7.96-17.99-19-17.99zm-9.46 14.5c1.25-3.99 4.67-6.5 9.45-6.5 4.79 0 8.21 2.51 9.45 6.5h-18.9zM248.72 16c-11.04 0-19 7.2-19 18s8.96 18 20 18c6.67 0 12.55-2.64 16.19-7.09l-7.65-4.42c-2.02 2.21-5.09 3.5-8.54 3.5-4.79 0-8.86-2.5-10.37-6.5h28.02c.22-1.12.35-2.28.35-3.5 0-10.79-7.96-17.99-19-17.99zm-9.45 14.5c1.25-3.99 4.67-6.5 9.45-6.5 4.79 0 8.21 2.51 9.45 6.5h-18.9zM200.24 34c0 6 3.92 10 10 10 4.12 0 7.21-1.87 8.8-4.92l7.68 4.43c-3.18 5.3-9.14 8.49-16.48 8.49-11.05 0-19-7.2-19-18s7.96-18 19-18c7.34 0 13.29 3.19 16.48 8.49l-7.68 4.43c-1.59-3.05-4.68-4.92-8.8-4.92-6.07 0-10 4-10 10zm82.48-29v46h-9V5h9zM36.95 0L73.9 64H0L36.95 0zm92.38 5l-27.71 48L73.91 5H84.3l17.32 30 17.32-30h10.39zm58.91 12v9.69c-1-.29-2.06-.49-3.2-.49-5.81 0-10 4-10 10V51h-9V17h9v9.2c0-5.08 5.91-9.2 13.2-9.2z" fill="#000"/>
</svg>
\ No newline at end of file
.container {
padding: 0 2rem;
}
.main {
min-height: 100vh;
padding: 4rem 0;
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.footer {
display: flex;
flex: 1;
padding: 2rem 0;
border-top: 1px solid #eaeaea;
justify-content: center;
align-items: center;
}
.footer a {
display: flex;
justify-content: center;
align-items: center;
flex-grow: 1;
}
.title a {
color: #0070f3;
text-decoration: none;
}
.title a:hover,
.title a:focus,
.title a:active {
text-decoration: underline;
}
.title {
margin: 0;
line-height: 1.15;
font-size: 4rem;
}
.title,
.description {
text-align: center;
}
.description {
margin: 4rem 0;
line-height: 1.5;
font-size: 1.5rem;
}
.code {
background: #fafafa;
border-radius: 5px;
padding: 0.75rem;
font-size: 1.1rem;
font-family: Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono,
Bitstream Vera Sans Mono, Courier New, monospace;
}
.grid {
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
max-width: 800px;
}
.card {
margin: 1rem;
padding: 1.5rem;
text-align: left;
color: inherit;
text-decoration: none;
border: 1px solid #eaeaea;
border-radius: 10px;
transition: color 0.15s ease, border-color 0.15s ease;
max-width: 300px;
}
.card:hover,
.card:focus,
.card:active {
color: #0070f3;
border-color: #0070f3;
}
.card h2 {
margin: 0 0 1rem 0;
font-size: 1.5rem;
}
.card p {
margin: 0;
font-size: 1.25rem;
line-height: 1.5;
}
.logo {
height: 1em;
margin-left: 0.5rem;
}
@media (max-width: 600px) {
.grid {
width: 100%;
flex-direction: column;
}
}
@media (prefers-color-scheme: dark) {
.card,
.footer {
border-color: #222;
}
.code {
background: #111;
}
.logo img {
filter: invert(1);
}
}
html,
body {
padding: 0;
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
}
a {
color: inherit;
text-decoration: none;
}
* {
box-sizing: border-box;
}
@media (prefers-color-scheme: dark) {
html {
color-scheme: dark;
}
body {
color: white;
background: rgb(10, 10, 10);
}
}
.card {
margin: 5% 0%;
padding: 2%;
background: rgb(0, 0, 0);
background: -moz-radial-gradient(
circle,
rgba(0, 0, 0, 1) 0%,
rgba(11, 11, 78, 1) 35%,
rgba(0, 0, 0, 1) 100%
);
background: -webkit-radial-gradient(
circle,
rgba(0, 0, 0, 1) 0%,
rgba(11, 11, 78, 1) 35%,
rgba(0, 0, 0, 1) 100%
);
background: radial-gradient(
circle,
rgba(0, 0, 0, 1) 0%,
rgba(11, 11, 78, 1) 35%,
rgba(0, 0, 0, 1) 100%
);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#000000",endColorstr="#000000",GradientType=1);
border: 1px solid;
border-radius: 5px;
}
.flex {
display: flex;
}
.flex-wrap {
flex-wrap: wrap;
}
.my-1 {
margin-top: 1%;
margin-bottom: 1%;
}
.mx-1 {
margin-left: 1%;
margin-right: 1%;
}
.btn {
background-color: rgb(46, 46, 244);
padding: 0.5em 1em;
border: none;
border-radius: 2px;
color: white;
}
.btn:hover {
background-color: rgb(87, 87, 240);
cursor: pointer;
}
.btn:active {
background-color: rgb(0, 0, 255);
}
.items-center {
align-items: center;
}
.rounded {
border-radius: 2px;
}
.rounded-md {
border-radius: 5px;
}
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment