Last active
April 3, 2023 07:32
-
-
Save snowden-fu/c3f7434e12ca0b4c4f0a07b3cfdf37d0 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.18+commit.87f61d96.js&optimize=false&runs=200&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.7.0 <0.9.0; | |
contract Auction { | |
address public owner; | |
// buyer list | |
address[] public buyerList; | |
// seller list | |
address[] public sellerList; | |
// is buyer | |
bool public isBuyer; | |
mapping(address => uint256) public addressToAmountDeposite; | |
// mapping of trader | |
mapping(address => Trader) public addressToTrader; | |
// mapping of bid | |
mapping(address => Bid) public addressToBid; | |
// mapping of offer | |
mapping(address => Offer) public addressToOffer; | |
// mapping of address to bool (is buyer) | |
mapping(address => bool) public IsBuyer; | |
// combine of buyer and seller as trader | |
struct Trader { | |
address traderAddress; | |
uint256 deposite; | |
} | |
// struct of bid | |
struct Bid { | |
uint256 bidAmount; | |
// price of bid | |
uint256 bidPrice; | |
} | |
// struct of offer | |
struct Offer { | |
uint256 offerAmount; | |
// price of offer | |
uint256 offerPrice; | |
} | |
constructor() { | |
owner = msg.sender; // 'msg.sender' is sender of current call, contract deployer for a constructor | |
} | |
/** | |
buyer deposit money to contract | |
buyer then will be added to buyer list | |
*/ | |
function buyerDeposite() public payable { | |
addressToAmountDeposite[msg.sender] += msg.value; | |
// add buyer to buyer list | |
buyerList.push(msg.sender); | |
// if isbuyer mapping not found, add to mapping | |
if (!IsBuyer[msg.sender]) { | |
IsBuyer[msg.sender] = true; | |
} | |
} | |
/** | |
seller deposit money to contract | |
seller then will be added to seller list | |
*/ | |
function sellerDeposite() public payable { | |
addressToAmountDeposite[msg.sender] += msg.value; | |
// add seller to seller list | |
sellerList.push(msg.sender); | |
} | |
/** | |
buyer withdraw money to original account | |
*/ | |
function buyerWithdraw() public { | |
// check whther caller is buyer | |
require(IsBuyer[msg.sender], "Caller is not buyer"); | |
uint256 buyerBalance = addressToAmountDeposite[msg.sender]; | |
removeBuyerListElementByAddress(msg.sender); | |
// set isbuyer mapping to false | |
IsBuyer[msg.sender] = false; | |
(bool callSuccess, ) = payable(msg.sender).call{ | |
value: buyerBalance | |
}(""); | |
require(callSuccess, "Call failed"); | |
} | |
/** | |
seller withdraw money to original account | |
*/ | |
function sellerWithdraw() public { | |
// check whther caller is buyer | |
require(!IsBuyer[msg.sender], "Caller is not seller"); | |
uint256 sellerBalance = addressToAmountDeposite[msg.sender]; | |
removeSellerListElementByAddress(msg.sender); | |
(bool callSuccess, ) = payable(msg.sender).call{ | |
value: sellerBalance | |
}(""); | |
require(callSuccess, "Call failed"); | |
} | |
function removeBuyerListElementByAddress(address addr) public { | |
// check whther caller is buyer | |
require(IsBuyer[msg.sender], "Caller address is not found in buyer list"); | |
uint index; | |
for (uint i = 0; i < buyerList.length; i++) { | |
if (buyerList[i] == addr) { | |
index = i; | |
break; | |
} | |
} | |
if (index < buyerList.length - 1) { | |
buyerList[index] = buyerList[buyerList.length - 1]; | |
} | |
buyerList.pop(); | |
} | |
function removeSellerListElementByAddress(address addr) public { | |
// check whther caller is buyer | |
require(!IsBuyer[msg.sender], "Caller address is not found in seller list"); | |
uint index; | |
for (uint i = 0; i < sellerList.length; i++) { | |
if (sellerList[i] == addr) { | |
index = i; | |
break; | |
} | |
} | |
if (index < sellerList.length - 1) { | |
sellerList[index] = sellerList[sellerList.length - 1]; | |
} | |
sellerList.pop(); | |
} | |
// get sellerlist | |
function getSellerList() public view returns (address[] memory) { | |
return sellerList; | |
} | |
// get buyerlist | |
function getBuyerList() public view returns (address[] memory) { | |
return buyerList; | |
} | |
// is price match | |
function isPriceMatch(uint256 bidPrice, uint256 offerPrice) | |
public | |
pure | |
returns (bool) | |
{ | |
return bidPrice >= offerPrice; | |
} | |
// buyer bid | |
function buyerBid(uint256 bidAmount, uint256 bidPrice) public { | |
// check whther caller is buyer | |
require(IsBuyer[msg.sender], "Caller is not buyer"); | |
// create new bid | |
Bid memory newBid = Bid(bidAmount, bidPrice); | |
// add bid to mapping | |
addressToBid[msg.sender] = newBid; | |
} | |
// seller offer | |
function sellerOffer(uint256 offerAmount, uint256 offerPrice) public { | |
// check whther caller is buyer | |
require(!IsBuyer[msg.sender], "Caller is not seller"); | |
// create new offer | |
Offer memory newOffer = Offer(offerAmount, offerPrice); | |
// add offer to mapping | |
addressToOffer[msg.sender] = newOffer; | |
} | |
// get bid by address | |
function getBidByAddress(address addr) public view returns (Bid memory) { | |
return addressToBid[addr]; | |
} | |
// get offer by address | |
function getOfferByAddress(address addr) | |
public | |
view | |
returns (Offer memory) | |
{ | |
return addressToOffer[addr]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment