如何在松露中测试多个帐户/地址的合同?

问题描述 投票:1回答:1

我想用多个msg.sender地址测试我的松露合同。就像“第一个用户销售令牌,第二个用户购买此令牌”。对于一个地址,我只是写一些像contract.buy.value(10 wei)();。但是我可以在哪里获得另一个地址以及如何向他汇款?

我把测试写在坚固性上,而不是javascript上。

testing solidity truffle
1个回答
1
投票

正如您在Truffle docs中看到的,您可以指定两个不同的帐户来与您部署的智能合约进行交互,如下所示(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.
})

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

如果要在帐户之间传输以太网,则可以在松露执行文件(javascript文件)中指定帐户。这些帐户可能来自您配置的本地区块链(Ganache,如果您使用Truffle Suite测试您的智能合约,它将为您提供多个帐户,您可以自己配置这些帐户)。

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

第一次回答问题,希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.