3

I'm working with truffle on testrpc. When starting the testrpc it gives as usual 9 account to work with. As default I execute my functions with account 0. Now I want to send e.g. transaction from account 1 to 2. So change my default from 0 to 1, right?

How do I change the "executing" account?

Thanks!

Working with solidity ^0.4.4

EthereumJS TestRPC v3.0.5

Available Accounts
==================
(0) 0xc434136c76a349c32f1ba070c988d9b69b8aa6e9
(1) 0x025a7b62a0273bbb2b4504a7a1a0a9e05c23f4ed
(2) 0xc257ed20889a0301de39134f20eb705397bcfda8
...etc.
4

You need to configure the truffle.js for giving the account to use - account will be mentioned in from paramter

networks: {
   development: {
     host: "localhost",
     port: 8545,
     network_id: "*" // match any network
                },
     live: {
     host: "178.25.19.88", // Random IP for example purposes (do not use)
     port: 80,
     network_id: 1,        // Ethereum public network
    // optional config values:
    // gas
    // gasPrice
    // from - default address to use for any transaction Truffle makes 
                  during migrations
    // provider - web3 provider instance Truffle should use to talk to the 
                  Ethereum network.
    //          - if specified, host and port are ignored.
             }
         }

For each network, if unspecified, transaction options will default to the following values:

  • gas: Gas limit used for deploys. Default is 4712388.
  • gasPrice: Gas price used for deploys. Default is 100000000000 (100 Shannon).
  • from: From address used during migrations. Defaults to the first available account provided by your Ethereum client.
  • provider: Default web3 provider using host and port options: new - Web3.providers.HttpProvider("http://:")

http://truffleframework.com/docs/advanced/configuration#networks

5
  • @JoAchim Yes this is the way to do – Ajay Jun 24 '17 at 9:28
  • I tried different ways but couldnt make it work. It makes sense how you explained it. Do I need to configure the test.js (for testrpc) directly and then migrate it again? So something like: ... from: "00xc434136c76a349c32f1ba070c988d9b69b8aa6e9", – Jo Achim Jun 24 '17 at 9:35
  • @JoAchim Yes just make sure the account address is right... it should be like "0xc434136c76a349c32f1ba070c988d9b69b8aa6e9" – Ajay Jun 24 '17 at 9:42
  • Something like this? Still gives me an SyntaxError when I compile: module.exports = { networks: { development: { host: "localhost", port: 8545, network_id: "*" // Match any network id, from: "0x025a7b62a0273bbb2b4504a7a1a0a9e05c23f4ed", } } }; – Jo Achim Jun 24 '17 at 9:53
  • @JoAchim Compile or Migrate?? ... the configuration of truffle.js has nothing to do with compile – Ajay Jun 24 '17 at 11:32
1

I don't know about changing your default account, but you can declare parameters like to and from where you're sending by adding an extra argument to your function. For example, if you have a function:

sendCoin(address receiver)

You can add an extra object argument as such:

sendCoin(address01, {from: address02});

Which determines the sender.

See Making A Transaction from the Truffle docs for more information.

1
  • Yes, this option I know. It good for specific contracts. However for testing a contract I would need to apply the same function from two different accounts...I guess. – Jo Achim Jun 24 '17 at 9:36
0

Ok, that should work. I tried different ways but coudnt apply it:

  1. To change the test.js (after truffle init) like this gave me always an error when I compiled or migrated:

module.exports = { networks: { development: { host: "localhost", port: 8545, network_id: "*" // Match any network id, from: "00xc434136c76a349c32f1ba070c988d9b69b8aa6e9", } } };

  1. When I applied the option --from similar to this https://www.npmjs.com/package/truffle-testrpc nothing changed and I was still working from the default account.
0

I had the same question, and I had to look around a while, but I came across a great example HERE that helped me with more detail to wtk219's excellent answer.

This link tests all functions and events for ERC20 compliant contracts. In the context of testing approve, allowance and transfer from ERC20 compliant functions, you come across the case where you can control which address is "using" the deployed contract function.

The following is that particular Mocha and Chai style testing where accounts[1] is actually using the smart contract in the test.

The main line you care about in the code below is:

return token.transferFrom(accounts[0], accounts[2], 200000, {from: accounts[1]});

Here is the code from the link above:

it("should give accounts[1] authority to spend account[0]'s token", function() {
  var token;
  return Token.deployed().then(function(instance){
   token = instance;
   return token.approve(accounts[1], 200000);
  }).then(function(){
   return token.allowance.call(accounts[0], accounts[1]);
  }).then(function(result){
   assert.equal(result.toNumber(), 200000, 'allowance is wrong');
   return token.transferFrom(accounts[0], accounts[2], 200000, {from: accounts[1]});
  }).then(function(){
   return token.balanceOf.call(accounts[0]);
  }).then(function(result){
   assert.equal(result.toNumber(), 300000, 'accounts[0] balance is wrong');
   return token.balanceOf.call(accounts[1]);
  }).then(function(result){
   assert.equal(result.toNumber(), 500000, 'accounts[1] balance is wrong');
   return token.balanceOf.call(accounts[2]);
  }).then(function(result){
   assert.equal(result.toNumber(), 200000, 'accounts[2] balance is wrong');
  })
});

I used this test with few modifications for my specific numbers, and it worked flawlessly.

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.