Skip to content

Commit 5a2b4c1

Browse files
committed
Initial commit
1 parent ecc21d6 commit 5a2b4c1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+90685
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
blockchain
3+
node_modules

bs-config.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"server": {
3+
"baseDir": [
4+
"./src",
5+
"./build/contracts"
6+
],
7+
"routes": {
8+
"/vendor": "./node_modules"
9+
}
10+
}
11+
}

build/contracts/Auction.json

+1,325
Large diffs are not rendered by default.

build/contracts/AuctionHouse.json

+4,539
Large diffs are not rendered by default.

build/contracts/Bid.json

+1,497
Large diffs are not rendered by default.

build/contracts/DutchAuction.json

+7,830
Large diffs are not rendered by default.

build/contracts/FastStrategy.json

+2,568
Large diffs are not rendered by default.

build/contracts/GenerateBid.json

+1,709
Large diffs are not rendered by default.

build/contracts/Migrations.json

+1,416
Large diffs are not rendered by default.

build/contracts/NormalStrategy.json

+2,568
Large diffs are not rendered by default.

build/contracts/SlowStrategy.json

+2,568
Large diffs are not rendered by default.

build/contracts/Strategy.json

+2,550
Large diffs are not rendered by default.

build/contracts/VickreyAuction.json

+19,132
Large diffs are not rendered by default.

commands.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ganache-cli --db="/Users/andrea/Desktop/workspace/DAPP_SmartAuction/blockchain" -d --mnemonic="denial excite bind resemble wink tankequire husband spice adult battle large" -i="5777"
2+
npm run dev
3+
truffle migrate --reset --network test

contracts/Auction.sol

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
pragma solidity ^ 0.5.1;
2+
3+
/// @title Auction abstract contract
4+
/// @author Andrea Bruno 585457
5+
/// @notice This contract implements the shared functionalities between the Dutch and Vickrey auction.
6+
/// @dev The following comments are written using the Solidity NatSpec Format.
7+
8+
contract Auction {
9+
10+
/// @notice This struct incorporates the basic info about the auction.
11+
struct Description {
12+
string category;
13+
address payable house;
14+
address payable seller;
15+
string itemName;
16+
uint startBlock;
17+
address winnerAddress;
18+
uint winnerBid;
19+
}
20+
21+
/// @dev The description is public to allow people to read it without paying any gas.
22+
Description public description;
23+
24+
25+
/// @dev This modifier allow only the auction house to invoke the function.
26+
modifier onlyHouse() {
27+
require(msg.sender == description.house, "Only the auction house can run this function");
28+
_;
29+
}
30+
31+
/// @notice This event communicate the beginning of the auction.
32+
event auctionStarted();
33+
34+
/// @notice This event communicate the end of the auction
35+
/// @param winnerAddress is the address of the winner
36+
/// @param winnerBid is the final price that the winner has paid.
37+
/// @param surplusFounds is the eventual surplus left to the contract.
38+
event auctionFinished(address winnerAddress, uint winnerBid, uint surplusFounds);
39+
40+
41+
/// @notice This abstract function allow to activate the auction.
42+
/// @dev For security reason, it is better to equip this function with `onlySeller` modifier.
43+
function activateAuction() public;
44+
45+
46+
/// @notice This abstract function finalize and close the auction.
47+
/// @dev For security reason, it is better to equip this function with `onlySeller` modifier.
48+
function finalize() public;
49+
}

