简体   繁体   中英

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 ie, 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";
    }
}

You can achieve the result from something called as Escrow contract.
Following is the implementation of Escrow contract by open-zeppelin :

contract Escrow is Secondary {
  using SafeMath for uint256;

  event Deposited(address indexed payee, uint256 weiAmount);
  event Withdrawn(address indexed payee, uint256 weiAmount);

  mapping(address => uint256) private _deposits;

  function depositsOf(address payee) public view returns (uint256) {
    return _deposits[payee];
  }

  /**
  * @dev Stores the sent amount as credit to be withdrawn.
  * @param payee The destination address of the funds.
  */
  function deposit(address payee) public onlyPrimary payable {
    uint256 amount = msg.value;
    _deposits[payee] = _deposits[payee].add(amount);

    emit Deposited(payee, amount);
  }

  /**
  * @dev Withdraw accumulated balance for a payee.
  * @param payee The address whose funds will be withdrawn and transferred to.
  */
  function withdraw(address payee) public onlyPrimary {
    uint256 payment = _deposits[payee];

    _deposits[payee] = 0;

    payee.transfer(payment);

    emit Withdrawn(payee, payment);
  }
}

You can just instantiate the contract in your contract and forward the funds to the contract.

For full implementation of similar functionality have a look at the refundable crowdsale contract

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM