Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[website/docs]: add open auction tutorial #930

Merged
merged 11 commits into from
Sep 29, 2023
4 changes: 4 additions & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
* [Using Fe](user-guide/index.md)
* [Installation](user-guide/installation.md)
* [Using projects](user-guide/projects.md)
* [Tutorials](user-guide/tutorials/index.md)
* [Open auction](user-guide/tutorials/auction.md)
* [Example Contracts](user-guide/example_contracts/index.md)
* [Open auction](user-guide/example_contracts/auction_contract.md)
* [Development](development/index.md)
* [Build & Test](development/build.md)
* [Release](development/release.md)
Expand Down
98 changes: 98 additions & 0 deletions docs/src/user-guide/example_contracts/auction_contract.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
```fe
// errors
struct AuctionAlreadyEnded {
}

struct AuctionNotYetEnded {
}

struct AuctionEndAlreadyCalled {}

struct BidNotHighEnough {
pub highest_bid: u256
}

// events
struct HighestBidIncreased {
#indexed
pub bidder: address
pub amount: u256
}

struct AuctionEnded {
#indexed
pub winner: address
pub amount: u256
}

contract Auction {
// states
auction_end_time: u256
beneficiary: address

highest_bidder: address
highest_bid: u256

pending_returns: Map<address, u256>

ended: bool

// constructor
pub fn __init__(mut self, ctx: Context, bidding_time: u256, beneficiary_addr: address) {
self.beneficiary = beneficiary_addr
self.auction_end_time = ctx.block_timestamp() + bidding_time
}

//method
pub fn bid(mut self, mut ctx: Context) {
if ctx.block_timestamp() > self.auction_end_time {
revert AuctionAlreadyEnded()
}
if ctx.msg_value() <= self.highest_bid {
revert BidNotHighEnough(highest_bid: self.highest_bid)
}
if self.highest_bid != 0 {
self.pending_returns[self.highest_bidder] += self.highest_bid
}
self.highest_bidder = ctx.msg_sender()
self.highest_bid = ctx.msg_value()

ctx.emit(HighestBidIncreased(bidder: ctx.msg_sender(), amount: ctx.msg_value()))
}

pub fn withdraw(mut self, mut ctx: Context) -> bool {
let amount: u256 = self.pending_returns[ctx.msg_sender()]

if amount > 0 {
self.pending_returns[ctx.msg_sender()] = 0
ctx.send_value(to: ctx.msg_sender(), wei: amount)
}
return true
}

pub fn auction_end(mut self, mut ctx: Context) {
if ctx.block_timestamp() <= self.auction_end_time {
revert AuctionNotYetEnded()
}
if self.ended {
revert AuctionEndAlreadyCalled()
}
self.ended = true
ctx.emit(AuctionEnded(winner: self.highest_bidder, amount: self.highest_bid))

ctx.send_value(to: self.beneficiary, wei: self.highest_bid)
}

pub fn check_highest_bidder(self) -> address {
return self.highest_bidder;
}

pub fn check_highest_bid(self) -> u256 {
return self.highest_bid;
}

pub fn check_ended(self) -> bool {
return self.ended;
}
}
```
3 changes: 3 additions & 0 deletions docs/src/user-guide/example_contracts/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Example Contracts

- [Simple open auction](./auction_contract.md)
Loading