1

I am trying to send Ether in Ether units i.e. 3 ethers by two different techniques using truffle console:

1)truffle(development)> C1.sendTo(C2.address, 3)

and

2)truffle(development)> C1.sendTo(C2.address, 3000000000000000)

(where sendTo(...) is a method of myContract1, C1 is the object of myContract1 and C2 is the object of myContract2. But at the receiver end i.e. C2, I am always getting '0.000000000000000003'. myContract1, myContract2 and the migration file is given below:

pragma solidity ^0.5.8;
contract myContract1 {
    address owner;
    constructor() public {
        owner = msg.sender;
    }
    function sendTo(address payable receiver, uint amount) public {
       receiver.transfer(amount);
    }
    function() external payable{
     }
}

myContract2:

pragma solidity ^0.5.8;
contract myContract2 {
    //This contract will receive Ether sent by myContract1
    address owner;
    constructor() public {
       owner = msg.sender;
    }
    function() external payable {   
    }
}

and the migration file is:

const myContract1 = artifacts.require("myContract1");
const myContract2 = artifacts.require("myContract2");

module.exports = function(deployer) {
deployer.deploy(myContract1);
deployer.deploy(MyContract2);
};

I am accessing sendTo(..) function of myContract1 through Truffle console. I want to send the amount in Ether through ‘sendTo(..)’ method but I think it is accepting the amount in Wei and printing the value in decimal point instead of whole number. Please guide me how to send the value through Truffle console so that it should be in whole number.The output is given below, currently C1 has a balance of 7 Ethers:

truffle(development)> C1bal = await web3.eth.getBalance(C1.address) undefined truffle(development)> web3.utils.fromWei(C1bal, "ether") '6.993999999999999997'

and similarly:

truffle(development)> web3.utils.fromWei(C2bal, "ether") '0.000000000000000003'

Somebody please guide me. Zulfi.

1

That's a unit conversion problem.

3 Ether = 3000000000000000000 Wei.

The following workflow works using the Truffle development console :

  1. Deploy the smart contracts

Open the truffle development console and run truffle migrate --reset. Note that your current migration file does not work : you have to change MyContract2 to myContract2.

  1. Get the smart contract instances

    C1 = await myContract1.deployed()
    C2 = await myContract2.deployed()
    
  2. Send 3 ETH to the smart contract 1

The value to be sent must be converted to Wei.

    web3.eth.sendTransaction({to:C1.address, from:accounts[0], value:web3.utils.toWei("3", "ether")})
  1. Send the ETH from smart contract 1 to smart contract 2

     C1.sendTo(C2.address, web3.utils.toWei('3',"ether"), {from: accounts[0]})
    
  2. Get the balance of the smart contract 2

     C2bal = await web3.eth.getBalance(C2.address) 
    
     web3.utils.fromWei(C2bal, "ether") //returns '3' 
    
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.