def tokens() -> Iterator: """ Get all tokens minted by the contract :return: an iterator that contains all of the tokens minted by the contract. """ debug(['tokens: ', find(TOKEN_PREFIX)]) return find(TOKEN_PREFIX)
def getPairAttributes(_pair: int) -> Iterator: """ Get all the attributes of a pair :param _pair: index of a pair (find this through `getPairsMap`) :return: b'pair_'{index.to_bytes()}{SEPARATOR}{attribute.to_bytes()}: attribute_value """ return find(bytearray(b'pair_') + bytearray(_pair.to_bytes()) + SEPARATOR)
def cancel_pool(pool_id: UInt256): creator_on_storage = get(POOL_OWNER_KEY + pool_id) if len(creator_on_storage) == 0: raise Exception("Pool doesn't exist.") creator = UInt160(creator_on_storage) if not check_witness(creator): raise Exception('No authorization.') if len(get(POOL_RESULT_KEY + pool_id)) > 0: raise Exception('Pool is finished already') executing_contract = executing_script_hash # refund players bets_key_prefix = POOL_BET_KEY + pool_id bet = find(bets_key_prefix) while bet.next(): result_pair: List[bytes] = bet.value storage_key = result_pair[0] account = UInt160(storage_key[len(bets_key_prefix):]) transfer_gas(executing_contract, account, PRICE_IN_GAS) # set result put(POOL_RESULT_KEY + pool_id, serialize('Cancelled by owner'))
def getPairsMap(_col: UInt160) -> Iterator: """ Get the allowed paired token of a collateral (find a collateral with `getCollaterals`) :param _col: the collateral token address as the key to the paired token addresses :return: b'pairs'{collateral_address}{SEPARATOR}{paired_address}{SEPARATOR}{expiry.to_bytes()}{SEPARATOR}{_mintRatio.to_bytes()}: pair_index.to_bytes() """ return find(bytearray(b'pairs') + _col, current_storage_context)
def get_pool(pool_id: UInt256) -> list: creator = get(POOL_OWNER_KEY + pool_id) if len(creator) == 0: raise Exception("Pool doesn't exist.") description = get(POOL_DESCRIPTION_KEY + pool_id).to_str() options: List[str] = deserialize(get(POOL_OPTIONS_KEY + pool_id)) result = None serialized_result = get(POOL_RESULT_KEY + pool_id) if len(serialized_result) > 0: result = deserialize(serialized_result) pool_bets: Dict[bytes, str] = {} bet_prefix = POOL_BET_KEY + pool_id bets = find(bet_prefix) while bets.next(): result_pair = bets.value storage_key = cast(bytes, result_pair[0]) player_bet = cast(str, result_pair[1]) player_id = storage_key[len(bet_prefix):].to_str().to_bytes() pool_bets[player_id] = player_bet return [pool_id, creator, description, options, result, pool_bets]
def search_storage(prefix: str) -> dict: data_list = {} data = storage.find(prefix) while data.next(): data_list[data.value[0]] = StdLib.deserialize( cast(bytes, data.value[1])) return data_list
def search_storage(prefix: str) -> dict: data_list = {} data = storage.find(prefix) while data.next(): iterator_value = data.value key: str = iterator_value[0] serialized_value: bytes = iterator_value[1] value = StdLib.deserialize(serialized_value) data_list[key] = value return data_list
def test_end_while_jump() -> bool: iterator = find(b'feesMap') fee_receiver = get(FEE_RECEIVER_KEY) while iterator.next(): token_bytes = cast(bytes, iterator.value[0]) token_bytes = token_bytes[ 7:] # cut 'feesMap' at the beginning of the bytes token = cast(UInt160, token_bytes) fee_amount = cast(int, iterator.value[1]) if fee_amount > 0: notify([token, executing_script_hash, fee_receiver, fee_amount]) return True
def tokens_of(owner: UInt160) -> Iterator: """ Get a iterator with all token ids owned by an address. The values inside the Iterator SHOULD be a ByteString with a length of no more than 64 bytes. The parameter owner must be a 20-byte address represented by a UInt160. :param owner: the account address to retrieve the balance for :type owner: UInt160 """ assert len(owner) == 20 return storage.find(account_prefix_key(owner))
def tokensOf(owner: UInt160) -> Iterator: """ Get all of the token ids owned by the specified address The parameter owner must be a 20-byte address represented by a UInt160. :param owner: the owner address to retrieve the tokens for :type owner: UInt160 :return: an iterator that contains all of the token ids owned by the specified address. :raise AssertionError: raised if `owner` length is not 20. """ assert len(owner) == 20, "Incorrect `owner` length" return find(mk_account_key(owner))
def getFeesMap() -> Iterator: """ How many fees of each kind of token can be collected :return: collateral_token: amount_of_fees.to_bytes() """ administrator = get(ADMINISTRATOR_KEY) if not (check_witness(administrator) or calling_script_hash == administrator): fee_receiver = get( FEE_RECEIVER_KEY ) # if not admin, get fee_receiver. This saves GAS for executing this API assert check_witness( fee_receiver) or calling_script_hash == fee_receiver return find(b'feesMap')
def list_on_going_pools() -> list: pools = [] created_pools = find(POOL_OWNER_KEY) while created_pools.next(): result_pair = created_pools.value storage_key = cast(bytes, result_pair[0]) pool_id = storage_key[len(POOL_OWNER_KEY):] pool_hash: UInt256 = pool_id if len(get(POOL_RESULT_KEY + pool_id)) == 0: # open is still open pool = get_pool(pool_hash) pools.append(pool) return pools
def finish_pool(pool_id: UInt256, winner_options: List[str]): creator = get(POOL_OWNER_KEY + pool_id) if len(creator) == 0: raise Exception("Pool doesn't exist.") if not check_witness(creator): raise Exception('No authorization.') if len(get(POOL_RESULT_KEY + pool_id)) > 0: raise Exception('Pool is finished already') if len(winner_options) == 0: raise Exception('At least one winner is required') # validate all winner options are valid options winner_options: List[str] = remove_duplicates(winner_options) pool_options: List[str] = deserialize(get(POOL_OPTIONS_KEY + pool_id)) for option in winner_options: if option not in pool_options: raise Exception('Invalid option for this pool') winners: List[UInt160] = [] # get winner players bets_key_prefix = POOL_BET_KEY + pool_id bet = find(bets_key_prefix) while bet.next(): result_pair = bet.value storage_key = cast(bytes, result_pair[0]) account_bet = cast(str, result_pair[1]) if account_bet in winner_options: address = storage_key[len(bets_key_prefix):] account = UInt160(address) winners.append(account) # distribute the prizes if len(winners) > 0: total_stake = get(POOL_TOTAL_STAKE_KEY + pool_id).to_int() prize_per_winner = total_stake // len(winners) executing_contract = executing_script_hash for winner in winners: transfer_gas(executing_contract, winner, prize_per_winner) # set result put(POOL_RESULT_KEY + pool_id, serialize(winner_options))
def getSenderStreams(sender: UInt160) -> str: """ Get all streams where address is sender Args: sender (UInt160): address as UInt160 Returns: str: JSON array of stream IDs """ sender64 = base64_encode(sender) streams = find('bysender/' + sender64) ret = '' while streams.next(): ret = ret + itoa(cast(bytes, streams.value[1]).to_int()) + ',' if len(ret) > 0: ret = '[' + ret[:-1] return ret + ']' return '[]'
def getRecipientStreams(recipient: UInt160) -> str: """ Get all streams where address is recipient Args: recipient (UInt160): address as UInt160 Returns: str: JSON array of stream IDs """ recipient64 = base64_encode(recipient) streams = find('byrecipient/' + recipient64) ret = '' while streams.next(): ret = ret + itoa(cast(bytes, streams.value[1]).to_int()) + ',' if len(ret) > 0: ret = '[' + ret[:-1] return ret + ']' return '[]'
def collectFees() -> bool: """ Collect all the fees No need to check witness? Because the fee is always given to the fee receiver. :return: True """ iterator = find(b'feesMap') fee_receiver = get(FEE_RECEIVER_KEY) while iterator.next(): token_bytes = cast(bytes, iterator.value[0]) token_bytes = token_bytes[ 7:] # cut 'feesMap' at the beginning of the bytes token = cast(UInt160, token_bytes) fee_amount = cast(bytes, iterator.value[1]).to_int() if fee_amount > 0: feesMap.put(token, 0) assert call_contract(token, 'transfer', [ executing_script_hash, fee_receiver, fee_amount, 'Collect Fees' ]) return True
def dict_iterator(prefix: str) -> str: it = find(prefix) it.next() return it.value # 'find' Iterator value is Union[str, bytes]
def find_by_prefix(prefix: Union[str, bytes]) -> Iterator: context = get_context() return find(prefix, context)
def test_iterator(prefix: str) -> Union[tuple, None]: it = find(prefix) if it.next(): return it.value return None
def getCollaterals() -> Iterator: """ :return: {collateral_address}: 1.to_bytes() """ return find(b'collaterals', current_storage_context)
def storage_find_is_context() -> bool: storage_find_iterator = find('unit_test') return is_iterator(storage_find_iterator)
def find_by_prefix(prefix: bytes) -> Iterator: return find(prefix)
def find_by_prefix(prefix: str) -> Iterator: return storage.find(prefix)
def return_iterator() -> iterator.Iterator: return find('random_prefix')
def has_next(prefix: str) -> bool: return find(prefix).next()
def find_by_prefix(prefix: int) -> Iterator: return find(prefix)
def find_by_prefix(prefix: ByteString) -> Iterator: context = get_context() return find(prefix, context)