3

Can you guys please help me to understand how does a contract temporarily keeps a funds inside itself? I am looking the Solidity docs and tutorials and I see, that it is possible to keep a funds inside a contract for some time and then to send these funds to owner using the suicide() method.

I refer to this tutorial. According to the code example, the contract looks like this:

contract Conference { 

   address public organizer;
   mapping (address => uint) public registrantsPaid;
   uint public numRegistrants;

   // ...

   function buyTicket() public returns (bool success) { 
     if (numRegistrants >= quota) { 
         return false; 
     }
     else {
         registrantsPaid[msg.sender] = msg.value;
         numRegistrants++;
         return true;
     }
   }

   // ...

  function destroy() { // so funds not locked in contract forever
     if (msg.sender == organizer) {
        suicide(organizer); // send funds to organizer
     }
  }
}

As far as I understand from this example, the function buyTicket() should take some funds from a user calling this transaction and persist it somehow inside a blockchain as a stored data. But I don't see any payments here, I can only see that we put user's value and address inside the internal mapping, such as registrantsPaid[msg.sender] = msg.value;.

Can you please explain to me, what exactly happens in the function buyTicket()? How does the suicide(organizer) knows where all funds are stored inside a contract? Is there actual money are transferred form user to a contract inside the buyTicket() function? Where exactly the destroy() is calling?

Thank you very much. Sorry, for noobish question :/

3

When you call buyTicket, your funds are saved to the contract. registrantsPaid only records funds for every account that has called buyTicket. All funds are saved to the contract, because a contract is an account.

If you call the destroy function, the funds of the contract will be refunded to organizer account. You can get the balance of the contract by this.balance.

Your code has a bug , if numRegistrants >= quota is true, the user calling buyTicket will lose some funds, so you must refund him. Modified as follows:

pragma solidity ^0.4.13;

contract Conference { 

   address public organizer;
   mapping (address => uint) public registrantsPaid;
   uint public numRegistrants;
    uint quota = 2000;
   // ...

   function buyTicket() payable public returns (bool success) { 
        require(numRegistrants >= quota);
        registrantsPaid[msg.sender] = msg.value;
         numRegistrants++;
         return true;
   }

   // ...

  function destroy() { // so funds not locked in contract forever
     if (msg.sender == organizer) {
        suicide(organizer); // send funds to organizer
     }
  }

  function getContractBalance() constant returns (uint){
    return this.balance;    
  }

}

Hope it helps~

  • So, the rule is: when a customer calls a public function marked as payable, then the sum of money equals to msg.value are moved from his wallet to the contract wallet automatically? Am I right? Sorry, this piese of code is a bit confusing for me, because it is implicit. – w32blaster Aug 11 '17 at 9:56
  • And thank you for the code correction! I appreciate that! – w32blaster Aug 11 '17 at 9:57
  • Yes, this money will be stored to contract~ – BinGoBinBin Aug 11 '17 at 10:00
  • If this helps you, please accept this answer, thanks a lot~ – BinGoBinBin Aug 11 '17 at 10:08

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.