def get_pool(pool_id: UInt256) -> list: creator = get(POOL_OWNER_KEY + pool_id) if len(creator) == 0: raise Exception("Pool doesn't exist.") description = get(POOL_DESCRIPTION_KEY + pool_id).to_str() options: List[str] = deserialize(get(POOL_OPTIONS_KEY + pool_id)) result = None serialized_result = get(POOL_RESULT_KEY + pool_id) if len(serialized_result) > 0: result = deserialize(serialized_result) pool_bets: Dict[bytes, str] = {} bet_prefix = POOL_BET_KEY + pool_id bets = find(bet_prefix) while bets.next(): result_pair = bets.value storage_key = cast(bytes, result_pair[0]) player_bet = cast(str, result_pair[1]) player_id = storage_key[len(bet_prefix):].to_str().to_bytes() pool_bets[player_id] = player_bet return [pool_id, creator, description, options, result, pool_bets]
def main(operation: int) -> Any: # create an array stuff = ['a', 3, ['j', 3, 5], 'jk', 'lmnopqr'] # serialize it to_save = serialize(stuff) put('serialized', to_save) if operation == 1: return to_save elif operation == 2: to_retrieve = get('serialized') return to_retrieve elif operation == 3: to_retrieve = get('serialized') deserialized = deserialize(to_retrieve) return deserialized elif operation == 4: to_retrieve = get('serialized') deserialized = deserialize(to_retrieve) if isinstance(deserialized, list): return deserialized[2] return False
def setWhitelistedAddress(address: UInt160, authorized: bool): """ Configure whitelisted addresses. When this contract address is included in the transaction signature, this method will be triggered as a VerificationTrigger to verify that the signature is correct. For example, this method needs to be called when using the no fee mint method. :param address: the address of the account that is being authorized :type address: UInt160 :param authorized: authorization status of this address :type authorized: bool :return: whether the transaction signature is correct :raise AssertionError: raised if witness is not verified. """ assert verify(), '`acccount` is not allowed for setWhitelistedAddress' serialized = get(WL_ADDRESSES) auth = cast(list[UInt160], deserialize(serialized)) if authorized: found = False for i in auth: if i == address: found = True if not found: auth.append(address) put(WL_ADDRESSES, serialize(auth)) on_auth(address, 1, True) else: auth.remove(address) put(WL_ADDRESSES, serialize(auth)) on_auth(address, 1, False)
def transfer(to: UInt160, token_id: bytes, data: Any): assert len(to) == 20 token = cast(Dict['str'], deserialize(get(TOKEN_PREFIX + token_id))) from_address: UInt160 = token['owner'] if from_address != calling_script_hash and not check_witness(from_address): return False # if it's not transferring to your own account if from_address != to: owner = cast(List[int], deserialize(get(ACCOUNT_PREFIX + from_address))) post_transfer() return True
def isWhitelisted() -> bool: """ Check if the address is allowed to mint without fees. If the address is whitelisted, it's allowed to mint without any fees. :return: whether the address is allowed to mint without fees """ serialized = get(WL_ADDRESSES) auth = cast(list[UInt160], deserialize(serialized)) for addr in auth: if check_witness(addr): debug(["Verification successful", addr]) return True debug(["Verification failed", addr]) return False
def finish_pool(pool_id: UInt256, winner_options: List[str]): creator = get(POOL_OWNER_KEY + pool_id) if len(creator) == 0: raise Exception("Pool doesn't exist.") if not check_witness(creator): raise Exception('No authorization.') if len(get(POOL_RESULT_KEY + pool_id)) > 0: raise Exception('Pool is finished already') if len(winner_options) == 0: raise Exception('At least one winner is required') # validate all winner options are valid options winner_options: List[str] = remove_duplicates(winner_options) pool_options: List[str] = deserialize(get(POOL_OPTIONS_KEY + pool_id)) for option in winner_options: if option not in pool_options: raise Exception('Invalid option for this pool') winners: List[UInt160] = [] # get winner players bets_key_prefix = POOL_BET_KEY + pool_id bet = find(bets_key_prefix) while bet.next(): result_pair = bet.value storage_key = cast(bytes, result_pair[0]) account_bet = cast(str, result_pair[1]) if account_bet in winner_options: address = storage_key[len(bets_key_prefix):] account = UInt160(address) winners.append(account) # distribute the prizes if len(winners) > 0: total_stake = get(POOL_TOTAL_STAKE_KEY + pool_id).to_int() prize_per_winner = total_stake // len(winners) executing_contract = executing_script_hash for winner in winners: transfer_gas(executing_contract, winner, prize_per_winner) # set result put(POOL_RESULT_KEY + pool_id, serialize(winner_options))
def verify() -> bool: """ Check if the address is allowed. When this contract address is included in the transaction signature, this method will be triggered as a VerificationTrigger to verify that the signature is correct. For example, this method needs to be called when withdrawing token from the contract. :return: whether the transaction signature is correct """ serialized = get(AUTH_ADDRESSES) auth = cast(list[UInt160], deserialize(serialized)) for addr in auth: if check_witness(addr): debug(["Verification successful", addr]) return True debug(["Verification failed", addr]) return False
def bet(player: UInt160, bet_id: UInt256, bet_option: str): if len(get(POOL_OWNER_KEY + bet_id)) == 0: raise Exception("Pool doesn't exist.") if not check_witness(player): raise Exception('No authorization.') if len(get(POOL_RESULT_KEY + bet_id)) > 0: raise Exception('Pool is finished already') player_vote_key = POOL_BET_KEY + bet_id + player if len(get(player_vote_key)) > 0: raise Exception('Only one bet is allowed per account') valid_options: List[str] = deserialize(get(POOL_OPTIONS_KEY + bet_id)) if bet_option not in valid_options: raise Exception('Invalid option for this pool') total_stake = get(POOL_TOTAL_STAKE_KEY + bet_id).to_int() total_stake += PRICE_IN_GAS transfer_gas(player, executing_script_hash, PRICE_IN_GAS) put(player_vote_key, bet_option) put(POOL_TOTAL_STAKE_KEY + bet_id, total_stake) request_image_change()
def deserialize_arg() -> Any: return deserialize(1)
def tokensOf(owner_: UInt160) -> List[bytes]: owner = get(ACCOUNT_PREFIX + owner_) assert len(owner) == 20 ids = cast(List[bytes], deserialize(owner)) return ids
def balanceOf(owner_: UInt160) -> int: owner = get(ACCOUNT_PREFIX + owner_) assert len(owner) == 20 ids = cast(List[bytes], deserialize(owner)) return len(ids)
def land_get(owner: UInt160) -> dict: land: dict = deserialize(get(owner)) return land
def ownerOf(token_id: bytes) -> UInt160: token = get(TOKEN_PREFIX + token_id) owner = cast(Dict['str'], deserialize(token))['owner'] return owner
def deserialize_arg(arg: bytes) -> Any: return deserialize(arg)
def land_get(index: str) -> dict: land: dict = deserialize(get(index)) return land
def tokensOf(owner: UInt160) -> List[str]: key = LAND_OWNERSHIP + owner current_ownership: List[str] = deserialize(get(key)) return current_ownership