为什么我的Uniswap v3合同交易失败



我正在使用web3创建一个交易机器人,并希望在脚本收到代币地址后立即购买代币地址:

var originalAmountToBuyWith = '0.001';
var bnbAmount = web3.utils.toWei(originalAmountToBuyWith, 'ether');
var amountToBuyWith = web3.utils.toHex(bnbAmount);

var privateKey = Buffer.from("PRIVATEKEY", 'hex');
var walletAddress = "0x28D2c723018c7c1C064Bd122e18290763b448287";
var wBNBAddress = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'; // WETH token address
var amountOutMin = '0';
var gasLimit = 400000;

var routerAbi = JSON.parse(fs.readFileSync('router-abi.json', 'utf-8'));
var routerAddress = '0x68b3465833fb72a70ecdf485e0e4c7bd8665fc45'
var routerContract = new web3.eth.Contract(routerAbi, routerAddress, {from: walletAddress});
var count = 0
async function buyOnlyOne(tokenAddress, path) 
{
var swapData = routerContract.methods.swapExactTokensForTokens
(
amountToBuyWith,
web3.utils.toHex(0),
path, 
walletAddress
);


await fetchTransactionCount() //GETS NONCE
var gasPrice = await getGasPrice()
var rawTransaction = 
{
"maxPriorityFeePerGas": web3.utils.toHex( web3.utils.toWei( '4' , 'gwei' ) ),
"maxFeePerGas": web3.utils.toHex( web3.utils.toWei( '50' , 'gwei' )),
"gasPrice":web3.utils.toHex(gasPrice),
"gasLimit":web3.utils.toHex(gasLimit),
"to":routerAddress,
"value":0,
"data":swapData.encodeABI(),
"nonce":web3.utils.toHex(count),
"type" : "0x02",
"chainId" : "0x01",
};
var transaction = Tx.FeeMarketEIP1559Transaction.fromTxData(rawTransaction, {hardfork});
console.log("Signing tx : " + new Date().toISOString())
const signedTransaction = transaction.sign( privateKey );
const serializedTransaction = '0x' + signedTransaction.serialize().toString( 'hex' );

try
{
console.log("Buying token : " + new Date().toISOString())
const txHash = await web3.utils.sha3( serializedTransaction );
console.log( "Tx Hash: " + txHash );
var result = await web3.eth.sendSignedTransaction(serializedTransaction );
console.log(result)

console.log("Waiting for " + waitAfterBuy + "seconds after buy")

return result;
}
catch(error)
{
if(error.message.includes('"status": false,'))
{
console.log("Error : " + error.message);
}
else
{
console.log("Error : " + error.message);
}
return null;
}

}
buyOnlyOne('0x84ba04876a9460c780f5795a5594a012f1eeef7c', [wBNBAddress, '0x84ba04876a9460c780f5795a5594a012f1eeef7c'])

但结果如下:https://etherscan.io/tx/0xc744cd951ca48489e7d89eb418cafea29ba5e08bea615fdd31f8e28b4059f423https://etherscan.io/tx/0xc88c8b76bf5bc2d0f6fd8c291437f0d482a5585ef7796ced3a35dd3f2f09b80bhttps://etherscan.io/tx/0xc5c59e23d047a8b6a6fe7fb25bc9abd22c1fbc9bec794f61e9ed7defedc0cbbf

routerContract.methods.swapExactTokensForTokens

uniswap v3中不再使用此方法。它已在uniswap v2中使用。

最新更新