ethers-provider-flashbots-bundle icon indicating copy to clipboard operation
ethers-provider-flashbots-bundle copied to clipboard

ChainID gets erroneously overridden

Open forza-panino opened this issue 3 years ago • 6 comments

BLOCKCHAIN: Binance Smart Chain - Testnet PROVIDER: Moralis speedy nodes

I am unable to send transaction since it seems that the library overrides erroneously the chainId of the transaction. Here's the relevant parts of the code:

this.ethers_provider = new providers.WebSocketProvider(this.WEBSOCKET_PROVIDER)
this.signer = new Wallet("0x" + this.private_keys[0], this.ethers_provider);
this.flashbotsProvider = await FlashbotsBundleProvider.create(
    this.ethers_provider,
    this.signer
);
const block_number = await this.ethers_provider.getBlockNumber()
const BLOCKS_IN_THE_FUTURE = 3
const populatedTransaction = await this.signer.populateTransaction(
    {
        gasLimit: "0x7A120",
        data: '0x',
        to: "0x51d522dfb50056ab41a1f6c248077ba85a2b97d5",
        value: "0x1",
        gasPrice: this.web3.utils.toHex(this.web3.utils.toWei("30", 'gwei')),
    }
);
//populatedTransaction.chainId = await this.signer.getChainId(); //Even with it I still have the same error
const signedTransaction = await this.signer.signTransaction(populatedTransaction);
const signedBundle = await this.flashbotsProvider.signBundle([{ signedTransaction }]);
this.flashbotsProvider.simulate(signedBundle, block_number + BLOCKS_IN_THE_FUTURE).then(
    (simulation : any) => {
        if ('error' in simulation) {
            console.warn(`Simulation Error: ${simulation.error.message}`)
            process.exit(1)
       } else {
            console.log(`Simulation Success: ${JSON.stringify(simulation, null, 2)}`)
       }
   }
);

And I always end getting the same error: Simulation Error: err: invalid chain id for signer; txhash 0x408c5a3a561f51e04ee1da4c534d8f385291a444deac871c065f5b25dc77b33f

Either the library overrides the chainId of the transaction, or it overrides the provider used with another on chainId, I cannot explain to me why I keep getting alwas this same exact error

forza-panino avatar Mar 27 '22 12:03 forza-panino

yo @forza-panino i was having the same issue, discovered that with gasPrice and type: 0 tx it works, and when trying to use maxPriorityFeePerGas and maxFeePerGas (expliciting type: 2) it fails with the same error:

this looks like a very weird behaviour :(

I've made a script to replicate it:

import * as hardhat from 'hardhat'
import '@nomiclabs/hardhat-ethers'
import { ethers } from 'hardhat'
import { FlashbotsBundleProvider } from '@flashbots/ethers-provider-bundle';

var provider = new ethers.providers.JsonRpcProvider({ url: '<JSON_RPC_URL>' }, 1) as any

var funderPK = '< PK >'
var funder = new ethers.Wallet(funderPK, provider)

var fund = {
    to: funder.address,
    value: ethers.utils.parseUnits('0.15','ether'),
    gasLimit: 1e5,
    maxPriorityFeePerGas: 10e9,
    maxFeePerGas: 100e9,
    type: 2
}

var fundType0 = {
    to: funder.address,
    value: ethers.utils.parseUnits('0.15','ether'),
    gasLimit: 1e5,
    gasPrice: 100e9,
    type: 0
}

var fundSignedTx = await funder.signTransaction(fund)
var fundType0SignedTx = await funder.signTransaction(fundType0)


var authSigner = ethers.Wallet.createRandom();

const flashbotsProvider = await FlashbotsBundleProvider.create(
  provider,
  authSigner,
  'https://relay.flashbots.net/',
  'mainnet')

// Type 2 TX
var transactionBundle = [
      { signedTransaction: fundSignedTx , chainId: 1},
    ]
var signedTransactions = await flashbotsProvider.signBundle(transactionBundle)
var targetBlockNumber = await provider.getBlockNumber() + 2
var simulation = await flashbotsProvider.simulate(signedTransactions, targetBlockNumber);
console.log(JSON.stringify(simulation, null, 1))

/*
{
 "error": {
  "message": "err: invalid chain id for signer; txhash < TX_HASH >",
  "code": -32000
 }
}
*/

// Type 0 TX
var transactionBundle = [
      { signedTransaction: fundType0SignedTx , chainId: 1},
    ]
var signedTransactions = await flashbotsProvider.signBundle(transactionBundle)
var targetBlockNumber = await provider.getBlockNumber() + 2
var simulation = await flashbotsProvider.simulate(signedTransactions, targetBlockNumber);
console.log(JSON.stringify(simulation, null, 1))

/*
{
 "error": {
  "message": "err: insufficient funds for gas * price + value: address < ADDRESS > have 0 want 160000000000000000; txhash < TX_HASH >",
  "code": -32000
 }
}
*/

wei3erHase avatar Apr 12 '22 18:04 wei3erHase

Hi! I'm having the exact same issue. Using type: 2 tx and the tx fails with invalid chain id for signer. Any idea how to fix that? Seems to also be related to https://github.com/flashbots/ethers-provider-flashbots-bundle/issues/63

teddav avatar May 17 '22 12:05 teddav

Ey @teddav, hardcode chain id!

wei3erHase avatar May 17 '22 12:05 wei3erHase

Thanks for answering @wei3erHase I tried but I got another error: cannot override "chainId" I'll try again and post back here with my solution when it works

teddav avatar May 17 '22 14:05 teddav

@teddav tried overriding both chainId and baseFee + maxPriorityFee? Some combination of it worked for me.

wei3erHase avatar May 17 '22 14:05 wei3erHase

Thanks @wei3erHase I just got back on it now. I ended up setting all the parameters manually and then passed it to signer.signTransaction(). If anyone is confused, in the end I have something like this:

const blockNumber = await httpProvider.getBlockNumber();
const block = await httpProvider.getBlock(blockNumber);
const BLOCKS_IN_THE_FUTURE = 3;
const maxBaseFeeInFutureBlock = FlashbotsBundleProvider.getMaxBaseFeeInFutureBlock(block.baseFeePerGas!, BLOCKS_IN_THE_FUTURE);
const PRIORITY_FEE = utils.parseUnits('5', 9);
const gasFees = {
  maxFeePerGas: PRIORITY_FEE.add(maxBaseFeeInFutureBlock),
  maxPriorityFeePerGas: PRIORITY_FEE,
  type: 2,
  gasLimit: 2e6,
  nonce: 0
};

const erc20Contract = new Contract(...);
const tx = await erc20Contract.populateTransaction.transfer(["0x0000", 1e18], { ...gasFees });

Hope it will help somebody 🙂

teddav avatar May 18 '22 09:05 teddav