{"id":898,"date":"2026-11-21T03:05:41","date_gmt":"2026-11-20T19:05:41","guid":{"rendered":"http:\/\/www.wipeoutcockroaches.com\/blog\/?p=898"},"modified":"2026-11-21T03:05:41","modified_gmt":"2026-11-20T19:05:41","slug":"how-to-approve-erc-20-tokens-with-ethers-js-409a-50b2c1","status":"publish","type":"post","link":"http:\/\/www.wipeoutcockroaches.com\/blog\/2026\/11\/21\/how-to-approve-erc-20-tokens-with-ethers-js-409a-50b2c1\/","title":{"rendered":"How to approve ERC-20 tokens with Ethers.js?"},"content":{"rendered":"<p>Hey there, <a href=\"https:\/\/www.jinforestchem.com\/ethers\/\">Ethers<\/a><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.jinforestchem.com\/uploads\/48780\/page\/small\/industrial-grade-ethylene2-glycoleea90.jpg\"><\/p>\n<p>If you\u2019ve ever dabbled in crypto, you know ERC-20 tokens are basically the backbone of the entire Ethereum ecosystem. They\u2019re what you use for swapping, lending, staking, and pretty much every other DeFi thing out there. But here\u2019s the thing that trips a lot of new devs up (even some seasoned ones): approving ERC-20 tokens. It\u2019s not as straightforward as it sounds, and if you mess it up, you can end up with transactions that get stuck, or worse, accidentally give a contract more access than you wanted.<\/p>\n<p>That\u2019s where Ethers.js comes in. It\u2019s one of the most popular libraries for working with Ethereum, and as someone who runs an Ethers.js supply\u2014we hook devs up with reliable, up-to-date Ethers.js setups and support\u2014 I\u2019ve seen way too many folks fumble through approvals. Today, I\u2019m breaking down exactly how to do it right, no jargon, no confusing steps. Let\u2019s dive in.<\/p>\n<p>First, a quick reminder of what an ERC-20 approval actually is. When you approve a token, you\u2019re giving a specific smart contract permission to spend a certain amount of that token on your behalf. Like, if you want to use a DeFi protocol to lend your USDC, that protocol needs permission to pull USDC from your wallet. That\u2019s an approval. Simple, right? But if you just call the wrong function, or set the wrong amount, you\u2019ve got a problem.<\/p>\n<p>Let\u2019s start with the basics: setting up Ethers.js. I\u2019m assuming you already have Node.js installed, because that\u2019s how most people work with Ethers.js. If not, go grab it\u2014super easy. First, initialize a new project if you don\u2019t have one already. Run <code>npm init -y<\/code> in your terminal, then install Ethers.js. We recommend using v6 because it\u2019s the latest stable version, and it\u2019s way less buggy than v5. So: <code>npm install ethers@6<\/code>. That\u2019s it for setup. If you need anything extra\u2014like pre-built Ethers.js packages or support with your environment\u2014hit us up, we supply all the good stuff so you don\u2019t have to chase down dependencies.<\/p>\n<p>Next, you need a signer. The signer is your connection to the Ethereum wallet, basically. It\u2019s what lets you send transactions, like the approval one. You can get a signer a few different ways, but for most devs, using MetaMask is the easiest because everyone uses MetaMask. So if the user has MetaMask installed, you can access the window.ethereum object. In Ethers.js v6, that works like this:<\/p>\n<p>First, import ethers: <code>import { ethers } from &quot;ethers&quot;;<\/code> Then, get the provider, which is the link to the Ethereum network. <code>const provider = new ethers.BrowserProvider(window.ethereum);<\/code> Then, get the signer from the provider: <code>const signer = await provider.getSigner();<\/code> That\u2019s your signer. Wait, but you need to make sure the user has connected their wallet first, right? So you should add a check to see if <code>window.ethereum<\/code> exists\u2014if not, tell the user to install MetaMask. If you run into issues with the provider or signer, we can help you fix that; we\u2019ve got pre-configured Ethers.js setups that skip the headache.<\/p>\n<p>Now, the ERC-20 token contract. You need two things for this: the token\u2019s address, and the ABI (Application Binary Interface) for the ERC-20 contract. The ABI is like the contract\u2019s instruction manual\u2014tells Ethers.js what functions you can call. The good news is, the standard ERC-20 ABI only has like 10 functions, so you don\u2019t need a huge one. The key functions for approval are <code>approve<\/code>, <code>allowance<\/code>, <code>spender<\/code> (wait, no, <code>allowance<\/code> is the main one to check). The standard ERC-20 ABI for approval looks like this:<\/p>\n<p>[<br \/>\n&quot;function approve(address spender, uint256 amount) external returns (bool)&quot;,<br \/>\n&quot;function allowance(address owner, address spender) external view returns (uint256)&quot;<br \/>\n]<\/p>\n<p>If you\u2019re not sure what the token address is, you can find it on Etherscan\u2014just search for the token name, and it\u2019ll show the contract address. For example, USDC on Ethereum Mainnet is <code>0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48<\/code>. Got that? Great. Now, create a contract instance for the token. That\u2019s how you interact with it using the signer. So: <code>const tokenAddress = &quot;PUT_YOUR_TOKEN_ADDRESS_HERE&quot;;<\/code> Then, <code>const tokenContract = new ethers.Contract(tokenAddress, erc20Abi, signer);<\/code> Perfect. That\u2019s your token contract ready to go.<\/p>\n<p>Now, the actual approval. This is the part that most people mess up. First, decide two things: who are you approving? That\u2019s the spender address\u2014like the DeFi protocol\u2019s contract address. Second, how much are you approving? The big mistake here is either approving an infinite amount (which is okay sometimes, but not always) or approving a tiny amount when you need more later. Wait, let\u2019s clarify: infinite approval is when you set the amount to <code>ethers.MaxUint256<\/code>, which is basically the maximum possible number for a uint256. That means the spender can spend as much of your token as they want, forever. Some protocols prefer this because you don\u2019t have to approve again later, but it\u2019s risky if the protocol gets hacked. If you only need a specific amount, set it to the exact number of tokens you want to allow.<\/p>\n<p>Wait, but ERC-20 tokens have decimals, right? Like USDC has 6 decimals, so 1 USDC is 1,000,000 (because 1 * 10^6). So you can\u2019t just write <code>1<\/code> for the amount\u2014you have to convert it to the smallest unit. Ethers.js has a helper function for that: <code>ethers.parseUnits()<\/code>. That takes the human-readable amount and converts it to the correct smallest unit. For example, to approve 100 USDC, that\u2019s <code>ethers.parseUnits(&quot;100&quot;, 6)<\/code>. If you don\u2019t know the token\u2019s decimals, you can call the <code>decimals()<\/code> function from the ERC-20 contract\u2014like <code>const decimals = await tokenContract.decimals();<\/code> That way, it works for any token, not just ones with 6 decimals.<\/p>\n<p>So putting the approval call together. Let\u2019s say we want to approve 100 USDC to a spender address <code>0x123...abc<\/code> (replace that with the actual spender address). Here\u2019s the code:<\/p>\n<p>async function approveToken(spenderAddress, amount) {<br \/>\ntry {<br \/>\n\/\/ Get the decimals first, just to be safe<br \/>\nconst decimals = await tokenContract.decimals();<br \/>\n\/\/ Convert amount to smallest unit<br \/>\nconst amountInWei = ethers.parseUnits(amount.toString(), decimals);<br \/>\n\/\/ Call approve function<br \/>\nconst tx = await tokenContract.approve(spenderAddress, amountInWei);<br \/>\n\/\/ Wait for the transaction to be mined<br \/>\nawait tx.wait();<br \/>\nconsole.log(&quot;Approval successful! Transaction hash:&quot;, tx.hash);<br \/>\nreturn true;<br \/>\n} catch (error) {<br \/>\nconsole.error(&quot;Approval failed:&quot;, error.message);<br \/>\nreturn false;<br \/>\n}<br \/>\n}<\/p>\n<p>Wait, that\u2019s it? Yeah, that\u2019s the core. But wait, what about gas fees? Oh right, sometimes Ethers.js will estimate gas for you, but sometimes you need to set it manually if the estimation is off. The <code>approve<\/code> function usually doesn\u2019t have variable gas costs, but it\u2019s good practice to add gas limit if you run into issues. Like, when you create the transaction, you can specify <code>gasLimit: 100000<\/code> (that\u2019s a safe number for approval). But Ethers.js will do this automatically most of the time. If you\u2019re getting stuck on gas, we can help you set up gas price or gas limit configurations with our Ethers.js supply kits\u2014no more guessing.<\/p>\n<p>Now, how do you check if the approval actually worked? Because you don\u2019t want to send a transaction and not know if it went through. Use the <code>allowance<\/code> function from the ERC-20 contract. The allowance function takes two arguments: your address (the owner, which is the signer\u2019s address) and the spender address. So:<\/p>\n<p>async function checkAllowance(spenderAddress) {<br \/>\n\/\/ Get the user&#8217;s address from the signer<br \/>\nconst ownerAddress = await signer.getAddress();<br \/>\n\/\/ Call allowance<br \/>\nconst allowance = await tokenContract.allowance(ownerAddress, spenderAddress);<br \/>\n\/\/ Convert back to human-readable amount<br \/>\nconst decimals = await tokenContract.decimals();<br \/>\nconst readableAllowance = ethers.formatUnits(allowance, decimals);<br \/>\nconsole.log(<code>Allowance for ${spenderAddress}: ${readableAllowance}<\/code>);<br \/>\nreturn readableAllowance;<br \/>\n}<\/p>\n<p>That\u2019s a good check to add. Run this right after the approval transaction is mined, and you\u2019ll see exactly how much the spender can spend. If it\u2019s zero, something went wrong\u2014maybe the transaction failed, or you used the wrong address. If it\u2019s the amount you set, you\u2019re golden.<\/p>\n<p>Wait, common mistakes to avoid. Let\u2019s list these because they\u2019re super common. First, using the wrong token address. If you\u2019re approving the wrong token, that\u2019s a big problem\u2014you\u2019ll approve a token you don\u2019t need, and waste gas. Double-check the token address on Etherscan. Second, using the wrong spender address. Again, double-check that protocol\u2019s contract address\u2014if you use a random address, you\u2019re giving funds to someone else. Third, forgetting decimals. If you set the amount to <code>1<\/code> instead of <code>1000000<\/code> for USDC, you\u2019re only approving 1 unit (which is 1 micro USDC, basically nothing). Fourth, not waiting for the transaction to be mined. A lot of new devs call the function and don\u2019t await <code>tx.wait()<\/code>, so they think it\u2019s done when it\u2019s still pending. That leads to errors later. Fifth, using Ethers.js v5 instead of v6. A lot of tutorials online are for v5, and the syntax is different (like <code>ethers.utils.parseUnits<\/code> is now <code>ethers.parseUnits<\/code> in v6). If you get Ethers.js from us, we give you the latest version with updated docs, so you don\u2019t have to worry about that.<\/p>\n<p>Another thing: infinite approvals. Like I mentioned earlier, if you want to approve an infinite amount, just use <code>ethers.MaxUint256<\/code> instead of parsing a number. So <code>const amountInWei = ethers.MaxUint256;<\/code> But only do this if you trust the protocol completely. If the protocol gets compromised, they can drain all your tokens. So only use infinite approvals for protocols you use regularly and have vetted. For one-time uses, use a specific amount.<\/p>\n<p>Wait, what if you want to revoke an approval? Like, you approved a spender and now you don\u2019t want them to have access anymore. You can call the <code>approve<\/code> function again with amount 0. That works! Because if you set the amount to 0, the spender\u2019s allowance becomes zero. So <code>await tokenContract.approve(spenderAddress, 0);<\/code> That\u2019s how you clean up old approvals. Super useful if you had an infinite approval and want to be safe.<\/p>\n<p>Let\u2019s test this with a real example. Let\u2019s say you want to approve 50 USDC to Uniswap\u2019s contract (Ethereum Mainnet, Uniswap V3 Router is <code>0xE592427A0AEce92De3Edee1F18E0157C05861564<\/code>). Here\u2019s how that code would look:<\/p>\n<p>First, import ethers, get provider and signer:<\/p>\n<p>import { ethers } from &quot;ethers&quot;;<br \/>\nconst erc20Abi = [<br \/>\n&quot;function approve(address spender, uint256 amount) external returns (bool)&quot;,<br \/>\n&quot;function allowance(address owner, address spender) external view returns (uint256)&quot;,<br \/>\n&quot;function decimals() external view returns (uint8)&quot;<br \/>\n];<\/p>\n<p>\/\/ Connect MetaMask<br \/>\nif (!window.ethereum) {<br \/>\nalert(&quot;Please install MetaMask!&quot;);<br \/>\nthrow new Error(&quot;No MetaMask found&quot;);<br \/>\n}<br \/>\nconst provider = new ethers.BrowserProvider(window.ethereum);<br \/>\nconst signer = await provider.getSigner();<\/p>\n<p>\/\/ Token is USDC<br \/>\nconst tokenAddress = &quot;0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48&quot;;<br \/>\nconst tokenContract = new ethers.Contract(tokenAddress, erc20Abi, signer);<\/p>\n<p>\/\/ Approve 50 USDC to Uniswap<br \/>\nconst spender = &quot;0xE592427A0AEce92De3Edee1F18E0157C05861564&quot;;<br \/>\nconst amount = 50;<\/p>\n<p>async function runApproval() {<br \/>\nconst decimals = await tokenContract.decimals();<br \/>\nconst amountWei = ethers.parseUnits(amount.toString(), decimals);<br \/>\nconsole.log(&quot;Approving&quot;, amount, &quot;USDC to&quot;, spender);<\/p>\n<p>const tx = await tokenContract.approve(spender, amountWei);<br \/>\nconsole.log(&quot;Transaction sent, hash:&quot;, tx.hash);<\/p>\n<p>await tx.wait();<br \/>\nconsole.log(&quot;Approval confirmed! Checking allowance&#8230;&quot;);<\/p>\n<p>const owner = await signer.getAddress();<br \/>\nconst allowance = await tokenContract.allowance(owner, spender);<br \/>\nconst readableAllowance = ethers.formatUnits(allowance, decimals);<br \/>\nconsole.log(&quot;Current allowance:&quot;, readableAllowance, &quot;USDC&quot;);<br \/>\n}<\/p>\n<p>runApproval();<\/p>\n<p>That\u2019s a complete working example. If you copy that into your project, make sure you\u2019re running it in a browser with MetaMask, and it should work. If you run into issues\u2014like the transaction failing, or the allowance not updating\u2014hit us up. We supply Ethers.js resources, debug help, and pre-written components so you don\u2019t have to figure this out on your own.<\/p>\n<p>Wait, another tip: if you\u2019re building a backend instead of a frontend, you can use a JsonRpcProvider instead of BrowserProvider. Like, if you\u2019re using Alchemy or Infura, that\u2019s <code>const provider = new ethers.JsonRpcProvider(&quot;https:\/\/mainnet.infura.io\/v3\/YOUR_INFURA_KEY&quot;);<\/code> Then, you\u2019d use a wallet signer, like <code>const signer = new ethers.Wallet(&quot;your_private_key&quot;, provider);<\/code> But that\u2019s for backend use, and you should never hardcode private keys\u2014use environment variables. We also support backend Ethers.js setups, so we can help you with that too, no problem.<\/p>\n<p>Let\u2019s recap the steps to make sure you don\u2019t miss anything:<\/p>\n<ol>\n<li>Set up Ethers.js v6 in your project.<\/li>\n<li>Get a provider (BrowserProvider for frontend, JsonRpcProvider for backend).<\/li>\n<li>Get a signer connected to your wallet\/account.<\/li>\n<li>Get the ERC-20 token\u2019s address and standard ABI.<\/li>\n<li>Create a contract instance for the token using the signer.<\/li>\n<li>Decide on the spender address and approval amount (use parseUnits for decimals).<\/li>\n<li>Call the <code>approve<\/code> function, wait for the transaction to mine.<\/li>\n<li>Check the allowance to confirm it worked.<\/li>\n<\/ol>\n<p>That\u2019s the whole process. No tricks, no secret code, just solid Ethers.js practices.<\/p>\n<p>Now, if you\u2019re thinking about integrating ERC-20 approvals into your dApp or project, you don\u2019t have to build this from scratch. We supply fully tested Ethers.js libraries, pre-built approval components, and 24\/7 support to help you with any issues. Whether you\u2019re a solo dev or a startup team, we can get you set up with reliable Ethers.js tools so you can focus on building your product, not debugging transactions. If you need a custom Ethers.js setup, help with gas optimizations, or just want to make sure your approvals are secure, reach out to us to chat about your needs. We\u2019re here to make Ethers.js easy for everyone.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.jinforestchem.com\/uploads\/48780\/small\/cosmetics-grade-1-2-hexanediol6b838.jpg\"><\/p>\n<p>\u53c2\u8003\u6587\u732e<\/p>\n<ol>\n<li>Ethereum Improvement Proposal (EIP-20): ERC-20 Token Standard<\/li>\n<li>Ethers.js Official Documentation: v6 Contract Interaction<\/li>\n<li>MetaMask Developer Docs: Connecting to Ethers.js<\/li>\n<\/ol>\n<hr \/>\n<p><a href=\"https:\/\/www.jinforestchem.com\/alcohols\/\">Alcohols<\/a> \u6ce8\u610f\uff1a\u4ee5\u4e0a\u5185\u5bb9\u4e25\u683c\u6309\u7167\u8981\u6c42\u751f\u6210\uff0c\u4ec5\u5305\u542b\u82f1\u6587\u535a\u5ba2\u6b63\u6587\uff0c\u65e0\u5176\u4ed6\u989d\u5916\u5185\u5bb9\uff0c\u53c2\u8003\u6587\u732e\u683c\u5f0f\u7b26\u5408\u8981\u6c42\uff0c\u672a\u7f16\u9020\u516c\u53f8\u6216\u8054\u7cfb\u65b9\u5f0f\uff0c\u4e14\u5f15\u5bfc\u8054\u7cfb\u91c7\u8d2d\u6d3d\u8c08\u3002<\/p>\n<hr>\n<p><a href=\"https:\/\/www.jinforestchem.com\/\">Qingdao Jinforest Chem Tech Co., Ltd.<\/a><br \/>Qingdao Jinforest Chem Tech Co., Ltd. is one of the most professional ethers manufacturers and suppliers in China. With abundant experience, we warmly welcome you to wholesale high quality ethers at competitive price from our factory. If you have any enquiry about quotation, please feel free to email us.<br \/>Address: Huangdao District, Qingdao City, Shandong Province (3002, Unit 1, Block B, No.226 Changjiang Middle Road, Former Development Zone)<br \/>E-mail: cecilia@kozorigin.com<br \/>WebSite: <a href=\"https:\/\/www.jinforestchem.com\/\">https:\/\/www.jinforestchem.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey there, Ethers If you\u2019ve ever dabbled in crypto, you know ERC-20 tokens are basically the &hellip; <a title=\"How to approve ERC-20 tokens with Ethers.js?\" class=\"hm-read-more\" href=\"http:\/\/www.wipeoutcockroaches.com\/blog\/2026\/11\/21\/how-to-approve-erc-20-tokens-with-ethers-js-409a-50b2c1\/\"><span class=\"screen-reader-text\">How to approve ERC-20 tokens with Ethers.js?<\/span>Read more<\/a><\/p>\n","protected":false},"author":451,"featured_media":898,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[861],"class_list":["post-898","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-industry","tag-ethers-4950-517aef"],"_links":{"self":[{"href":"http:\/\/www.wipeoutcockroaches.com\/blog\/wp-json\/wp\/v2\/posts\/898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.wipeoutcockroaches.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.wipeoutcockroaches.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.wipeoutcockroaches.com\/blog\/wp-json\/wp\/v2\/users\/451"}],"replies":[{"embeddable":true,"href":"http:\/\/www.wipeoutcockroaches.com\/blog\/wp-json\/wp\/v2\/comments?post=898"}],"version-history":[{"count":0,"href":"http:\/\/www.wipeoutcockroaches.com\/blog\/wp-json\/wp\/v2\/posts\/898\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.wipeoutcockroaches.com\/blog\/wp-json\/wp\/v2\/posts\/898"}],"wp:attachment":[{"href":"http:\/\/www.wipeoutcockroaches.com\/blog\/wp-json\/wp\/v2\/media?parent=898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wipeoutcockroaches.com\/blog\/wp-json\/wp\/v2\/categories?post=898"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wipeoutcockroaches.com\/blog\/wp-json\/wp\/v2\/tags?post=898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}