I have a contract with a method that deploys another contract. The contract it deploys is a contract that is very close to a basic ERC20 that inherits from OpenZep’s Burnable and Mintable ERC20’s, with a two very simple methods attached, with the constructor taking an array of addresses and writing that to storage.
However, when I deploy it using MyContract myContract = new MyContract(arrayOf2To20Addresses), it costs about 4.5 million gas.
What’s up with that?
Environment
dapptools with hevm version .34 (latest)
Code to reproduce
this is the code of the contract that is created:
pragma solidity ^0.5.0;
import "openzeppelin-contracts/contracts/token/ERC20/ERC20Burnable.sol";
import "openzeppelin-contracts/contracts/token/ERC20/ERC20Mintable.sol";
contract MyContract is ERC20Mintable, ERC20Burnable {
using SafeMath for uint256;
address[] public tokens;
constructor(address[] memory _tokens) public {
tokens = _tokens;
}
function testBurn(address account, uint amount) public {
_burn(account, amount);
}
function getTokens() public view returns(address[] memory) {
return tokens;
}
}
The factory contract has nothing to do with it - I stripped the method down to nothing but the call to new MyContract and i’m getting 5 mil gas readouts
although, there are a whole bunch of other methods and a small handful of storage variables in the factory contract. Would that make a difference even if the method that this new contract is being created from does nothing but creates that contract?
I tried deploying the contract without a factory on Ropsten with Remix and it quoted me 2242882 gas for transaction cost and 1626954 gas for execution cost.
When you deploy a contract regularly, you need to:
a) send a tx with the contract bytecode
b) store that bytecode in the blockchain
Both of these actions cost Ether.
When you deploy a contract via a factory, the factory contract already has the contract bytecode stored, so you don't need to send it over (you already paid for this when deploying the factory itself). So what you pay for is:
a) calling a function on your factory
b) storing the bytecode in the blockchain
This call is much cheaper because there's less data involved.