# MRV aggregation and splitting for minting tokens

For a demo example of following steps, here is the policy timestamp: **1675266693.102366003**

### Adding aggregateDocumentBlock in separate containerAdding aggregateDocumentBlock in separate container**Task**

MRV documents feature a numerical measurement field. The values in this field are periodically aggregated, in this example we will use 1 minute period.

The purpose of the aggregation is to mint tokens, in this example 1 token will be minted for each part which equal to 1000 of the aggregated value.

### **Preparation**

Create a token and 2 schemas (**User** and **Report**). For simplicity the token will not require a KYC.

Build the first part of the document entry:

<figure><img src="https://3006114282-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXVOaWpJKxLZf1Tee9eCO%2Fuploads%2FhxmbcyAl6hs9MjGhN7WS%2Fimage.png?alt=media&#x26;token=3445aa85-b3d6-4dcb-918a-872162dc5ffd" alt=""><figcaption></figcaption></figure>

*(Please see* *examples* [*2* ](https://docs.guardianservice.io/technical-information/library-of-policy-examples/data-input-via-forms-using-roles-to-partition-user-activities)*and* [*3*](https://docs.guardianservice.io/technical-information/library-of-policy-examples/token-operations) *for more detailed description of this)*

#### **Aggregate:**

1. Add ‘**aggregateDocumentBlock**' after saving the document in the database. This is the block which would accumulate documents/values until the condition is met (which in this case is the event from the timer).

1.1 Put '**aggregateDocumentBlock**' in a separate container to avoid it from interfering with the working of '**interfaceStepBlock**'

<figure><img src="https://3006114282-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXVOaWpJKxLZf1Tee9eCO%2Fuploads%2Frg2oO8D0KPTCaLaipSbT%2Fimage.png?alt=media&#x26;token=2ed4d405-6600-4f16-a442-129b4ee9afd1" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3006114282-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXVOaWpJKxLZf1Tee9eCO%2Fuploads%2FzSC8UYFGHyuFJu7nb1Io%2Fimage.png?alt=media&#x26;token=e036ec88-c90f-4c86-89a7-3be6d12e26d3" alt=""><figcaption></figcaption></figure>

1.2 Pass the document to the ‘***aggregateDocumentBlock***' after it is saved.

<figure><img src="https://3006114282-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXVOaWpJKxLZf1Tee9eCO%2Fuploads%2FSlNYPZdwRSNdK3WQwP4R%2Fimage.png?alt=media&#x26;token=fffa81e3-822e-4e1c-ab06-43b45dbb0859" alt=""><figcaption></figcaption></figure>

2. Timer is a separate block called '***timerBlock***'

2.1 For each user there is a separate timer execution context (i.e., each user has an independent timer). For this it requires a document owned by the user to be passed into this. To ensure this, let’s add the timer immediately after user registration.

<figure><img src="https://3006114282-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXVOaWpJKxLZf1Tee9eCO%2Fuploads%2FlsTU4UfLEJcy4bVXzwlr%2Fimage.png?alt=media&#x26;token=5260d9a8-5816-44e5-80c2-0df90c8b1a2c" alt=""><figcaption></figcaption></figure>

2.2 Set the timer to trigger every minute

<figure><img src="https://3006114282-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXVOaWpJKxLZf1Tee9eCO%2Fuploads%2F0apCSyFxEsEDCbAidhVf%2Fimage.png?alt=media&#x26;token=542eecdc-70b1-4c8a-b80c-5eb17d6fa05e" alt=""><figcaption></figcaption></figure>

2.3 Now after the timer is launched, it would create a ‘**TimerEvent**’ periodically every minute. And then connect it with ‘***aggregateDocumentBlock***':

<figure><img src="https://3006114282-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXVOaWpJKxLZf1Tee9eCO%2Fuploads%2FOAluuqExOY9vxW23UOe9%2Fimage.png?alt=media&#x26;token=b081c1db-f36c-4e13-8634-18cbeb491403" alt=""><figcaption></figcaption></figure>

#### **Split:**

1. To split the document into equal parts, add '***splitBlock***'

1.1 Add '***splitBlock***' immediately after the '***aggregateDocumentBlock***'

<figure><img src="https://3006114282-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXVOaWpJKxLZf1Tee9eCO%2Fuploads%2FT4ux6jHwggCglRD23FwW%2Fimage.png?alt=media&#x26;token=bae3a259-daf2-48fe-a7d1-72e6cb57af93" alt=""><figcaption></figcaption></figure>

1.2 Configure the document field which would be used for splitting/aggregation

<figure><img src="https://3006114282-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXVOaWpJKxLZf1Tee9eCO%2Fuploads%2FVhllzxqLQPU2eqYlAEBX%2Fimage.png?alt=media&#x26;token=2e6e8d80-c216-4cb4-91bc-b6981184644e" alt=""><figcaption></figcaption></figure>

1.3 Set a ‘**Threshold**’ to configure the value for splitting the document.

<figure><img src="https://3006114282-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXVOaWpJKxLZf1Tee9eCO%2Fuploads%2FICkEpZvIO9swkSxx3eXp%2Fimage.png?alt=media&#x26;token=08cd348e-a5cb-4d31-a9a5-8fef6deff3c3" alt=""><figcaption></figcaption></figure>

Setting the Threshold for split

#### **Mint:**

Add '***mintDocumentBlock***' immediately after ‘***splitBlock***' to mint tokes and configure the minting rule

<figure><img src="https://3006114282-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXVOaWpJKxLZf1Tee9eCO%2Fuploads%2Fy9mrXKQII7UW5IETNr09%2Fimage.png?alt=media&#x26;token=f1f24720-5760-48ab-8553-ecdff3d6b621" alt=""><figcaption></figcaption></figure>
