0

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

  • Fix coding indentation please. – goodvibration Jan 19 at 9:31
  • "The transfer function when called from App.js gives me a metamask transaction pop to confirm the transaction" - That's because you're declaring this function payable, which you shouldn't (I mean, you don't need to, since this function is not designated to receive ether like the Deposit function is). – goodvibration Jan 19 at 9:34
  • "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 .sol" - please fix up this paragraph, and then explain what you mean by "not able". For example, do you get an error? If yes, what exactly is the error message? – goodvibration Jan 19 at 9:39
  • if you really know anything or could dm me i would really appreciate from a week i have been posting if you could help me to make my contract work it would really make me understand where i am going wrong and why cant i proceed further. – raghav Jan 19 at 10:14
  • I have read a lot on the internet that we can send ether to contract address (like an escrow) but can't find how to build it with web3 or front end. – raghav Jan 19 at 10:15
0

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.

  • If I don't specify it, This gives back an error Error: Invalid number of arguments to Solidity function. Now i am really confused i thought this would work but how can i specify argument then? – raghav Jan 19 at 11:33
  • @raghav: Of course, you need to pass to function transfer some value, not ignore it!!! But did you even bother to read my answer??? You are passing a value of 1 wei instead of the value that you input to your program from the outside. And 1 wei is just way to small for you to notice the difference when you view the balances on metamask. I have a guts feeling that you haven't learned the basic of programming to begin with... Am I right? You might wanna start with that before trying to implement complicated applications. – goodvibration Jan 19 at 11:40
  • actually i replied because i tried to pass contract.transfer(fromAddress2, 1000000000000000000, this too but even after that the situation is same as screenshort no 2 it still shows 0 ETH – raghav Jan 19 at 11:47
  • yeah i previously developed basic android applications thats it, i accept i took a project which i am not able to code but if i am getting errors i dont know what to do. sorry for any trouble please be calm. If you dont want to look into this anymore i dont mind. thank you for your replies – raghav Jan 19 at 11:49
  • Please look at the last 2 screenshots ----->> When i click on edit i view 0 ETH – raghav Jan 19 at 11:59
0

First of all, found too many bugs in the contract. i would suggest you use remix to solve your bugs first. 'throw' is deprecated use require.

Second. It is best that when you contract.transfer just transfer the balances from payer1 and player2 and let player 2 be notified of the deposit and he can now withdraw the money from contract. This is best practice of sorts in the ethereum world.

I put the contract in the ropsten network and did the transactions find it in the below link https://ropsten.etherscan.io/address/0xf70e4dc3e3eada4c6c8a53985aabcf4809ff04b1

bug free Contract:

pragma solidity ^0.6;

contract New{ mapping (address => uint256) balances;

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

function deposit() public payable returns(bool success) {

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

function transfer(address payable to, uint value) public returns(bool success) 
{
    require( value <= balances[msg.sender],"Not enough balance") ;
    balances[msg.sender] -= value;
    to.transfer(value);
    emit LogTransfer(msg.sender, to, value);
    return true;
}

function getBalance() public view returns(uint) {
    return balances[msg.sender];
}

}

  • Also do not use contracts to send money between players, that would be waste of gas !!. Let the player2 withdraw as per his choice. – Sudeep Sagar Jan 19 at 11:42
  • Actually i want to build a betting contract and i am only building this part over here rest all i have already developed. Was not able to find good tutorials so i started with an old version (I know that i should have not done that) But, now the problem is i need to make it with fron end and i think i am not calling it the correct way through web3 – raghav Jan 19 at 11:56
  • could you please make it work with metamask, web3? – raghav Jan 19 at 12:22

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.