Skip to content

Commit

Permalink
Auction contract
Browse files Browse the repository at this point in the history
  • Loading branch information
antico5 committed Sep 22, 2021
1 parent cc3b5ee commit eb19f81
Show file tree
Hide file tree
Showing 11 changed files with 28,557 additions and 0 deletions.
2 changes: 2 additions & 0 deletions auction/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALCHEMY_API_KEY=
ROPSTEN_PRIVATE_KEY=
6 changes: 6 additions & 0 deletions auction/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
.env

#Hardhat files
cache
artifacts
106 changes: 106 additions & 0 deletions auction/contracts/Auction.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.7;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/math/Math.sol";

contract Auction is Ownable {
uint256 constant BLOCKS_IN_A_WEEK = 40320;

enum State {
STARTED,
RUNNING,
ENDED,
CANCELED
}

uint256 public startBlock;
uint256 public endBlock;
string public ipfsHash;
State public state;
uint256 public highestBindingBid;
address payable public highestBidder;
mapping(address => uint256) public bids;
uint256 bidIncrement;

constructor(address owner) {
state = State.RUNNING;
startBlock = block.number;
endBlock = startBlock + BLOCKS_IN_A_WEEK;
ipfsHash = "";
bidIncrement = 100;
transferOwnership(owner);
}

modifier notOwner() {
require(msg.sender != owner(), "not callable by owner");
_;
}

modifier afterStart() {
require(block.number >= startBlock, "auction didnt start yet");
_;
}

modifier beforeEnd() {
require(block.number <= endBlock, "auction already ended");
_;
}

function cancel() public onlyOwner {
state = State.CANCELED;
}

function placeBid() public payable notOwner afterStart beforeEnd {
require(state == State.RUNNING, "auction is not running");
require(msg.value >= 100, "minimum bid is 100 wei");

uint256 currentSenderBid = bids[msg.sender] + msg.value;
require(currentSenderBid > highestBindingBid);

bids[msg.sender] = currentSenderBid;

uint256 currentHighestBid = bids[highestBidder];
if (currentSenderBid <= currentHighestBid) {
highestBindingBid = Math.min(
currentSenderBid + bidIncrement,
currentHighestBid
);
} else {
highestBindingBid = Math.min(
currentSenderBid,
currentHighestBid + bidIncrement
);
highestBidder = payable(msg.sender);
}
}

function finalize() public {
require(state == State.CANCELED || block.number > endBlock);
require(msg.sender == owner() || bids[msg.sender] > 0);

address payable recipient;
uint256 value;

if (state == State.CANCELED) {
recipient = payable(msg.sender);
value = bids[msg.sender];
} else {
if (msg.sender == payable(owner())) {
recipient = payable(owner());
value = highestBindingBid;
} else {
if (msg.sender == highestBidder) {
recipient = highestBidder;
value = bids[highestBidder] - highestBindingBid;
} else {
recipient = payable(msg.sender);
}
}
}

bids[recipient] = 0;
recipient.transfer(value);
}
}
15 changes: 15 additions & 0 deletions auction/contracts/AuctionCreator.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.7;

import "@openzeppelin/contracts/access/Ownable.sol";
import "./Auction.sol";

contract AuctionCreator is Ownable {
Auction[] public auctions;

function createAuction() public {
Auction auction = new Auction(msg.sender);
auctions.push(auction);
}
}
28 changes: 28 additions & 0 deletions auction/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { task } from "hardhat/config";
import "@nomiclabs/hardhat-waffle";

import dotenv from 'dotenv'
dotenv.config()

// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async (args, hre) => {
const accounts = await hre.ethers.getSigners();

for (const account of accounts) {
console.log(await account.address);
}
});

// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more

export default {
solidity: "0.8.7",
networks: {
ropsten: {
url: `https://eth-ropsten.alchemyapi.io/v2/${process.env.ALCHEMY_API_KEY}`,
accounts: [`0x${process.env.ROPSTEN_PRIVATE_KEY}`],
},
},
};
Loading

0 comments on commit eb19f81

Please sign in to comment.