def _deploy(data: Any, upgrade: bool): """ The contracts initial entry point, on deployment. """ if upgrade: return if get(DEPLOYED).to_bool(): abort() tx = cast(Transaction, script_container) #DEBUG_START #custom owner for tests if tx.sender is None: owner = UInt160(b'\x96Wl\x0e**\x1c!\xc4\xac^\xbd)31\x15%A\x1f@') #DEBUG_END put(DEPLOYED, True) put(PAUSED, False) put(TOKEN_COUNT, 0) put(MINT_FEE, MINT_FEE_ON_DEPLOY) auth: List[UInt160] = [] auth.append(tx.sender) serialized = serialize(auth) put(AUTH_ADDRESSES, serialized) wl: List[UInt160] = [] wl.append(tx.sender) wl_serialized = serialize(auth) put(WL_ADDRESSES, wl_serialized)
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 cancel_pool(pool_id: UInt256): creator_on_storage = get(POOL_OWNER_KEY + pool_id) if len(creator_on_storage) == 0: raise Exception("Pool doesn't exist.") creator = UInt160(creator_on_storage) if not check_witness(creator): raise Exception('No authorization.') if len(get(POOL_RESULT_KEY + pool_id)) > 0: raise Exception('Pool is finished already') executing_contract = executing_script_hash # refund players bets_key_prefix = POOL_BET_KEY + pool_id bet = find(bets_key_prefix) while bet.next(): result_pair: List[bytes] = bet.value storage_key = result_pair[0] account = UInt160(storage_key[len(bets_key_prefix):]) transfer_gas(executing_contract, account, PRICE_IN_GAS) # set result put(POOL_RESULT_KEY + pool_id, serialize('Cancelled by owner'))
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 land_save(land: dict, owner: UInt160) -> bool: index: str = land["index"] put(index, serialize(land)) key = LAND_OWNERSHIP + owner #current_ownership: List[str] = deserialize(get(key)) #current_ownership.append(index) #put(key, serialize(current_ownership)) return True
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 create_pool(creator: UInt160, description: str, options: List[str]) -> UInt256: if not check_witness(creator): raise Exception('No authorization.') options: List[str] = remove_duplicates(options) if len(options) < 2: raise Exception('Not enough options to create a pool') for option in options: if len(option) == 0: raise Exception('Cannot have an empty option') tx: Transaction = script_container pool_id = tx.hash put(POOL_OWNER_KEY + pool_id, creator) put(POOL_TOTAL_STAKE_KEY + pool_id, 0) put(POOL_OPTIONS_KEY + pool_id, serialize(options)) put(POOL_DESCRIPTION_KEY + pool_id, description) request_image_change() return pool_id
def serialize_bool() -> bytes: return serialize(True)
def serialize_str() -> bytes: return serialize('42')
def land_save(land: dict) -> bool: owner = cast(UInt160, land["owner"]) put(owner, serialize(land)) return True
def serialize_int() -> bytes: return serialize(42)
def serialize_dict() -> bytes: return serialize({1: 1, 2: 1, 3: 2})
def serialize_sequence() -> bytes: return serialize([2, 3, 5, 7])