0

I created a function to deposit and transfer ether and it was working great until i tried to deploy again and now the transfer function is not working anymore

No errors are being thrown, the transfer function is simply giving back transaction recipt without checking the amount stored anymore Before if i deposit like 1 ether and tried to transfer it was working But if i transfer it again then it threw error now it is not checking transfer function anymore

I have not slept for a day or so,.. it might be a simple mistake but i cant spot if out please if you can help as i need to show it today and it was working great a few hours before

solidity

 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;
        emit LogDeposit(msg.sender, msg.value);
        return true;
    }

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

App.js


    var betAmount = document.querySelector("#bet #betAmount").value;

    var contract = web3.eth.contract(OraclizeContract.abi).at('0xBe5e4Be8Bc4800d4Fcf3509dDcCcE3630D7699C7');

    console.log(contract)

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

      console.log("Initializing");
        instance.deposit({from: fromAddress2, 
                    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(fromAddress1, 1000000000000000000,
                {gas: 3000000,
                 from: fromAddress2},
                            function (error, result){ 
                                   if(!error){
                                     console.log(result);//transaction successful
                                    } else{
                                     console.log(error);//transaction failed
                                     }
         })});
})
}

I tried a lot to compile again, migrate, but its not working...

  • Your code is not incomplete and I'm unable to test it properly. In solidity you shouldn't use throw, it is deprecated. Your code compiles in remix and it works as expected (I'd recommend to use at least solc 0.5). Your js code references the variable contract and it is not clear where it comes from. – Ismael Jan 19 at 19:53
  • updated but it was working good with metamask i dont know why now it is not – raghav Jan 19 at 22:45
  • It is a strange situation. Both your solidity code and js appears to be right. Are you testing against a ganache, public testnet or private testnet? My guess is the problem is somewhere else, check if the address and abi are incorrect. Can you check the status in transaction receipt returned by calling transfer? Also transfer is a bad name because it already is part of solidity, try a different name. – Ismael Jan 20 at 2:29
  • address and abi are definately correct but the mapping is not enforced like it was working before dont know what went wrong that it is not working anymore. I am testing on private net – raghav Jan 20 at 4:58
  • I made some changes to html file and deploy again but the changes are not being reflected any idea? – raghav Jan 20 at 7:22
0

So my mapping was not working fine, I was using metamask and web3, I worked for 2 days but couldn't find anything But finally, I was able to fix it by adding add ethereum.enable() before calling ethereum.send

MetaMask introduced a Privacy Mode that requires dapps to ask permission to view users’ accounts

Here is my code

 window.addEventListener('load', function() {
  // Checking if Web3 has been injected by the browser (Mist/MetaMask)
  if (typeof web3 !== 'undefined') {
    ethereum.enable();
    console.warn("Using web3 detected from external source. If you find that your accounts don't appear or you have 0 MetaCoin, ensure you've configured that source properly. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask")
    // Use Mist/MetaMask's provider
    window.web3 = new Web3(web3.currentProvider);
  } else {
    console.warn("No web3 detected. Falling back to http://127.0.0.1:9545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");
    // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
    window.web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:9545"));
  }

Now my mapping finally works and truffle accounts can be accessed without any issues

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.