A few different examples on connecting your dApps to metamask / wallets.
full-stack-web3-metamask-connectors's Introduction
full-stack-web3-metamask-connectors's People
Forkers
makarmat130 weigriffiths pratyaksh85 glaucogt bulalu 9christine doctorkervin geradramirez1629 alessiodos anthonymadia antonioduran photonbounce erriccc bori7 sleechigo khomcvla cavefishy phillip-d-shields bombermine3 curest0x1021 hermessecund 0xjaypi sea-viseth minhkhuy7601 awaisshah228 jacob-mr-yang rfart zakcroft doug-c czsdfhj sunnextt olivia-tran anibus cumulustonks cryowebtoken azharul-lincoln zjhiphop zh-tx sandalwoods peacebeuponu94 bps1980 ipfsdapps rumpelstiltskinjnr kangoulya shobeirimajid am-ramona prabalparihar arnautei tcxcx follyb2810 feed-dev clavixeno oawang suvadra-barua dragonskyside dashbaord202401 devkehinde monishwarmc klovistore2 jorbo82full-stack-web3-metamask-connectors's Issues
Question: How to Ask Users to "Allow Contract to Spend Tokens On Your Behalf"
Hi, Mr. Collins,
I don't have any issues thus far with this repo, but I just had a question. I've been watching your tutorial on "how to connect your smart contract to your frontend in 6 different ways", and I was wondering how I could ask the users to approve a contract to spend their tokens, using the JS that I've typed so far. This is my code (your code, really):
import Head from 'next/head';
import Image from 'next/image';
import styles from '../styles/Home.module.css';
import {useState} from "react";
import {ethers} from "ethers";
export default function Home() {
const [isConnected, setIsConnected] = useState(false);
const [provider, setProvider] = useState();
async function connect() {
if (typeof window.ethereum !== "undefined") {
try {
await ethereum.request({method: "eth_requestAccounts"});
setIsConnected(true);
let connectedProvider = new ethers.providers.Web3Provider(window.ethereum);
setSigner(connectedProvider.getSigner());
} catch(e) {
console.log(e);
}
} else {
setIsConnected(false);
}
}
async function approve() {
if (typeof window.ethereum !== "undefined") {
const contractAddress = "...";
const abi = [...];
const connectedProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = connectedProvider.getSigner();
const contract = new ethers.Contract(contractAddress, abi, signer);
try {
await contract.approve();
} catch (error) {
console.log(error);
}
} else {
console.log("Please install MetaMask");
}
}
return(<div className={styles.container}>Hello, user! {isConnected
? ("You're connected!") : (<button onClick= {() => connect()}>Connect!</button>)}
{isConnected ? <button onClick={() => approve()}>Approve</button> : ""}
</div>);
}
I just want to have it like most swaps (I'm not making a swap) where they ask you to approve their spending your tokens before you actually spend anything. When I click "approve" on my localhost, nothing happens. What am I doing wrong?
Thanks!
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.