示例#1
0
class BittrexWrapper(object):

    def __init__(self, apiKey, apiSecret):
	self.apiKey = apiKey
	self.apiSecret = apiSecret
	self.api = Bittrex(self.apiKey, self.apiSecret)
	self.failure = (False, 0, 0)

    def get_price(self, market_code, base_code='BTC'):
	self.currency_code = '%s-%s' % (base_code, market_code)
	price_ticker = bittrex_api.get_ticker(currency_code)
	if price_ticker['success']:
		return price_ticker
	else:
		return {}

    def get_address(self):
        deposit_address_query = self.api.get_deposit_address(base_code)
        if deposit_address_query['success'] == False and deposit_address_query['message'] == 'ADDRESS_GENERATING':
                time.sleep(10)
                deposit_address_query = self.api.get_deposit_address(base_code)

    def buy_coins(self, market_code, price, base_code='BTC'):
	currency_code = '%s-%s' % (base_code, market_code)

	price_ticker = self.api.get_ticker(currency_code)
	if price_ticker['success']:
		ask_price = price_ticker['result']['Ask']
	else:
		return self.failure

	quantity = price / ask_price 
	sell_market_query = self.api.buy_limit(currency_code, quantity, ask_price)
	if sell_market_query['success']:
		order_uuid = sell_market_query['result']['uuid']
	elif not sell_market_query['success']:
		return self.Failure

	# Sleep for long enough for bittrex to process the order
	time.sleep(30)  # 4) Check the Order
	order_status_query = bittrex_api.get_order(order_uuid)
	if order_status_query['success']:
		aOrder = dict()
		aOrder['price'] = order_status_query['result']['Price']
		aOrder['Quantity'] = order_status_query['result']['Quantity']
		aOrder['Exchange'] = order_status_query['result']['Exchange']
		aOrder['completed'] = True
		return aOrder['completed'], aOrder['price'], aOrder['Quantity']
	else:
		return self.Failure
示例#2
0
class TestBittrexAccountAPI(unittest.TestCase):
    def setUp(self):
        self.bittrex = Bittrex()

    def test_get_balances(self):
        actual = self.bittrex.get_balances()
        test_basic_response(self, actual, 'get_balances')

    def test_get_balance(self):
        self.assertRaises(TypeError, self.bittrex.get_balance)
        actual = self.bittrex.get_balance('BTC')
        test_basic_response(self, actual, 'get_balance')
        invalid_actual = self.bittrex.get_balance('Invalid currency')
        test_failed_response(self, invalid_actual, 'get_balance')

    def test_get_deposit_address(self):
        self.assertRaises(TypeError, self.bittrex.get_deposit_address)
        actual = self.bittrex.get_deposit_address('BTC')
        test_basic_response(self, actual, 'get_deposit_address')
        invalid_actual = self.bittrex.get_deposit_address('Invalid currency')
        test_failed_response(self, invalid_actual, 'get_deposit_address')

    def test_withdraw(self):
        self.assertRaises(TypeError, self.bittrex.withdraw)

    def test_get_order(self):
        self.assertRaises(TypeError, self.bittrex.get_order)
        actual = self.bittrex.get_order('test')
        test_response_structure(self, actual, 'get_order')

    def test_get_order_history(self):
        actual = self.bittrex.get_order_history()
        test_basic_response(self, actual, 'get_order_history')
        actual = self.bittrex.get_order_history('BTC-LTC')
        test_basic_response(self, actual, 'get_order_history')

    def test_get_withdrawal_historyself(self):
        actual = self.bittrex.get_withdrawal_history()
        test_basic_response(self, actual, 'get_withdrawal_history')
        actual = self.bittrex.get_withdrawal_history('BTC')
        test_basic_response(self, actual, 'get_withdrawal_history')

    def test_get_deposit_history(self):
        actual = self.bittrex.get_deposit_history()
        test_basic_response(self, actual, 'get_deposit_history')
        actual = self.bittrex.get_deposit_history('BTC')
        test_basic_response(self, actual, 'get_deposit_history')
