def CanWithdrawNeo(): print("[can withdraw Neo]") tx = GetScriptContainer() context = GetContext() for output in tx.Outputs: shash = GetScriptHash(output) output_asset_id = GetAssetId(output) if output_asset_id == NEO_ASSET_ID: print("Asset is Neo") else: return False return True saved_timestamp = Get(context, "timestamp") Notify("saved_timestamp: ", saved_timestamp) current_timestamp = GetHeight() Notify("current_timestamp: ", current_timestamp) return current_timestamp > (saved_timestamp + 4)
def Main(operation, key, stop): context = GetContext() height = GetHeight() header = GetHeader(height) now = header.Timestamp() if operation == 'create': Put(context, key, stop) print(now) return stop if operation == 'end': out = Get(context, key) if now > out: print('money returned!') return out return False
def fs_status(fs: FundingStage) -> int: """ Gets the completion status of the funding stage Args: fs (FundingStage): Funding Stage object containing specific attributes Return: (int): Id of current stage """ height = GetHeight() # Success if fs.in_circulation >= fs.supply: print("Funding Stage completed successfully") return 1 # Active if height < fs.end_block: print("Funding Stage still active") return 2 # Fail print("Funding Stage failed") return 3
def Main(): """ :return: Current block height. :rtype: int """ current_height = GetHeight() # Obtain height value (in integer) via built-in GetHeight() function return current_height
def timestamp(): current_height = GetHeight() # current_block = GetHeader(current_height) # timestamp1 = current_block.Timestamp # timestamp = concat(timestamp1,":") # time = concat(timestamp,current_height) # Log(time) return current_height
def CheckTimestamp(timestamp_normalised): # Checks if TS() > T_n + 8 minutes height = GetHeight() hdr = GetHeader(height) ts = GetTimestamp(hdr) if ts > timestamp_normalised + 480: return True return False
def _TimeHasExpired(): context = GetContext() currentHeight = GetHeight() print("got current height") currentHeader = GetHeader(currentHeight) print("got current block...") endTime = Get(context, 'endTime') return endTime < currentHeader.Timestamp
def CheckTimestamp(timestamp_normalised): # Checks if TS() > T_n + deadline height = GetHeight() hdr = GetHeader(height) ts = GetTimestamp(hdr) if ts > timestamp_normalised + deadline: return True return False
def calculate_can_exchange(self, token: Token, amount: int, address, verify_only: bool): """ Perform custom token exchange calculations here. :param token:Token The token settings for the sale :param amount:int Number of tokens to convert from asset to tokens :param address:bytearray The address to mint the tokens to :return: bool: Whether or not an address can exchange a specified amount """ height = GetHeight() storage = StorageAPI() current_in_circulation = storage.get(token.in_circulation_key) new_amount = current_in_circulation + amount if new_amount > token.total_supply: print("amount greater than total supply") return False print("trying to calculate height????") if height < token.block_sale_start: print("sale not begun yet") return False # if we are in free round, any amount if height > token.limited_round_end: print("Free for all, accept as much as possible") return True # check amount in limited round if amount <= token.max_exchange_limited_round: # check if they have already exchanged in the limited round r1key = concat(address, self.limited_round_key) has_exchanged = storage.get(r1key) # if not, then save the exchange for limited round if not has_exchanged: # note that this method can be invoked during the Verification trigger, so we have the # verify_only param to avoid the Storage.Put during the read-only Verification trigger. # this works around a "method Neo.Storage.Put not found in ->" error in InteropService.py # since Verification is read-only and thus uses a StateReader, not a StateMachine if not verify_only: storage.put(r1key, True) return True print("already exchanged in limited round") return False print("too much for limited round") return False
def submitPost(args): # Default args user = args[0] postHash = args[1] category = args[2] """ Validation Validating that the IPFS_Hash is not yet used """ postDataDomain = concat("post.data.", postHash) condition = Get(GetContext, postDataDomain) if condition != '': return False """ Adding to post domain post.latest Latest index of a post in common post.{postIndex} Getting a post by index post.data.{IPFS_PostHash} Getting a post by Hash From IPFS """ # Add to post domain addToDomain("post", postHash) # Setting post.data.{postHash} = {serializedPostData} currentHeight = GetHeight() currentBlock = GetHeader(currentHeight) time = currentBlock.Timestamp postData = [user, category, time] to_save = serialize_array(postData) Put(GetContext, postDataDomain, to_save) """ Adding to user domain user.{userAddress} User address of a user (Public key/hash) user.{userAddress}.latest Getting the latest post index of a certain user user.{userAddress}.{postIndex} Getting a post from a certain user by index """ # Creating user domain userDomain = concat("user.", user) # Add to user domain - {user}.latest && {user}.{latestIndex} addToDomain(userDomain, postHash) """ Adding to category domain category.{category}.latest Getting the latest post index of a certain category category.{category}.{postIndex} Getting a post from a certain category by index """ # Creating category domain categoryDomain = concat("category.", category) # Add to category domain - {category}.latest && {category}.{latestIndex} addToDomain(categoryDomain, postHash) return True
def Main(): """ :return: Current block timestamp :rtype: int """ current_height = GetHeight() current_block = GetHeader(current_height) timestamp = current_block.Timestamp return timestamp
def DepositNeo(): # get reference to the tx the invocation is in tx = GetScriptContainer() references = tx.References if len(references) < 1: print("no neo attached") return False # we need to determine who sent the tx reference = references[0] sender = GetScriptHash(reference) # this will keep track of how much neo was deposited value = 0 output_asset_id = GetAssetId(reference) if output_asset_id != NEO_ASSET_ID: print("Must deposit NEO") return False # this is the contract's address receiver = GetExecutingScriptHash() # go through all the outputs # and check if the receiver is the contract # if that is true, we add the value of the output to the # total sum that was deposited for output in tx.Outputs: shash = GetScriptHash(output) if shash == receiver: output_val = GetValue(output) value = value + output_val if value > 0: print("neo was deposited") Notify(value) timestamp = GetHeight() # now we know value was deposited from the sender context = GetContext() print("timestamp: ", timestamp) Put(context, "timestamp", timestamp) print("deposited") return True return False
def SetTime(): if _TimeHasExpired(): context = GetContext() currentHeight = GetHeight() currentHeader = GetHeader(currentHeight) time = currentHeader.Timestamp + TWOWEEKS print('setting time') print(time) Put(context, "endTime", time) return time return 0
def ResultNotice(agreement_key, weather_param, oracle_cost): """ Method to signal resulte by oracle :param agreement_key: the key of the agreement :type agreement_key: bytearray :param weather_param: weather parameter that the contract is depending on :type weather_param: int :param oracle_cost: costs made by the oracle to do this assignment :type oracle_cost: int :return: whether a pay out to the customer is done :rtype: bool """ # Check if the method is triggered by the oracle for this agreement context = GetContext() agreement_data = Get(context, agreement_key) oracle = agreement_data[8] if not CheckWitness(oracle): Log("Must be oracle to notice results") return False timestamp = agreement_data[3] utc_offset = agreement_data[4] status = agreement_data[12] if not status == 'initialized': Log("Contract has incorrect status to do a result notice") return False agreement_data[12] = 'result-noticed' agreement_data[13] = weather_param agreement_data[14] = oracle_cost # Get timestamp of current block currentHeight = GetHeight() currentBlock = GetHeader(currentHeight) current_time = currentBlock.Timestamp Put(context, agreement_key, agreement_data) timezone_timestamp = timestamp + (3600 * utc_offset) timezone_current_time = current_time + (3600 * utc_offset) if timezone_current_time < timezone_timestamp: Log("Datetime of result notice is lower than agreed datetime") return False else: DispatchResultNoticeEvent(agreement_key, weather_param, oracle_cost) return True
def GetCurrentTimestamp(): """ Get the current timestamp from the block's info :return: Unix timestamp. :rtype: int """ currentHeight = GetHeight() currentBlock = GetHeader(currentHeight) time = currentBlock.Timestamp return time
def do_current_timestamp() -> bytearray: Log('do_current_timestamp triggered.') current_height = GetHeight() Log('current_height:') Log(current_height) current_block = GetHeader(current_height) # Log('current_block:') # Log(current_block) timestamp = current_block.Timestamp Log('timestamp:') Log(timestamp) # Example b'\xc1\xc7|Z' return timestamp
def calculate_can_exchange(self, token: Token, amount: int, address): """ Perform custom token exchange calculations here. :param token:Token The token settings for the sale :param amount:int Number of tokens to convert from asset to tokens :param address:bytearray The address to mint the tokens to :return: bool: Whether or not an address can exchange a specified amount """ height = GetHeight() storage = StorageAPI() current_in_circulation = storage.get(token.in_circulation_key) new_amount = current_in_circulation + amount if new_amount > token.total_supply: print("amount greater than total supply") return False print("trying to calculate height????") if height < token.block_sale_start: print("sale not begun yet") return False # if we are in free round, any amount if height > token.limited_round_end: print("Free for all, accept as much as possible") return True # check amount in limited round if amount <= token.max_exchange_limited_round: # check if they have already exchanged in the limited round r1key = concat(address, self.limited_round_key) has_exchanged = storage.get(r1key) # if not, then save the exchange for limited round if not has_exchanged: storage.put(r1key, True) return True print("already exchanged in limited round") return False print("too much for limited round") return False
def calculate_can_exchange(self, token: Token, amount: int, address): """ Führt benutzerdefinierte Token exchange Kalkulationen aus. :param token:Token Die Token Einstellungen für den Verkauf :param amount:int Nummer der Tokens die von Asset zu Tokens konvertiert werden :param address:bytearray Die Adresse wo die Tokens erzeugt werden sollen :return: bool: Ob eine Adresse einen bestimmten Betrag tauschen kann oder nicht """ height = GetHeight() storage = StorageAPI() current_in_circulation = storage.get(token.in_circulation_key) new_amount = current_in_circulation + amount if new_amount > token.total_supply: # print("amount greater than total supply") return False if height < token.block_sale_start: # print("sale not begun yet") return False # Ob wir in einer freien Runde sind, jeder Betrag if height > token.limited_round_end: # print("Free for all, accept as much as possible") return True # Überprüfe Betrag in einer limitierten Runde if amount <= token.max_exchange_limited_round: # Überprüfe ob die Adresse bereits in einer limitierten Runde teilgenommen haben r1key = concat(address, self.limited_round_key) has_exchanged = storage.get(r1key) # falls nicht, dann sichere den exchange für die limitierte Runde if not has_exchanged: storage.put(r1key, True) return True print("already exchanged in limited round") return False print("too much for limited round") return False
def calculate_can_exchange(amount: int, address): """ Perform custom token exchange calculations here. :param amount:int Number of tokens to convert from asset to tokens. :param address:bytearray The address to mint the tokens to. :return: bool: Whether or not an address can exchange a specified amount. """ height = GetHeight() context = GetContext() current_in_circulation = get_circulation() # Get the new amount of tokens. new_amount = current_in_circulation + amount if new_amount > total_supply: print("amount greater than total supply") return False print("trying to calculate height") if height < block_sale_start: print("sale not begun yet") return False # If we are in free round, any amount is acceptable. if height > limited_round_end: print("Free for all, accept as much as possible.") return True # Check amount in limited round. if amount <= max_exchange_limited_round: # Check if they have already exchanged in the limited round. r1key = concat(address, limited_round_key) has_exchanged = Get(context,r1key) # If not, then save the exchange for limited round. if not has_exchanged: Put(context,r1key, True) return True print("Already exchanged in limited round.") return False print("Too much for limited round.") return False
def license_product(args): """ Grant a license for a specific product, can only be called by the product owner: :param args: args[0]: product_id args[1]: user_id (user public key) :return: bool """ # unpacking args product_id = args[0] user_id = args[1] if not is_owner(product_id): return False product = init_product(product_id) height = GetHeight() product_expiration = product.pe # check if the product can still be licensed if height > product_expiration: print('This product cannot be licensed anymore, it has expired') return False # check if there are still licenses available max_license_number = product.mln current_licenses = product.cln if current_licenses == max_license_number: print('licenses have been sold out!') return False license_id = concat(product_id, user_id) # block height at which the license expires license_duration = product.le license_expiration = height + license_duration license_data = list(length=3) license_data[0] = user_id license_data[1] = license_expiration license_data[2] = product_id Put(GetContext, license_id, license_data) cln = increment_cln(product_id) return True
def generate_uid(ref=''): height = GetHeight() # header = GetHeader(height) # header.Timestamp block = GetBlock(height) timestamp = block.Timestamp tx = block.Transactions[0] tx_hash = tx.Hash time_hash = concat(timestamp, tx_hash) return concat(time_hash, ref)
def CheckTimestamp(timestamp_normalised): # Checks if TS() > T_n + deadline height = GetHeight() hdr = GetHeader(height) ts = GetTimestamp(hdr) Log(ts) Log(timestamp_normalised) nd = ts - deadline Log(nd) diff = nd - timestamp_normalised Log(diff) if nd > timestamp_normalised: return True return False
def CheckTiming(timestamp_normalised): # Check T_n relative to current TS() height = GetHeight() hdr = GetHeader(height) ts = GetTimestamp(hdr) Log(ts) t_n_plus_one = timestamp_normalised + Game_rules.timestep Log(t_n_plus_one) Log(timestamp_normalised) if ts > t_n_plus_one: return 1 # expired elif ts < timestamp_normalised: return 2 # too early to submit, ignore else: return 0 # all good
def IncBlk(): print("********IncBlk()") height = GetHeight() context = GetContext() h = Get(context, "block/height") if h == height: return False b = Get(context, "block/NRC") + 1 Put(context, "block/NRC", b) Put(context, "block/height", height) SettleCredit() return True
def Main(operation): """ :param operation: get or put :param args: optional arguments :return: Bool """ trigger = GetTrigger() if trigger == Application: Log("trigger: Application") height = GetHeight() hdr = GetHeader(height) ts = GetTimestamp(hdr) Log(ts) return True return False
def fs_calculate_can_exchange(fs: FundingStage, amount: int): """ Checks weather the input amount is avaliable to exchange Args: fs (FundingStage): Funding Stage object containing specific attributes amount (int): Amounnt in question Return: (bool): Is avaliable """ height = GetHeight() # Gets the SmartTokenShare object sts = sts_get(fs.project_id) # Gets the current total in circulation total_in_circulation = get_total_in_circulation(sts) # Calculates TOTAL circulation after input amount is added new_total_amount = total_in_circulation + amount # Calculates current circulation after input amount is added new_fs_amount = fs.in_circulation + amount total_supply = sts_get_attr(sts, 'total_supply') if new_total_amount > total_supply: print("amount greater than total supply") return False if new_fs_amount > fs.supply: print("amount greater than funding stage supply") return False if height < fs.start_block: print("Funding stage not begun yet") return False if height > fs.end_block: print("Funding stage has ended") return False return True
def calculate_can_exchange(ctx, amount, address, verify_only): """ Perform custom token exchange calculations here. :param amount:int Number of tokens to convert from asset to tokens :param address:bytearray The address to mint the tokens to :return: bool: Whether or not an address can exchange a specified amount """ height = GetHeight() current_in_circulation = Get(ctx, TOKEN_CIRC_KEY) new_amount = current_in_circulation + amount if new_amount > TOKEN_TOTAL_SUPPLY: return False if height < BLOCK_SALE_START: return False # if we are in free round, any amount if height > LIMITED_ROUND_END: return True # check amount in limited round if amount <= MAX_EXCHANGE_LIMITED_ROUND: # check if they have already exchanged in the limited round r1key = concat(address, LIMITED_ROUND_KEY) has_exchanged = Get(ctx, r1key) # if not, then save the exchange for limited round if not has_exchanged: # note that this method can be invoked during the Verification trigger, so we have the # verify_only param to avoid the Storage.Put during the read-only Verification trigger. # this works around a "method Neo.Storage.Put not found in ->" error in InteropService.py # since Verification is read-only and thus uses a StateReader, not a StateMachine if not verify_only: Put(ctx, r1key, True) return True return False return False
def start_public_sale(self, token: Token): storage = StorageAPI() owner = storage.get(token.owner_key) if not CheckWitness(owner): return False pub_sale_start_block = storage.get(self.pub_sale_start_block_key) if pub_sale_start_block: print("can't start the public sale twice") return False height = GetHeight() storage.put(self.pub_sale_start_block_key, height) return True
def CurrentSwapRate(): """ Method to calculate the current 'going rate' or exchange ratio of NEO to NEP5 tokens :return: the current rate :rtype: int """ basic = 1000 * FACTOR duration = ICO_END_TIME - ICO_START_TIME print("getting swap rate") context = GetContext() print("got context") total_supply = Get(context, 'totalSupply') print("got total supply") if total_supply >= TOTAL_AMOUNT: return False print("getting current height...") currentHeight = GetHeight() print("got current height") currentBlock = GetHeader(currentHeight) print("got current block...") time = currentBlock.Timestamp - ICO_START_TIME if time < 0: return 0 elif time < 86400: return basic * 130 / 100 elif time < 259200: return basic * 120 / 100 elif time < 604800: return basic * 110 / 100 elif time < duration: return basic return 0
def IsPromoExpired(promo_id): """ Check if promo has expired or not Args: promo_id (str): promo unique id Returns: (bool): True if promotion has expired """ context = GetContext() expiration_key = concat(promo_id, 'expiration') expiration = Get(context, expiration_key) height = GetHeight() current_block = GetHeader(height) current_time = current_block.Timestamp expired = current_time > expiration return expired