使用web3js调用PancakeSwap V3 exactInputSingle交易失败求助
PancakeSwap V3 Router 代币兑换交易回滚问题
我尝试用web3js调用PancakeSwapRouterV3(合约地址:0x13f4EA83D0bd40E75C8222255bc855a974568Dd4)的exactInputSingle方法进行代币兑换。V2版本中我可以通过swapExactTokensForETH实现,但该方法在V3合约中已不存在。我想在SmartChain上将原生代币兑换为其他代币(例如BUSD),但交易持续回滚。目前我尝试了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); } }
内容的提问来源于stack exchange,提问作者Szedou
相关产品推荐
相关产品推荐

