# P2P

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

{% code title="p2p.py" %}

```python
from buycoins import P2P
```

{% endcode %}

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

```python
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.&#x20;

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

#### Example

{% code title="p2p.py" %}

```python
p2p = P2P()

coins_price = p2p.get_prices()

print(coins_price)
```

{% endcode %}

The response printed out is:

```javascript
[
  {
    '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:

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

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

```python
["buy", "sell"]
```

#### Example

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

{% code title="p2p.py" %}

```python
p2p = P2P()

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

print(price_of_bitcoin)
```

{% endcode %}

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

```javascript
[
    {
        '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:**

{% code title="p2p.py" %}

```python
p2p = P2P()

open_orders = p2p.get_dynamic_price_expiry("open")

print(open_orders)
```

{% endcode %}

The response printed out is:

```javascript
{
    'dynamicPriceExpiry': 1612305372
}

```

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

{% code title="p2p.py" %}

```python
p2p = P2P()

completed_orders = p2p.get_dynamic_price_expiry("completed")

print(completed_orders)
```

{% endcode %}

The response printed out is:

```javascript
{
    '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:

{% code title="p2p.py" %}

```python
p2p = P2P()

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

print(bitcoin_limit_order)
```

{% endcode %}

The limit order detail is printed out:

```javascript
{
     '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:

{% code title="p2p.py" %}

```python
p2p = P2P()

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

print(bitcoin_order)
```

{% endcode %}

The response from the `bitcoin_order` is:

```javascript
{
     '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:**

{% code title="p2p.py" %}

```python
p2p = P2P()

open_orders = p2p.get_orders("open")

print(open_orders)
```

{% endcode %}

The printed response:

```javascript
{
    '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**:

{% code title="p2p.py" %}

```python
p2p = P2P()

completed_orders = p2p.get_orders("completed")

print(completed_orders)
```

{% endcode %}

The response:

```javascript
{
    '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

{% code title="p2p.py" %}

```python
p2p = P2P()

market_history = p2p.get_market_book()

print(market_history)
```

{% endcode %}

The printed response:

```javascript
{
  '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**.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://buycoins.youngest.dev/p2p.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
