P2P

The P2P class handles the peer-2-peer transactions which includes selling, buying, placing limit and posting market orders.

To use the P2P class, import it from the buycoins package:

p2p.py
from buycoins import P2P

You can use the class directly using P2P().method_name or instantiate the class into a variable and use:

p2p = P2P()
p2p.method_name(**args)

get_prices()

This method returns an array of the price of each currency listed on BuyCoins at the current time.

The price of each currency is susceptible to changes as time passes

Example

p2p.py
p2p = P2P()

coins_price = p2p.get_prices()

print(coins_price)

The response printed out is:

[
  {
    'id': 'QnV5Y29pbnNQcmljZS0wMTdjYzBlMS05NWFjLTQ5N2YtODg4Mi0yNjU1NDRiNGRmODM=',
    'cryptocurrency': 'bitcoin',
    'buyPricePerCoin': '18073103.62',
    'minBuy': '0.001',
    'maxBuy': '2.37516074',
    'expiresAt': 1612430322
  },
  {
    'id': 'QnV5Y29pbnNQcmljZS0wOTM1NmM1Mi0zZTAyLTRjMjgtYWIzMy00Y2Q0ZDY3OGMzMGM=',
    'cryptocurrency': 'ethereum',
    'buyPricePerCoin': '782639.91',
    'minBuy': '0.02',
    'maxBuy': '54.84837368',
    'expiresAt': 1612430323
  },
  {
    'id': 'QnV5Y29pbnNQcmljZS1hODdhZmE3Ny02OGI2LTQ2N2ItYjhkNS05OWI4N2QzZTlhMzE=',
    'cryptocurrency': 'litecoin',
    'buyPricePerCoin': '71365.489',
    'minBuy': '0.1',
    'maxBuy': '601.50258679',
    'expiresAt': 1612430327
  },
  {
    'id': 'QnV5Y29pbnNQcmljZS0zYmU5Yzk5NC0zYmY4LTQ3MmItYjEwZi0wNTk5NWM3ZmMzZmM=',
    'cryptocurrency': 'usd_coin',
    'buyPricePerCoin': '485.81',
    'minBuy': '5',
    'maxBuy': '88360.73',
    'expiresAt': 1612430325
  }
]

get_current_price(order_side, currency)

This method returns the price of a currency. The price of the cryptocurrency returned is also based on the order_side argument passed, i.e., if the side passed is buy, the 'buy' price of that cryptocurrency is returned.

The supported cryptocurrencies which can be passed as the method's argument include:

["bitcoin", "ethereum", "litecoin", "naira_token", "usd_coin", "usd_tether"]

Likewise, the supported orderSide which can be passed as the method argument include:

["buy", "sell"]

Example

To retrieve the current buy price of bitcoin, the method will be called like this:

p2p.py
p2p = P2P()

price_of_bitcoin = p2p.get_current_price(order_side="buy", currency="bitcoin")

print(price_of_bitcoin)

The print statement above returns a JSON object containing the current buy price of bitcoin buyPricePerCoin and the current sell price sellPricePerCoin:

[
    {
        'buyPricePerCoin': '17164294.308', 
        'cryptocurrency': 'bitcoin', 
        'id': 'QnV5Y29pbnNQcmljZS05NjNmZTExOS02ZGVhLTRlMDItYTc3NC1lZjViYjk3YWZiNGE=', 
        'maxBuy': '24.90738193', 
        'maxSell': '12.6217372', 
        'minBuy': '0.001', 
        'minCoinAmount': '0.001', 
        'minSell': '0.001', 
        'sellPricePerCoin': '16824359.1781', 
        'status': 'active'
    }
]

get_dynamic_price_expiry(status)

This method returns when the next dynamic prices for listed cryptocurrencies will be updated in timestamps. The method takes in an argument status which can either be open or completed.

Examples

To retrieve when next the dynamic prices for open orders are updated, the argument supplied to the method will be open:

p2p.py
p2p = P2P()

open_orders = p2p.get_dynamic_price_expiry("open")

print(open_orders)

The response printed out is:

{
    'dynamicPriceExpiry': 1612305372
}

Likewise, to retrieve when next the dynamic prices for completed orders are updated, the argument supplied to the method will be completed:

p2p.py
p2p = P2P()

completed_orders = p2p.get_dynamic_price_expiry("completed")

print(completed_orders)

The response printed out is:

{
    'dynamicPriceExpiry': 1612305552
}

The timestamps for the dynamic prices expiry differ for open and completed orders, as shown in the responses above.

place_limit_order(order_side, coin_amount, currency, static_price, price_type)

This method places either a buy or sell limit order for a cryptocurrency depending on the order_side argument passed. The static_price argument is the price in Naira the user is placing the limit order on.

Example

To place a buy limit order for 1 bitcoin for ₦16,000,000, the method will be called like this:

p2p.py
p2p = P2P()

bitcoin_limit_order = p2p.place_limit_order("buy", 1, "bitcoin", 16000000)

print(bitcoin_limit_order)

The limit order detail is printed out:

