testing - 如何在松露中测试具有多个帐户/地址的合约?
问题描述
我想用多个msg.sender
地址测试我的松露合约。就像“第一个用户卖出代币,第二个用户购买这个代币”。对于一个地址,我只需写类似contract.buy.value(10 wei)();
. 但是我在哪里可以获得另一个地址以及如何从他那里汇款?
我在solidity 上编写测试,而不是在javascript 上。
解决方案
正如您在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。
第一次回答问题,希望对你有帮助。
推荐阅读
- python - 如果主题标签在 tweepy 中包含非 ASCII 字符或非英文字符,如何通过主题标签获取推文?
- javascript - 用另一个对象值引用对象名称
- google-app-engine - 广告错误:AdError 1009:VAST 响应文档为空
- c# - 无法创建 UWP - Microsoft .NET Core 5.0.0
- bash - 如何绑定向上箭头键以转到下一个唯一命令(macOS 终端)?
- rest - RESTful 端点过长可以吗?
- python - 从函数/类返回后如何使列表为空
- powershell - 删除内置管理员
- hadoop - 基于hive中每种文件格式的场景
- reactjs - Axios 请求在 React-Native 中返回错误