def get_default_limit( user: User, token: Token) -> Optional[Tuple[AggregateLimit, TransferAmount]]: """ :param user: :param token: :return: lowest amount limit applicable for a given CreditTransfer """ with ephemeral_alchemy_object( CreditTransfer, transfer_type=TransferTypeEnum.PAYMENT, transfer_subtype=TransferSubTypeEnum.AGENT_OUT, sender_user=user, recipient_user=user, token=token, amount=0) as dummy_transfer: limits = dummy_transfer.get_transfer_limits() if len(limits) == 0: return None amount_limits = filter( lambda l: isinstance(l, AggregateTransferAmountMixin), limits) with_amounts = [(limit, limit.available(dummy_transfer)) for limit in amount_limits] sorted_amount_limits = sorted(with_amounts, key=lambda l: l[1]) return sorted_amount_limits[0] if len( sorted_amount_limits) > 0 else None
def get_default_limit( user: User, token: Token, transfer_account: TransferAccount) -> Optional[TransferLimit]: """ :param user: :param token: :param transfer_account: :return: lowest limit applicable for a given CreditTransfer """ with ephemeral_alchemy_object( CreditTransfer, transfer_type=TransferTypeEnum.PAYMENT, transfer_subtype=TransferSubTypeEnum.AGENT_OUT, sender_user=user, recipient_user=user, token=token, amount=0) as dummy_transfer: limits = dummy_transfer.get_transfer_limits() if len(limits) == 0: return None else: # should only ever be one ge limit ge_limit = [ limit for limit in limits if 'GE Liquid Token' in limit.name ] lowest_limit = None lowest_amount_avail = float('inf') for limit in limits: if 'GE Liquid Token' not in limit.name: transaction_volume = limit.apply_all_filters( dummy_transfer, db.session.query( func.sum(CreditTransfer.transfer_amount).label( 'total'))).execution_options( show_all=True).first().total amount_avail = limit.total_amount - (transaction_volume or 0) fraction = ge_limit[0].transfer_balance_fraction or 0 if amount_avail < (fraction * transfer_account.balance)\ and amount_avail < lowest_amount_avail: lowest_limit = limit lowest_amount_avail = amount_avail if lowest_limit: return lowest_limit else: return ge_limit[0]
def token_string(limit_type: Type[AggregateLimit], t: dict): if isinstance(t['limit'], limit_type): with ephemeral_alchemy_object( CreditTransfer, transfer_type=TransferTypeEnum.PAYMENT, transfer_subtype=TransferSubTypeEnum.AGENT_OUT, sender_user=user, recipient_user=user, token=t['token'], amount=0) as dummy_transfer: allowed_amount = f"{rounded_dollars(str(t['limit'].available_base(dummy_transfer)))}" rounded_rate = round_to_sig_figs(t['exchange_rate'], 3) return ( f"{allowed_amount} {t['name']} (1 {t['name']} = {rounded_rate} {reserve_token.symbol})" ) else: return ""