0

I'm trying to create a script that makes a swap transaction and then print out the transaction hash of that submitted transaction, but I keep getting back "undefined" even though I'm expecting something like "0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx". The trx does go through the network just fine. can someone explain what i must do to get the trx hash? thanks in advance!

const ethers = require("ethers");    
const url = 'ws://172.33.x.x:8546';
const router_abi = require('./uniswapABI.json');
var uniswap_address = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D'.toLowerCase()
const weth_contract_address = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'.toLowerCase();
const contract_address = '0xE7a7De362179108E26E464a39B204a9561B352cf'.toLowerCase();

var accounts = 'xxxxxxxxxxxxxx';
var pks = 'xxxxxxxxxxxxx';

const input = {
    GAS_LIMIT: '428000',
    ETH_AMOUNT: '.01', 
    AMOUNT_OUT: '0'
};

async function Buy_fx11() {
    
    const connection = new ethers.providers.WebSocketProvider(url);
    const pk_fixed = Buffer.from(pks, 'hex');
    const wallet = new ethers.Wallet(pk_fixed, connection);
    const signer = wallet.connect(connection)
    const count = await connection.getTransactionCount(wallet.address);

    const router = new ethers.Contract(uniswap_address, router_abi, signer);

    const tx = await router.swapExactETHForTokensSupportingFeeOnTransferTokens(
        input.AMOUNT_OUT,
        [weth_contract_address,contract_address],
        wallet.address,
        Date.now() + 90000,
        {
            value: ethers.utils.parseEther(input.ETH_AMOUNT),
            gasLimit: input.GAS_LIMIT,
            maxPriorityFeePerGas: ethers.utils.parseUnits("20", "gwei"),
            maxFeePerGas: ethers.utils.parseUnits("100", "gwei"),
            }
        );

    const finaltx = await signer.signTransaction(tx);  
    console.log(finaltx.hash);
}

undefined trx hash

1 Answer 1

1

you already create router with signer, so calling router.swapExactETHForTokensSupportingFeeOnTransferTokens make it sign and send transaction.

try to use tx.hash for get transaction_hash and tx.wait() for make sure transaction was mined.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.