How to send the value in ether through truffle console while calling solidity function

Hi,
Please tell me how to send the mount in Ether through the Solidity function. I have got the following contract with transferTo(…) method.

pragma solidity ^0.4.18;
contract A {address owner;
   function constructor() {  owner = msg.sender;}
   function transferTo(address to, uint amount) public {    
   to.call.value(amount)();}
   function() payable public {}
}

I am accessing transferTo(…) function through Truffle console. I want to send the amount in Ether through ‘transferTo(…)’ 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.
I invoke the function like:

truffle(development)> await victim.transferTo(receiver.address, 3),

I am talking about the second argument ‘3’. When I am printing its value at the receiver, I am getting the answer in points.

    truffle(development)> recev = await web3.eth.getBalance(receiver.address)
    undefined
    truffle(development)> web3.utils.fromWei(recev, "ether")
    '0.000000000000000003'

I want to print just ‘3’, please guide me.

Zulfi.

Do you know what web3.utils.fromWei(recev, “ether”) does?

If you do, what is the reverse function of fromWei?
Is it possible to call the reverse function of fromWei and then use it in the transferTo function as a parameter?

If you can discover the answers to these questions, I believe you’d be able to solve all your questions.

2 Likes

Welcome to the community @chuacw :wave:

1 Like

Hi @chuacw w what web3.utils.fromWei(recev, “ether”) does?

It would convert recev into Ether

If you do, what is the reverse function of fromWei?

Reverse function is toWei

const weiValue = Web3.utils.toWei(‘1’, ‘ether’);

Is it possible to call the reverse function of fromWei and then use it in the transferTo function as a parameter?

Yes, but it would convert the value into Wei, I want to convert the value into Ether.

I did the following but still I am not getting the proper answer:

truffle(development)> const etherValue = web3.utils.fromWei('3000000000000000000', 'ether');

undefined

truffle(development)> A.transferTo(VT.address, etherValue )

truffle(development)> VTbal = await web3.eth.getBalance(VT.address)

undefined

truffle(development)> web3.utils.fromWei(VTbal, "ether")

'0.000000000000000003'

truffle(development)>

{ tx:

Somebody please guide me. Still I am not getting 3 Ethers.
Zulfi.

What does transferTo takes as parameters?
An address, and a value to transfer. What is the value specified in?

If you think about it further, the answer you want is already in your hands.

For your reference:
fromWei
toWei

1 Like

Hi,

Value is specified in uint. Sorry still I can’t understand why I am not getting the Ether value as sent through 'transferTo(…) ’ method. Why its dividing by 10^18? Please guide me, I am stuck.
Zulfi.

Maybe a few more questions for you.
If it’s dividing by 10^18, what should you do to get it back? Maybe reverse it?
What is toWei and fromWei again? Describe its operation mathematically. What does it do? Divide or multiply or both? And by how much again?

1 Like

Hi,
I have already answered your questions in post#4 and post#6, please provide me the solution now. Otherwise, I would think that you don’t know the answer.

Zulfi.

Hi @chuacw ,

This was not a multiply divide question. I have found the solution here:
Sending Ether using Solidity function through Truffle console

1 Like

What exactly do you think toWei and fromWei is doing if it’s not multiply by 10^18 and dividing by 10^18, when you specify ‘ether’ in the second parameter?

When I asked you what transferTo takes as a value, and what it’s specified in, it was to let you think about it, and the answer is that the value is specified in Wei.

In your code, and in your line of thinking, you only used fromWei, without using the reverse, toWei. That’s why you couldn’t get the answer.

Asking somebody what the answer is, is rather easy.

Thinking about how to derive the answer is more challenging.

1 Like