Deploying Contract with Solidity-Create2-Deployer

I keep getting the cannot estimate gas error when attempting to deploy a contract using solidity-create2-deployer package.

I am using the dai contract for testing purposes. I collect all the required parameters and follow the github instructions (https://github.com/thegostep/solidity-create2-deployer) however I cannot seem to get it right. I have also looked online without luck thus far. If anyone could assist Iโ€™d be very grateful.

const creat2 = async () =>{

    var daiAddress = "0x6B175474E89094C44Da98b954EedeAC495271d0F"
    const sourceCode = await fetch(`https://api.etherscan.io/api?module=contract&action=getsourcecode&address=${daiAddress}&apikey=${apiKey}`)
    const getAbi = await fetch(`https://api.etherscan.io/api?module=contract&action=getAbi&address=${daiAddress}&apikey=${apiKey}`)
    const jsonAbi = await getAbi?.json()
    const abi = await jsonAbi?.result
    var abijson = JSON.parse(abi)
    
    console.log(abijson)
    const saucy = await sourceCode?.json()

    var source = await saucy?.result
    var constructr = abijson.filter(object => object.type == "constructor")
    var constructor = constructr[0]?.inputs
    console.log(constructor)
   
    
    if(source[0]?.Proxy == "1"){
      var daiAddress = source[0]?.Implementation 
      const sourceCode = await fetch(`https://api.etherscan.io/api?module=contract&action=getsourcecode&address=${daiAddress}&apikey=${apiKey}`)
      const saucy = await sourceCode?.json()
      var source = await saucy?.result
      console.log(source[0])
      console.log(source[0]?.ConstructorArguments)

      const getAbi = await fetch(`https://api.etherscan.io/api?module=contract&action=getAbi&address=${daiAddress}&apikey=${apiKey}`)
      const jsonAbi = await getAbi?.json()
      const abi = await jsonAbi?.result
      var abijson = JSON.parse(abi)
      var constructr = abijson.filter(object => object.type == "constructor")
      var constructor = constructr[0]?.inputs
      console.log(constructor)

      // const constructor = ethers.utils.AbiCoder.prototype.decode(source[0]?.ConstructorArguments)
      // console.log(constructor)
      // decode args 
      

    }
    let constructorTypes = []
    for (let i =0; i<constructor.length; i++){
      constructorTypes.push(constructor[i]?.internalType)
    }
    console.log(constructorTypes)
    console.log(source[0]?.ConstructorArguments)
    
    const bytecode = await web3.eth.getCode(daiAddress)
    console.log(bytecode)
    const salt = generateRandomSalt(); // Choose a salt value
    
    // const constructorTypes = ["uint256"]
    const constructorArgue = "0x" + source[0]?.ConstructorArguments
    console.log(constructorArgue)
    const constructorArgs = (ethers.utils.AbiCoder.prototype.decode(constructorTypes, constructorArgue)).toString()
    console.log(constructorArgs)
    

    const signer = new ethers.Wallet(initiatorPK, provider1); // Create a wallet with your private key and provider

    const zaiAddress = getCreate2Address({
      salt: salt,
      contractBytecode: bytecode,
      constructorTypes: constructorTypes,
      constructorArgs: [constructorArgs]
    });

   console.log(zaiAddress)
 
    const { txHash, address, receipt }  = await deployContract({
      salt: salt,
      contractBytecode: bytecode,
      constructorTypes: constructorTypes,
      constructorArgs: [constructorArgs],
      signer: signer,
    }); 

    const deployed = await isDeployed(address, provider1);
    console.log(deployed)

 }```

Hi @CleanMcGerk
Can you verify if you are deploying on the correct network? and have the required balance to pay the gas fee on that network.

Not sure how to verify this. I am on mainnet in the above example. I am not aware of the gas cost to deploy the contract. However usually the cannot estimate gas error is not due to insufficient funds. I will add funds as I only have $30 on the account. I will try with goerli. I assume deploying the contract on the network is dependent on the rpc. If so I am doing it correctly.

hmm yeah it is better to test in goerli or sepolia first. Make sure you have the require ETH for gas.

Thanks @johnversus - I have tried that to no avail. I would appreciate it (given that you had the time and will) if you could replicate it. I cannot seem to solve this.

Do you have your code on github? Maybe I can try to clone your code and run it on my end.

I donโ€™t have it on github. the packages required are ethers, web3js, and solidity-create2-deployer and the keys required are an etherscan api key, an alchemy api key as well as a privatekey for the wallet in use.

as well as the function above. Unfortunately if I had to upload to github Iโ€™d be uploading an entire project instead of the single function in question.

the example usage is here https://github.com/thegostep/solidity-create2-deployer

The original repository of this project seems to be last updated before 4-6 years. So it could be possible there are some outdated code.

Try following for any new tutorial projects

Thanks will do @johnversus

Hi brother @johnversus I have gone and used ethers.js instead of the above package. It was in fact caused by expired packages. However now I face issue with the arguments within the deploy instance. with create2 its necessary to include the salt in the deploy instance. I assume I am doing something wrong here as I now get the error too many arguments in the constructor. The constructor in the contract in question is of type uint256 and is a chainId of the deployed contract.

    var daiAddress = "0x11fE4B6AE13d2a6055C8D9cF65c55bac32B5d844"
   
    const getAbi = await fetch(`https://api-goerli.etherscan.io/api? 
    module=contract&action=getAbi&address=${daiAddress}&apikey=${apiKey}`)
    const jsonAbi = await getAbi?.json()
    const abi = await jsonAbi?.result
    var abijson = JSON.parse(abi)   
      
    const bytecode = await web3.eth.getCode(daiAddress)
    console.log(bytecode)
    const initCodeHash = ethers.utils.keccak256(bytecode)
    console.log(initCodeHash)   
    const salt = generateRandomSalt(); // Choose a salt value
    

    const signer = new ethers.Wallet(`${process.env.NEXT_PUBLIC_INITIATOR_KEY}`, provider1); 

   const zaiAddress = ethers.utils.getCreate2Address(
      initiator, salt, initCodeHash
    ); 
    console.log(zaiAddress)
   
    const factory = new ethers.ContractFactory(abijson, bytecode, signer)
    console.log(factory)
    const gas = await web3.eth.getGasPrice()
    console.log(gas)
    const options = {gasPrice: Number(gas*120/100), gasLimit: 200000}
    const contract = await factory.deploy(salt,5, options)
   
    const succ = await contract.deployed()
    console.log(succ)
    console.log(contract)

error could be related to this line. Please check if you need 3 arguments to deploy the contract.

Its definitely that line. It works with (5, options) as arguments however his deploys to a different address than the zaiAddress generated by the getCreate2Address function which is counter-intuitive in this instance. The salt value is paramount in deploying to the same address.

1 Like

so my question stands unfortunately @johnversus . I donโ€™t see why it wonโ€™t deploy to the same address

Do you have any docs on where you have found about deploying to the same address

the above two is what I use to execute using ethersjs.

and I used the following video to make sense of the principle

I did not understand how it worked. But as per the contract in the video is defined to accept salt param on line 17.

Did you also define a salf param in your contract code? The should be the only param that keep the address same.

Thank you! Makes sense

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.