NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

pragma solidity ^0.4.17;

contract Auction {

// Data

// Structure to hold details of Bidder
struct IBidder {
uint8 token;
uint8 deposit;
}

// Structure to hold details of Rule
struct IRule {
uint8 startingPrice;
uint8 minimumStep;
}


// State Enum to define Auction's state
enum State { CREATED, STARTED, CLOSING, CLOSED }

State public state = State.CREATED; // State of Auction

uint8 public announcementTimes = 0; // Number of announcements
uint8 public currentPrice = 0; // Latest price is bid.
IRule public rule; // Rule of this session
address public currentWinner; // Current Winner, who bid the highest price.
address public auctioneer;

uint16 private totalDeposit = 0;

mapping(address => IBidder) public bidders; // Mapping to hold bidders' information
// mapping với key là địa chỉ của thằng đấu giá, value là struct của thằng đấu giá ( thông tin cá nhân của thằng đấu giá)

function Auction(uint8 _startingPrice, uint8 _minimumStep) public {

// Task #1 - Initialize the Smart contract // khởi động Smart Contract
// + Initialize Auctioneer with address of smart contract's owner
// khởi tạo thằng Auctioneer với địa chỉ của smart contract (owner)
// + Set the starting price & minimumStep
// Đặt giá khởi điểm và các bước tối thiểu.
// + Set the current price as the Starting price
// đặt giá hiện tại làm giá khởi điểm (current price = starting price)


// ** Start code here. 4 lines approximately. ** /
auctioneer = msg.sender;
rule.startingPrice = _startingPrice;
rule.minimumStep = _minimumStep;
currentPrice = _startingPrice;

// ** End code here. ** /
}

// đăng kí thằng đấu giá
// Register new Bidder
// truyền vào địa chỉ của account, số token mà có.
function register(address _account, uint8 _token) public onlyAuctioneer validStage(State.CREATED) {


// Task #2 - Register the bidder
// Task 2: đăng kí thằng đấu thầu.
// + Initialize a Bidder with address and token are given.
// Khởi tạo Thằng đấu thầu với địa chỉ và token đã được cung cấp.
// + Initialize a Bidder's deposit with 0
// Khởi tạo khoản tiền gửi của nhà thầu bằng 0.
bidders[_account].token = _token;
bidders[_account].deposit = 0;

// ** Start code here. 3 lines approximately. ** /

// ** End code here. **/
}


// Start the session.
// bắt đầu phiên đấu giá -> chuyển state sang trạng thái bắt đầu
function startSession() public onlyAuctioneer validStage(State.CREATED) {
state = State.STARTED;
}


// xử lý phiên đấu giá.
// truyền vào giá.
function bid(uint8 _price) public validStage(State.STARTED) {

// Task #3 - Bid by Bidders
// + Check the price with currentPirce and minimumStep. Revert if invalid.
// + Check if the Bidder has enough token to bid. Revert if invalid.
// + Move token to Deposit.

address bidderAddr = msg.sender;
IBidder storage currentBidder = bidders[bidderAddr];

// ** Start code here. ** /
require(_price >= (currentPrice + rule.minimumStep));
require(currentBidder.token >= _price);
currentBidder.deposit += _price;
currentBidder.token = currentBidder.token - _price;

// ** End code here. **/

// Tracking deposit
totalDeposit += _price;

// Update the price and the winner after this bid.
currentPrice = _price;
currentWinner = bidderAddr;

// Reset the Annoucements Counter
announcementTimes = 0;
}

//thông báo
function announce() public onlyAuctioneer validStage(State.STARTED) {

// Task #4 - Handle announcement.
// Task 4: Xử lý thông báo
// + When Auctioneer annouce, increase the counter.
// Khi mà thằng chủ thầu thông báo, sẽ tăng bộ đếm lên
// + When Auctioneer annouced more than 3 times, switch session to Closing state.
// Khi người bán đấu giá thông báo quá 3 lần, chuyển sang phiên là (Đóng)

// ** Start code here. ** /
announcementTimes++;
if(announcementTimes > 3){
state = State.CLOSING;
}

// ** End code here. **/
}

// Lấy ra giá Deposit;
function getDeposit() public validStage(State.CLOSING) exceptWinner {

// Task #5 - Handle get Deposit.
// Task 5: Xử lý nhận tiền gửi
// + Allow bidders (except Winner) to withdraw their deposit
// CHo phép các người đặt thầu còn lại ( đ phải người chiến thắng) thì sẽ nhận lại deposit
// + When all bidders' deposit are withdrew, close the session
// Khi tất cả tiền cọc của nhà thầu được nhân về, sẽ đóng phiên

// ** Start code here. ** /
// HINT: Remember to decrease totalDeposit.
// Ghi nhớ: Phải giảm totalDeposit;

bidders[msg.sender].token += bidders[msg.sender].deposit;
totalDeposit -= bidders[msg.sender].deposit;

// cho deposit = 0;
bidders[msg.sender].deposit = 0;
if(bidders[currentWinner].deposit == totalDeposit){
state = State.CLOSED;
}

// ** End code here ** /

if (totalDeposit <= 0) {
state = State.CLOSED;
}
}





// PART 2 - Using Modifier to:
// - Check if the action (startSession, register, bid, annoucement, getDeposit) can be done in current State.
// Kiểm tra nếu hành động (bắt đầu phiên, đăng kí, đấu giá, thông báo, nhận tiền cọc) có thể được thực hiện trong trạng thái hiện tại
// - Check if the current user can do the action.
// kiểm tra xem người dùng hiện tại có thể thực hiện hành động này không.

modifier onlyAuctioneer(){
require(auctioneer == msg.sender);
_;
}
modifier validStage(State stateReq){
require(state == stateReq);
_;
}
modifier exceptWinner(){
require(msg.sender != currentWinner);
_;
}
}


     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.