def create_address(self, currency: str = "bitcoin"): """Creates a wallet address for the supplied cryptocurrency. Args: currency (str): The cryptocurrency wallet address to be created. Returns: response: A JSON object containing response from the request. """ try: if currency not in self.supported_cryptocurrencies: raise WalletError("Invalid or unsupported cryptocurrency", 400) self._query = """ mutation CreateWalletAddress($currency: Cryptocurrency) { createAddress(cryptocurrency: $currency) { cryptocurrency address } } """ _variables = {"currency": currency} response = self._execute_request(query=self._query, variables=_variables) check_response(response, WalletError) except (WalletError, ClientError, ServerError) as e: return e.response else: return response["data"]["createAddress"]
def get_prices(self): """Returns the current price for supported cryptocurrencies on BuyCoins Returns: response: An array of cryptocurrency data. """ try: self._query = """ query { getPrices { id cryptocurrency buyPricePerCoin minBuy maxBuy expiresAt } } """ response = self._execute_request(query=self._query) check_response(response, P2PError) except (P2PError, ClientError, ServerError) as e: return e.response else: return response["data"]["getPrices"]
def get_network_fee(self, currency: str = "bitcoin", coin_amount: float = 0.01): """Retrieves NetworkFee for the supplied cryptocurrency. Args: currency (str): The cryptocurrency whose network fee is been checked. coin_amount(float): Amount of currency. Returns: response: A JSON object containing response from the request. """ try: if currency not in self.supported_cryptocurrencies: raise WalletError("Invalid or unsupported cryptocurrency", 400) self._query = """ query NetworkFee($currency: Cryptocurrency, $amount: BigDecimal!) { getEstimatedNetworkFee(cryptocurrency: $currency, amount: $amount) { estimatedFee total } } """ _variables = {"currency": currency, "amount": coin_amount} response = self._execute_request(query=self._query, variables=_variables) check_response(response, WalletError) except (WalletError, ClientError, ServerError) as e: return e.response else: return response["data"]["getEstimatedNetworkFee"]
def send_crypto(self, address: str, currency: str = "bitcoin", coin_amount: float = 0.01): """Sends a cryptocurrency, for the given amount passed. Args: currency (str): The cryptocurrency to be sold. coin_amount(float): Amount of currency to be bought. Returns: response: A JSON object containing response from the request. """ try: if currency not in self.supported_cryptocurrencies: raise WalletError("Invalid or unsupported cryptocurrency", 400) if not address: raise WalletError("Invalid address", 400) self._query = """ mutation SendCrypto($amount: BigDecimal!, $currency: Cryptocurrency, $address: String!){ send(cryptocurrency: $currency, amount: $amount, address: $address) { id address amount cryptocurrency fee status transaction { txhash id } } } """ _variables = { "address": address, "amount": coin_amount, "currency": currency } response = self._execute_request(query=self._query, variables=_variables) check_response(response, WalletError) except (WalletError, ClientError, ServerError) as e: return e.response else: return response["data"]["SendCoin"]
def post_market_order(self, order_side: str = "buy", coin_amount: float = 0.01, currency: str = "bitcoin"): """Posts a market order for the supplied cryptocurrency. Args: order_side (str): The type of order to be placed. It could either be `buy` or `sell`. coin_amount (float): Amount of coin to be sold. currency (str): Cryptocurrency involved in the market order. Returns: response: A JSON object containing the response from the request. """ try: if order_side not in self.side: raise P2PError("Invalid side passed", 400) if currency not in self.supported_cryptocurrencies: raise P2PError("Invalid or unsupported cryptocurrency", 400) self._query = """ mutation PostMarketOrder($orderSide: OrderSide!, $coinAmount: BigDecimal!, $cryptocurrency: Cryptocurrency){ postMarketOrder(orderSide: $orderSide, coinAmount: $coinAmount, cryptocurrency: $cryptocurrency){ id cryptocurrency coinAmount side status createdAt pricePerCoin priceType staticPrice dynamicExchangeRate } } """ _variables = { "orderSide": order_side, "coinAmount": coin_amount, "cryptocurrency": currency, } response = self._execute_request(query=self._query, variables=_variables) check_response(response, P2PError) except (P2PError, ClientError, ServerError) as e: return e.response else: return response["data"]["postMarketOrder"]
def place_limit_order(self, order_side: str = "buy", coin_amount: float = 0.01, currency: str = "bitcoin", static_price: int = 100000, price_type: str = "static"): """Places limit order for the supplied cryptocurrency. Args: order_side (str): The side of the order. This could be `buy` or `sell`. coin_amount (str): The amount the limit order is based on. currency (str): The cryptocurrency involved in the limit order. static_price (str, optional): Static price for the cryptocurrency in Naira. price_type (str): Static or dynamic price for the cryptocurrency. Returns: response: A JSON object containing the result from the request. """ try: if order_side not in self.side: raise P2PError("Invalid side passed", 400) if currency not in self.supported_cryptocurrencies: raise P2PError("Invalid or unsupported cryptocurrency", 400) self._query = """ mutation PostLimitOrder($orderSide: OrderSide!, $coinAmount: BigDecimal!, $cryptocurrency: Cryptocurrency, $staticPrice: BigDecimal, $priceType: PriceType!){ postLimitOrder(orderSide: $orderSide, coinAmount: $coinAmount, cryptocurrency: $cryptocurrency, staticPrice: $staticPrice, priceType: $priceType) { id cryptocurrency coinAmount side status createdAt pricePerCoin priceType staticPrice dynamicExchangeRate } } """ _variables = {"orderSide": order_side, "coinAmount": coin_amount, "cryptocurrency": currency, "staticPrice": static_price, "priceType": price_type} response = self._execute_request(query=self._query, variables=_variables) check_response(response, P2PError) except (P2PError, ClientError, ServerError) as e: return e.response else: return response["data"]["postLimitOrder"]
def sell_crypto(self, currency: str = "bitcoin", coin_amount: float = 0.01): """Sells a cryptocurrency, for the given amount passed. Args: currency (str): The cryptocurrency to be sold. coin_amount(float): Amount of currency to be bought. Returns: response: A JSON object containing response from the request. """ try: if currency not in self.supported_cryptocurrencies: raise WalletError("Invalid or unsupported cryptocurrency", 400) p2p = P2P() price_info = p2p.get_current_price(order_side="sell", currency=currency) price_id = price_info[0]["id"] self._query = """ mutation SellCoin($price: ID!, $coin_amount: BigDecimal!, $currency: Cryptocurrency){ sell(price: $price, coin_amount: $coin_amount, cryptocurrency: $currency) { id cryptocurrency status totalCoinAmount side } } """ _variables = { "price": price_id, "coin_amount": coin_amount, "currency": currency } response = self._execute_request(query=self._query, variables=_variables) check_response(response, WalletError) except (WalletError, ClientError, ServerError) as e: return e.response else: return response["data"]["sell"]
def get_orders(self, status: str = "open"): """Retrieves orders based on their status. Args: status (str): Status of the order which could either be `open` or `completed`. Returns: response: A JSON object containing the response from the request. """ try: if status not in self.status: raise P2PError("Invalid status passed", 400) self._query = """ query GetOrders($status: GetOrdersStatus!){ getOrders(status: $status) { dynamicPriceExpiry orders { edges { node { id cryptocurrency coinAmount side status createdAt pricePerCoin priceType staticPrice dynamicExchangeRate } } } } } """ _variables = {"status": status} response = self._execute_request(query=self._query, variables=_variables) check_response(response, P2PError) except (P2PError, ClientError, ServerError) as e: return e.response else: return response["data"]["getOrders"]
def get_current_price(self, order_side: str = "buy", currency: str = "bitcoin"): """Retrieves the current `side` price for the supplied cryptocurrency. Args: order_side (str): The order side which can either be buy or sell. currency (str): The cryptocurrency whose current price is to be retrieved. Returns: response: A JSON object containing response from the request. """ try: if currency not in self.supported_cryptocurrencies: raise P2PError("Invalid or unsupported cryptocurrency", 400) if order_side not in self.side: raise P2PError("Invalid order side", 400) self._query = """ query GetBuyCoinsPrices($side: OrderSide, $currency: Cryptocurrency) { getPrices(side: $side, cryptocurrency: $currency){ buyPricePerCoin cryptocurrency id maxBuy maxSell minBuy minCoinAmount minSell sellPricePerCoin status } } """ _variables = {"side": order_side, "currency": currency} response = self._execute_request(query=self._query, variables=_variables) check_response(response, P2PError) except (P2PError, ClientError, ServerError) as e: return e.response else: return response["data"]["getPrices"]
def get_balances(self, currency=None): """Retrieves user cryptocurrency balances Returns: response: A JSON object containing the user cryptocurrency balances. """ try: if currency: self._query = """ query($currency: Cryptocurrency) { getBalances(cryptocurrency: $currency) { id cryptocurrency confirmedBalance } } """ _variables = {"currency": currency} response = self._execute_request(query=self._query, variables=_variables) check_response(response, WalletError) else: self._query = """ query { getBalances { id cryptocurrency confirmedBalance } } """ response = self._execute_request(query=self._query) check_response(response, WalletError) except (WalletError, ClientError, ServerError) as e: return e.response else: return response["data"]["getBalances"]
def get_dynamic_price_expiry(self, status: str = "open", side: str = "buy", currency: str = "bitcoin"): """Retrieves the dynamic prices for available cryptocurrencies. Args: status (str): The status of the current order. Returns: response: A JSON object containing the response from the request. """ try: if status not in self.status: raise P2PError("Invalid status passed", 400) if side not in self.side: raise P2PError("Invalid side passed", 400) if currency not in self.supported_cryptocurrencies: raise P2PError("Invalid or unsupported cryptocurrency", 400) self._query = """ query GetOrders($status: GetOrdersStatus!){ getOrders(status: $status) { dynamicPriceExpiry } } """ _variables = { "status": status, } response = self._execute_request(query=self._query, variables=_variables) check_response(response, P2PError) except (P2PError, ClientError, ServerError) as e: return e.response else: return response["data"]["getOrders"]
def get_market_book(self): """Retrieves market history. Returns: response: A JSON object containing response from the request. """ try: self._query = """ query { getMarketBook { dynamicPriceExpiry orders { edges { node { id cryptocurrency coinAmount side status createdAt pricePerCoin priceType staticPrice dynamicExchangeRate } } } } } """ response = self._execute_request(query=self._query) check_response(response, P2PError) except (P2PError, ClientError, ServerError) as e: return e.response return response["data"]["getMarketBook"]
def create_deposit_account(self, account_name: str): """Creates a virtual deposit account under the supplied name. Args: account_name (str): Name of the new virtual deposit account to be generated*. Returns: response: A JSON object containing the response from the request. """ try: if not account_name: raise AccountError("Invalid account name passed", 400) self.account_name = account_name _variables = {"accountName": self.account_name} self._query = """ mutation createDepositAccount($accountName: String!) { createDepositAccount(accountName: $accountName) { accountNumber accountName accountType bankName accountReference } } """ response = self._execute_request(query=self._query, variables=_variables) check_response(response, AccountError) except (AccountError, ClientError, ServerError) as e: return e.response else: return response["data"]["createDepositAccount"]