Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

574
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda