Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

567
Views
How to Approve spend of one Token with Web3.js?

I seek via my web page of test to create a button which allows to authorize the expenditure of a Contract (Token).. If I go to the page and I click I would like the script to load web3 (it loads well) then if I press the button that Metamask authorizes the spending of the contract.

Metamask opens fine and does request the connection for my test site on the BSC in Web3js. However I can't find the exact code for the approve function.

Here is the code:

<head>
<script src='https://cdnjs.cloudflare.com/ajax/libs/web3/1.7.0/web3.min.js'></script>
</head>
 
<button onclick="approvebutton();">Approve button to authorize tokens to be spent</button>

<script type="text/javascript">
if (typeof window.ethereum !== 'undefined') {
    ethereum.request({ method: 'eth_requestAccounts' });
} else {
    alert('Please install metamask')
}

var Web3 = require('web3');
const web3 = new Web3('https://bsc-dataseed1.binance.org:443');

 async function approvebutton() {

 /// APPROVE FUNCTION WITH THE CONTRACT 
 
 }
        
        
  </script>

I tried this approach but it doesn't work (metamask confirmation won't show up):

if (typeof window.ethereum !== 'undefined') {
    ethereum.request({ method: 'eth_requestAccounts' });
} else {
    alert('Please install metamask')
}

var Web3 = require('web3');
const web3 = new Web3('https://bsc-dataseed1.binance.org:443');
const Contract = ('0xContractAddress');
const spenderAdr = ('0xSpenderAddress');
const amount = ('AmountTokensNumber')


async function approvebutton(Contract,spenderAdr){
  Contract.methods.approve(spenderAddr, amount).send({
   from: ownerAddr
})
}

Metamask won't show up to confirm the TX.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

First of all, the approve method takes 2 parameters, the spender and the amount so it will be something like this:

Contract.methods.approve(spenderAddr, amount).send({
   from: ownerAddr
})

The gas parameter is optional.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!