diff --git a/lib/atoms/.gitkeep b/lib/atoms/.gitkeep new file mode 100644 index 0000000..a09bf54 --- /dev/null +++ b/lib/atoms/.gitkeep @@ -0,0 +1 @@ +All atoms shoul be created in this folder \ No newline at end of file diff --git a/lib/hooks/.gitkeep b/lib/hooks/.gitkeep new file mode 100644 index 0000000..01d0b7b --- /dev/null +++ b/lib/hooks/.gitkeep @@ -0,0 +1 @@ +All reusable hooks should be created under this folder diff --git a/lib/web3/actions.ts b/lib/web3/actions.ts new file mode 100644 index 0000000..d737c1e --- /dev/null +++ b/lib/web3/actions.ts @@ -0,0 +1,14 @@ +import Web3Service from "./service"; + +class Web3Actions { + public async connectWallet() { + const web3Instance = new Web3Service(); + const signer = await web3Instance.connect(); + + console.log(signer); + } +} + +const web3Actions = new Web3Actions(); + +export default web3Actions; diff --git a/lib/web3/index.ts b/lib/web3/index.ts index bc5e9b2..4a7dab7 100644 --- a/lib/web3/index.ts +++ b/lib/web3/index.ts @@ -1,28 +1,4 @@ -import { ethers } from "ethers"; -import { TChain } from "../../types/chains"; -import { CHAINS_ENV } from "../config/chains"; -import NFTABI from "../web3/abis/NFT.json"; -class Web3Instance { - public provider; +import web3Actions from "./actions"; +import Web3Service from "./service"; - constructor() { - this.provider = new ethers.providers.JsonRpcProvider( - CHAINS_ENV.polygon.nodeRPC - ); - } - - public contract(chain: TChain = "polygon") { - const network = this.getChainByName(chain); - return new ethers.Contract( - network.mintContractAddress, - NFTABI, - this.provider - ); - } - - public getChainByName(chain: TChain = "polygon") { - return CHAINS_ENV[chain]; - } -} - -export default Web3Instance; +export { Web3Service, web3Actions }; diff --git a/lib/web3/service.ts b/lib/web3/service.ts new file mode 100644 index 0000000..8b0cd5d --- /dev/null +++ b/lib/web3/service.ts @@ -0,0 +1,47 @@ +import { ethers } from "ethers"; +import { TChain } from "../../types/chains"; +import { CHAINS_ENV } from "../config/chains"; +import NFTABI from "./abis/NFT.json"; + +class Web3Service { + public provider; + + constructor() { + // Initial implementation of connecting to the blockchain with an hardcoded chain + this.provider = new ethers.providers.JsonRpcProvider( + CHAINS_ENV.polygon.nodeRPC + ); + } + + public contract(chain: TChain = "polygon") { + const network = this.getChainByName(chain); + return new ethers.Contract( + network.mintContractAddress, + NFTABI, + this.provider + ); + } + + public getChainByName(chain: TChain = "polygon") { + return CHAINS_ENV[chain]; + } + + /** + * Hey folks, this is just the initial way of connecting wallet with support to MetaMask. + * It should be improved to support other wallets. + * @returns signer - The currently connected user + */ + public async connect() { + if (window && !window?.ethereum) + return new Error("Browser does not support Web3"); + + const provider = new ethers.providers.Web3Provider(window.ethereum); + + await provider.send("eth_requestAccounts", []); + + const signer = provider.getSigner(); + return signer; + } +} + +export default Web3Service; diff --git a/pages/index.tsx b/pages/index.tsx index 9302f05..249a18f 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -1,6 +1,7 @@ import { Button } from "antd"; import type { NextPage } from "next"; import Head from "next/head"; +import { web3Actions } from "../lib/web3"; const Home: NextPage = () => { return ( @@ -13,7 +14,9 @@ const Home: NextPage = () => {