我正在编写一个 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";
}
}
您可以通过称为托管合约的方式获得结果。
以下是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);
}
}
您只需在合约中实例化合约并将资金转入合约即可。
要完整实现类似功能,请查看可退款众售合同
谢谢你们的回答,但后来我知道随交易一起发送到合约的 VALUE 存储在合约本身中,您可以使用
address(this).balance
来访问它,它总是为您提供可用的余额在该合同的情况下。因此,您不需要任何变量或其他东西来将 ETHER 存储在您的合约中。
以太坊交易:
使用 sendTransactionWithParams 函数发送时,ETH 存储在合约中,该函数允许自定义参数。这些定制婴儿车可用于我们需要的多种用途。
为了限制访问,只允许合约所有者提取 ETH,而任何人都应该能够发送 ETH。
此外,合约应支持提取自定义金额的 ETH,而不是全部余额。
可靠性代码:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract TransactionWithParams {
event TransactionDetails(address indexed from, address indexed to, uint256 value, string customParam);
event Withdrawal(address indexed to, uint256 amount);
address public owner;
mapping(address => string) public customParams;
constructor() {
owner = msg.sender; // Set the contract deployer as the owner
}
modifier onlyOwner() {
require(msg.sender == owner, "Only the contract owner can perform this action");
_;
}
function sendTransactionWithParams(string memory customParam) public payable {
require(msg.value > 0, "No ETH sent");
customParams[msg.sender] = customParam;
emit TransactionDetails(msg.sender, address(this), msg.value, customParam);
}
function withdraw(uint256 amount) public onlyOwner {
uint256 contractBalance = address(this).balance;
require(amount > 0, "Withdrawal amount must be greater than 0");
require(amount <= contractBalance, "Insufficient contract balance");
payable(owner).transfer(amount);
emit Withdrawal(owner, amount);
}
function getCustomParam(address user) public view returns (string memory) {
return customParams[user];
}
function getContractBalance() public view returns (uint256) {
return address(this).balance;
}
}