Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

568
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda