Truffle Console: Not able to send Ether to an account using Smart Contract’s method

Hi,
I have got a contract which is using transfer to send Ether. The transaction is happening but the balance of the account is not increasing.

`pragma solidity ^0.5.1;`
`  ` `contract TransferTest{`
`     ` `uint8  public testVal =97;`
`     ` `function
testFunc(address payable addr) public returns (uint8)         {`
`        ` `addr.transfer(testVal);`
`        ` `return testVal;`
`     ` `}
    `
`}`

I am using accounts[2]. Truffle console is showing the transaction of calling the ‘testFunc’ but the
balance of SC and the accounts[2] i.e. acc2 is not changing:

truffle(development)>TC = await TransferTest.at(addr)
truffle(development)> balance = await web3.eth.getBalance(TC.address)
undefined
truffle(development)> web3.utils.fromWei(balance, "ether")
'193.999999999999999903'
truffle(development)> acc2 = accounts[2]
0x20642D4d868da20f5268b0Be7d52fD26FcD9940d'
truffle(development)> b2 = await web3.eth.getBalance(acc2)
undefined
truffle(development)> web3.utils.fromWei(b2, "ether")
'100.000000000000000097'
truffle(development)> await TC.testFunc(acc2)
{ tx:
'0xb751de0c31147d2baf2330c50c62c70facacf3e35e201061c12260bda601e4df',
receipt:
truffle(development)> b2 = await web3.eth.getBalance(acc2)
undefined
truffle(development)> web3.utils.fromWei(b2, "ether")
'100.000000000000000194'

Somebody please guide me.

Zulfi

1 Like

Sorry, I am not sure what do you mean acc2 is not changing, according to your log in the truffle console:

before calling testFunc()

web3.utils.fromWei(balance, "ether").  <<----- eth balance of the contract.
web3.utils.fromWei(b2, "ether")        <<----- eth balance of the account2.
= 100.000000000000000097

after calling testFunc()

web3.utils.fromWei(b2, "ether").        <<----- eth balance of the account2.
=100.000000000000000194

so you can see the result as above, the eth balance of the account2 has increased 97 as expected.
Maybe you also need to check the current eth balance of the contract.

2 Likes

Hi,
Thanks, i thought it should become 197.000000. How can I raise the balance to 197.000000. God blesses you.

But any way why its increasing the balance? testFunc(…) is not payable, why testFunc(…) is sending the Ether?
Please guide me.
Zulfi.

1 Like

Ohhh, I see, cause you defined in the contract as this: uint8 public testVal =97, so every time, the balance increases 97 wei, if you want to make it become 97 ether, I think you can define as following: uint256 public testVal = 97e18;.

Kind reminder, if you rewrite like above, 97e18 is equal to 97 eth, this is really a large mount of money!

1 Like

Hi,
Your idea is correct, it worked:

pragma solidity ^0.5.1;
   contract TransferTest{
      uint  public testVal =97 ether;//Note we can't use uint8 with 97 ether
      function testFunc(address payable addr) public returns (uint) {//when we use payable SC would receive Ether
         //addr.transfer(msg.value); We can't use msg.value, it would //give an error, msg.value can be used only if function is payable
         addr.transfer(testVal);
         return testVal;
      }     
      function  deposit() payable public{}
}

Zulfi.

1 Like