-
Notifications
You must be signed in to change notification settings - Fork 4
/
englishauction.rb
97 lines (74 loc) · 2.54 KB
/
englishauction.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# SPDX-License-Identifier: public domain
# pragma rubity 0.0.1
class EnglishAuction < Contract
event :Start
event :Bid, sender: Address, # +indexed
amount: UInt
event :Withdraw, bidder: Address, # +indexed
amount: UInt
event :End, winner: Address,
amount: UInt
storage nft: Address, # IERC721
nftId: UInt,
seller: Address, # +payable
endAt: UInt,
started: Bool,
ended: Bool,
highestBidder: Address,
highestBid: UInt,
bids: mapping( Address, UInt )
sig [Address, UInt, UInt]
def constructor( nft:, nftId:, startingBid: )
@nft = nft
@nftId = nftId
@seller = msg.sender # payable(msg.sender)
@highestBid = startingBid
end
sig []
def start
assert !@started, "started"
assert msg.sender == @seller, "not seller"
IERC721(@nft).transferFrom( msg.sender, address(this), @nftId )
@started = true
@endAt = block.timestamp + 7.days
log Start
end
sig [], :payable
def bid
assert @started, "not started"
assert block.timestamp < @endAt, "ended"
assert msg.value > @highestBid, "value < highest"
if @highestBidder != address(0)
@bids[@highestBidder] += @highestBid;
end
@highestBidder = msg.sender;
@highestBid = msg.value;
log Bid, msg.sender, msg.value
end
sig []
def withdraw
bal = @bids[msg.sender]
@bids[msg.sender] = 0
address(msg.sender).transfer(bal) # payable(msg.sender)
log Withdraw, msg.sender, bal
end
sig []
def end
assert @started, "not started"
assert block.timestamp >= @endAt, "not ended"
assert !@ended, "ended"
@ended = true
if @highestBidder != address(0)
IERC721(@nft).safeTransferFrom( address(this), @highestBidder, @nftId )
seller.transfer( @highestBid )
else
IERC721(@nft).safeTransferFrom( address(this), @seller, @nftId )
end
log End, @highestBidder, @highestBid
end
end
__END__
interface IERC721 {
function safeTransferFrom(address from, address to, uint tokenId) external;
function transferFrom(address, address, uint) external;
}