class TestBittrexV20AccountAPI(unittest.TestCase):
    """
    Integration tests for the Bittrex Account API.
      * These will fail in the absence of an internet connection or if bittrex API goes down.
      * They require a valid API key and secret issued by Bittrex.
      * They also require the presence of a JSON file called secrets.json.
      It is structured as such:
    {
      "key": "12341253456345",
      "secret": "3345745634234534"
    }
    """
    def setUp(self):
        with open("secrets.json") as secrets_file:
            self.secrets = json.load(secrets_file)
            secrets_file.close()
        self.bittrex = Bittrex(self.secrets['key'],
                               self.secrets['secret'],
                               api_version=API_V2_0)

    def test_handles_invalid_key_or_secret(self):
        self.bittrex = Bittrex('invalidkey',
                               self.secrets['secret'],
                               api_version=API_V2_0)
        actual = self.bittrex.get_balance('BTC')
        test_auth_basic_failures(self, actual, 'Invalid key, valid secret')

        self.bittrex = Bittrex(None,
                               self.secrets['secret'],
                               api_version=API_V2_0)
        actual = self.bittrex.get_balance('BTC')
        test_auth_basic_failures(self, actual, 'None key, valid secret')

        self.bittrex = Bittrex(self.secrets['key'],
                               'invalidsecret',
                               api_version=API_V2_0)
        actual = self.bittrex.get_balance('BTC')
        test_auth_basic_failures(self, actual, 'valid key, invalid secret')

        self.bittrex = Bittrex(self.secrets['key'], None, api_version=API_V2_0)
        actual = self.bittrex.get_balance('BTC')
        test_auth_basic_failures(self, actual, 'valid key, None secret')

        self.bittrex = Bittrex('invalidkey',
                               'invalidsecret',
                               api_version=API_V2_0)
        actual = self.bittrex.get_balance('BTC')
        test_auth_basic_failures(self, actual, 'invalid key, invalid secret')

    def test_get_openorders(self):
        actual = self.bittrex.get_open_orders('BTC-LTC')
        test_basic_response(self, actual, "get_openorders")
        self.assertTrue(isinstance(actual['result'], list),
                        "result is not a list")

    def test_get_balances(self):
        actual = self.bittrex.get_balances()
        test_basic_response(self, actual, "get_balances")
        self.assertTrue(isinstance(actual['result'], list),
                        "result is not a list")

    @unittest.skip(
        "the return result is an empty dict.  API bug?  the 2.0 get_balances works as expected"
    )
    def test_get_balance(self):
        actual = self.bittrex.get_balance('BTC')
        test_basic_response(self, actual, "get_balance")
        self.assertTrue(isinstance(actual['result'], dict),
                        "result is not a dict")
        self.assertEqual(
            actual['result']['Currency'], "BTC",
            "requested currency {0:s} does not match returned currency {1:s}".
            format("BTC", actual['result']['Currency']))

    @unittest.skip("my testing account is acting funny this should work")
    def test_get_depositaddress(self):
        actual = self.bittrex.get_deposit_address('BTC')
        test_basic_response(self, actual, "get_deposit_address")

    def test_get_order_history_all_markets(self):
        actual = self.bittrex.get_order_history()
        test_basic_response(self, actual, "get_order_history")
        self.assertIsInstance(actual['result'], list, "result is not a list")

    def test_get_order_history_one_market(self):
        actual = self.bittrex.get_order_history(market='BTC-LTC')
        test_basic_response(self, actual, "get_order_history")
        self.assertIsInstance(actual['result'], list, "result is not a list")

    def test_get_withdrawlhistory_all_currencies(self):
        actual = self.bittrex.get_withdrawal_history()
        test_basic_response(self, actual, "get_withdrawal_history")
        self.assertIsInstance(actual['result'], list, "result is not a list")

    def test_get_withdrawlhistory_one_currency(self):
        actual = self.bittrex.get_withdrawal_history('BTC')
        test_basic_response(self, actual, "get_withdrawal_history")
        self.assertIsInstance(actual['result'], list, "result is not a list")

    def test_get_deposithistory_all_currencies(self):
        actual = self.bittrex.get_deposit_history()
        test_basic_response(self, actual, "get_deposit_history")
        self.assertIsInstance(actual['result'], list, "result is not a list")

    def test_get_deposithistory_one_currency(self):
        actual = self.bittrex.get_deposit_history('BTC')
        test_basic_response(self, actual, "get_deposit_history")
        self.assertIsInstance(actual['result'], list, "result is not a list")

    def test_get_pending_withdrawals_all_currencies(self):
        actual = self.bittrex.get_pending_withdrawals()
        test_basic_response(self, actual, "get_pending_withdrawals")
        self.assertIsInstance(actual['result'], list, "result is not a list")

    def test_get_pending_withdrawals_one_currency(self):
        actual = self.bittrex.get_pending_withdrawals('BTC')
        test_basic_response(self, actual, "get_pending_withdrawals")
        self.assertIsInstance(actual['result'], list, "result is not a list")

    def test_get_pending_deposits_all_currencies(self):
        actual = self.bittrex.get_pending_deposits()
        test_basic_response(self, actual, "get_pending_deposits")
        self.assertIsInstance(actual['result'], list, "result is not a list")

    def test_get_pending_deposits_one_currency(self):
        actual = self.bittrex.get_pending_deposits('BTC')
        test_basic_response(self, actual, "get_pending_deposits")
        self.assertIsInstance(actual['result'], list, "result is not a list")

    def test_generate_deposit_address(self):
        actual = self.bittrex.generate_deposit_address(currency='BTC')
        test_basic_response(self, actual, "generate_deposit_address")
        self.assertIsInstance(actual['result'], list, "result is not a list")
