简体   繁体   中英

How to test contract with multiple accounts / addresses in truffle?

I want to test my truffle contract with multiple msg.sender addresses. Like "the first user sell token, the second user buys this token". For one address I simply write something like contract.buy.value(10 wei)(); . But where I could get another address and how to send money from him?

I write my tests on solidity, not on javascript.

as you can see in the Truffle docs , you can specify two different accounts to interact with your deployed smart contract like below (Metacoin example):

var account_one = "0x1234..."; // an address
var account_two = "0xabcd..."; // another address

var meta;
MetaCoin.deployed().then(function(instance) {
meta = instance;
    return meta.sendCoin(account_two, 10, {from: account_one});
    }).then(function(result) {
    // If this callback is called, the transaction was successfully processed.
    alert("Transaction successful!")
}).catch(function(e) {
    // There was an error! Handle it.
})

This is about how you can do with your own created token.

If you want to transfer Ether between accounts, you can specify accounts in your truffle execution file (a javascript file). And these accounts may come from your configured local blockchain (Ganache, if you are using Truffle Suite to test your smart contract, it will provide you with several accounts and you can configure these by yourself).

Moreover, you may need javascript API to specify the sender and receiver: web3.eth.sendTransaction .

First time to answer a question, hope this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM