Solidity – How Are Funds Stored Inside a Contract?

contract-developmentsolidity

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 :/

Best Answer

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~

Related Topic