Call contract from another account using web3.js

by Henrique Barcelos   Last Updated October 09, 2019 15:28 PM

I have a contract with which 2 accounts should interact.

The accounts are in the same node in web3.eth.accounts.

The contract was created by accounts[0].

How can I call that contract from accounts[1] from web3.js?



Answers 1


when you call your function you can specify a from parametern in the transactionOptions. Suppose you have a contract instance contract and want to call vote function. Then this calls it from account 1:

let account0;
web3.eth.getAccounts().then(function(result) {
  account0 = result[0];
  contract.vote(2, {
    from: account0
  });
});

You can read more about it here: https://github.com/ethereum/wiki/wiki/JavaScript-API#contract-methods

Denis
Denis
March 30, 2016 19:18 PM

Related Questions


Updated August 04, 2017 14:28 PM

Updated August 31, 2017 17:28 PM

Updated July 31, 2017 15:28 PM

Updated August 18, 2017 14:28 PM

Updated July 10, 2018 19:28 PM