INFURA / infura

Official Public Repository for INFURA
https://infura.io
381 stars 62 forks source link

Delay Transaction 3 to 5 days #202

Open vinooMj opened 4 years ago

vinooMj commented 4 years ago

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 }`