{
     'id': 'UG9zdE9yZGVyLTgwY2M3MjdmLWQzYjEtNDE0OS04MDg3LTJkNjI0MDdhMWMzMw==',
     'cryptocurrency': 'bitcoin', 
     'coinAmount': '1.0', 
     'side': 'buy',
     'status': 'inactive',
     'createdAt': 1612307038, 
     'pricePerCoin': '16000000.0', 
     'priceType': 'static', 
     'staticPrice': '1600000000', 
     'dynamicExchangeRate': None
}

post_market_order(order_side, coin_amount, currency)

This method places an order_side market order for a specified amount coin_amount of the specified currency.

Example

To place a buy market order for 0.01 amount of bitcoin, the method will be called like this:

p2p.py
p2p = P2P()

bitcoin_order = p2p.post_market_order("sell", 0.01, "bitcoin")

print(bitcoin_order)

The response from the bitcoin_order is:

{
     'id': 'UG9zdE9yZGVyLTgwY2M3MjdmLWQzYjEtNDE0OS04MDg3LTJkNjI0MDdhMWMzMw==',
     'cryptocurrency': 'bitcoin', 
     'coinAmount': '0.01', 
     'side': 'sell',
     'status': 'inactive',
     'createdAt': 1612307038, 
     'pricePerCoin': '16000000.0', 
     'priceType': None, 
     'staticPrice': None, 
     'dynamicExchangeRate': None
}
    

get_orders(status)

This method returns all orders with status matching the one supplied in the method argument.

Examples

To retrieve all open orders, the method is called with the status argument set to open:

p2p.py
p2p = P2P()

open_orders = p2p.get_orders("open")

print(open_orders)

The printed response:

{
    'dynamicPriceExpiry': 1612308792,
     'orders': {'edges': []}
}

The orders key has empty edges as a result of the unavailability of open orders. When a user has open orders, they are listed in the edges array.

Likewise, to retrieve completed orders, the method is called with the status argument set to completed:

p2p.py
p2p = P2P()

completed_orders = p2p.get_orders("completed")

print(completed_orders)

The response:

{
    'dynamicPriceExpiry': 1612309092,
     'orders': {'edges': []}
}

The orders key has empty edges as a result of the unavailability of completed orders. When a user has completed orders, they will be listed into the edges array.

get_market_book()

This method returns a complete history of peer-2-peer transactions that have been carried out.

Example

p2p.py
p2p = P2P()

market_history = p2p.get_market_book()

print(market_history)

The printed response:

{
  'dynamicPriceExpiry': 1612309392,
  'orders': {
    'edges': [
      {
        'node': {
          'id': 'UG9zdE9yZGVyLTdjZmIxMTFiLTIyMjEtNGEyNS1iMTUwLTI2YmRhZjdlY2RiMw==',
          'cryptocurrency': 'bitcoin',
          'coinAmount': '0.003196',
          'side': 'buy',
          'status': 'active',
          'createdAt': 1612308511,
          'pricePerCoin': '16500000.0',
          'priceType': 'static',
          'staticPrice': '1650000000',
          'dynamicExchangeRate': None
        }
      },
      {
        'node': {
          'id': 'UG9zdE9yZGVyLWQ1MTAyYzFjLTU3N2ItNDZmZi1iOGQ4LTBiMTcyODU3ODgxMg==',
          'cryptocurrency': 'bitcoin',
          'coinAmount': '0.05',
          'side': 'buy',
          'status': 'active',
          'createdAt': 1612305315,
          'pricePerCoin': '17120000.0',
          'priceType': 'static',
          'staticPrice': '1712000000',
          'dynamicExchangeRate': None
        }
      },
      {
        'node': {
          'id': 'UG9zdE9yZGVyLTNhMmE1NmM5LTZkZDMtNDliOC1iZmU2LTk5NGEyODE5YmFhNA==',
          'cryptocurrency': 'bitcoin',
          'coinAmount': '0.00044656',
          'side': 'sell',
          'status': 'active',
          'createdAt': 1612304977,
          'pricePerCoin': '17345000.0',
          'priceType': 'static',
          'staticPrice': '1734500000',
          'dynamicExchangeRate': None
        }
      },
      {
        'node': {
          'id': 'UG9zdE9yZGVyLTZlYTIxMzQ0LWYxMDctNGY4ZC1hMTNlLWU0NGNkYTVhMGZmNw==',
          'cryptocurrency': 'bitcoin',
          'coinAmount': '0.00210526',
          'side': 'sell',
          'status': 'active',
          'createdAt': 1612303643,
          'pricePerCoin': '18500000.0',
          'priceType': 'static',
          'staticPrice': '1850000000',
          'dynamicExchangeRate': None
        }
      },
      {
        'node': {
          'id': 'UG9zdE9yZGVyLWJmYmNjOTE5LTU4M2MtNDU4NS1iOWE5LThhNTQ0MTcwNjYyMg==',
          'cryptocurrency': 'bitcoin',
          'coinAmount': '0.03338711',
          'side': 'sell',
          'status': 'active',
          'createdAt': 1612302497,
          'pricePerCoin': '17260000.0',
          'priceType': 'static',
          'staticPrice': '1726000000',
          'dynamicExchangeRate': None
        }
      }
    ]
  }
}

Error Responses

The common error responses from the P2P class consists of:

  • Insufficient currency or naira token

  • Invalid market orders: order side, currency, and amount.

These errors are accompanied by a status code of 400.

Last updated