1

Let's say I have a complex operation to execute, for example deploying 6 contracts and calling functions to each one, passing addreses to few of them and executing some extra more functions. I can, of course, call each function manually to pass address and so on, but it takes time and might cause a mistake. So the best way, in theory, would be to have a chunk of code ready to go and executing it once and automatically do the operation.

It would be pretty simple to do it using migration or test, but somebody told me before:

No, this is not a good use of migrations. Migrations are about deploying the system to the network and one-time initialization steps

Adding a builder contract is impossible, because holding few contracts exceeds ethereum limits.

So should I use the Test feature for executing chunks of complex code of my contracts' environment? Even on the mainnet?

  • Why not write a function for it? A migration is basically just a function that gets executed at deploy-time. – Rosco Kalis Jan 13 at 13:30
  • Where should I write a function for it? In migration, test or solidity contract? – Radeg90 Jan 13 at 13:37
  • If by chunks of complex code you mean implement this chunk in Solidity and then invoke it in a single line from a Truffle test, then you've missed the whole point of Truffle, which is to test your contract functionalities - separately (unit-test) and combined (functional-test). Once you implement a whole chunk in Solidity, then you've technically moved the might cause a mistake part which you're concerned about, from the test to the contract. And believe me, you're much better off with a bug in your test, than a bug in your contract. – goodvibration Jan 13 at 13:37
  • You can write Javascript (or other programming languages) code to interact with your smart contracts, using something like web3js. So you can write Javascript functions that call functions inside you smart contracts. – Rosco Kalis Jan 13 at 13:40
1

If your code doesn't fit the concept of the migration script, you can use truffle exec to execute a custom JS script within the truffle environment. See https://truffleframework.com/docs/truffle/reference/truffle-commands#exec

The script is required to have the following structure:

const SomeContract = artifacts.require("SomeContract");
...

module.exports = function(done) {
    // ...your custom code here ...

    // call this to signal truffle that your script is done
    done();
}

You can access the contracts in the same way like you do in migration/test scripts.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.