4

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.

2
  • 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. Commented Jan 13, 2020 at 16:26
  • @goodvibration I have read it but thanks for the hint. :) Commented Jan 13, 2020 at 16:51

1 Answer 1

3

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.