如何在智能合约中存储 ETH?
How to store ETH in the Smart Contract?
我正在编写一个 LibraryPortal 智能合约,多个用户可以在其中相互租借他们的书籍。因此,在此合同中,msg.value
包含总金额,即保证金和租金的组合。
我要做的是立即将租金转给图书所有者,并将剩余金额存储在合同中,即保证金。
如果承租人未在指定时间内 return 图书,则保证金将转给图书所有者,否则 return 给承租人。
这是我的片段:
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";
}
}
您可以通过称为托管合同的东西获得结果。
以下是open-zeppelin对Escrow合约的实现:
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
谢谢你们的回答,但后来我才知道随交易一起发送到合约的 VALUE 存储在合约本身中,您可以使用 address(this).balance
访问它始终为您提供合同实例中可用的余额。因此,您不需要任何变量或其他东西来将 ETHER 存储在您的合约中。
我正在编写一个 LibraryPortal 智能合约,多个用户可以在其中相互租借他们的书籍。因此,在此合同中,msg.value
包含总金额,即保证金和租金的组合。
我要做的是立即将租金转给图书所有者,并将剩余金额存储在合同中,即保证金。
如果承租人未在指定时间内 return 图书,则保证金将转给图书所有者,否则 return 给承租人。
这是我的片段:
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";
}
}
您可以通过称为托管合同的东西获得结果。
以下是open-zeppelin对Escrow合约的实现:
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
谢谢你们的回答,但后来我才知道随交易一起发送到合约的 VALUE 存储在合约本身中,您可以使用 address(this).balance
访问它始终为您提供合同实例中可用的余额。因此,您不需要任何变量或其他东西来将 ETHER 存储在您的合约中。