0

I was recently viewing a DApp on GitHub. It had the following in .sol, and test.js respectively.

    address contractOwner;
    address withdrawWallet;

    function contractName(address _contractOwner, address _withdrawWallet) {
        require(_contractOwner != address(0));
        require(_withdrawWallet != address(0));

        contractOwner = _contractOwner;
        withdrawWallet = _withdrawWallet;
    }
  contract('contractName', function(accounts) {
  const owner = accounts[0]; // this is the account we deploy as owner, see 2_deploy_contracts.js
  const withdrawWallet = accounts[1];
  const account1 = accounts[2];
  const account2 = accounts[3];

  web3.eth.sendTransaction({from:owner, to:account1, value: 10000000000000000000 }, function(err, r) { /* NOP */ });

  it("should have correct constants by default", function() {
    let CN;
    return contractName.new(owner, withdrawWallet)
      .then(function(instance) {
        CN = instance;

        return CN.weiProductPrice.call();
      })
      .then(function(wpp) {
        assert.equal(weiProductPrice, wpp.toNumber());
        return CN.productPerPage.call();
      })
      .then(function(ppp) {
        assert.equal(productPerPage, ppp.toNumber());
      });
  });

2_deploy_contracts.js contains

  // We deploy the contract with the owner being the first address from accounts
  const owner = accounts[0];
  if (network == "live") {
    const withdrawWallet = "0x00010dB ... legit wallet address";
    deployer.deploy(contractName, owner, withdrawWallet);
    return;
  }

What do accounts[0], accounts[1], accounts[2], accounts[3] mean ? Are they supposed to indicate that the user has to assign values, or do they have a specific meaning? They aren't defined anywhere in the project.

Thank you.

9
  • are you using truffle and ganache? by default, ganache has 10 accounts (with ether), which you can use for testing in your scripts. Truffle can access the addresses of those accounts and give them to you as an array of accounts. – Majd TL Mar 19 at 12:52
  • @MajdTL, I am using truffle. So when I deploy, I have to enter real values in place of accounts[2] etc., right? – dragic34 Mar 19 at 15:49
  • yes, there is many ethereum test network where you can try. The deployer account need test-ether and should sign the transaction (take look at something called truffle-hd-wallet-provider) – Majd TL Mar 19 at 17:22
  • @MajdTL thank you. If I wish to, I can replace accounts[0] with a wallet address in 2_deploy_contracts.js, right? Also in the .sol file, I have to provide wallet addresses for contractOwner and withdrawWallet in the first 2 lines, correct? – dragic34 Mar 26 at 5:22
  • No you don't have to change in .sol -> address(0) has nothing to do with accounts[0]. require(_contractOwner != address(0)); just check that you didn't give an empty account. The contract takes 2 parameters the address of the owner and the address of the withdrawWallet. and you should have the private key of those addresses cause you will use them later I guess. – Majd TL Mar 26 at 8:58
1

accounts was passed into the test, here:

contract('contractName', function(accounts)

It's an array that truffle conveniently picks up from the wallet.

when I deploy, I have to enter real values in place of accounts[2] etc., right?

No.

When you deploy it is a foregone conclusion that you have at least one Ethereum account and it has money to pay for gas. It signs the transactions.

Truffle is inspecting the client to discover what accounts exist. Those are your addresses on the mainnet, testnet or ganache-cli as the case may be.

One of the nice things about ganache is that it sets you up with 10 funded accounts, by default.

The tests are written to be agnostic about the actual addresses of the accounts and rely on whatever is there. You can use the from: member of the transaction object to test multi-party processes, i.e. from: owner, from: alice, from: bob where all three are your accounts.

contract("Factory", accounts => {

  const owner = accounts[0];
  const alice = accounts[1];
  const bob = accounts[2];

  it("should do something", async () => {
    // transfer between MY accounts;
    await web3.eth.sendTransaction({ value: 10, from: alice, to: bob });
    ...

You could go like this and it might be illustrative:

it("should be ready to test", async () => {
   assert.isAtLeast(accounts.length, 3, "There are not at least three accounts to work with");
});

In case it is not clear, on a mainnet or testnet you would create accounts until you have at least three. The test is unconcerned with how you accomplish it but it needs to be done or it won't be able to sign. https://geth.ethereum.org/docs/interface/managing-your-accounts

Hope it helps.

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.