contracts/AuctionHouse.sol

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
pragma solidity ^ 0.5.1;
2+
3+
import "./DutchAuction.sol";
4+
import "./VickreyAuction.sol";
5+
6+
/// @title Auction House contract
7+
/// @author Andrea Bruno 585457
8+
9+
/// @notice This contract implements the shared functionalities between the Dutch and Vickrey auction.
10+
/// @dev The following comments are written using the Solidity NatSpec Format.
11+
12+
13+
contract AuctionHouse {
14+
15+
uint fees = 0.01 ether;
16+
17+
address[] public auctions;
18+
uint public num_auctions;
19+
20+
address payable houseOwner;
21+
22+
event newAuctionAvailable(string _type, address _address);
23+
24+
constructor() public {
25+
houseOwner = msg.sender;
26+
}
27+
28+
modifier onlyOwner {
29+
require(msg.sender == houseOwner);
30+
_;
31+
}
32+
33+
modifier payFees() {
34+
require(msg.value == fees, "Please send a correct value");
35+
_;
36+
}
37+
38+
function newDutch(
39+
string memory _itemName,
40+
uint _reservePrice,
41+
uint _initialPrice,
42+
Strategy _strategy
43+
) public payable payFees{
44+
DutchAuction dutch = new DutchAuction(houseOwner, msg.sender, _itemName, _reservePrice, _initialPrice, _strategy);
45+
auctions.push(address(dutch));
46+
num_auctions += 1;
47+
emit newAuctionAvailable("Dutch", address(dutch));
48+
}
49+
// ******** TEST **********
50+
// house = await AuctionHouse.deployed()
51+
// slow = await SlowStrategy.deployed()
52+
// accounts = await web3.eth.getAccounts()
53+
// house.newDutch('XXX',100,100, slow.address, {value: 10000000000000000, from: accounts[0]})
54+
55+
56+
57+
58+
function newVickrey(
59+
string memory _itemName,
60+
uint _reservePrice,
61+
uint _min_deposit,
62+
uint _commitment_len,
63+
uint _withdrawal_len,
64+
uint _opening_len
65+
) public payable payFees{
66+
VickreyAuction vickrey = new VickreyAuction(houseOwner, msg.sender, _itemName, _reservePrice, _min_deposit, _commitment_len, _withdrawal_len, _opening_len);
67+
auctions.push(address(vickrey));
68+
num_auctions += 1;
69+
emit newAuctionAvailable("Vickrey", address(vickrey));
70+
}
71+
// ******** TEST **********
72+
// house = await AuctionHouse.deployed()
73+
// accounts = await web3.eth.getAccounts()
74+
// house.newVickrey('aaa',100,100,10,10,10, {value: 10000000000000000, from: accounts[0]})
75+
76+
77+
function getFees () public onlyOwner {
78+
houseOwner.transfer(address(this).balance);
79+
}
80+
}

contracts/Bid.sol

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
pragma solidity ^ 0.5.1;
2+
3+
/// @title Bid contract
4+
/// @author Andrea Bruno 585457
5+
/// @notice This contract helps people in making bids. If deployed locally, no gas is required.
6+
/// @dev The following comments are written using the Solidity NatSpec Format.
7+
contract Bid {
8+
/// @notice This function generates the nonce and the hash needed in the Vickrey Auction.
9+
/// @param _bidValue is the desired bid.
10+
function generate(uint _bidValue) public view returns(uint, bytes32, bytes32) {
11+
uint value;
12+
bytes32 nonce;
13+
bytes32 hash;
14+
15+
value = _bidValue;
16+
nonce = keccak256(abi.encodePacked(block.timestamp));
17+
hash = keccak256(abi.encodePacked(_bidValue, nonce));
18+
19+
return (value, nonce, hash);
20+
}
21+
}

