Solidity – Unable to Transfer Ether from Smart Contract to Another Address: Troubleshooting Tips

javascriptsoliditytruffleweb3js

I am using truffle and when we type
truffle develop it provides us with 10 address **

So, the problem I am facing is I have created 2 functions one to deposit ether and the other one to transfer the ether to another address **
You can view those 2 functions in .sol file

The deposit function is meant to take ether and store it into the smart contract
But the problem is with the transfer function as i am not able to access that stored ether and not able to transfer it to another truffle address

UPDATE: What i want is to access the stored ether and transfer it to another truffle address/account. Currently, I am getting no error. I have removed the "payable" Keyword from the transfer() function but the situation is still the same that–>>
Metamask transaction pop up comes up it clearly mentions that i want to transfer ether from player 1 to player 2 but when I confirm it does not sends any ether and no error is being displayed. And when i check transaction history it shows that only gas was sent to the player 2

Think of player 1 and player 2 as input address taking the address of accounts which truffle provides us

.sol

  mapping (address => uint256) public balances;

    event LogDeposit(address sender, uint amount);
    event LogTransfer(address sender, address to, uint amount);

    function deposit() payable returns(bool success) {

        balances[msg.sender] += msg.value;
        LogDeposit(msg.sender, msg.value);
        return true;
    }

    function transfer(address to, uint value) payable public returns(bool success) 
    {
        if(balances[msg.sender] < value) throw;
        balances[msg.sender] -= value;
        to.transfer(value);
        LogTransfer(msg.sender, to, value);
        return true;
    }

The transfer function when called from App.js gives me a metamask transaction pop to confirm the transaction (similar to when i call the deposit function)
But even after confirming it does not sends out any ether to the another address
App.js

OraclizeContract.deployed().then(function(instance) {

  console.log("Initializing");
  instance.deposit({from: fromAddress1, 
                    gas: 3000000,
                    value: web3.toWei(betAmount, 'ether')}) //betAmount is a input box and fetching its value into betamount variable and passing it over here
                               .then(function(v){
                                       console.log(v);
                                       console.log("Function Executed");

                                 });
                       }).then(function() {
                                              console.log("Testing");
                       }).catch(function(e) {
                                               console.log(e);
                       });

document.getElementById("transfer").addEventListener("submit", function(e){
      e.preventDefault();
       console.log("Initializing");
         contract.transfer(fromAddress2, 1,
              {gas: 3000000,
               from: fromAddress1},
                            function (error, result){ 
                                   if(!error){
                                     console.log(result);//transaction successful
                                    } else{
                                     console.log(error);//transaction failed
                                     }
         })})

This is best, I can do if you have any solution I would really appreciate it
or simply if you could provide me with the way through which i can store and release ether

enter image description here
enter image description here

Best Answer

You have hard-coded an amount of 1 (one wei!!!) when calling function transfer:

contract.transfer(fromAddress2, 1, ...

After transferring that amount, the difference will not show up when you view the balances on metamask, because the display-resolution of this application is 1 ether (10^18 wei).

And of course, because you've hard-coded that amount, your program completely ignores whatever amount you input from metamask.

Related Topic