首页 > 解决方案 > 如何在松露中测试具有多个帐户/地址的合约?

问题描述

我想用多个msg.sender地址测试我的松露合约。就像“第一个用户卖出代币,第二个用户购买这个代币”。对于一个地址,我只需写类似contract.buy.value(10 wei)();. 但是我在哪里可以获得另一个地址以及如何从他那里汇款?

我在solidity 上编写测试,而不是在javascript 上。

标签: testingsoliditytruffle

解决方案


正如您在Truffle 文档中看到的那样,您可以指定两个不同的帐户来与您部署的智能合约进行交互,如下所示(Metacoin 示例):

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.
})

这是关于如何使用自己创建的令牌。

如果你想在账户之间转移以太币,你可以在你的 truffle 执行文件(一个 javascript 文件)中指定账户。而这些账户可能来自你配置的本地区块链(Ganache,如果你使用 Truffle Suite 来测试你的智能合约,它会为你提供几个账户,你可以自己配置)。

此外,您可能需要 javascript API 来指定发送者和接收者: web3.eth.sendTransaction

第一次回答问题,希望对你有帮助。


推荐阅读