2

I want to specify the address I perform a transaction with in my truffle test.

My test looks approximately like this:

contract('Test contract', async (accounts) => {

    let contract;

    before(async () => {
        contract = await Contract.new();
    });

    it("Check something...", async () => {
        await studentV1.addPerson("Jon", "Doe");
        await studentV1.addPerson("Max", "Musterman");

        //....
    });
});

The contract methode addPerson(...) works approximately like this:

function addPerson(string _firstname, string _lastname) public {
    persons[msg.sender] = Person(_firstname, _lastname);
}

Now I want to call my test at await studentV1.addPerson("Jon", "Doe"); with account[0] and the line below with account[1] so my map will also get two different entries. How can I do this? By default truffle test uses for each transaction and call accounts[0].

Im working with the truffle develop chain which automatically creates 10 accounts on startup.

1 Answer 1

2

this is the general formula to call a method using an account i :

await contract.yourFuncitonName(arg1,arg2,{ from: accounts[i] })

notice s at the end of accounts[]

you end up with :

 await studentV1.addPerson("Jon", "Doe",{ from: accounts[0] });
 await studentV1.addPerson("Max", "Musterman",{ from: accounts[1] });
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.