4

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?

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));
)}
Adremalin
  • 137
  • 3
  • 9

5 Answers5

6

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

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:

File path:

./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.

Full source code for my solution can be found here.

Zulhilmi Zainudin
  • 9,017
  • 12
  • 62
  • 98
2

Solved! I forgot I had to send a transaction via web3 and eth like this:

web3.eth.sendTransaction({})

Thanks anyway!

Adremalin
  • 137
  • 3
  • 9
0

You myst send to an address, not an object.

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

Shane Fontaine
  • 2,431
  • 3
  • 13
  • 23
0

For using Web3.js v1.4.0 in your truffle test files.

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)
    });
});
ovo
  • 1,904
  • 13
  • 26
0

Hello you just forgot the await before the instance.send, thus making the get balance call to do not “see” the ether sent, hope it will help future readers

CYRUS
  • 41
  • 1
  • 7