contracts/DutchAuction.sol

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
pragma solidity ^ 0.5.1;
2+
3+
import "./Auction.sol";
4+
import "./Strategy.sol";
5+
6+
/// @title Dutch Auction contract
7+
/// @author Andrea Bruno 585457
8+
/// @notice This contract implements the Dutch auction functionalities.
9+
/// @dev The following comments are written using the Solidity NatSpec Format.
10+
contract DutchAuction is Auction {
11+
12+
/// @dev This variable identifies the block number at the moment of contract deploy.
13+
uint creationBlock;
14+
15+
/// @dev This variable identifies the block number of the winner bid.
16+
uint winnerBlock;
17+
18+
/// @dev The reserve price decided by the seller.
19+
uint reservePrice;
20+
21+
/// @dev The initial price decided by the seller.
22+
uint initialPrice;
23+
24+
/// @dev The current price of the item.
25+
uint actualPrice;
26+
27+
/// @dev To decrease the price a strategy is needed. Check the contract `Strategy` to learn more.
28+
Strategy strategy;
29+
30+
/// @dev Those are the possible states of the auction.
31+
enum State {
32+
GracePeriod,
33+
Active,
34+
Validating,
35+
Finished
36+
}
37+
State public state;
38+
39+
/// @notice This event communicate that the auction is in validation.
40+
event auctionValidation();
41+
42+
/// @notice The (ex) constructor of the Dutch auction. See FACTORY PATTERN...
43+
/// @param _itemName is a short description of what is going to be sold.
44+
/// @param _reservePrice is the reserve price decided by the seller.
45+
/// @param _initialPrice is the initial price decided by the seller.
46+
/// @param _strategy the address of a `Strategy` contract.
47+
constructor (
48+
address payable _houseOwner,
49+
address payable _seller,
50+
string memory _itemName,
51+
uint _reservePrice,
52+
uint _initialPrice,
53+
Strategy _strategy
54+
) public{
55+
56+
require(_reservePrice > 0, "Reserve price should be greater than zero.");
57+
require(_initialPrice > _reservePrice, "The initial price should be greater than the reserve price");
58+
59+
// Set into the description the auction house address,
60+
description.house = _houseOwner;
61+
// the address of the seller,
62+
description.seller = _seller;
63+
// the name of the item
64+
description.itemName = _itemName;
65+
// and the category of the Auction
66+
description.category = "Dutch";
67+
68+
69+
//Set state into "grace period"
70+
state = State.GracePeriod;
71+
72+
reservePrice = _reservePrice;
73+
initialPrice = _initialPrice;
74+
actualPrice = _initialPrice;
75+
strategy = _strategy;
76+
77+
//Remember the block number at the moment of contract deploy.
78+
creationBlock = block.number;
79+
}
80+
81+
/// @notice This function will activate the auction.
82+
/// @dev Only the seller can invoke it.
83+
function activateAuction() public onlyHouse {
84+
85+
// In order to activate the auction we need to be in the "grace period"
86+
require(state == State.GracePeriod, "To activate the contract you must be in the Grace Period");
87+
88+
// and also 5 minutes (20 blocks) must be elapsed.
89+
require(block.number - creationBlock > 20, "Grace period is not finished yet");
90+
91+
// Set the current state to "Active".
92+
state = State.Active;
93+
94+
// Set into the description the starting block of the auction.
95+
description.startBlock = block.number;
96+
97+
// Communicate that the auction is started.
98+
emit auctionStarted();
99+
}
100+
101+
/// @notice This function will update the price.
102+
/// @dev Since it is public, the gas will be spent by the people willing to know the current price.
103+
/// @return The updated current price.
104+
function getActualPrice() public returns(uint) {
105+
106+
// Compute the delta between the block in which the auction is started and the actual one.
107+
uint deltaBlocks = description.startBlock - block.number;
108+
109+
// Let the strategy compute the price.
110+
uint tmp = strategy.getPrice(actualPrice, -deltaBlocks);
111+
112+
// If the price computed is smaller than the reserve price
113+
// set the actual price to the reserve price
114+
// otherwise everything is fine.
115+
if (tmp <= reservePrice) {
116+
actualPrice = reservePrice;
117+
} else {
118+
actualPrice = tmp;
119+
}
120+
121+
return actualPrice;
122+
}
123+
124+
/// @notice Use this function to make a bid.
125+
/// @dev The bidder should send a value greater or equal than the actual price.
126+
/// @dev Finally, the auction passes in a "validation" state, to avoid inconsistencies due to forks on the blockchain
127+
function bid() public payable {
128+
129+
// To maka a bid the auction need to be active.
130+
require(state == State.Active, "This contract is not active yet");
131+
132+
// The bidder should send an appropriate value.
133+
require(msg.value >= getActualPrice(), "The value sent is not sufficient");
134+
135+
description.winnerAddress = msg.sender;
136+
description.winnerBid = msg.value;
137+
138+
winnerBlock = block.number;
139+
140+
//Activate the validation
141+
validateAuction();
142+
}
143+
144+
145+
146+
/// @dev This function allow to pass into a "validation" state.
147+
function validateAuction() internal {
148+
require(state == State.Active, "You can't validate a contract before activating it");
149+
state = State.Validating;
150+
emit auctionValidation();
151+
}
152+
153+
154+
/// @notice The following function finalize the contract and send the ether to the seller of the item.
155+
/// @dev The security pattern Checks-Effects-Interaction is respected.
156+
function finalize() public onlyHouse {
157+
require(state == State.Validating, "You can't finalize a contract before validation");
158+
require(block.number - winnerBlock > 12, "For security reasons, you need to wait to validate the contract");
159+
160+
state = State.Finished;
161+
162+
// Emit the event on the blockchain
163+
emit auctionFinished(description.winnerAddress, description.winnerBid, address(this).balance);
164+
165+
// Transfer the money to the seller
166+
description.seller.transfer(description.winnerBid);
167+
}
168+
}

contracts/Migrations.sol

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pragma solidity >=0.4.21 <0.6.0;
2+
3+
contract Migrations {
4+
address public owner;
5+
uint public last_completed_migration;
6+
7+
constructor() public {
8+
owner = msg.sender;
9+
}
10+
11+
modifier restricted() {
12+
if (msg.sender == owner) _;
13+
}
14+
15+
function setCompleted(uint completed) public restricted {
16+
last_completed_migration = completed;
17+
}
18+
19+
function upgrade(address new_address) public restricted {
20+
Migrations upgraded = Migrations(new_address);
21+
upgraded.setCompleted(last_completed_migration);
22+
}
23+
}

0 commit comments

Comments
 (0)