My contracts have some methods that used only for testing i.e. exposes some internal contract data and I do not want to deploy it to live network. for now Solidity does not supports C-style preprocessor instructions like #if/#else. But on npmjs I found module preprocessor that does exactly what I want.

Only one question remains - how to compile contract after preprocessing? Obviously I cannot replace original contract and in the same time want to keep resulting artifacts file with same name as original contract. Maybe it is possible to compile contracts from stdin so I can chain preprocessor output to truffle input?

  • My solution to expose some functions from a contract is to create a child contract that inherits from the contract I want to test. This child contract is not deployed, and it will have access to internal state of the base contract. See an example here ethereum.stackexchange.com/a/47010. This have some issues like you cannot access private data or functions, the contract is larger and you have to deploy explicitly in each unit test. But it works without requiring new tools. – Ismael May 18 '18 at 13:53
  • An approach that might work in your situation is to use the migration scripts. Have a dummy migration 2_preprocess_contracts.js that will call the preprocessor and output the contracts. Then have the real migration script in the next step 3_deploy_contracts.js. You can use the 'network' option to select different configurations inside your deployment scripts. – Ismael May 18 '18 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.