I think my contract is too big to deploy, because I got
Error encountered, bailing. Network state unknown. Review successful transactions manually. Error: oversized data
when I am trying to deploy on ropsten network. (but it works fine on testrpc, because I set a very large number of gas limit)
So I want to estimate my code size to be deployed in truffle test. I know how to estimate gas to run a function in contract using estimateGas()
, but I don't know how to estimate gas(or code size?).
Best Answer
How to estimate gas for deploying a smart contract
I'm using the Yellow Paper, Appendix G, page 25 as reference.
The cost of gas to deploy your contract can be calculated like this:
Your transaction will have input data, which will cost you in gas:
Initialising variables and running the constructor, costs you:
Finally, you have to pay to store your code, and that will cost you:
When you compile your code, you get your bytecode, and that's where you can find all the OPCODES your smart contract executes.
You will also get your running (or deployed) bytecode, which is the code that will be stored on the state. It's equal to the bytecode minus the initialisation and constructor code (that are not stored in the state).
How to know the code size using a truffle javascript test file
You can use the following code in a js file inside the test folder:
Afterwards, run
truffle test
.