由于您没有在问题中提供合同,因此我在这里假设您的合同如下所示。
文件路径:
./contracts/TestContract.sol
pragma solidity ^0.4.23;
contract TestContract {
// all logic goes here...
function() public payable {
// payable fallback to receive and store ETH
}
}
这样,如果您想从以下位置发送 ETH:accounts[1]
to the TestContract
使用 JS,具体操作方法如下:
文件路径:
./test/TestContract.js
const tc = artifacts.require("TestContract");
contract('TestContract', async (accounts) => {
let instance;
// Runs before all tests in this block.
// Read about .new() VS .deployed() here:
// https://twitter.com/zulhhandyplast/status/1026181801239171072
before(async () => {
instance = await tc.new();
})
it('TestContract balance should starts with 0 ETH', async () => {
let balance = await web3.eth.getBalance(instance.address);
assert.equal(balance, 0);
})
it('TestContract balance should has 1 ETH after deposit', async () => {
let one_eth = web3.toWei(1, "ether");
await web3.eth.sendTransaction({from: accounts[1], to: instance.address, value: one_eth});
let balance_wei = await web3.eth.getBalance(instance.address);
let balance_ether = web3.fromWei(balance_wei.toNumber(), "ether");
assert.equal(balance_ether, 1);
})
})
请参阅我在上面代码中的评论以了解有关之间差异的更多信息.new()
and .deployed()
Truffle 中的关键字。
可以找到我的解决方案的完整源代码here https://github.com/zulhfreelancer/send_eth_to_contract.