Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

How do I set the amount of gas when deploying a contract? #2811

Closed
sh3ifu opened this issue Sep 13, 2024 · 2 comments
Closed

How do I set the amount of gas when deploying a contract? #2811

sh3ifu opened this issue Sep 13, 2024 · 2 comments

Comments

@sh3ifu
Copy link

sh3ifu commented Sep 13, 2024

Hello, i have this function for contract deployment:

    async fn _deploy_contract(
    network: &str,
    contract_path: &str,
    constructor_args: Option<Vec<Token>>,    
    ) -> Result<Address> {
    
    let (client, abi, bytecode) = init(&contract_path, &network)?;
    
    // Create a contract factory
    let factory = ContractFactory::new(abi, bytecode.into(), client);

    // Convert the vector of tokens to a tuple for deployment
    let contract = if let Some(args) = constructor_args {
        match args.len() {
            1 => {
                factory.deploy(args[0].clone())?.send().await?
            }
            4 => {
                let args = (args[0].clone(), args[1].clone(), args[2].clone(), args[3].clone());
                factory.deploy(args)?.send().await?
            }
            _=> {
                panic!("Incorrect number of arguments!");
            }
        }        
    } else {        
        factory.deploy(())?.send().await?;        
    };

    Ok(contract.address())
}

Can you please tell me how to set the amount of gas for deployment?

I've tried doing something like this:
factory.deploy(())?.gas_price(10000000).gas_limit(10000000000000).send().await?;
but it doesn't work that way.

In general, the need to set gas parameters arises when I try to deploy on polygon and bsc blockchains, this is not the case with ethereum.

@SmartArray
Copy link

SmartArray commented Sep 17, 2024

Experiencing the same issue with Polygon:

code: -32000, message: transaction underpriced: tip needed 25000000000, tip permitted 3000000000, data: None

Note that in my case I used a ProviderOracle Middleware too. I thought this would automatically take care of that situation but I was wrong

@SmartArray
Copy link

Turns out that it is actually very simple to modify the priority fee.

// Deploy the contract using ContractFactory
let factory = ContractFactory::new(abi, bytecode.parse()?, client);

// Create deployer
let mut deployer = factory.deploy(())?;

// Set fees
if let Eip1559(ref mut tx) = deployer.tx {
    tx.max_priority_fee_per_gas = Some(/* U256 here */);
    tx.max_fee_per_gas = Some(/* U256 here */);

    // Send transaction and get contract instance
    let contract = deployer.send().await?;

    log::debug!("Contract deployed at: {:?}", contract.address());

    Ok(true)            
} else {
    Err(ConnectorError { message: format!("Expected contract to be a Eip1559 transaction") })
}

@sh3ifu sh3ifu closed this as completed Sep 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@SmartArray @sh3ifu and others