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

484
Visualizações
ethers.js swapExactTokensForTokens error transferFrom failed

I have a problem about pancakeswap ethers.js code on BSC testnet. My tx always failed with error "Fail with error 'TransferHelper::transferFrom: transferFrom failed'" My Balance are 0.4317 BNB, 20 BUSD and 0.074 ETH

Can someone helpme?, I'm strucking for 2 days

const ethers = require('ethers');
const addresses = {
    WBNB: '0xae13d989dac2f0debff460ac112a837c89baa7cd', //Testnet
    BUSD: '0x78867BbEeF44f2326bF8DDd1941a4439382EF2A7', //Testnet
    router: '0x9Ac64Cc6e4415144C455BD8E4837Fea55603e5c3',
    recipient: '{Censored}'
}
const privateKey = '{Censored}';
const provider = new ethers.providers.JsonRpcProvider('https://data-seed-prebsc-1-s1.binance.org:8545/'); // TESTNET
const wallet = new ethers.Wallet(privateKey, provider);
const account = wallet.connect(provider);
const router = new ethers.Contract(
    addresses.router,
    [
        'function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)',
        'function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)',
        'function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) external payable returns (uint[] memory amounts)',
        'function swapExactETHForTokensSupportingFeeOnTransferTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) external payable'
    ],
    account
);

const wbnb = new ethers.Contract(
    addresses.WBNB,
    [
        'function approve(address spender, uint256 amount) external returns (bool)',
    ],
    account
);
const WBNBAmountToPay = ethers.utils.parseUnits('1');
const init = async () => {
    const amountIn = ethers.utils.parseUnits('0.001', 'ether');
    const gas = {
        gasPrice: ethers.utils.parseUnits('50', 'gwei'),
        gasLimit: '2000000'
    };
    const tx = await wbnb.approve(addresses.router, WBNBAmountToPay.toString(), gas);
    const receipt = await tx.wait(); 
    console.log('Transaction receipt');
    console.log(receipt);
    const amounts = await router.getAmountsOut(amountIn, [addresses.WBNB, addresses.BUSD]);
    const amountOutMin = amounts[1].sub(amounts[1].div(10));
    console.log('Swapping', 
        ethers.utils.formatUnits(amountIn), 'WBNB for ', 
        ethers.utils.formatUnits(amountOutMin), 'BUSD'
    );
    const tx2 = await router.swapExactTokensForTokens(
        amountIn,
        amountOutMin,
        [addresses.WBNB, addresses.BUSD],
        addresses.recipient,
        Date.now() + 1000 * 60 * 10,
        gas
    );
        const receipt2 = await tx2.wait(); 
        console.log('Transaction receipt');
        console.log(receipt2);  
}
init();

Sample error tx here "https://testnet.bscscan.com/tx/0xd17e13cec5778226414b191a5f274e7ba4f59c8f683979eb9c1aa17077aead2f"

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

if you are using swapExactTokensForTokens you need to approve the router contract before call the function. But from the code above you can use swapExactETHForTokensSupportingFeeOnTransferTokens instead

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