Pancake swap v3 exactInputSingle 无故失败

问题描述 投票:0回答:1

我试图使用方法exactInputSingle在PancakeSwapRouterV3(0x13f4EA83D0bd40E75C8222255bc855a974568Dd4)上与web3js进行交换。在 v2 版本中,我可以使用 swapExactTokensForETH 方法来执行此操作,但不再在新合约中。

我想交换 SmartChain 中的本机代币并将其交换为其他代币(例如busd),但我的交易不断被恢复。

想要通过 web3js 购买/出售一些代币。

例如我尝试进行 BUSD-USDT 互换。这是我的代码:

1.BUSD 批准合同支出

exports.approve_busd = async function (pancakeSwapContractAddress) { let BUSDContractAddress = process.env.BUSD_CONTRACT_ADDRESS; let BUSDContractFile = process.env.BUSD_CONTRACT_FILE; let BUSDAbi = JSON.parse(fs.readFileSync(BUSDContractFile)); let BUSDContract = new web3.eth.Contract(BUSDAbi, BUSDContractAddress);

try {
    let fromAddress = process.env.USER_ADDRESS;

    let gasPrice = await web3.eth.getGasPrice();
    let gas = 320000;
    let private_key = process.env.USER_PK;

    let count = await web3.eth.getTransactionCount(fromAddress);
    let txHash = null;
    try {
        let rawTxApproveBUSD = {
            nonce: web3.utils.toHex(count),
            gasPrice: web3.utils.toHex(gasPrice),
            from: fromAddress,
            to: BUSDContractAddress,
            value: '0x00',
            gas: gas,
            data: BUSDContract.methods.approve(
                pancakeSwapContractAddress,
                BigInt(10000000000000000000000000),
            ).encodeABI(),
        };

        let signedTransactionApprove = await web3.eth.accounts.signTransaction(rawTxApproveBUSD, private_key, function () {
                log.info(`Transaction signed from ${fromAddress} to ${BUSDContractAddress} for approve;`);
            }
        );

        await web3.eth.sendSignedTransaction(signedTransactionApprove.rawTransaction, async function (error, hash) {
            txHash = hash;
        });
    } catch (e) {
        log.error('Error while transfer eth: ', e);
        return;
    }
    log.info(`Transaction sent from ${fromAddress} to ${BUSDContractAddress} for approve`);
    console.log("Transaction hash: " + txHash);

    console.log(`\n \n ***** END *****`);
} catch (e) {
    log.error('Error while transfer eth: ', e);
}
}

2.我的交易发送方式:

exports.buy_tokens_by_busd_v3 = async function (pancakeSwapContractAddress, pancakeSwapContractFile) { await this.approve_busd(pancakeSwapContractAddress)

const BUSDContractAddress = process.env.BUSD_CONTRACT_ADDRESS;
const USDTContractAddress = process.env.USDT_CONTRACT_ADDRESS;
const BUSDContractFile = process.env.BUSD_CONTRACT_FILE;
const fromAddress = process.env.USER_ADDRESS;

try {
    const BUSDAbi = JSON.parse(fs.readFileSync(BUSDContractFile));
    const BUSDContract = new web3.eth.Contract(BUSDAbi, BUSDContractAddress);

    const pancakeSwapAbi = JSON.parse(fs.readFileSync(pancakeSwapContractFile));
    const pancakeSwapContract = new web3.eth.Contract(pancakeSwapAbi, pancakeSwapContractAddress);
    const BUSDBalance = await BUSDContract.methods.balanceOf(fromAddress).call();

    const count = await web3.eth.getTransactionCount(fromAddress);
    const deadline = (await web3.eth.getBlock("latest")).timestamp + 10000;

    console.log("BUSD BALANCE");
    console.log(BUSDBalance);

    console.log('deadline');
    console.log(deadline);

    console.log('actual timestamp');
    console.log((await web3.eth.getBlock("latest")).timestamp);

    let gasPrice = await web3.eth.getGasPrice();

    let gas = 2100000;
    const valueToSend = web3.utils.toWei("0.1", 'ether');
    const params = {
        tokenIn: BUSDContractAddress,
        tokenOut: USDTContractAddress,
        fee: 3000,
        recipient: fromAddress,
        deadline: deadline,
        amountIn: web3.utils.toHex(valueToSend),
        amountOutMinimum: 0,
        sqrtPriceLimitX96: 0,
    }

    const tx = pancakeSwapContract.methods.exactInputSingle(params);
    // const gas = await tx.estimateGas({from: fromAddress});

    console.log('gas');
    console.log(gas);

    console.log('value to send: ');
    console.log(valueToSend);

    const txArgs = {
        nonce: web3.utils.toHex(count),
        gasPrice: web3.utils.toHex(gasPrice),
        from: fromAddress,
        to: pancakeSwapContractAddress,
        value: '0x00',
        gas: gas,
        data: tx.encodeABI()
    };

    let private_key = process.env.USER_PK;
    let txHash = null;
    try {
        let signedTransaction = await web3.eth.accounts.signTransaction(txArgs, private_key, function () {
                log.info(`Transaction signed from ${fromAddress} to ${pancakeSwapContractAddress} on ${valueToSend} WEI`);
            }
        );

        await web3.eth.sendSignedTransaction(signedTransaction.rawTransaction, async function (error, hash) {
            console.log(error);
            txHash = hash;
        });
    } catch (e) {
        console.log(e);
        log.error('Error while transfer: ', e);
        console.log(` Balance: ${balance}, ValueToSend: ${valueToSend}. Gas price ${gasPrice}, Gas Value ${gasValue}`);
        return;
    }

    log.info(`Transaction sent from ${fromAddress} to ${pancakeSwapContractAddress} on ${valueToSend} WEI`);
    console.log("Transaction hash: " + txHash);
    console.log(`\n \n ***** END *****`);
} catch (e) {
    log.error('Error occurred: ', e);
}
}
web3js pancakeswap
1个回答
0
投票

如果交易到达网络并且费率计算良好,您需要调试 tx,为此我推荐您使用像enderly这样的工具

© www.soinside.com 2019 - 2024. All rights reserved.