def view_bank(address): byte_address = Base58ToAddress(address) if len(byte_address) != 20: Notify(['Invalid address']) return False # check if user list exists user_info = Get(ctx, USERKEY) if user_info: all_users = Deserialize(user_info) else: Notify(['User list is empty']) return False # check if user has been created if address not in all_users: Notify(['User not yet created']) return False # if the user has been created, the bank map exists. Check the user's bank else: bank_info = Get(ctx, BANKEY) bank_map = Deserialize(bank_info) bank_balance = bank_map[address] Notify([bank_balance]) return bank_balance
def view_rep(address): byte_address = Base58ToAddress(address) if len(byte_address) != 20: Notify(['Invalid address']) return False # check if user list exists user_info = Get(ctx, USERKEY) if user_info: all_users = Deserialize(user_info) else: Notify(['User list is empty']) return False # check if user has been created if address not in all_users: Notify(['User not yet created']) return False # if the user has been created, the rep map exists. Check the user's rep else: rep_info = Get(ctx, REPKEY) rep_map = Deserialize(rep_info) rep_balance = rep_map[address] Notify([rep_balance]) return rep_balance
def bind(ont_id, bucket): """ bind ontID with address :param ont_id: :param bucket: storage server bucket id or url :return: """ assert CheckWitness(ADMIN) bound_bucket = Get(ctx, concat(KEY_ONT, ont_id)) if bound_bucket == bucket: raise Exception("ont id bind to the same bucket") if not bound_bucket: bind_map = get_bind_map(bucket) bind_map[ont_id] = True Put(ctx, concat(KEY_ONT, ont_id), bucket) Put(ctx, concat(KEY_BUCKET, bucket), Serialize(bind_map)) else: bound_data = Get(ctx, concat(KEY_BUCKET, bound_bucket)) bound_map = Deserialize(bound_data) bound_map.remove(ont_id) Put(ctx, concat(KEY_BUCKET, bound_bucket), Serialize(bound_map)) bind_map = get_bind_map(bucket) bind_map[ont_id] = True Put(ctx, concat(KEY_ONT, ont_id), bucket) Put(ctx, concat(KEY_BUCKET, bucket), Serialize(bind_map)) Notify(["bind", ont_id, bucket]) return True
def transfer(fulldomain, idx, todid): ''' transfer domain to other did ''' assert (len(fulldomain) > 0) lowerdomain = lower(fulldomain) assert (isDomainValid(lowerdomain)) owner = ownerOf(lowerdomain) assert (_verifyOntid(owner, idx)) Put(ctx, mulconcat(OWNER_KEY, lowerdomain), todid) fromrecordkey = _concatkey(RECORDS_KEY, owner) fromrecords = Deserialize(Get(ctx, fromrecordkey)) fromrecords = list_remove_elt(fromrecords, lowerdomain) if len(fromrecords) == 0: Delete(ctx, fromrecordkey) else: Put(ctx, fromrecordkey, Serialize(fromrecords)) torecordkey = _concatkey(RECORDS_KEY, todid) torecordsRaw = Get(ctx, torecordkey) if not torecordsRaw: torecords = [lowerdomain] Put(ctx, torecordkey, Serialize(torecords)) else: torecords = Deserialize(torecordsRaw) torecords.append(lowerdomain) assert (len(torecords) <= MAX_COUNT) Put(ctx, torecordkey, Serialize(torecords)) TransferEvent(fulldomain, owner, todid) return True
def unbind(ont_id, bucket): """ unbind ont id with address :param ont_id: :param bucket: bucket id or url :return: """ assert CheckWitness(ADMIN) bound_bucket = Get(ctx, concat(KEY_ONT, ont_id)) if not bound_bucket: raise Exception("ont id bind with nothing") assert bound_bucket == bucket bind_data = Get(ctx, concat(KEY_BUCKET, bucket)) bind_map = Deserialize(bind_data) bind_map.remove(ont_id) Put(ctx, concat(KEY_BUCKET, bucket), Serialize(bind_map)) Delete(ctx, concat(KEY_ONT, ont_id)) Notify(["unbind", ont_id, bucket]) return True
def purchase_bank(address, amount): byte_address = Base58ToAddress(address) assert (CheckWitness(byte_address)) if len(byte_address) != 20: Notify(['Invalid address']) return False else: # check if user has been created. if not, create the user user_info = Get(ctx, USERKEY) if user_info: all_users = Deserialize(user_info) else: Notify(['No existing users']) create_user(address) if address not in all_users: Notify(['not a registered user']) create_user(address) # if the user has been created, that means the bank map exists. update the user's bank and wallet else: bank_info = Get(ctx, BANKEY) bank_map = Deserialize(bank_info) bank_map[address] += amount bank_info = Serialize(bank_map) Put(ctx, BANKEY, bank_info) Notify(['bank_map updated']) add_bank(address, amount) Notify(['user wallet updated']) return True
def transferProperty(transferList): """ :param transferList: [[toAccount1, DNA1],[toAccount2, DNA2]] :return: bool """ DNACheck = transferList[0][1] account = Get(context, concatKey(DNA_PRE_KEY, DNACheck)) RequireWitness(account) for transferE in transferList: toAccount = transferE[0] DNA = transferE[1] DNAlist = Get(context, concatKey(PLAYER_ADDRESS_PRE_KEY, account)) DNAlist = Deserialize(DNAlist) toDNAlist = Get(context, concatKey(PLAYER_ADDRESS_PRE_KEY, toAccount)) if not toDNAlist: toDNAlist = [] else: toDNAlist = Deserialize(DNAlist) num = 0 while num < len(DNAlist): if DNAlist[num] == DNA: Put(context, concatKey(DNA_PRE_KEY, DNA), toAccount) DNAlist.remove(num) toDNAlist.append(DNA) Put(context, concatKey(PLAYER_ADDRESS_PRE_KEY, account), Serialize(DNAlist)) Put(context, concatKey(PLAYER_ADDRESS_PRE_KEY, toAccount), Serialize(toDNAlist)) num += 1 Notify(["Transfer property successfully"]) return True
def purchase_bank(address, amount): byte_address = Base58ToAddress(address) if len(byte_address) != 20: Notify(['Invalid address']) return False else: # check if user has been created. if not, create the user user_info = Get(ctx, USERKEY) all_users = Deserialize(user_info) if address not in all_users: create_user(address) # if the user has been created, that means the bank map exists. update the user's bank and wallet else: bank_info = Get(ctx, BANKEY) bank_map = Deserialize(bank_info) bank_map[address] += amount bank_info = Serialize(bank_map) Put(ctx, BANKEY, bank_info) add_bank(address, amount) return True
def profile(address): byte_address = Base58ToAddress(address) # check if user list exists user_info = Get(ctx, USERKEY) if user_info: all_users = Deserialize(user_info) else: Notify(['User list is empty']) return False # check if user has been created if address not in all_users: Notify(['User not created']) return False # check if address is valid if len(byte_address) != 20: Notify(['Invalid address']) return False profile_info = Get(ctx, concatkey(address, PI_PREFIX)) if profile_info: profile = Deserialize(profile_info) else: Notify(['The profile does not exist']) return False Notify(['profile', profile]) return profile
def getGP(gpId): gpMapInfo = Get(GetContext(), _concatkey(GP_PREFIX, gpId)) if not gpMapInfo: return [] gpMap = Deserialize(gpMapInfo) price = gpMap["price"] contentInfo = gpMap["content"] content = Deserialize(contentInfo) return [price, content]
def remove_list(element): ab_info = Get(ctx, ABKEY) ab = Deserialize(ab_info) ab1 = ab.remove(element) ab1_info = Serialize(ab1) Put(ctx, ABKEY, ab1_info) return True
def add_user_list(bet, element, prefix): list_info = Get(ctx, concatkey(bet, prefix)) lists = Deserialize(list_info) lists.append(element) list_info = Serialize(lists) Put(ctx, concatkey(bet, prefix), list_info) Notify(['add_user_list', element, prefix]) return True
def removeFromAssetFromList(index): assert (CheckWitness(Get(GetContext(), OPERATOR_PREFIX))) fahListInfo = Get(GetContext(), FROM_ASSET_LIST_KEY) if not fahListInfo: return True fahList = Deserialize(fahListInfo) fahList.remove(index) Put(GetContext(), FROM_ASSET_LIST_KEY, Serialize(fahList)) Notify(["removeFromAssetFromList", index]) return True
def add_user_list(element, prefix): bet = Get(ctx, BETKEY) list_info = Get(ctx, concatkey(bet, prefix)) lists = Deserialize(list_info) lists.append(element) list_info = Serialize(lists) Put(ctx, concatkey(bet, prefix), list_info) return True
def addFromAssetFromList(fromAssetHash): assert (CheckWitness(Get(GetContext(), OPERATOR_PREFIX))) fahListInfo = Get(GetContext(), FROM_ASSET_LIST_KEY) fahList = [] if not fahListInfo: fahList.append(fromAssetHash) else: fahList = Deserialize(fahListInfo) fahList.append(fromAssetHash) Put(GetContext(), FROM_ASSET_LIST_KEY, Serialize(fahList)) Notify(["addFromAssetFromList", fromAssetHash]) return True
def addList(listToBeAppend): list1Info = Get(GetContext(), LISTKEY) list1 = Deserialize(list1Info) Notify(["before add, list is ", list1]) for element in listToBeAppend: list1.append(element) list1Info = Serialize(list1) Put(GetContext(), LISTKEY, list1Info) Notify(["after add, list is ", list1]) return list1
def register_trust_anchor(ta_name, ont_id): require_witness(admin) ta_key = concat('TA', ta_name) require_not_exist(ta_key) serialize_ta_list = Get(ctx, TA_LIST_KEY) if serialize_ta_list is None: ta_list = [] else: ta_list = Deserialize(serialize_ta_list) ta_list.append(ta_name) serialize_ta_list = Serialize(ta_list) Put(ctx, TA_LIST_KEY, serialize_ta_list) Put(ctx, ta_key, ont_id)
def check_result(bet, current_price): # check if active bet list is populated/exists ab_info = Get(ctx, ABKEY) if ab_info: active_bets = Deserialize(ab_info) else: Notify(['There are no active bets']) return False # check if bet argument is an active bet if bet not in active_bets: Notify(['Bet is not active']) return False else: # FR, AR, FS, AS, FC, AC, UD, MA, TP, SE val_info = Get(ctx, concatkey(bet, VAL_PREFIX)) val_list = Deserialize(val_info) sign = val_list[6] target_price = val_list[8] # get the margin of the bet val_info = Get(ctx, concatkey(bet, VAL_PREFIX)) val_list = Deserialize(val_info) margin = val_list[7] if margin == 0: if current_price == target_price: Notify(['No price change']) return 0 # case where user bets that stock will rise if sign > 0: if current_price >= target_price: Notify(['For side correctly predicted rise']) return 1 else: Notify(['Against side correctly predicted fall']) return -1 # case where user bets that stock will fall if sign < 0: if current_price <= target_price: Notify(['For side correctly predicted fall']) return 1 else: Notify(['Against side correctly predicted rise']) return -1 else: Notify(['Stock was not predicted to make any movement']) return False
def bet_info(bet): # check if active bet list is populated/exists ab_info = Get(ctx, ABKEY) if ab_info: active_bets = Deserialize(ab_info) else: Notify(['There are no active bets']) return False # check if bet argument is an active bet if bet not in active_bets: Notify(['Bet is not active']) return False else: # FR, AR, FS, AS, FC, AC, UD, MA, TP, SE val_info = Get(ctx, concatkey(bet, VAL_PREFIX)) val_list = Deserialize(val_info) fm_info = Get(ctx, concatkey(bet, FM_PREFIX)) for_map = Deserialize(fm_info) am_info = Get(ctx, concatkey(bet, AM_PREFIX)) against_map = Deserialize(am_info) stock_ticker = Get(ctx, concatkey(bet, ST_PREFIX)) # retrieve values of interest for this bet from val_list for_rep = val_list[0] against_rep = val_list[1] for_staked = val_list[2] against_staked = val_list[3] for_count = val_list[4] against_count = val_list[5] sign = val_list[6] margin = val_list[7] target_price = val_list[8] seconds = val_list[9] # current price = use oracle # calculate avg rep on both sides of bet for_avg_rep = for_rep / for_count against_avg_rep = against_rep / against_count # calculate probability/odds from amount staked on both sides prob = for_staked / (for_staked + against_staked) # add current price return [ stock_ticker, target_price, sign * margin, for_rep, against_rep, for_avg_rep, against_avg_rep, for_staked, against_staked, prob ]
def addMap(key, value): map1Info = Get(GetContext(), MAPKEY) map1 = Deserialize(map1Info) Notify(["before add, map is ", map1["key1"], map1["key2"]]) map1[key] = value map1.remove("key1") map1Info = Serialize(map1) Put(GetContext(), MAPKEY, map1Info) Notify(["after add, map is ", map1["key2"], map1[key]]) return True
def removeAuthorizedLevel(account): assert (CheckWitness(CEOAddress)) assert (len(account) == 20) authorizedAddressListInfo = Get(GetContext(), AUTHORIZED_ADDRESS_LIST_KEY) assert (authorizedAddressListInfo) authorizedAddressList = Deserialize(authorizedAddressListInfo) assert (_checkInList(account, authorizedAddressList)) index = _findInList(account, authorizedAddressList) # make sure index did exist in authorizedAddressList assert (index < len(authorizedAddressList)) authorizedAddressList.remove(index) Put(GetContext(), AUTHORIZED_ADDRESS_LIST_KEY, Serialize(authorizedAddressList)) Notify(["removeAuthorizedLevel", account]) return True
def _addFromAssetHash(fromAssetHash): fahListInfo = Get(GetContext(), FROM_ASSET_LIST_KEY) fahList = [] if len(fahListInfo) == 0: fahList = [fromAssetHash] else: fahList = Deserialize(fahListInfo) # check exist in current list if not _checkExistInList(fromAssetHash, fahList): # 1024 is the maximum length of an array supported in NeoVM if len(fahList) >= 1024: return False fahList.append(fromAssetHash) Put(GetContext(), FROM_ASSET_LIST_KEY, Serialize(fahList)) return True
def view_ong(address): byte_address = Base58ToAddress(address) if len(byte_address) != 20: Notify(['Invalid address']) return False # check if user list exists user_info = Get(ctx, USERKEY) if user_info: all_users = Deserialize(user_info) else: Notify(['User list is empty']) return False # check if user has been created if address not in all_users: Notify(['User not yet created']) return False # if the above checks hold, we can check the user's wallet. else: param = state(byte_address) ong_balance = Invoke(0, contract_address_ONG, 'balanceOf', param) if ong_balance == '': ong_balance = 0 Notify([ong_balance]) return ong_balance
def createProperty(createList): """ :param createList: [[account1, DNA1],[account2, DNA2]] :return: bool """ RequireWitness(Owner) for createE in createList: account = createE[0] DNA = createE[1] accountCheck = Get(context, concatKey(DNA_PRE_KEY, DNA)) Require(len(account) == 20) Require(DNA > 100000000000000) Require(DNA < 1000000000000000) Require(not accountCheck) DNAlist = Get(context, concatKey(PLAYER_ADDRESS_PRE_KEY, account)) if not DNAlist: DNAlist = [] else: DNAlist = Deserialize(DNAlist) DNAlist.append(DNA) Put(context, concatKey(DNA_PRE_KEY, DNA), account) Put(context, concatKey(PLAYER_ADDRESS_PRE_KEY, account), Serialize(DNAlist)) Notify(["Create property successfully."]) return True
def registerDomain(fulldomain, registerdid, idx, validto): ''' register domain fulldomain: domain string registerdid: register ontid idx:owner walletid validto : valid period ''' currenttime = GetTime() if validto > 0: assert (validto > currenttime) assert (len(fulldomain) > 0) _validateDNSName(fulldomain) lowerdomain = lower(fulldomain) assert (not ownerOf(lowerdomain)) _checkParentAuth(lowerdomain, idx) Put(ctx, _concatkey(OWNER_KEY, lowerdomain), registerdid) Put(ctx, _concatkey(VALID_KEY, lowerdomain), validto) recordskey = _concatkey(RECORDS_KEY, registerdid) records = Get(ctx, recordskey) if not records: records = [lowerdomain] else: records = Deserialize(records) records.append(lowerdomain) assert (len(records) <= MAX_COUNT) Put(ctx, recordskey, Serialize(records)) RegisterDomainEvent(lowerdomain, registerdid, validto) return True
def deleteDomain(fulldomain, idx): ''' delete domain ''' assert (len(fulldomain) > 0) #domain is exist owner = ownerOf(fulldomain) assert (owner) lowerdomain = lower(fulldomain) _checkParentAuth(lowerdomain, idx) Delete(ctx, _concatkey(OWNER_KEY, lowerdomain)) Delete(ctx, _concatkey(VALID_KEY, lowerdomain)) Delete(ctx, _concatkey(VALUE_KEY, lowerdomain)) recordkey = _concatkey(RECORDS_KEY, owner) records = Deserialize(Get(ctx, recordkey)) records = list_remove_elt(records, lowerdomain) if len(records) == 0: Delete(ctx, recordkey) else: Put(ctx, recordkey, Serialize(records)) DeleteDomainEvent(lowerdomain) return True
def unlock(account, idx): KEY_lockCount = concat(USER_LOCK_CNT_PREFIX, account) lockCount = Get(ctx, KEY_lockCount) # idx is smaller than count of lock # require(idx<lockCount,"idx out of range") if idx >= lockCount: return False # Get lock info and releaseAmount KEY_lockInfo = concat(concat(USER_LOCK_PREFIX, account), idx) serializedLockinfo = Get(ctx, KEY_lockInfo) lockInfo = Deserialize(serializedLockinfo) releaseAmount = lockInfo['releaseAmount'] releaseTime = lockInfo['releaseTime'] # releaseAmount is Added to balance balance = Get(ctx, concat(BALANCE_PREFIX, account)) Put(ctx, concat(BALANCE_PREFIX, account), balance + releaseAmount) # last lockinfo copy to this idx lastLockInfo = Get( ctx, concat(concat(USER_LOCK_PREFIX, account), lockCount - 1)) Put(ctx, KEY_lockInfo, lastLockInfo) # delete last lockinfo (duplicated) Delete(ctx, concat(concat(USER_LOCK_PREFIX, account), lockCount - 1)) # decrease lockup count Put(ctx, KEY_lockCount, lockCount - 1) Notify([releaseTime, releaseAmount]) return True
def xshardInvoke(a, b): list = [a, b] argsByteArray = Serialize(list) targetShardId = 2 res = InvokeRemoteShard(targetShardId, X_SHARD_INVOKED_CONTRACT, "invokeCallee", argsByteArray) Put(ctx, X_SHARD_INVOKE_KEY, Deserialize(res)) return True
def view_wallet(address): byte_address = Base58ToAddress(address) if len(byte_address) != 20: Notify(['Invalid address']) return False # check if user list exists user_info = Get(ctx, USERKEY) if user_info: all_users = Deserialize(user_info) else: Notify(['User list is empty']) return False # check if user has been created if address not in all_users: Notify(['User not yet created']) return False # if the above checks hold, we can check the user's wallet. else: params = [byte_address] wallet_balance = RepContract("balanceOf", params) if wallet_balance == '': wallet_balance = 0 Notify([wallet_balance]) return wallet_balance
def user_tab(address): byte_address = Base58ToAddress(address) # check if user list exists user_info = Get(ctx, USERKEY) if user_info: all_users = Deserialize(user_info) else: Notify(['User list is empty']) return False # check if user has been created if address not in all_users: Notify(['User not created']) return False # check if address is valid if len(byte_address) != 20: Notify(['Invalid address']) return False total_rep = view_rep(address) total_wallet = view_wallet(address) total_ong = view_ong(address) Notify([total_rep, total_wallet, total_ong]) return [total_rep, total_wallet, total_ong]