> For the complete documentation index, see [llms.txt](https://qualitax.gitbook.io/polymesh/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://qualitax.gitbook.io/polymesh/exploring-polymesh-data/settlements-and-legs.md).

# Settlements & Legs

Exploring Settlements and Legs

The settlements metrics aims to provide an understanding the dynamics of digital asset trades Polymesh network, covering key aspects of settlement activities.

| Metric Category | Metric                          | Metric Description                                       |
| --------------- | ------------------------------- | -------------------------------------------------------- |
| Settlements     | Nb of settlements               | Total number of transactions settled on the network      |
| Settlements     | Number of settlements by status | Number of successful vs. failed vs rejected settlements. |
| Settlements     | Settlements over time           | Number of settlements by status by month.                |

## Nb of settlements

This metric helps gauging the overall activity level within the Polymesh network in terms of trade settlements.

**How many trades of assets have been settled on the Polymesh network?**

<mark style="color:purple;">**SQL**</mark>

```
SELECT COUNT(DISTINCT ID)
FROM 
    settlements
```

<figure><img src="https://243096197-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEJbeWS9CeNvCKkvrw412%2Fuploads%2FKzbyuy9LYRNZtX6c6Ogs%2Fimage.png?alt=media&amp;token=c93f2e2c-1041-40c3-b6fe-e824285108db" alt=""><figcaption><p>Nb of settlements</p></figcaption></figure>

<mark style="color:purple;">**GraphQL**</mark>

```
query {
  settlements {
    totalCount
  }
}
```

<figure><img src="https://243096197-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEJbeWS9CeNvCKkvrw412%2Fuploads%2FyGrLNXbymet8LTYIl2I8%2Fimage.png?alt=media&amp;token=56cdfb55-b8b8-4af1-896c-4cb81420a399" alt=""><figcaption></figcaption></figure>

## Number of settlements by status

This metric captures the operational efficiency and reliability of the settlement process on the network by differentiating between successful, failed, and rejected settlements.

**How efficient and reliable is the settlement process on the Polymesh network, based on the outcomes of settlement attempts?**

<mark style="color:purple;">**SQL**</mark>

```
SELECT 
    s.result,
    COUNT(*) AS number_of_settlements
FROM 
    settlements s
JOIN 
    blocks b ON s.created_block_id::integer = b.block_id
GROUP BY 
   s.result
ORDER BY 
   s.result;
```

<figure><img src="https://243096197-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEJbeWS9CeNvCKkvrw412%2Fuploads%2F3YHLZuW5wFvb70k1VZDA%2Fimage.png?alt=media&amp;token=ed8edac9-868b-48c7-9db2-18d1b7512b4b" alt=""><figcaption><p>Number of settlements by status</p></figcaption></figure>

<mark style="color:purple;">**GraphQL**</mark>

```
query {
  settlements {
    nodes {
      result
    }
  }
}
```

This query retrieves all the settlements and their corresponding `result` field. Since the provided schema doesn't support aggregation directly, you'll need to handle the grouping and counting of settlements by their result in the application code.

{% hint style="info" %}
**Possible improvement #13**

To support the query for grouping and counting settlements by their result directly in GraphQL, one idea could be to modify the schema by introducing a new field in the `Query` type and defining a custom type for the aggregated result. Add the `settlementResultCounts` field to the `Query` type. Implement the resolver for the `settlementResultCounts` field in your GraphQL server.&#x20;
{% endhint %}

## Settlements over time

This metric provides an overview of the settlement process's outcomes on the Polymesh network, segmented into categories like successful, failed, and rejected settlements over each month.

<mark style="color:purple;">**SQL**</mark>

```
SELECT 
    DATE_TRUNC('month', b.datetime) AS settlement_month,
    s.result as status,
    COUNT(*) AS number_of_settlements
FROM 
    settlements s
JOIN 
    blocks b ON s.created_block_id::integer = b.block_id
GROUP BY 
    settlement_month, s.result
ORDER BY 
    settlement_month ASC, s.result;
```

<figure><img src="https://243096197-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEJbeWS9CeNvCKkvrw412%2Fuploads%2FrI9An5ltzM7NHecVx6Gc%2Fimage.png?alt=media&amp;token=600f3c0f-0496-4c65-a51d-0cb275c1a048" alt=""><figcaption></figcaption></figure>

<mark style="color:purple;">**GraphQL**</mark>

```
query {
  settlements {
    nodes {
      result
      createdBlock {
        datetime
      }
    }
  }
}
```

This query retrieves all the settlements along with their corresponding `result` and `createdBlock.datetime` fields. Since the provided schema doesn't support aggregation directly, you'll need to handle the grouping and counting of settlements by settlement month and result in the application code.

{% hint style="info" %}
**Possible improvement #14**

To support the query for grouping and counting settlements by settlement month and result directly in GraphQL, one idea could be to modify the schema by introducing a new entity called `SettlementAggregateByMonthAndResult .` Add the `SettlementAggregateByMonthAndResult` entity theen Modify the `Settlement` entity to include a reference to the `SettlementAggregateByMonthAndResult.`
{% endhint %}
