def move_to_main( coin_properties: CoinProperties, user, wallet_balance ):
    if wallet_balance <= 0:
        return

    if clientcommandprocessor.run_client_command( coin_properties.rpc_configuration, 'move', None, user, '', wallet_balance ):
        connection = database.create_connection()
        with connection:
            database.execute_query( connection, Statements[ 'INSERT_USER' ], (user,) )
            database.execute_query( connection, Statements[ 'UPDATE_USER_BALANCE' ], (user, coin_properties.ticker, str( wallet_balance ),) )
    else:
        raise Exception( f'Failed to move {user} balance {wallet_balance} to main account.' )
def get_user_balance( user, coin_properties: CoinProperties ):
    try:
        user_balance = clientcommandprocessor.run_client_command( coin_properties.rpc_configuration, 'getbalance', None, user )
        connection = database.create_connection()

        with connection:
            user_off_chain_balance = database.fetch_result( connection, Statements[ 'SELECT_USER_OFF_CHAIN_BALANCE' ], (user, coin_properties.ticker) )

        if user_off_chain_balance is None:
            user_off_chain_balance = 0

        user_off_chain_balance = round_down( user_off_chain_balance, 8 )
        total_balance = user_balance + user_off_chain_balance

        return total_balance, user_balance
    except BotUserError:
        raise BotUserError
Beispiel #3
0
def withdraw(update, coin_properties: CoinProperties):
    ticker = coin_properties.ticker
    arguments = update.message.text.split(' ')

    user = commonhelper.get_username(update)

    if len(arguments) < 3:
        raise BotUserError(messages.withdraw_error(ticker.lower()))

    address = arguments[1]
    address = commonhelper.get_validated_address(address, coin_properties)
    amount = arguments[2]
    total_balance, wallet_balance = commonhelper.get_user_balance(
        user, coin_properties)
    amount = commonhelper.get_validated_amount(amount, user, total_balance)
    configured_transaction_fee = round_down(coin_properties.withdrawal_fee, 8)

    if amount < coin_properties.minimum_withdraw:
        raise BotUserError(
            f'Minimum allowed withdrawal amount is {configured_transaction_fee}{ticker}.'
        )

    if arguments[2].lower is 'all':
        amount -= configured_transaction_fee
    elif amount + configured_transaction_fee > total_balance:
        raise BotUserError(
            f'Unable to withdraw {amount}{ticker}. '
            f'Amount combined with withdrawal fee {configured_transaction_fee}{ticker} exceeds the available balance: '
            f'{round_down( amount + configured_transaction_fee, 8 )}{ticker} > {round_down( total_balance, 8 )}{ticker}.'
        )

    commonhelper.move_to_main(coin_properties, user, wallet_balance)
    transaction_id = clientcommandprocessor.run_client_command(
        coin_properties.rpc_configuration, 'sendtoaddress', None, address,
        amount)
    real_transaction_fee = commonhelper.get_transaction_fee(
        coin_properties.rpc_configuration, transaction_id)

    users_balance_changes = [
        (user, ticker, str(-amount - configured_transaction_fee)),
        (Configuration.TELEGRAM_BOT_NAME, ticker,
         str(configured_transaction_fee + real_transaction_fee))
    ]

    try:
        connection = database.create_connection()
        with connection:
            database.execute_many(connection,
                                  Statements['UPDATE_USER_BALANCE'],
                                  users_balance_changes)
    except Exception as e:
        logger.error(e)
        return messages.GENERIC_ERROR

    blockchain_explorer: BlockchainExplorerConfiguration = coin_properties.blockchain_explorer

    if blockchain_explorer is not None:
        address = f'<a href="{blockchain_explorer.url}/{blockchain_explorer.address_prefix}/{address}">{address}</a>'
        transaction_id = f'<a href="{blockchain_explorer.url}/{blockchain_explorer.tx_prefix}/{transaction_id}">{transaction_id}</a>'

    return f'@{user} has successfully withdrawn {amount}{ticker} to address: {address}.<pre>\r\n</pre>TX:&#160;{transaction_id}. ' \
           f'Withdrawal fee of {configured_transaction_fee}{ticker} was applied.', 'HTML'
Beispiel #4
0
def deposit(update, coin_properties: CoinProperties):
    user = commonhelper.get_username(update)
    deposit_address = clientcommandprocessor.run_client_command(
        coin_properties.rpc_configuration, 'getaccountaddress', None, user)

    return f'@{user}, Your depositing address is: {deposit_address}',
def get_transaction_fee( rpc_configuration: RpcConfiguration, transaction_id ):
    if transaction_id is None:
        raise Exception( 'Transaction id is missing' )

    return clientcommandprocessor.run_client_command( rpc_configuration, 'gettransaction', 'fee', transaction_id )
def get_validated_address( address, coin_properties: CoinProperties ):
    if len( address ) == 34:
        if clientcommandprocessor.run_client_command( coin_properties.rpc_configuration, 'validateaddress', 'isvalid', address ):
            return address

    raise BotUserError( f'´{address}´ is not a valid address.' )