Created
October 9, 2019 14:30
-
-
Save nakajo2011/cbba76c1b1f3361396e1918325998b68 to your computer and use it in GitHub Desktop.
Devcon5 Day2 hands-on
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Devcon 5 Auction | |
# Masked Reveal Auction | |
#******************************* | |
#Every user has a bid amount, hash (bidamount,pass), sent masked amount | |
struct Data: | |
bidval: wei_value | |
hashval: bytes32 | |
sentval: wei_value | |
hasBid: bool | |
hasRevealed: bool | |
winner: bool | |
refunded: bool | |
# map each address with the above information | |
# *************** State Variables ****************** | |
addToData: public(map(address, Data)) | |
owner: public(address) | |
minBid: public(wei_value) | |
minWin: public(wei_value) | |
countOfTickets: public(int128) | |
bidOpenTime: public(timestamp) | |
bidCloseTime: public(timestamp) | |
revealOpenTime: public(timestamp) | |
revealCloseTime: public(timestamp) | |
# ************************************************** | |
# specify the minimum bid amount at the starting of the contract | |
@public | |
def __init__(_minBid: wei_value, revealTime: timedelta): | |
self.owner = msg.sender | |
self.minBid = _minBid | |
self.bidOpenTime = block.timestamp | |
self.bidCloseTime = self.bidOpenTime + revealTime | |
# calculate hash of -> bid amount + password | |
@public | |
@constant | |
def calculateHash(_bidval: wei_value, passw: bytes[10]) -> bytes32: | |
return keccak256(concat(convert(_bidval,bytes32), passw)) | |
# bid using the obatained hash | |
# conditions - check if bid placed during the bidding phase | |
# check if bid is greater than minimum bid amount | |
# allow only one bid per person | |
@public | |
@payable | |
def bid(hash: bytes32): | |
assert block.timestamp >= self.bidOpenTime and block.timestamp <= self.bidCloseTime | |
assert msg.value >= self.minBid, "The masked amount is lower." | |
assert self.addToData[msg.sender].hasBid == False, "Only one bid." | |
self.addToData[msg.sender] = Data({bidval: 0, hashval: hash, sentval: msg.value, hasBid: True, | |
hasRevealed: False, winner: False, refunded: False}) | |
# let the owner set the win amount, let the reveal phase open | |
@public | |
def selectWinningBidAmount(_amount: wei_value, revealTime: timedelta): | |
assert msg.sender == self.owner, "No access." | |
self.minWin = _amount | |
self.revealOpenTime = block.timestamp | |
self.revealCloseTime = self.revealOpenTime + revealTime | |
# reveal the bid by checking with the stored hash and computing hash from the inputs | |
# check if the masked amount was actually greater than the bid amount | |
# check if we are in the reveal period | |
# if bid value is favorable make the person a winner | |
@public | |
def reveal(_bidval: wei_value, passw: bytes[10]): | |
assert keccak256(concat(convert(_bidval, bytes32), passw)) == self.addToData[msg.sender].hashval, "Wrong Bid amount provided." | |
assert self.addToData[msg.sender].sentval >= _bidval, "Wrong Nice Try!" | |
self.addToData[msg.sender].hasRevealed = True | |
self.addToData[msg.sender].bidval = _bidval | |
if self.countOfTickets <= 50: | |
assert self.addToData[msg.sender].winner == False | |
self.addToData[msg.sender].winner = True | |
self.countOfTickets += 1 | |
# if you want to able to select winners your self. | |
# def selectWinners(winners: address[50]): | |
# for i in range(50): | |
# self.addToData[winners[i]].winner = True | |
# let bidders withdraw, check if has revealed or if refund has not been done already | |
@public | |
def withdraw(): | |
assert self.addToData[msg.sender].hasRevealed == True, "Reveal First to ..." | |
assert self.addToData[msg.sender].refunded == False, "Already Refunded." | |
if self.addToData[msg.sender].winner == True: | |
send(msg.sender, self.addToData[msg.sender].sentval - self.addToData[msg.sender].bidval) | |
else: | |
self.addToData[msg.sender].refunded = True | |
send(msg.sender, self.addToData[msg.sender].sentval) | |
# find out the contract balance ' | |
@public | |
@constant | |
def checkBalance() -> wei_value: | |
return self.balance | |
# let the owner withdraw the contract balance | |
@public | |
def withdrawAll(): | |
assert msg.sender == self.owner, "Deny!" | |
send(self.owner, self.balance) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment