简体   繁体   中英

Why is my Uniswap v3 contract failing transactions

I am creating a trading bot using web3 and wanted to buy a token address instantly after script received token address:

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'])

But here the results: https://etherscan.io/tx/0xc744cd951ca48489e7d89eb418cafea29ba5e08bea615fdd31f8e28b4059f423 https://etherscan.io/tx/0xc88c8b76bf5bc2d0f6fd8c291437f0d482a5585ef7796ced3a35dd3f2f09b80b https://etherscan.io/tx/0xc5c59e23d047a8b6a6fe7fb25bc9abd22c1fbc9bec794f61e9ed7defedc0cbbf

routerContract.methods.swapExactTokensForTokens

This method is no longer used in uniswap v3. It was used in uniswap v2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM