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

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

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 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