Web3 connection to Optimism chain not stable

Using Optimism mainnet rpc url, we are facing web3 connection issue. For example the below methods maximum times fail with error - “Couldn’t connect to node https://optimism-mainnet.infura.io/v3/my-api-key

web3.eth.getBlockNumber();
web3.eth.estimateGas(rawTx);

And couple of times it gave us the expected outputs, no errors.

1 Like

Hi @shubhangi could you please share with me a repository so I can try to replicate the above-mentioned issue?

1 Like

Hi @Flaveeu, thanks for your response. Any help will be appreciated.

You can use the below repo to setup.

&

refer to below code to replicate the error.

const Vault = require('../safle-vault/src/lib/vault')
const { KeyringController, getBalance } = require('./vault-optimism-controller/src/index')
const CryptoJS = require('crypto-js');
var Web3 = require('web3');
const { bufferToHex } = require('ethereumjs-util')


// SET 1 generated 
const encryptionKey = [48, 0, 236, 187, 187, 172, 177, 90, 255, 184, 9, 116, 142, 96, 197, 158, 87, 35, 26, 101, 187, 30, 116, 138, 50, 131, 166, 50, 51, 197, 198, 83, 238, 167, 105, 178, 182, 108, 174, 199, 124, 141, 155, 73, 21, 85, 81, 109, 78, 233, 152, 108, 242, 238, 192, 31, 147, 86, 174, 195, 55, 229, 4, 36]
const pin = 246802;
const mnemonic = 'example install inhale chronic quick kangaroo tonight awful vast resist best very';
const address = "0x1b14123e061f1ea1c2a12d42183969a42d179a52"
privateKey = 'c29943bcdd71ad6c5976503c673d98381ed9a340a37a33b4100fe2aef7a5b4cf';

async function optimismtest(){
    const keyringController = new KeyringController({
        encryptor: {
            encrypt(pass, object) {
                const ciphertext = CryptoJS.AES.encrypt(JSON.stringify(object), pass).toString();

                return ciphertext;
            },
            decrypt(pass, encryptedString) {
                const bytes = CryptoJS.AES.decrypt(encryptedString, pass);
                const decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));

                return decryptedData;
            },
        },
        });

    
    const vault = await keyringController.createNewVaultAndRestore(JSON.stringify(encryptionKey), mnemonic)
    console.log(vault)

    const rpcUrl = 'https://optimism-mainnet.infura.io/v3/my-api-key';  //  API KEY
    const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl));


    //sign()    get r, s, v
    // const bN = await web3.eth.getGasPrice() //.......Couldn't connect to node

    const rawTx1 = {
        from: address,
        gasPrice: "20000000000",
        // gas: "21000",
        to: '0xacde0f575d8caf7bdba417326797c1a1d1b21f88',
        value: "1000000000000000000",
        data: ""
    };

    const rawTx2 = {
        type: 2,
        nonce: 0,
        to: "0xacde0f575d8caf7bdba417326797c1a1d1b21f88", // Address to send to
        maxPriorityFeePerGas: 10000, // Recommended maxPriorityFeePerGas
        maxFeePerGas: 10000, // Recommended maxFeePerGas
        value: web3.utils.toHex(1000000), // .01 ETH
        gasLimit: "21000", // basic transaction costs exactly 21000
        chainId: 10, 
      };
    

    // const block = await web3.eth.getBlockNumber();
    // console.log(block)
    // estimatedGas = await web3.eth.estimateGas(rawTx);
    
    const signedTx2 = await keyringController.sign(rawTx1, privateKey, web3)    //.......Couldn't connect to node
    console.log(signedTx2)

    }

optimismtest();
1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.