0

I am trying to execute the following sender SC to send funds to the receiver SC below it:

// SPDX-License-Identifier: MIT
pragma solidity ^0.5.16;
contract OwnableWallet {//sender
address payable owner;
// called by the constructor
function initWallet(address payable _owner) public {
   owner = _owner; // any user can change owner
   // more setup
}            
// allows the owner to withdraw ether
function withdraw(uint _amount) public {
   if (msg.sender == owner) {
      owner.transfer(_amount);
  }
}
function() external payable{}
}

==Receiver Smart Contract

pragma solidity ^0.5.16;
contract TxUserWalletAtt {//receiver
   uint public  count;
   address owner;
   constructor() public {
       owner = msg.sender;
   }
   function() external payable  {  
  }
}

==Below is the Truffle script

$ truffle console
truffle(development)> const acc2= accounts[2]
undefined
truffle(development)> acc2bal = await web3.eth.getBalance(acc2)
undefined
truffle(development)> web3.utils.fromWei(acc2bal, "ether")
'44.997896'
truffle(development)> OW = await OwnableWallet.new()
undefined
truffle(development)> OWbal = await web3.eth.getBalance(OW.address)
undefined
truffle(development)> web3.utils.fromWei(OWbal, "ether")
'0'
truffle(development)> options = {from: acc2, to : OW.address, value: web3.utils.toWei('11', 'ether')}
{ from: '0xc7B89453Ba9902C4aD41f47c0324b36ccc347d2E',
 to: '0xA4d07c85aB3238Ed17F7f5311B9F4DF95b85f3b1',
 value: '11000000000000000000' }
truffle(development)> OW.sendTransaction(options)
{ tx:
  '0x6d64b18a4354828ee9d2e5fd785cee08fecb4c6415e1a70ecca4ec0bffe3d364',
 receipt:
  { transactionHash:
     '0x6d64b18a4354828ee9d2e5fd785cee08fecb4c6415e1a70ecca4ec0bffe3d364',
truffle(development)> OWbal = await web3.eth.getBalance(OW.address)
undefined
truffle(development)> web3.utils.fromWei(OWbal, "ether")
'11'

Sender has 11 Ether, now receiver

truffle(development)> TX = await TxUserWalletAtt.new()
undefined
truffle(development)> TXBal = await web3.eth.getBalance(TX.address)//
undefined
truffle(development)> web3.utils.fromWei(TXBal, "ether")
'0'
Executing initWallet(..) method of sender
truffle(development)> arg1 = TX.address
'0xF8E4d4d7254a6dA91Cc9A910B876123cA1A3A52f'
truffle(development)> await OW.initWallet(arg1)
{ tx:
  '0xabf5ed781799e6cdcb9bcc0864c6b5785c825d45158a7a8c6dc39115d2643e2b',
 receipt:
  { transactionHash:
     '0xabf5ed781799e6cdcb9bcc0864c6b5785c825d45158a7a8c6dc39115d2643e2b',

Now executing the transfer method withdraw(..)

  truffle(development)> arg2 = '6' //Note I tried with integer also
        '6'
        truffle(development)> await OW.withdraw(arg2)
        { tx:
          '0x83099adea1970040ee2252c622e121828d4320f79090c3a8129594feec7328f6',
         receipt:
          { transactionHash:
             '0x83099adea1970040ee2252c622e121828d4320f79090c3a8129594feec7328f6',
            transactionIndex: 0,

Now displaying the balance which are same as before sending:

truffle(development)> TXBal = await web3.eth.getBalance(TX.address)
undefined
truffle(development)> web3.utils.fromWei(TXBal, "ether")
'0'
truffle(development)> OWbal = await web3.eth.getBalance(OW.address)
undefined
truffle(development)> web3.utils.fromWei(OWbal, "ether")
'11'
truffle(development)> arg2 = 6//Againwith integer value
6
truffle(development)> await OW.withdraw(arg2)
{ tx:
  '0x62fa25a4581960cc98bb415e9178cd7d485aa8722fb09f74999fb1b5f825eea3',
 receipt:
  { transactionHash:
     '0x62fa25a4581960cc98bb415e9178cd7d485aa8722fb09f74999fb1b5f825eea3',
    transactionIndex: 0,

Now displaying the balances:

truffle(development)> OWbal = await web3.eth.getBalance(OW.address)
undefined
truffle(development)> web3.utils.fromWei(OWbal, "ether")
'11'
truffle(development)> 
truffle(development)> attBal = await web3.eth.getBalance(att.address)
undefined
truffle(development)> web3.utils.fromWei(attBal, "ether")
'0'

Somebody guide why the Ether is not received by the receiver and why the victim balance is the same after transfer. Zulfi.

1 Answer 1

0

Owner should be a payable address in your case.

I suggest you use the receive function to allow the contracts to receive ether and update your solidity version.

You should call the recipient with a message

owner().call{value: _amount}("")

//or

payable(owner).transfer(_amount);

receive() external payable{
        // this contract can receive ether
    }
1
  • Hi, I am using version 5 which does not use receive. Tell me a version 5-related solution. Zulfi.
    – zak100
    Dec 18, 2022 at 3:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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