def Main(operation): # create an array stuff = ['a', 3, ['j', 3, 5], 'jk', 'lmnopqr'] # serialize it to_save = Serialize(stuff) Put(ctx, 'serialized', to_save) if operation == 1: return to_save elif operation == 2: to_retrieve = Get(ctx, 'serialized') return to_retrieve elif operation == 3: to_retrieve = Get(ctx, 'serialized') deserialized = Deserialize(to_retrieve) return deserialized elif operation == 4: to_retrieve = Get(ctx, 'serialized') deserialized = Deserialize(to_retrieve) return deserialized[2] return False
def add_to_board_list(board_id): serialized_list = Get(ctx, AD_LIST_KEY) board_list = Deserialize(serialized_list) board_list.append(board_id) serizlized_list = Serialize(board_list) Put(ctx, AD_LIST_KEY, serizlized_list) return True
def GetUserPublications(args): user = args[0] context = GetContext() publications_key = concat('publications', user) publications = Get(context, publications_key) user_publications = [] if not publications: return [True, user_publications] publications = Deserialize(publications) # Go through each user publication and get details for i in range(0, len(publications)): publication_key = concat(publications_key, sha1(publications[i])) publication = Get(context, publication_key) publication = Deserialize(publication) # Append only if publication is active if publication[5]: user_publications.append(publication) return [True, user_publications]
def GetNewPublications(): context = GetContext() publications = Get(context, 'new_publications') new_publications = [] if not publications: return [True, new_publications] publications = Deserialize(publications) # Go through each new publication and get details for i in range(0, len(publications)): user = publications[i][0] name = publications[i][1] publications_key = concat('publications', user) publication_key = concat(publications_key, sha1(name)) publication = Get(context, publication_key) publication = Deserialize(publication) # Append only if publication is active if publication[5]: new_publications.append(publication) return [True, new_publications]
def buyCat(from_acc, animal, amount): if not CheckWitness(from_acc): Notify("Not the owner, can't buy") return False tmplist = Get(ctx, from_acc + "CAT") if len(tmplist) != 0: delist = Deserialize(tmplist) Notify(delist) delist.append(animal) Notify(delist) Put(ctx, from_acc + "CAT", Serialize(delist)) else: Put(ctx, from_acc + "CAT", Serialize([animal])) Notify(Serialize([animal])) current_balance = Get(ctx, from_acc) if current_balance <= amount: Notify("Insufficient funds") return False to_balance = Get(ctx, TOKEN_OWNER) to_balance += amount Put(ctx, TOKEN_OWNER, to_balance) current_balance -= amount if current_balance != 0: Put(ctx, from_acc, current_balance) else: Delete(ctx, from_acc) OnTransfer(from_acc, TOKEN_OWNER, amount) return True
def removeQuestionId(questionId): Notify("[!] Remove QuestionID to all_question_ids object") serial = Get(GetContext(), get_all_ids) all_question_ids = Deserialize(serial) all_question_ids.remove(questionId) new_serial = Serialize(all_question_ids) Put(GetContext(), get_all_ids, new_serial) Notify("[!] Removed QuestionID from all_question_ids object")
def removePostId(postId): Notify("[!] Remove PostID to all_posts object") serial = Get(GetContext(), get_all_ids) all_posts = Deserialize(serial) all_posts.remove(postId) new_serial = Serialize(all_posts) Put(GetContext(), get_all_ids, new_serial) Notify("[!] Removed PostID from all_posts object")
def unFollow(ctx, uid, uFAddr, index_a, index_b): """ Unfollow another user Args: uid -> unique user id uFAddr -> to-follow script hash index_a -> Index of iterated storage of uid, for the following of fUid index_b -> Index of iterated storage of FUid, for the follow of uid """ uFUid = isRegistered(ctx, uFAddr) if not uFUid == False: if uFUid == uid: Notify("User cannot unfollow himself") return False if isFollowing(ctx, uid, uFUid): #User has to be follower, respecitve follower has to be followed by user a_temp = GetThree(ctx, uid, ".following.", index_a) a_temp_d = Deserialize(a_temp) b_temp = GetThree(ctx, uFUid, ".followers.", index_b) b_temp_d = Deserialize(b_temp) if a_temp_d[0] == uFUid and b_temp_d[0] == uid: # Count unfollowing += 1 for uid ##a_save = Get(ctx, uid) ##a_save_d = Deserialize(a_save) ##a_count = a_save_d[8] ##aa_count = a_count + 1 ##a_save_d[8] = aa_count ##a_save_s = Serialize(a_save_d) ##Put(ctx, uid, a_save_s) updateAccCount(ctx, uid, 8, False) # Count unfollowed += 1 for uFUid ##b_save = Get(ctx, uFUid) ##b_save_d = Deserialize(b_save) ##b_count = b_save_d[7] ##bb_count = b_count + 1 ##b_save_d[7] = bb_count ##b_save_s = Serialize(b_save_d) ##Put(ctx, uFUid, b_save_s) updateAccCount(ctx, uFUid, 7, False) # Mark index as unfollowed for uid PutThree(ctx, uid, ".following.", index_a, "unfollowing") # Mark index as unfollowed for uFUid PutThree(ctx, uFUid, ".followers.", index_b, "unfollowed") #Set follow indicator = false PutThree(ctx, uid, ".followcheck.", uFUid, False) OnUnfollow(uid, uFUid) return True Notify("Following and Follower indexes do not match") return False Notify("User is not following.") return False Notify("User to unfollow not registered") return False
def addPostId(postId): check_allPostIds() Notify("[!] Add PostID to all_posts object") serial = Get(GetContext(), get_all_ids) all_posts = Deserialize(serial) all_posts.append(postId) new_serial = Serialize(all_posts) Put(GetContext(), get_all_ids, new_serial) Notify("[!] Added PostID to all_posts object")
def _save_space_ids(new_id): ids = Get(ctx, KEY_SPACE_IDS) if ids: deserialized = Deserialize(ids) deserialized.append(new_id) Put(ctx, KEY_SPACE_IDS, Serialize(deserialized)) else: Put(ctx, KEY_SPACE_IDS, Serialize([new_id]))
def put_serialized(ctx, key, value): list_bytes = Get(ctx, key) if len(list_bytes) != 0: lst = Deserialize(list_bytes) lst.append(value) else: lst = [value] list_bytes = Serialize(lst) Put(ctx, key, list_bytes) return True
def checkCat(owner): exists = Get(ctx, owner + "CAT") if len(exists) != 0: tmp = Deserialize(exists) Notify(tmp) return exists return False
def vote(args): """ Vote for a proposal :param args: list of arguments [PROPOSAL_ID, VOTE] :return: bool, result of the execution """ if len(args) != 2: return False # get proposal from storage proposal_id = args[0] proposal_storage = Get(ctx, proposal_id) # check proposal existence if not proposal_storage: print("Proposal not found") return False # get caller address references = GetScriptContainer().References if len(references) < 1: return False caller_addr = references[0].ScriptHash # check if address already voted if Get(ctx, concat(proposal_id, caller_addr)): print('Already voted') return False # Deserialize proposal array proposal = Deserialize(proposal_storage) # iterate authorized address index = 0 while index < len(proposal[3]): if proposal[3][index] == caller_addr: authorized = True break index += 1 if not authorized: print('Not allowed to vote') return False # increment vote counter if args[1] == 1: print('Yes!') proposal[1] = proposal[1] + 1 else: print('No!') proposal[2] = proposal[2] + 1 # serialize proposal and write to storage proposal_storage = Serialize(proposal) Put(ctx, proposal_id, proposal_storage) # mark address as voted Put(ctx, concat(proposal_id, caller_addr), True) return True
def validatePublicationAuction(context, args): owner = args[0] name = args[1] date = args[2] date = date + 0 if date < MIN or date > MAX: print('Date must be within bounds') return False modulo = (date - SECONDS_IN_HOUR * -TIMEZONE) % SECONDS_IN_DAY if modulo != 0: print('Date must be 00:00 in contract timezone') return False publications_key = concat('publications', owner) publication_key = concat(publications_key, sha1(name)) publication = Get(context, publication_key) if len(publication) == 0: print('Publication does not exist') return False else: publication = Deserialize(publication) if not publication[5]: print('Publication is not currently active') return False return publication_key
def del_serialized(ctx, key, value): list_bytes = Get(ctx, key) new_list = [] deleted = False if len(list_bytes) != 0: deserialized_list = Deserialize(list_bytes) for item in deserialized_list: if item == value: deleted = True else: new_list.append(item) if (deleted): if len(new_list) != 0: serialized_list = Serialize(new_list) Put(ctx, key, serialized_list) else: Delete(ctx, key) print("Target element has been removed.") return True print("Target element has not been removed.") return False
def GetAuctionWinner(args): owner = args[0] name = args[1] date = args[2] context = GetContext() publication_key = validatePublicationAuction(context, args) if not publication_key: return [False, 'Invalid auction params'] date = date + 0 auction_key = concat(publication_key, sha1(date)) bids_key = concat(auction_key, 'bids') bids = Get(context, bids_key) if not bids: print('No bids') return [False, 'No bids'] bids = Deserialize(bids) best_bid = bids[len(bids) - 1] return [True, best_bid]
def GetCurrentAuctionWinner(args): owner = args[0] name = args[1] context = GetContext() date = getWarpedTime(context) + ( SECONDS_IN_DAY - getWarpedTime(context) % SECONDS_IN_DAY) + SECONDS_IN_HOUR * -TIMEZONE args.append(date) publication_key = validatePublicationAuction(context, args) if not publication_key: return [False, 'Invalid auction params'] date = date + 0 auction_key = concat(publication_key, sha1(date)) bids_key = concat(auction_key, 'bids') bids = Get(context, bids_key) if not bids: print('No bids') return [False, 'No bids'] bids = Deserialize(bids) best_bid = bids[len(bids) - 1] return [True, best_bid]
def cancel_bookinng(ctx, space_id, guest_address, date): if not CheckWitness(guest_address): return False booking_id = _build_booking_id(space_id, date) serialized_booking = Get(ctx, booking_id) if not serialized_booking: return False booking = Deserialize(serialized_booking) # guest_address, start_at, approved, canceled start_at = booking[1] canceled = booking[3] height = GetHeight() if height > start_at: return False if canceled: return False booking[3] = True Put(ctx, booking_id, Serialize(booking)) return_percent = _caluclate_return_percent(start_at, height) OnBookingCancel(space_id, date, return_percent) return return_percent
def GetAuctionByMonth(args): owner = args[0] name = args[1] date = args[2] context = GetContext() publication_key = validatePublicationAuction(context, args) if not publication_key: return [False, 'Invalid auction params'] auctions = [] # For simplicity, assume given first day of month & always retreive 31 days worth of info for days in range(0, 30): next_date = date + days * SECONDS_IN_DAY auction_key = concat(publication_key, sha1(next_date)) auction_info = Get(context, auction_key) if auction_info: auction_info = Deserialize(auction_info) auctions.append(auction_info) else: auctions.append([]) return [True, auctions]
def DeletePublication(args): sender = args[0] name = args[1] if not CheckWitness(sender): print('Account owner must be sender') return [False, 'Account owner must be sender'] context = GetContext() publications_key = concat('publications', sender) publication_key = concat(publications_key, sha1(name)) publication = Get(context, publication_key) if not publication: print('Publication does not exist') return [False, 'Publication does not exist'] publication = Deserialize(publication) if not publication[5]: print('Publication has already been deleted') return [False, 'Publication has already been deleted'] """ Check for active bids etc here - revisit if time (OOS) """ publication[5] = False Put(context, publication_key, Serialize(publication)) return [True, '']
def count(args): """ Count the votes of a proposal :param args: list of arguments [PROPOSAL_ID] :return: Array, return proposal """ if len(args) != 1: return False # get proposal from storage proposal_id = args[0] proposal_storage = Get(ctx, proposal_id) # check proposal existence if not proposal_storage: print("Proposal not found") return False # deserialize proposal array proposal = Deserialize(proposal_storage) # get proposal description description = proposal[0] print(description) # get proposal votes votes_yes = proposal[1] votes_no = proposal[2] print(votes_yes) print(votes_no) return proposal
def deserialize(data): ''' This function deserialize's an offer from storage :param data: :return: data ''' return Deserialize(data)
def get_challenge(ctx, challenge_key): Log("Retrieving challenge.") to_retrieve = Get(ctx, challenge_key) if to_retrieve: challenge = Deserialize(to_retrieve) return challenge return False
def get_ad_count(): serialized_list = Get(ctx, AD_LIST_KEY) if not serialized_list: return 0 board_list = Deserialize(serialized_list) return len(board_list)
def get_space_ids(ctx): ids = Get(ctx, KEY_SPACE_IDS) if ids: return Deserialize(ids) else: return []
def check_board_exist(board_id): serialized_list = Get(ctx, AD_LIST_KEY) board_list = Deserialize(serialized_list) for _id in board_list: if _id == board_id: return True print('Board not found') return False
def addQuestionId(questionId, question): Notify("[!] Add QuestionID to all_question_ids object") serial = Get(GetContext(), get_all_ids) all_question_ids = Deserialize(serial) all_question_ids[questionId] = question new_serial = Serialize(all_question_ids) Put(GetContext(), get_all_ids, new_serial) Notify("[!] Added QuestionID to all_question_ids object")
def get_rates(_type): result = Get(context, _type) if result != bytearray(b''): rates = Deserialize(result) if len(rates) > 0: return rates return []
def addPostId(postId, title): Notify("[!] Add PostID to all_posts object") serial = Get(GetContext(), get_all_ids) all_posts = Deserialize(serial) all_posts[postId] = title new_serial = Serialize(all_posts) Put(GetContext(), get_all_ids, new_serial) Notify("[!] Added PostID to all_posts object")
def get_bet(from_address, bet_id, option): # bet_id and option will be concatenated. They must be string result = Get(context, from_address + bet_id + option) if result != bytearray(b''): result = Deserialize(result) if result > 0: return result return 0