ethereumsolidity

How to store ETH in the Smart Contract?


I'm writing a LibraryPortal Smart Contract in which multiple users can rent their books to each other. So, in this contract the msg.value contain the total amount which is a combination of the Security Deposit & the Renting Rate.

What I have to do is transfer the Renting amount to the owner of the book instantly & store the remaining amount in the contract i.e., the Security Deposit.

If the Renter will not return the book with in the time specified then the security amount will be transfered to the Owner of the book, otherwise get returned to the Renter.

Here is my snippet:

function borrowBook(string _bName) payable returns (string){
    if(msg.sender != books[_bName].owner){
        if(books[_bName].available == true){
            if(getBalance()>=(books[_bName].amtSecurity + books[_bName].rate) ){
                books[_bName].borrower = msg.sender;
                books[_bName].available = false;
                books[_bName].owner.transfer(msg.value - books[_bName].amtSecurity);
                //  Code missing
                //  For storing
                //  ETH into the Contact
                return "Borrowed Succesful";
            }else{
                return "Insufficient Fund";
            }
        }else{
            return "Currently this Book is Not Available!";
        }
    }else{
        return "You cannot Borrow your own Book";
    }
}

Solution

  • Thank you guys for answers but, later I came to know that the VALUE sent along with the transaction to the contract is stored in the Contract itself, & you can access that by using the address(this).balance which will always gives you the Balance available in that instance of the contract. Due to this you don't require any variable or other stuff to store ETHER in your contract.