def __init__(self, sender): self.sender = sender self.chairperson = 0 self.voters = SDict(code, table_id=1, value_type=Voter) self.proposals = SList(code, table_id=2, value_type=Proposal) #FIXME only need to set once chairperson = N('vote') v = Voter() v.weight = 1 self.voters[chairperson] = v
def __init__(self): #Reference to contract tracking NFT ownership #ERC721 public nonFungibleContract; self.nonFungibleContract = ERC721() #Cut owner takes on each auction, measured in basis points (1/100 of a percent). #Values 0-10,000 map to 0%-100% #uint256 public ownerCut; self.ownerCut = uint256(0) #Map from token ID to their corresponding auction. #mapping (uint256 => Auction) tokenIdToAuction; self.tokenIdToAuction = SDict(key_type=uint256, value_type=Auction)
def apply(name, type): if type == N('sayhello'): a = SDict(code, N('a')) msg = read_action() a[msg] = msg return a1 = a[100] a2 = a[101] a3 = a[102] # print(a1, a2, a3) a[100] = 'hello1' a[101] = 'hello2' a[102] = 'hello3'
def apply(receiver, code, action): if action == N('sayhello'): a = SDict(code, N('a')) msg = read_action() a[msg] = msg return a1 = a[100] a2 = a[101] a3 = a[102] # print(a1, a2, a3) a[100] = 'hello1' a[101] = 'hello2' a[102] = 'hello3'
def __init__(self): # Parameters of the auction. Times are either # absolute unix timestamps (seconds since 1970-01-01) # or time periods in seconds. self._table_id = N('auction') self.beneficiary = 0 #address(0) self.auctionEnd = 0 # Current state of the auction. self.highestBidder = 0 self.highestBid = 0 # Allowed withdrawals of previous bids # mapping(address => uint) pendingReturns; self.pendingReturns = SDict(code, table_id=1) # Set to true at the end, disallows any change self.ended = 0 self.load()
def __init__(self): super(KittyBase, self).__init__() '''FIXME: self.cooldowns = [ uint32(1 minutes), uint32(2 minutes), uint32(5 minutes), uint32(10 minutes), uint32(30 minutes), uint32(1 hours), uint32(2 hours), uint32(4 hours), uint32(8 hours), uint32(16 hours), uint32(1 days), uint32(2 days), uint32(4 days), uint32(7 days) ] ''' # An approximation of currently how many seconds are in between blocks. self.secondsPerBlock = uint256(15) #/*** STORAGE ***/ # @dev An array containing the Kitty struct for all Kitties in existence. The ID # of each cat is actually an index into this array. Note that ID 0 is a negacat, # the unKitty, the mythical beast that is the parent of all gen0 cats. A bizarre # creature that is both matron and sire... to itself! Has an invalid genetic code. # In other words, cat ID 0 is invalid... ;-) #Kitty[] kitties; self.kitties = SList(0) # @dev A mapping from cat IDs to the address that owns them. All cats have # some valid owner address, even gen0 cats are created with a non-zero owner. #mapping (uint256 => address) public kittyIndexToOwner; self.kittyIndexToOwner = SList(0, value_type=Kitty) # @dev A mapping from owner address to count of tokens that address owns. # Used internally inside balanceOf() to resolve ownership count. #mapping (address => uint256) ownershipTokenCount; self.ownershipTokenCount = SDict(0) # @dev A mapping from KittyIDs to an address that has been approved to call # transferFrom(). Each Kitty can only have one approved address for transfer # at any time. A zero value means no approval is outstanding. #mapping (uint256 => address) public kittyIndexToApproved; self.kittyIndexToApproved = SDict(0) # @dev A mapping from KittyIDs to an address that has been approved to use # this Kitty for siring via breedWith(). Each Kitty can only have one approved # address for siring at any time. A zero value means no approval is outstanding. #mapping (uint256 => address) public sireAllowedToAddress; self.sireAllowedToAddress = SDict(0) # @dev The address of the ClockAuction contract that handles sales of Kitties. This # same contract handles both peer-to-peer sales as well as the gen0 sales which are # initiated every 15 minutes. #SaleClockAuction public saleAuction; self.saleAuction = None # @dev The address of a custom ClockAuction subclassed contract that handles siring # auctions. Needs to be separate from saleAuction because the actions taken on success # after a sales and siring auction are quite different. #SiringClockAuction public siringAuction; self.siringAuction = None
class Ballot(object): def __init__(self, sender): self.sender = sender self.chairperson = 0 self.voters = SDict(code, table_id=1, value_type=Voter) self.proposals = SList(code, table_id=2, value_type=Proposal) #FIXME only need to set once chairperson = N('vote') v = Voter() v.weight = 1 self.voters[chairperson] = v def addProposal(self, name): p = Proposal(name, 0) self.proposals.append(p) # Give `voter` the right to vote on this ballot. # May only be called by `chairperson`. def giveRightToVote(self, voter): require_auth(self.sender) if self.voters.find(voter): return v = Voter() v.weight = 1 # save it by assign a new value self.voters[voter] = v ''' require( msg.sender == chairperson, "Only chairperson can give right to vote." ); require( !voters[voter].voted, "The voter already voted." ); require(voters[voter].weight == 0); voters[voter].weight = 1; ''' # Delegate your vote to the voter `to`. def delegate(self, to): #not a voter if not self.voters.find(to): return # require(self.voters.find(to), n2s(to) + ' is not a voter') require(to != self.sender, "Self-delegation is disallowed.") # assigns reference voter = self.voters[self.sender] require(not voter.voted, "You already voted.") # Forward the delegation as long as # `to` also delegated. # In general, such loops are very dangerous, # because if they run too long, they might # need more gas than is available in a block. # In this case, the delegation will not be executed, # but in other situations, such loops might # cause a contract to get "stuck" completely. while True: v = self.voters.find(to) if not v: break if v.delegate == 0: break to = v.delegate # We found a loop in the delegation, not allowed. require(to != self.sender, "Found loop in delegation.") # Since `sender` is a reference, this # modifies `voters[msg.sender].voted` voter.voted = True voter.delegate = to #-- save changed voter self.voters[self.sender] = voter delegate_ = self.voters[to] if delegate_.voted: # If the delegate already voted, # directly add to the number of votes self.proposals[delegate_.vote].voteCount += voter.weight else: # If the delegate did not vote yet, # add to her weight. delegate_.weight += voter.weight #/ Give your vote (including votes delegated to you) #/ to proposal `proposals[proposal].name`. def vote(self, proposal): voter = self.voters[self.sender] require(not voter.voted, "Already voted.") voter.voted = True voter.vote = proposal #-- save voter self.voters[self.sender] = voter # If `proposal` is out of the range of the array, # this will throw automatically and revert all # changes. self.proposals[proposal].voteCount += voter.weight #/ @dev Computes the winning proposal taking all #/ previous votes into account. def winningProposal(self): winningVoteCount = 0 winningProposal_ = 0 for p in range(len(self.proposals)): if self.proposals[p].voteCount > winningVoteCount: winningVoteCount = self.proposals[p].voteCount winningProposal_ = p return winningProposal_ # Calls winningProposal() function to get the index # of the winner contained in the proposals array and then # returns the name of the winner def winnerName(self): index = self.winningProposal() return self.proposals[index].name