# 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:

![image1.png](https://1556785885-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNYWPEEAknX9Vki1yV5HY%2Fuploads%2Fgit-blob-653635f3a498664313895cc49953655249a79f75%2F0%20\(3\).png?alt=media)

*(Please see* [*examples 2*](https://guardian.hedera.com/guardian/standard-registry/policies/library-of-policy-examples/data-input-via-forms-using-roles-to-partition-user-activities.) *and* [*3*](https://guardian.hedera.com/guardian/standard-registry/policies/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**'

![Adding aggregateDocumentBlock](https://1556785885-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNYWPEEAknX9Vki1yV5HY%2Fuploads%2Fgit-blob-7eff645d9eacb8c1de95b486747afbdd14360e1e%2F1%20\(4\).png?alt=media)

![Adding aggregateDocumentBlock in separate container](https://1556785885-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNYWPEEAknX9Vki1yV5HY%2Fuploads%2Fgit-blob-4af4ea576455ba5b2583b883f60710903fb930da%2F2%20\(4\).png?alt=media\&token=87af4b73-7ccc-479e-8317-a9662b321193)

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

![Passing the saved document to the aggregateDocumentBlock](https://1556785885-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNYWPEEAknX9Vki1yV5HY%2Fuploads%2Fgit-blob-dba03580b3a0c318840f8b8085e770e5e2ffb36d%2F3%20\(5\)%20\(1\).png?alt=media)

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.

![Adding timerBlock after user registration](https://1556785885-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNYWPEEAknX9Vki1yV5HY%2Fuploads%2Fgit-blob-ab4d2628b9d712217c6b830e3f26825f74d3c495%2F4%20\(3\)%20\(1\)%20\(1\).png?alt=media)

2.2 Set the timer to trigger every minute

![Setting the timer for every minute](https://1556785885-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNYWPEEAknX9Vki1yV5HY%2Fuploads%2Fgit-blob-5ff5e6909469e10aa10d711ecd4196bd333aff89%2F5%20\(2\)%20\(1\)%20\(1\).png?alt=media)

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

![Connecting TimerBlock with aggregateDocumentBlock](https://1556785885-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNYWPEEAknX9Vki1yV5HY%2Fuploads%2Fgit-blob-f7ad3339ec8f84bab11ccc555c2ee5631810e60a%2F6%20\(3\).png?alt=media\&token=bc89407f-833d-4dee-be0b-1ef9046725f2)

### **Split:**

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

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

![Adding splitBlock after aggregateDocumentBlock](https://1556785885-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNYWPEEAknX9Vki1yV5HY%2Fuploads%2Fgit-blob-ca3c9b5540cd42be74ec580b7526e1a844dadfeb%2F7%20\(4\).png?alt=media)

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

![Configuring Source Field](https://1556785885-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNYWPEEAknX9Vki1yV5HY%2Fuploads%2Fgit-blob-e167364ef29b8fc52ee015d2edc60fc35551c8b8%2F8%20\(4\).png?alt=media)

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

<figure><img src="https://1556785885-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNYWPEEAknX9Vki1yV5HY%2Fuploads%2Fgit-blob-6417e264452d1f66e153ef98ea1cddf435c1b002%2F9%20(3)%20(2).png?alt=media&#x26;token=8217faf1-4abe-43fe-971f-d2231efd9abb" alt=""><figcaption><p>Setting the Threshold for split</p></figcaption></figure>

### **Mint:**

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

![Adding mintDocumentBlock after splitBlock](https://1556785885-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNYWPEEAknX9Vki1yV5HY%2Fuploads%2Fgit-blob-8a362cf84841e5c66af99684d9ddbd121812cbe0%2F10%20\(4\).png?alt=media)
