-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
28,557 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ALCHEMY_API_KEY= | ||
ROPSTEN_PRIVATE_KEY= |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
node_modules | ||
.env | ||
|
||
#Hardhat files | ||
cache | ||
artifacts |
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
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); | ||
} | ||
} |
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
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); | ||
} | ||
} |
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
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}`], | ||
}, | ||
}, | ||
}; |
Oops, something went wrong.