Skip to content

Commit

Permalink
English Auction
Browse files Browse the repository at this point in the history
  • Loading branch information
antico5 committed Jan 6, 2022
1 parent 9581779 commit b768ac9
Show file tree
Hide file tree
Showing 11 changed files with 45,050 additions and 10 deletions.
20 changes: 10 additions & 10 deletions bidirectional_payment_channel/contracts/PaymentChannel.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
contract PaymentChannel {
using ECDSA for bytes32;

event ChallengedExit(address indexed sender, uint256 nonce);
event BalancesUpdated(address indexed sender, uint256 nonce);
event Withdrawn(address indexed to, uint256 amount);

address payable[2] public users;
mapping(address => bool) public isUser;

mapping(address => uint256) public balances;

uint256 public immutable challengeTime;
uint256 public immutable lockTime;
uint256 public expiresAt;
uint256 public nonce;

Expand All @@ -27,13 +27,13 @@ contract PaymentChannel {
address payable[2] memory _users,
uint256[2] memory _balances,
uint256 _expiresAt,
uint256 _challengeTime
uint256 _lockTime
) payable checkBalances(_balances) {
require(_expiresAt > block.timestamp);
require(_challengeTime > 0);
require(_lockTime > 0);

expiresAt = _expiresAt;
challengeTime = _challengeTime;
lockTime = _lockTime;

for (uint256 i = 0; i < _users.length; i++) {
address payable user = _users[i];
Expand Down Expand Up @@ -91,26 +91,26 @@ contract PaymentChannel {
_;
}

function challengeExit(
function updateBalances(
uint256[2] memory _balances,
uint256 _nonce,
bytes[2] memory _signatures
) public onlyUser checkSignatures(_signatures, _balances, _nonce) checkBalances(_balances) {
require(block.timestamp < expiresAt, "challenge period expired");
require(block.timestamp < expiresAt, "lock period expired");
require(_nonce > nonce, "nonce must be greater than the current nonce");

for (uint256 i = 0; i < _balances.length; i++) {
balances[users[i]] = _balances[i];
}

nonce = _nonce;
expiresAt = block.timestamp + challengeTime;
expiresAt = block.timestamp + lockTime;

emit ChallengedExit(msg.sender, nonce);
emit BalancesUpdated(msg.sender, nonce);
}

function withdraw() public onlyUser {
require(block.timestamp >= expiresAt, "Challenge period not expired yet");
require(block.timestamp >= expiresAt, "Lock period not expired yet");

uint256 amount = balances[msg.sender];
balances[msg.sender] = 0;
Expand Down
2 changes: 2 additions & 0 deletions english_auction/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALCHEMY_API_KEY=
ROPSTEN_PRIVATE_KEY=
8 changes: 8 additions & 0 deletions english_auction/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
node_modules
.env

#Hardhat files
cache
artifacts
dist
typechain
16 changes: 16 additions & 0 deletions english_auction/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"overrides": [
{
"files": "*.sol",
"options": {
"printWidth": 120,
"tabWidth": 4,
"useTabs": false,
"singleQuote": false,
"bracketSpacing": false,
"explicitTypes": "always",
"semi": true
}
}
]
}
104 changes: 104 additions & 0 deletions english_auction/contracts/EnglishAuction.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.7;

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

interface IERC721 {
function safeTransferFrom(
address from,
address to,
uint256 tokenId
) external;

function transferFrom(
address from,
address to,
uint256 tokenId
) external;
}

contract EnglishAuction is Ownable {
event Started();
event Bid(address indexed bidder, uint256 amount);
event Withdrawn(address indexed bidder, uint256 amount);
event Ended(address winner, uint256 amount);

IERC721 public immutable nft;
uint256 public immutable tokenId;

uint256 public endAt;
bool public started;
bool public ended;

address public highestBidder;
uint256 public highestBid;

mapping(address => uint256) public bids;

constructor(
address _nft,
uint256 _tokenId,
uint256 _startingBid
) {
require(_nft != address(0), "!nft");

nft = IERC721(_nft);
tokenId = _tokenId;
highestBid = _startingBid;
}

function start(uint256 duration) external onlyOwner {
require(!started, "already started");
require(duration > 0, "duration > 0");

nft.transferFrom(owner(), address(this), tokenId);
started = true;
endAt = block.timestamp + duration;

emit Started();
}

function bid() external payable {
require(started, "!started");
require(block.timestamp < endAt, "sale already ended");

uint256 newBid = bids[msg.sender] + msg.value;

require(newBid > highestBid, "bid too low");

highestBidder = msg.sender;
highestBid = newBid;

bids[msg.sender] = newBid;

emit Bid(msg.sender, newBid);
}

function withdraw() external {
uint256 userBid = bids[msg.sender];
require(highestBidder != msg.sender, "highest bidder cant withdraw");
require(userBid > 0, "user has no bids");

bids[msg.sender] = 0;
payable(msg.sender).transfer(userBid);

emit Withdrawn(msg.sender, userBid);
}

function end() external {
require(started, "!started");
require(!ended, "already ended");
require(block.timestamp > endAt, "auction didnt end yet");

ended = true;

if (highestBidder != address(0)) {
payable(owner()).transfer(highestBid);
nft.transferFrom(address(this), highestBidder, tokenId);
} else {
nft.transferFrom(address(this), owner(), tokenId);
}

emit Ended(highestBidder, highestBid);
}
}
56 changes: 56 additions & 0 deletions english_auction/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { config as dotenvConfig } from "dotenv";
import "@typechain/hardhat";
import "@nomiclabs/hardhat-ethers";
import "@nomiclabs/hardhat-waffle";
import "@nomiclabs/hardhat-etherscan";
import "solidity-coverage";
import "hardhat-gas-reporter";
import { HardhatUserConfig } from "hardhat/types/config";

dotenvConfig();

const gasPrice = parseInt(process.env.GAS_PRICE || "1000000000");

const config: HardhatUserConfig = {
solidity: {
compilers: [
{
version: "0.8.10",
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
],
},
networks: {
hardhat: {
initialBaseFeePerGas: 0,
},
rinkeby: {
url: process.env.RINKEBY_URL || "",
accounts:
process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
gasPrice,
},
mainnet: {
url: process.env.MAINNET_URL || "",
accounts:
process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
gasPrice,
},
},
gasReporter: {
enabled: process.env.REPORT_GAS !== undefined,
currency: "USD",
gasPrice: 120,
coinmarketcap: process.env.COINMARKETCAP_API_KEY,
},
etherscan: {
apiKey: process.env.ETHERSCAN_API_KEY,
},
};

export default config;
Loading

0 comments on commit b768ac9

Please sign in to comment.