1

I need to create contracts from various accounts in a Truffle JS Test. To do so, I set:

let accounts = await web3.eth.getAccounts();
MyContract.web3.from = accounts[9];

Before creating my contract instance via:

let myContract = await MyContract.new("constructor argument");

However, the contract is still deployed by account 0 (accounts[0]).

How can I specify that that instance of MyContract shall be created using account 9?

I'm also interested in then calling functions of that contract using various accounts, so it's not just about deploying a contract using a specific account.

  • In case you've missed it, read the supplemental that I've added at the bottom of my answer; you might find it useful to your understanding of the system. – goodvibration Jan 13 at 16:26
  • @goodvibration I have read it but thanks for the hint. :) – UTF-8 Jan 13 at 16:51
1

Try this:

let myContract = await MyContract.new("constructor argument", {from: accounts[9]});

Same thing for calling functions of that contract using various accounts.

You can use that object argument in order to specify various transaction details such as from, value, gas and gasPrice. It is optional (i.e., assigned default values if not specified explicitly), and it must therefore be provided after all mandatory arguments.

In your contract's code, those arguments will be available to the function via the implicit input argument called msg. For example, msg.sender will represent the value that you have passed in from, and msg.value will represent the value which you have passed in value.

| improve this answer | |

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.