0

I'm playing Ethernaut level 1, and I created a truffle script. But the transaction is not mined. So I increased the gasPrice to gasPrice * 5, but it still times out in 750 seconds.

Does anyone know what's wrong? The program first prints txHash: XXXX, and times out after a long time:

require('dotenv').config()
let privateKey = process.env["RINKEBY_PRIVATE_KEY"]
let account = web3.eth.accounts.privateKeyToAccount(privateKey)

async function main () {
    let level1 = new web3.eth.Contract(
        artifacts.require("Level1").abi,
        "0x7717554cE81f6255D223e64f6cA9ABF4c131e4cf"
    );
    console.log('contract object created')

    await level1.methods.contribute().send({
        from:     account.address,
        value:    web3.utils.toWei("0.0001", "ether"),
        gas:      10000000,
        gasPrice: await web3.eth.getGasPrice() * 5,
        gasLimit: 5000000
    }).on('transactionHash', (txHash) => { 
        console.log("txHash: "+ txHash)
    }).on('error', (error) => { 
        console.log("error: " + error)
    })

    console.log('contribute() done')

    await level1.send(web3.utils.toWei("0.0001", "ether")).on('receipt', console.log);
    console.log('send() done')

    await level1.methods.withdraw().send({
        from:     account.address,
        value:    web3.utils.toWei("0.0001", "ether"),
        gasPrice: await web3.eth.getGasPrice() * 5,
        gasLimit: 50000
    }).on('transactionHash', (txHash) => { 
        console.log("txHash: "+ txHash)
    }).on('error', (error) => { 
        console.log("error: " + error)
    })
    console.log('withdraw() done')

    let balance = await web3.eth.getBalance(account)
    console.log(balance.toString())
}

main()
.then(() => {
    
})
.catch((err) => {
    console.log(err)
})

module.exports = () => {}
TylerH
  • 20,799
  • 66
  • 75
  • 101
daisy
  • 22,498
  • 29
  • 129
  • 265
  • Is the tx hash visible on Rinkeby (error or not)? Could you post a sample tx hash? – Mohamed Sohail Jul 12 '22 at 08:59
  • @MohamedSohail Just created one: 0x6c8146c4a425d4090680f71dff62db718beed6d6df8758320237fa30d393e512 – daisy Jul 12 '22 at 13:08
  • @MohamedSohail https://dashboard.tenderly.co/tx/rinkeby/0x6c8146c4a425d4090680f71dff62db718beed6d6df8758320237fa30d393e512 – daisy Jul 12 '22 at 13:10

1 Answers1

0

I've reduced the gas price and it worked, but I don't know why:

require('dotenv').config()
let privateKey = process.env["RINKEBY_PRIVATE_KEY"]
let account = web3.eth.accounts.privateKeyToAccount(privateKey)

async function main () {
    let level1 = new web3.eth.Contract(
        artifacts.require("Level1").abi,
        "0x792E9c766436Aa7CA99Fd1eBDdCFf0480243Fd1c"
    );
    console.log('contract created')

    await level1.methods.contribute().send({
        from:     account.address,
        value:    web3.utils.toWei("0.0009", "ether"),
        gasPrice: await web3.eth.getGasPrice(),
        gasLimit: 1000000
    }).on('transactionHash', (txHash) => { 
        console.log("txHash: "+ txHash)
    })

    console.log('contribute() done')

    await web3.eth.sendTransaction({
        from:  account.address,
        to:    level1._address, 
        value: web3.utils.toWei("0.0009", "ether")
    }).on('error', (error) => { 
        console.log("error: " + error)
    })

    console.log('sendTransaction() done')

    await level1.methods.withdraw().send({
        from:     account.address,
        gasPrice: await web3.eth.getGasPrice(),
        gasLimit: 1000000
    }).on('transactionHash', (txHash) => { 
        console.log("txHash: "+ txHash)
    })

    console.log('withdraw() done')

    let balance = await web3.eth.getBalance(level1._address)
    console.log(balance.toString())
}

main()
.then(() => {
    
})
.catch((err) => {
    console.log(err)
})

module.exports = () => {}
daisy
  • 22,498
  • 29
  • 129
  • 265