如何发送wei/eth到合约地址?(使用松露 javascript 测试) [英] How to send wei/eth to contract address? (using truffle javascript test)

查看:42
本文介绍了如何发送wei/eth到合约地址?(使用松露 javascript 测试)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 wei/eth 发送到具有外部应付回退功能的 Solidity 合约的地址.我下面的松露 javascript 测试不会导致 instance.address 的平衡得到任何 wei.instance.address 不是接收wei 的智能合约地址吗?谁能发现为什么 console.logging 余额结果为 0?或者发现我遗漏了什么?

I'm trying to send wei/eth to the address of my solidity contract which have an external payable fallback function. My truffle javascript test below doesn't result in the balance of instance.address getting any wei. Is not instance.address the smart contract address receiving wei? Can anyone spot why console.logging the balance results in 0? Or spot what I'm missing?

谢谢!

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

推荐答案

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

Since you are not providing the contract in your question, I'm making an assumption here that your contract looks like below.

文件路径:

./contracts/TestContract.sol

pragma solidity ^0.4.23;

contract TestContract {

    // all logic goes here...

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

}

有了这个,如果你想使用 JS 从 accounts[1] 发送 ETH 到 TestContract,这里是如何做到的:

With that, if you want to send ETH from accounts[1] to the TestContract using JS, here is how to do it:

文件路径:

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

})

查看我在上面代码中的评论以了解有关 Truffle 中 .new().deployed() 关键字之间差异的更多信息.

See my comment in above code to learn more about the differences between .new() and .deployed() keyword in Truffle.

可以在此处找到我的解决方案的完整源代码.

Full source code for my solution can be found here.

这篇关于如何发送wei/eth到合约地址?(使用松露 javascript 测试)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