class TestBittrexV11AccountAPI(unittest.TestCase):
    """
    Integration tests for the Bittrex Account API.
      * These will fail in the absence of an internet connection or if bittrex API goes down.
      * They require a valid API key and secret issued by Bittrex.
      * They also require the presence of a JSON file called secrets.json.
      It is structured as such:
    {
      "key": "12341253456345",
      "secret": "3345745634234534"
    }
    """
    def setUp(self):
        with open("secrets.json") as secrets_file:
            self.secrets = json.load(secrets_file)
            secrets_file.close()
        self.bittrex = Bittrex(self.secrets['key'], self.secrets['secret'])

    def test_handles_invalid_key_or_secret(self):
        self.bittrex = Bittrex('invalidkey', self.secrets['secret'])
        actual = self.bittrex.get_balance('BTC')
        test_auth_basic_failures(self, actual, 'Invalid key, valid secret')

        self.bittrex = Bittrex(None, self.secrets['secret'])
        actual = self.bittrex.get_balance('BTC')
        test_auth_basic_failures(self, actual, 'None key, valid secret')

        self.bittrex = Bittrex(self.secrets['key'], 'invalidsecret')
        actual = self.bittrex.get_balance('BTC')
        test_auth_basic_failures(self, actual, 'valid key, invalid secret')

        self.bittrex = Bittrex(self.secrets['key'], None)
        actual = self.bittrex.get_balance('BTC')
        test_auth_basic_failures(self, actual, 'valid key, None secret')

        self.bittrex = Bittrex('invalidkey', 'invalidsecret')
        actual = self.bittrex.get_balance('BTC')
        test_auth_basic_failures(self, actual, 'invalid key, invalid secret')

    def test_get_openorders(self):
        actual = self.bittrex.get_open_orders('BTC-LTC')
        test_basic_response(self, actual, "get_openorders")
        self.assertTrue(isinstance(actual['result'], list),
                        "result is not a list")

    def test_get_balances(self):
        actual = self.bittrex.get_balances()
        test_basic_response(self, actual, "get_balances")
        self.assertTrue(isinstance(actual['result'], list),
                        "result is not a list")

    def test_get_balance(self):
        actual = self.bittrex.get_balance('BTC')
        test_basic_response(self, actual, "get_balance")
        self.assertTrue(isinstance(actual['result'], dict),
                        "result is not a dict")
        self.assertEqual(
            actual['result']['Currency'], "BTC",
            "requested currency {0:s} does not match returned currency {1:s}".
            format("BTC", actual['result']['Currency']))

    def test_get_depositaddress(self):
        actual = self.bittrex.get_deposit_address('BTC')
        if not actual['success']:
            self.assertTrue(actual['message'], 'ADDRESS_GENERATING')
        else:
            test_basic_response(self, actual, "get_deposit_address")

    def test_get_order_history_all_markets(self):
        actual = self.bittrex.get_order_history()
        test_basic_response(self, actual, "get_order_history")
        self.assertIsInstance(actual['result'], list, "result is not a list")

    def test_get_order_history_one_market(self):
        actual = self.bittrex.get_order_history(market='BTC-LTC')
        test_basic_response(self, actual, "get_order_history")
        self.assertIsInstance(actual['result'], list, "result is not a list")

    def test_get_withdrawlhistory_all_currencies(self):
        actual = self.bittrex.get_withdrawal_history()
        test_basic_response(self, actual, "get_withdrawal_history")
        self.assertIsInstance(actual['result'], list, "result is not a list")

    def test_get_withdrawlhistory_one_currency(self):
        actual = self.bittrex.get_withdrawal_history('BTC')
        test_basic_response(self, actual, "get_withdrawal_history")
        self.assertIsInstance(actual['result'], list, "result is not a list")

    def test_get_deposithistory_all_currencies(self):
        actual = self.bittrex.get_deposit_history()
        test_basic_response(self, actual, "get_deposit_history")
        self.assertIsInstance(actual['result'], list, "result is not a list")

    def test_get_deposithistory_one_currency(self):
        actual = self.bittrex.get_deposit_history('BTC')
        test_basic_response(self, actual, "get_deposit_history")
        self.assertIsInstance(actual['result'], list, "result is not a list")

    def test_get_pending_withdrawals(self):
        self.assertRaisesRegexp(Exception, 'method call not available',
                                self.bittrex.get_pending_withdrawals)

    def test_get_pending_deposits(self):
        self.assertRaisesRegexp(Exception, 'method call not available',
                                self.bittrex.get_pending_deposits)

    def test_generate_deposit_address(self):
        self.assertRaisesRegexp(Exception,
                                'method call not available',
                                self.bittrex.generate_deposit_address,
                                currency='BTC')