我正在编写LibraryPortal智能合约,多个用户可以在其中相互租借他们的书。因此,在此合同中,msg.value
包含了保证金和租金率的总和。
我要做的就是立即将租金金额转移给图书的所有者,并将剩余的金额存储在合同中,即保证金。
如果承租人不会在指定的时间内归还该书,则保证金将转移给该书的所有者,否则将退还给承租人。
这是我的摘录:
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";
}
}
答案 0 :(得分:0)
您可以通过称为托管合同的结果来实现结果。
以下是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);
}
}
您只需实例化合同中的合同,然后将资金转发给合同即可。
要完全实现类似功能,请查看refundable crowdsale contract
答案 1 :(得分:0)
谢谢大家的回答,但是,后来我才知道,与交易一起发送到合同的VALUE存储在合同本身中,并且您可以使用address(this).balance
来访问它,它将始终显示您在该合同实例中可以使用的余额。因此,您不需要任何变量或其他东西即可在合同中存储ETHER。