简体   繁体   English

如何发送wei/eth到合约地址? (使用松露 javascript 测试)

[英]How to send wei/eth to contract address? (using truffle javascript test)

I'm trying to send wei/eth to the address of my solidity contract which have an external payable fallback function.我正在尝试将 wei/eth 发送到具有外部应付回退功能的 Solidity 合同的地址。 My truffle javascript test below doesn't result in the balance of instance.address getting any wei.我下面的松露 javascript 测试不会导致 instance.address 的平衡得到任何 wei。 Is not instance.address the smart contract address receiving wei? instance.address 不是接收wei 的智能合约地址吗? Can anyone spot why console.logging the balance results in 0?谁能发现为什么 console.logging 余额结果为 0? Or spot what I'm missing?或者发现我遗漏了什么?

Thanks!谢谢!

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.由于您没有在问题中提供合同,因此我在这里假设您的合同如下所示。

File path :文件路径

./contracts/TestContract.sol ./contracts/TestContract.sol

pragma solidity ^0.4.23;

contract TestContract {

    // all logic goes here...

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

}

With that, if you want to send ETH from accounts[1] to the TestContract using JS, here is how to do it:有了这个,如果你想使用 JS 将 ETH 从accounts[1]发送到TestContract ,这里是如何做到的:

File path :文件路径

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

})

See my comment in above code to learn more about the differences between .new() and .deployed() keyword in Truffle.请参阅我在上面代码中的注释,以了解有关 Truffle 中.new().deployed()关键字之间差异的更多信息。

Full source code for my solution can be found here .我的解决方案的完整源代码可以在这里找到。

Solved!解决了! I forgot I had to send a transaction via web3 and eth like this:我忘了我必须像这样通过 web3 和 eth 发送交易:

web3.eth.sendTransaction({})

Thanks anyway!不管怎么说,还是要谢谢你!

你 myst 发送到一个地址,而不是一个对象。

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

For using Web3.js v1.4.0 in your truffle test files.用于在您的松露测试文件中使用Web3.js v1.4.0

const SolidityTest = artifacts.require('SolidityTest');

contract('SolidityTest', (accounts) => {

    let solidityTest;

    before(async () => {
        solidityTest = await SolidityTest.new();
    })

    it('Test something', async () => {

        // Send 100 wei to the contract.
        // `sendEth` is your payable method.
        await solidityTest.sendEth({from: accounts[1], value: 100});

        // Check account 1 balance.
        let acc1Balance = await web3.eth.getBalance(accounts[1]);
        // Convert the unit from wei to eth
        acc1Balance = web3.utils.fromWei(acc1Balance, 'ether')
        console.log('acc1 balance:', acc1Balance)

        // Check the contract balance.
        let contractBalance = await web3.eth.getBalance(solidityTest.address);
        contractBalance = web3.utils.fromWei(contractBalance, 'ether')
        console.log('contract balance:', contractBalance)
    });
});

您好,您刚刚忘记了 instance.send 之前的 await,因此调用 get balance 不会“看到”发送的以太币,希望对未来的读者有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为javascript / truffle中的每个测试创建新的以太坊/实体合约 - how to create new ethereum/solidity contract for each test in javascript/truffle 在使用js的松露测试中,如何在`contract`之后设置参数? - In truffle test using js, how to set the parameters after the `contract `? 如何在松露中测试具有多个帐户/地址的合同? - How to test contract with multiple accounts / addresses in truffle? 如何测试松露中的应付方式? - How to test payable method in truffle? 如何创建/添加帐户到松露测试环境 - How to create/add an account to truffle test environment 如何使用松露测试代码牢固地读取公共变量? - How to read public variable in solidity with truffle test codes? 如果消费者合同发生变化,如何自动触发生产者合同测试 - How to trigger producer contract test automatically if consumer contract changes 测试松露中的智能合约要求:如果功能不无效,则交易将被还原 - Testing Smart contract requires in Truffle: transaction reverted if function isnt void 将 Pact 合约测试从 JavaScript 重写为 C# - Rewriting Pact contract test from JavaScript to C# 如何在不构建项目中的所有单元和合同测试的情况下生成和运行单个合同测试? Java Spring Cloud 合约验证器 - How to generate and run a single contract test without building all the unit and contract tests in the project? Java Spring Cloud Contract Verifier
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM