我正在使用松露控制台来测试我的合同,但我在通过争论时遇到了麻烦。例如,我有这样一个方法
function sendEtherTo(address addr) public payable {
require(addr != 0x0);
require(msg.value > 0);
addr.transfer(msg.value);
}我试图使用sendTransaction,但不知道如何传递这个论点。
这是:
web3.eth.sendTransaction({from: web3.eth.accounts[1], to: myContract.address, data: myContract.sendEtherTo.sendTransaction(web3.eth.accounts[2]), value: web3.toWei(10, "finney")})结果我的测试失败了,松露说错了
Error: Invalid JSON RPC response: undefined我已经尝试了在web3中成功地完成了以下操作
web3.eth.sendTransaction({from: web3.eth.accounts[1], to: myContract.address, data: myContract['sendEtherTo'].getData(web3.eth.accounts[2]), value: web3.toWei(10, "finney")})但这告诉我getData是未定义的。
发布于 2018-03-19 03:27:00
松露使用不提供getData的自定义对象包装契约,但我们可以使用request方法获取sendTransaction的参数。
const MyContract = artifacts.require('MyContract');
contract('MyContract', function(accounts) {
it('Call with sendTransaction', async () => {
const myContract = await MyContract.deployed();
// We want to call foo(23) on myContract
const data = myContract.foo.request(23);
// Here data.params[0].data contains the parameters to the call
const txhash = web3.eth.sendTransaction({
from: accounts[0],
to: myContract.address,
value: 1,
data: data.params[0].data
});
});
});https://ethereum.stackexchange.com/questions/43028
复制相似问题