def alias_available_for_registration(alias): """ :returns true if alias is available for registration: Based on expiration of alias """ owner = alias_get_owner(alias) for_sale = alias_get_for_sale(alias) if for_sale or CheckWitness(owner): alias_reservation = 0 else: hard_coded_minimum = 0 # prevent negative reservation alias_reservation = storage_load(b'res_dur') if not alias_reservation or alias_reservation < hard_coded_minimum: alias_reservation = hard_coded_minimum expiration = alias_get_expiration(alias) end = expiration + alias_reservation timestamp = get_header_timestamp() free = storage_load(b'non_expirable_accs') if free and alias_get_type(alias) == 4: return False if end > timestamp: return False return True
def alias_expired(alias): """ : returns True if alias already expired: May be expired but not available for registrations, because of defined reservation """ # if NEO accs free of charge # NEO account never expire, can be only sold free = storage_load(b'non_expirable_accs') if free and alias_get_type(alias) == 4: return False expiration = alias_get_expiration(alias) return expiration < get_header_timestamp()
def offer_sell(alias, args): """ :param alias: \n:param args [price, type]: \n:returns True if success or False if failed: """ nargs = len(args) if nargs < 1: msg = "Not enough args provided. Requres prices." Notify(msg) return msg elif nargs > 1: alias_type = args[1] else: alias_type = 0 price = args[0] if price < 0: msg = "Price lower than 0." Notify(msg) return msg alias_to_sell = init_alias(alias, alias_type) if not alias_exists(alias_to_sell): msg = concat("Alias not found: ", alias) Notify(msg) return return_value(False, msg) alias_to_sell = alias_load(alias_to_sell) if alias_expired(alias_to_sell): msg = concat("Alias expired: ", alias) Notify(msg) return return_value(False, msg) alias_owner = alias_get_owner(alias_to_sell) if not CheckWitness(alias_owner): msg = "This operation can invoke only alias owner." Notify(msg) return return_value(False, msg) timestamp = get_header_timestamp() buy_offer_expiration = alias_get_buy_offer_expiration(alias_to_sell) alias_to_sell_buy_offer_price = alias_get_buy_offer_price(alias_to_sell) if alias_to_sell_buy_offer_price >= price and timestamp < buy_offer_expiration: # sell new_alias_owner = alias_get_buy_offer_owner(alias_to_sell) new_alias_target = alias_get_buy_offer_target(alias_to_sell) configured_commission = get_trade_commission() service_fee_part = (price * configured_commission) / 100 alias_owner_part = price - service_fee_part add_account_available_assets(alias_owner, alias_owner_part) add_fee_to_pool(service_fee_part) alias_set_target(alias_to_sell, new_alias_target) alias_set_owner(alias_to_sell, new_alias_owner) alias_set_owner_since(alias_to_sell, timestamp) alias_set_buy_offer_owner(alias_to_sell, b'') alias_set_buy_offer_price(alias_to_sell, 0) alias_set_buy_offer_expiration(alias_to_sell, 0) alias_save(alias_to_sell) TradeSuccesfullEvent(alias, alias_type, alias_owner, new_alias_owner, price) msg = "Sold." Notify(msg) return return_value(True, msg) alias_set_for_sale(alias_to_sell, 1) alias_set_sell_offer_price(alias_to_sell, price) alias_save(alias_to_sell) SellOfferEvent(alias, alias_type, price) msg = "Put on sale." Notify(msg) return return_value(True, msg)
def offer_buy(alias, args): """ :param alias: \n:param args [owner, target, price, expiration]: \n:returns True if success or False if failed: """ nargs = len(args) if nargs < 4: Notify( "Not enough args provided. Requres buy_offer_owner,buy_offer_target,buy_offer_price, buy_offer_expiration." ) return False elif nargs > 4: alias_type = args[4] else: alias_type = 0 buy_offer_owner = args[0] buy_offer_target = args[1] buy_offer_price = args[2] buy_offer_expiration = args[3] alias_to_buy = init_alias(alias, alias_type) if not alias_exists(alias_to_buy): msg = concat("Alias not found: ", alias) Notify(msg) return return_value(False, msg) alias_to_buy = alias_load(alias_to_buy) if alias_expired(alias_to_buy): msg = concat("Alias expired: ", alias) Notify(msg) return return_value(False, msg) if not CheckWitness(buy_offer_owner): msg = "You can offer buy only for yourself." Notify(msg) return return_value(False, msg) alias_owner = alias_get_owner(alias_to_buy) if buy_offer_owner == alias_owner: msg = "You already own this alias." Notify(msg) return return_value(False, msg) timestamp = get_header_timestamp() if buy_offer_expiration < timestamp: msg = "Cannot put expired offer." Notify(msg) return return_value(False, msg) # check if enough assets for offer offerer_available_assets = available_account_assets(buy_offer_owner) if offerer_available_assets < buy_offer_price: msg = "Not enough assets provided." Notify(msg) return return_value(False, msg) other_buy_offer_expiration = alias_get_buy_offer_expiration(alias_to_buy) alias_to_buy_buy_offer_price = alias_get_buy_offer_price(alias_to_buy) if alias_to_buy_buy_offer_price > buy_offer_price and other_buy_offer_expiration > timestamp: msg = "There is higher offer." Notify(msg) return return_value(False, msg) else: # refund assets to stored_buy_offer_owner old_buy_offer_owner = alias_get_buy_offer_owner(alias_to_buy) add_account_available_assets(old_buy_offer_owner, alias_to_buy_buy_offer_price) for_sale = alias_get_for_sale(alias_to_buy) sell_offer_price = alias_get_sell_offer_price(alias_to_buy) if for_sale and sell_offer_price <= buy_offer_price: # perform trade sub_account_available_assets(buy_offer_owner, sell_offer_price) configured_commission = get_trade_commission() service_fee_part = (sell_offer_price * configured_commission) / 100 alias_owner_part = sell_offer_price - service_fee_part old_owner = alias_get_owner(alias_to_buy) add_account_available_assets(old_owner, alias_owner_part) add_fee_to_pool(service_fee_part) alias_set_target(alias_to_buy, buy_offer_target) alias_set_owner(alias_to_buy, buy_offer_owner) alias_set_owner_since(alias_to_buy, timestamp) alias_set_for_sale(alias_to_buy, 0) alias_set_sell_offer_price(alias_to_buy, 0) alias_save(alias_to_buy) TradeSuccesfullEvent(alias, alias_type, old_owner, buy_offer_owner, buy_offer_price) msg = "Sold." Notify(msg) return return_value(True, msg) alias_set_buy_offer_target(alias_to_buy, buy_offer_target) alias_set_buy_offer_owner(alias_to_buy, buy_offer_owner) alias_set_buy_offer_price(alias_to_buy, buy_offer_price) alias_set_buy_offer_expiration(alias_to_buy, buy_offer_expiration) alias_save(alias_to_buy) BuyOfferEvent(alias, alias_type, buy_offer_owner, buy_offer_price) msg = "Buy offer submitted." Notify(msg) return return_value(True, msg)
def na_renew(alias, args): """ :param alias: \n:param args [expiration, type]: \n:returns new expiration if success or False if failed: \nif sub_nas defined passes call to sub_nas otherwise we try to renew alias """ nargs = len(args) if nargs < 1: msg = "Renew requires alias_expiration." Notify(msg) return return_value(False,msg) alias_expiration = args[0] if nargs > 1: alias_type = args[1] else: alias_type = 0 NEO_acc_free_of_chage = are_NEO_acc_free_of_charge() if alias_expiration< get_header_timestamp(): if not NEO_acc_free_of_chage or alias_type != 4: msg = "You provided already expired alias_expiraton." Notify(msg) return return_value(False,msg) alias_for_renewal = init_alias(alias,alias_type) if not alias_exists(alias_for_renewal): msg = concat("Alias not found: ", alias) Notify(msg) return return_value(False,msg) alias_for_renewal = alias_load(alias_for_renewal) if alias_expired(alias_for_renewal): msg = concat("Alias expired: ", alias) Notify(msg) return return_value(False,msg) owner = alias_get_owner(alias_for_renewal) if not CheckWitness(owner): msg = "You do not own this alias, so you cannot invoke renew" Notify(msg) return return_value(False,msg) # check if the alias registration is not too long (to keep alias circulating) # adjust duration timestamp = get_header_timestamp() maximum_duration = get_maximum_registration_duration(alias_type) if alias_expiration - timestamp > maximum_duration: alias_expiration = timestamp + maximum_duration # check if requested expiration not already payed if NEO_acc_free_of_chage and alias_type == 4: msg = "Alias already payed for requested or maximum duration." Notify(msg) return return_value(False,msg) current_alias_expiration = alias_get_expiration(alias_for_renewal) if current_alias_expiration >= alias_expiration: msg = "Alias already payed for requested or maximum duration." Notify(msg) return return_value(False,msg) # calculate duration to pay duration_to_pay = alias_expiration - current_alias_expiration if not try_pay_holding_fee(owner, alias_type, duration_to_pay): # handles assets update msg = "Not enough assets to pay for alias." Notify(msg) return return_value(False,msg) # renew alias dummy = alias_set_expiration(alias_for_renewal,alias_expiration) dummy = alias_save(alias_for_renewal) RenewAliasEvent(alias, alias_type, alias_expiration) msg = concat("Alias renew success. New expiration: ", alias_expiration) Notify(msg) return return_value(alias_expiration,msg)
def na_register(alias, args): """ :param alias: \n:param args [owner, type, target, expiration]: \n:returns expiration if success or False if failed: \nif sub_nas defined passes call to sub_nas otherwise we try to register alias """ if len(args) < 4: Notify("Not enough arguments provided. Requires: alias_owner, alias_type, alias_target, alias_expiration") return False alias_owner = args[0] alias_type = args[1] alias_target = args[2] alias_expiration = args[3] if not CheckWitness(alias_owner): msg = "You can register alias only for yourself." Notify(msg) return return_value(False,msg) timestamp = get_header_timestamp() if alias_expiration < timestamp: free_neo_acc = are_NEO_acc_free_of_charge() if not free_neo_acc or alias_type != 4: msg = "You provided already expired alias_expiraton." Notify(msg) return return_value(False,msg) new_alias = init_alias(alias,alias_type) if alias_exists(new_alias): new_alias = alias_load(new_alias) available = alias_available_for_registration(new_alias) if not available: msg = "Alias is already in use. You can submit buy offer if you are interested." Notify(msg) return return_value(False,msg) dummy = alias_set_target(new_alias, alias_target) if not alias_is_valid(new_alias): msg = "This alias cannot be registered. Invalid name or target property for given alias_type." Notify(msg) return return_value(False,msg) # check if the alias registration is not too long (to keep alias circulating) # adjust duration maximum_duration = get_maximum_registration_duration(alias_type) if alias_expiration - timestamp > maximum_duration: alias_expiration = timestamp + maximum_duration # calculate duration to pay duration_to_pay = alias_expiration - timestamp if not try_pay_holding_fee(alias_owner, alias_type, duration_to_pay): # handles assets update msg = "Not enough assets to pay for alias." Notify(msg) return return_value(False,msg) dummy = alias_set_owner(new_alias, alias_owner) dummy = alias_set_owner_since(new_alias, timestamp) dummy = alias_set_expiration(new_alias,alias_expiration) dummy = alias_save(new_alias) RegisterAliasEvent(alias, alias_type, alias_owner, alias_target, alias_expiration) msg = concat("Alias registered: ", alias) Notify(msg) return return_value(True,msg)