Testing 如何将wei/eth发送至合同地址?(使用truffle javascript测试)

Testing 如何将wei/eth发送至合同地址?(使用truffle javascript测试),testing,blockchain,ethereum,solidity,truffle,Testing,Blockchain,Ethereum,Solidity,Truffle,我正试图将wei/eth发送到我的solidity合同的地址,该合同具有外部应付回退功能。下面我的truffle javascript测试不会导致instance.address的平衡。instance.address不是智能合约地址吗?有人知道为什么console.logging会导致0吗?或者发现我错过了什么 谢谢 const TestContract = artifacts.require("TestContract"); contract('TestContract', async

我正试图将wei/eth发送到我的solidity合同的地址,该合同具有外部应付回退功能。下面我的truffle javascript测试不会导致instance.address的平衡。instance.address不是智能合约地址吗?有人知道为什么console.logging会导致0吗?或者发现我错过了什么

谢谢

const TestContract = artifacts.require("TestContract");


contract('TestContract', async (accounts) => { 

it('should send 1 ether to TestContract', async () => {
  let instance = await TestContract.deployed();

  instance.send({from: accounts[1], value: 1000000000000000000}); 
  let balance = await web3.eth.getBalance(instance.address);
  console.log('instance.address balance: ' + parseInt(balance));
)}

您不能发送到地址,而不是对象


instance.send({from:accounts[1],value:10000000000000000000000})

已解决!我忘了我必须通过web3和eth发送交易,如下所示:

web3.eth.sendTransaction({})

无论如何谢谢你

由于您的问题中没有提供合同,我在这里假设您的合同如下所示

文件路径

./contracts/TestContract.sol

因此,如果您想使用JS将ETH从
帐户[1]
发送到
TestContract
,下面是如何做到的:

文件路径

/test/TestContract.js

请参阅上面代码中的我的评论,以了解更多关于Truffle中
.new()
.deployed()
关键字之间的区别


可以找到我的解决方案的完整源代码。

Ah!更改了该行,但得到:TypeError:instance.address.send不是您试图将1 ETH从
帐户[1]
发送到
TestContract
?是!好的,显式地转换到TestContract的实例地址。现在可以使用
web3.utils.toWei
pragma solidity ^0.4.23;

contract TestContract {

    // all logic goes here...

    function() public payable {
        // payable fallback to receive and store ETH
    }

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

})