def checkTokenId(tokenId): # here we check if the tokenId is legal with the help of getting its name if Get(GetContext(), concatkey(tokenId, NAME)): return True else: return False #################### Optional methods defination starts ######################
def allowance(owner, spender, tokenId): """ :param owner: :param spender: :param tokenId: :return: """ key = concatkey(concatkey(concatkey(tokenId, APPROVE), owner), spender) return Get(GetContext(), key)
def _transfer(_from, _to, _amount): fromKey = concat(BALANCE_PREFIX, _from) fromBalance = Get(ctx, fromKey) if _amount > fromBalance: return False if _amount == fromBalance: Delete(ctx, fromKey) else: Put(ctx, fromKey, fromBalance - _amount) toKey = concat(BALANCE_PREFIX, _to) toBalance = Get(ctx, toKey) Put(ctx, toKey, toBalance + _amount) # Notify(["transfer", AddressToBase58(from_acct), AddressToBase58(to_acct), amount]) # TransferEvent(AddressToBase58(from_acct), AddressToBase58(to_acct), amount) TransferEvent(_from, _to, _amount) return True
def allowance(owner, spender): """ check how many token the spender is allowed to spend from owner account :param owner: token owner :param spender: token spender :return: the allowed amount of tokens """ key = concat(concat(APPROVE_PREFIX, owner), spender) return Get(ctx, key) + 0
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 get_user_name(user_address): if len(user_address) == 34: user_address = Base58ToAddress(user_address) assert (len(user_address) == 20 and user_address != ZERO_ADDRESS) user_name = Get(ctx, concat(user_address, USER_KEY)) if not user_name: user_name = "" return user_name
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 getDiskStatus(diskId): """ :param diskId: :return: 0 means the diskId has NOT been ended yet. 1 means the diskId has already been ended. ENDED means all the players' accounts have been settled. """ return Get(GetContext(), concatKey(DISK_STATUS_PERFIX, diskId))
def balanceOf(owner): """ :param owner: :return: token balance of the owner """ if len(owner) != 20: return False key = concatkey(OWNER_BALANCE_PREFIX, owner) return Get(ctx, key)
def queryTokenIDByIndex(idx): ''' query tokenid by index :param idx: :return: ''' tokenID = Get(ctx, concatkey(TOKEN_INDEX_PREFIX, idx)) # Notify(["111_queryTokenIDByIndex", tokenID]) return tokenID
def active_bets(): ab_info = Get(ctx, ABKEY) if not ab_info: Notify(['Active bet list is empty']) return False else: active_bets = Deserialize(ab_info) Notify([active_bets]) 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 averageGen0SalePrice(): sum = 0 prices = [0, 1, 2, 3, 4] for i in prices: price = Get(ctx, _concatkey(LAST_GEN0_SALE_PRICE, i)) if not price: price = 0 sum = Add(sum, price) return Div(sum, 5)
def balanceOf(acct, tokenId): """ get balance of account in terms of token with the tokenId :param acct: used to check the acct balance :param tokenId: the tokenId determines which token balance of acct needs to be checked :return: the balance of acct in terms of tokenId tokens """ return Get(GetContext(), _concatkey(_concatkey(BALANCE_PREFIX, tokenId), acct))
def balanceOf(address): """ Returns the balance for the given address :param address: The address to check """ RequireIsAddress(address) key = getBalanceKey(address) return Get(ctx, key)
def bindAssetHash(fromAssetHash, toChainId, toAssetHash): assert (CheckWitness(Get(GetContext(), OPERATOR_PREFIX))) assert (_addFromAssetHash(fromAssetHash)) Put(GetContext(), concat(ASSET_HASH, concat(fromAssetHash, toChainId)), toAssetHash) curBalance = getBalanceFor(fromAssetHash) Notify( ["bindAssetHash", fromAssetHash, toChainId, toAssetHash, curBalance]) return True
def allowance(owner, spender, tokenId): """ :param owner: :param spender: :param tokenId: :return: """ key = _concatkey(_concatkey(_concatkey(APPROVE_PREFIX, tokenId), owner), spender) return Get(GetContext(), key)
def _onlyAuthorizedLevel(): authorizedAddressListInfo = Get(GetContext(), AUTHORIZED_ADDRESS_LIST_KEY) if not authorizedAddressListInfo: return False authorizedAddressList = Deserialize(authorizedAddressListInfo) for authorizedAddress in authorizedAddressList: if CheckWitness(authorizedAddress): return True return False
def user_record(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 # check if user has participated in any bets bl_info = Get(ctx, concatkey(address, BL_PREFIX)) if bl_info: bet_list = Deserialize(bl_info) else: Notify(['No bets']) return False # if the user has participated in bets, he/she has a track record_info, as well as profit info record_info = Get(ctx, concatkey(address, RC_PREFIX)) record_map = Deserialize(record_info) profit_info = Get(ctx, concatkey(address, PT_PREFIX)) profit_map = Deserialize(profit_info) # create returnable list of user's record record_list = [] profit_list = [] for bet in bet_list: record_list.append(record_map[concatkey(bet, BT_PREFIX)]) profit_list.append(profit_map[concatkey(bet, BT_PREFIX)]) Notify(['record', bet_list, record_list, profit_list]) return [bet_list, record_list, profit_list]
def transferOwnership(account): onlyOwner() require(len(account) == 20, "address length error") previousOwner = Get(ctx, OWNER_KEY) Put(ctx, OWNER_KEY, account) TransferOwnershipEvent(previousOwner, account) return True
def valueOf(fulldomain): ''' value of domain ''' assert (isDomainValid(fulldomain)) lowerdomain = lower(fulldomain) rawvalue = Get(ctx, _concatkey(VALUE_KEY, lowerdomain)) value = Deserialize(rawvalue) return _concatkey(value[0], value[1])
def transferFrom(spender,from_acct,to_acct,amount): """ spender spends amount of tokens on the behalf of from_acct, spender makes a transaction of amount of tokens from from_acct to to_acct :param spender: :param from_acct: :param to_acct: :param amount: :return: """ require(len(spender) == 20 and len(from_acct) == 20 and len(to_acct) == 20, "address length error") require(CheckWitness(spender) == True, "Invalid invoker") whenNotPaused() requireNotFreeze(spender) requireNotFreeze(from_acct) requireNotFreeze(to_acct) fromKey = concat(BALANCE_PREFIX, from_acct) fromBalance = Get(ctx, fromKey) require(amount <= fromBalance and amount > 0, "Invalid amount") approveKey = concat(concat(APPROVE_PREFIX,from_acct),spender) approvedAmount = Get(ctx,approveKey) toKey = concat(BALANCE_PREFIX,to_acct) require(amount <= approvedAmount, "Invalid amount") if amount == approvedAmount: Delete(ctx,approveKey) Put(ctx, fromKey, fromBalance - amount) else: Put(ctx,approveKey,approvedAmount - amount) Put(ctx, fromKey, fromBalance - amount) toBalance = Get(ctx, toKey) Put(ctx, toKey, toBalance + amount) # Notify(["transfer", AddressToBase58(from_acct), AddressToBase58(to_acct), amount]) # TransferEvent(AddressToBase58(from_acct), AddressToBase58(to_acct), amount) TransferEvent(from_acct, to_acct, amount) return True
def get_bet_details(betId): if betId > get_latest_bet(): return 0 bet_details_info = Get(ctx, concatkey(betId, BET_DETAILS_PREFIX)) bet_details = Deserialize(bet_details_info) return [ bet_details["sign"], bet_details["margin"], bet_details["target_price"], bet_details["vote_end_time"], bet_details["stock_ticker"] ]
def get_bet_content(betId, for_against): assert (for_against == 0 or for_against == 1) assert (check_bet_status(betId) > 0) bet_content_info = Get( ctx, concatkey(betId, concatkey(for_against, BET_CONTENT_PREFIX))) assert (bet_content_info) bet_content = Deserialize(bet_content_info) return [ bet_content["reputation"], bet_content["count"], bet_content["staked"] ]
def add_map(bet, key, value, prefix): map_info = Get(ctx, concatkey(bet, prefix)) maps = Deserialize(map_info) # add data maps[key] = value map_info = Serialize(maps) Put(ctx, concatkey(bet, prefix), map_info) Notify(['add_map', key, value]) return True
def getDomains(did): ''' get domains owned by did ''' reocordsRaw = Get(ctx, _concatkey(RECORDS_KEY, did)) if not reocordsRaw: return '' else: records = Deserialize(reocordsRaw) return join(',', records)
def setGen0Price(_caller, _price): Require(_onlyDragonCore(_caller)) gen0SaleCount = Get(ctx, GEN0_SALE_COUNT) if not gen0SaleCount: gen0SaleCount = 0 # Track gen0 sale prices Put(ctx, _concatkey(LAST_GEN0_SALE_PRICE, gen0SaleCount % 5), _price) Put(ctx, GEN0_SALE_COUNT, Add(gen0SaleCount, 1)) return True
def MigrateContract(code): addr = Get(GetContext(), KeyOwnerAddress) assert (len(addr) != 0) assert (CheckWitness(addr)) success = Migrate(code, True, "name", "version", "author", "email", "description") assert (success) Notify(["Migrate successfully", success]) return success
def get_bind_map(bucket): bind_data = Get(ctx, concat(KEY_BUCKET, bucket)) bind_map = None if not bind_data: bind_map = {} else: bind_map = Deserialize(bind_data) return bind_map
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 # since this is an active bet, all of the below data structures are populated else: # FR, AR, FS, AS, FC, AC, UD, MA, TP, SE val_info = Get(ctx, concatkey(bet, VAL_PREFIX)) val_list = Deserialize(val_info) # 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] date = val_list[9] stock_ticker = val_list[10] for_avg_rep = val_list[11] against_avg_rep = val_list[12] prob = val_list[13] Notify([ 'bet_info', bet, stock_ticker, target_price, sign, margin, for_rep, against_rep, for_avg_rep, against_avg_rep, for_staked, against_staked, date, prob ]) return True