π§ΎImplementing Transactions with Smart Contracts
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract BitEstateTransaction {
address public contractOwner;
address public buyer;
address public seller;
enum ContractState { InProgress, Completed, Cancelled }
ContractState public state;
struct Property {
string propertyId;
string propertyDetails;
uint256 price;
bool isListed;
}
Property public property;
event PropertyListed(string propertyId, uint256 price);
event OfferSubmitted(address buyer, uint256 offerAmount);
event OfferAccepted(address buyer, uint256 offerAmount);
event TransactionCompleted(address buyer, address seller, uint256 finalPrice);
modifier onlyOwner() {
require(msg.sender == contractOwner, "Only the contract owner can call this function");
_;
}
modifier onlyBuyer() {
require(msg.sender == buyer, "Only the buyer can call this function");
_;
}
modifier onlySeller() {
require(msg.sender == seller, "Only the seller can call this function");
_;
}
modifier inProgress() {
require(state == ContractState.InProgress, "Transaction is not in progress");
_;
}
constructor() {
contractOwner = msg.sender;
state = ContractState.InProgress;
}
function listProperty(string memory _propertyId, string memory _propertyDetails, uint256 _price) external onlyOwner inProgress {
property.propertyId = _propertyId;
property.propertyDetails = _propertyDetails;
property.price = _price;
property.isListed = true;
emit PropertyListed(_propertyId, _price);
}
function submitOffer() external payable inProgress {
require(property.isListed, "Property is not listed");
require(msg.sender != seller, "Seller cannot submit an offer");
emit OfferSubmitted(msg.sender, msg.value);
}
function acceptOffer() external onlyOwner inProgress {
require(property.isListed, "Property is not listed");
buyer = msg.sender;
seller = payable(msg.sender);
state = ContractState.Completed;
emit OfferAccepted(buyer, property.price);
// Transfer funds to the seller
seller.transfer(property.price);
}
function cancelTransaction() external onlyOwner {
require(state == ContractState.InProgress, "Transaction is not in progress");
state = ContractState.Cancelled;
}
function completeTransaction() external onlyOwner inProgress {
require(buyer != address(0), "No valid buyer");
state = ContractState.Completed;
emit TransactionCompleted(buyer, seller, property.price);
// Transfer funds to the seller
seller.transfer(property.price);
}
}
Last updated