Skip to content

Instantly share code, notes, and snippets.

@trentrand
Created August 26, 2021 14:33
Show Gist options
  • Save trentrand/d84b228d6685f84d9646bfce5157ae9b to your computer and use it in GitHub Desktop.
Save trentrand/d84b228d6685f84d9646bfce5157ae9b to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.4+commit.c7e474f2.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
contract ItemTransaction {
address public owner;
struct Buyer {
uint depositAmount;
bool isReported;
bool exists;
}
mapping(address => Buyer) public buyers;
address payable[] public buyersList;
enum Status {
UNPAID,
ACTIVE,
SOLD,
REPORTED
}
Status public status;
string public identifier;
uint askingPrice;
/* Seller methods */
constructor(string memory _identifier, uint _askingPrice) payable {
owner = msg.sender;
status = Status.UNPAID;
identifier = identifier;
askingPrice = _askingPrice;
}
function listItemForSale() public payable {
require(msg.sender == owner);
require(status == Status.UNPAID);
require(msg.value >= getSellerDepositCost());
status = Status.ACTIVE;
}
function getSellerDepositCost() public view returns(uint){
return askingPrice / 10;
}
function yieldBuyerDeposits() private {
require(msg.sender == owner);
for (uint i = 0; i < buyersList.length; i++) {
address payable buyerAddress = buyersList[i];
if (!buyers[buyerAddress].isReported) {
buyerAddress.transfer(buyers[buyerAddress].depositAmount);
}
}
status = Status.SOLD;
}
function confirmTransaction(address buyer) public {
require(msg.sender == owner);
require(status != Status.REPORTED);
yieldBuyerDeposits();
status = Status.SOLD;
}
function destroy() payable public {
require(msg.sender == owner);
require(status == Status.ACTIVE);
yieldBuyerDeposits();
selfdestruct(payable(owner));
}
function reportBuyer(address offendingBuyer) public {
require(msg.sender == owner);
buyers[offendingBuyer].isReported = true;
}
/* Buyer methods */
function contactSellerToTransact() public payable {
require(msg.sender != owner);
require(status != Status.REPORTED && status != Status.SOLD);
require(buyers[msg.sender].depositAmount >= getBuyerDepositCost() || msg.value >= getBuyerDepositCost());
buyersList.push(payable(msg.sender));
buyers[msg.sender] = Buyer({
depositAmount: msg.value,
isReported: false,
exists: true
});
}
function getBuyerDepositCost() public view returns(uint){
return askingPrice / 100;
}
function confirmSuccessfulTransaction() public {
require(msg.sender != owner);
require(buyers[msg.sender].exists);
yieldSellerDeposit();
}
function yieldSellerDeposit() private {
require(status != Status.REPORTED);
address payable sellerAddress = payable(owner);
sellerAddress.transfer(getSellerDepositCost());
}
function reportSeller() public {
require(msg.sender != owner);
require(status == Status.ACTIVE);
status = Status.REPORTED;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment