Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delay Transaction 3 to 5 days #202

Open
vinooMj opened this issue Jul 27, 2020 · 0 comments
Open

Delay Transaction 3 to 5 days #202

vinooMj opened this issue Jul 27, 2020 · 0 comments

Comments

@vinooMj
Copy link

vinooMj commented Jul 27, 2020

Our Ethereum project, We are using free infura key(mainnet - https) for the provider. but the transaction takes 3 to 5 days. We send transaction less than five only per day, We have more balance in the account. Is any problem with the Free infura account?
We share our code here? Please help us

`const { web3, Tx } = require("../includes/web3")

async function sendSignedTransactionsForContracts(data, senderAddress, senderPrivateKey) {
let txObject = {};
let nonce = null;
let gasPrice = null;
return new Promise(async (resolve, reject) => {
await Promise.all([web3.eth.getTransactionCount(senderAddress), web3.eth.getGasPrice()])
.then(result => {
nonce = result[0];
gasPrice = result[1];
gasPrice = (parseInt(gasPrice) + parseInt(gasPrice / 2)).toString();
console.log(gasPrice);
txObject = {
from: senderAddress,
nonce: web3.utils.toHex(nonce),
data: data
};
return web3.eth.estimateGas(txObject);
})
.then(estimateGas => {
txObject.gasPrice = web3.utils.toHex(gasPrice);
txObject.gas = web3.utils.toHex(estimateGas);
const tx = new Tx.Transaction(txObject, {'chain':'ropsten'});
const privateKey = Buffer.from(senderPrivateKey, 'hex');
tx.sign(privateKey);
const serializedTx = tx.serialize();
const raw = '0x' + serializedTx.toString('hex');
web3.eth.sendSignedTransaction(raw, async (err, txHash) => {
if (err) {
reject(err)
return
}
console.log('err:', err, 'txHash:', txHash)
let getTransaction = setInterval(async () => {
web3.eth.getTransactionReceipt(txHash, async function (error, result) {
if (error) {
clearInterval(getTransaction)
console.log({ 'errrr': error })
reject(error)
}

                        if (result) {
                            clearInterval(getTransaction)
                            resolve(result.contractAddress)
                        }
                    });
                }, 600)
            })
        })
})

}

async function sendSignedTransactionsForMethods(data, contract_address, senderAddress, senderPrivateKey) {

return new Promise(async (resolve, reject) => {
    Promise.all([web3.eth.getTransactionCount(senderAddress), web3.eth.getGasPrice()])
        .then(async result => {
            nonce = result[0];
            const tx = {
                nonce: web3.utils.toHex(nonce),

                from: senderAddress,
                to: contract_address,
                gas: result[1],
                data: data,
		        chainId : 3,
                chain: 'ropsten',
                hardfork: 'petersburg'
            };

	let gasLimit = await web3.eth.estimateGas(tx);
	tx.gasPrice = web3.utils.toHex(result[1]);
	tx.gas = web3.utils.toHex(gasLimit);

            web3.eth.accounts.signTransaction(tx, senderPrivateKey).then(signed => {
                const tran = web3.eth
                    .sendSignedTransaction(signed.rawTransaction, async (err, txHash) => {
			console.log(err);
                        if (err) {
                            reject(err)
                            return
                        }
                        console.log('err:', err, 'txHash:', txHash)
                        // Use this txHash to find the contract on Etherscan!
                         let getTransaction = setInterval(async () => {
                             await web3.eth.getTransactionReceipt(txHash, async function (error, result) {
                                 if (error) {
                                     clearInterval(getTransaction)
                                     console.log({ 'errrr': error })
                                     reject(new Error(error))
                                 }
                                 resolve(txHash)

                             });
                         }, 600)

                    });
            });
        })
})

}
module.exports = { sendSignedTransactionsForContracts, sendSignedTransactionsForMethods }`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant