## 5 Automation of Debt Compensation Process by Means of Smart Contracts
```
contractCompany{ uint16 cif; addresspayableowner; //For each company (address), we annotate the // debt (invoice) mapping (address \(\Rightarrow\) uint24) public debts; function addInvoice (address addr, uint24 amnts) public returns (uint24) { require (amnts > 0, \"Amount must be greater than 0\"); debts [addr] = debts [addr] + amnts; return debts [addr]; } function cancellInvoice (address addr, uint24 amnts) public returns (uint24) { require (amnts > 0, \"Amount must be greater than 0\"); require ((debts [addr] \(-\) amnts) >= 0, \"The amount cannot be cancelled since it is greater than the current debt.\"); debts [addr] = debts [addr] \(-\) amnts; return debts [addr]; }
```  
Listing 1: Solidity contract of company entity. It is important to note that the authors implemented many auxiliary functions such as csv processing, anonymizing companies, and back-end services to ship the graph debt to the algorithm, but this material is not included in this works since it is outside of the scope of the paper and their contribution is limited. Below, the graphical tool the authors developed in order to obtain a set of smart contracts instances starting from an input debt graph is presented. This chart is provided as a set of edges showing the creditor, the debtor, and the amount of the debt. These data are given by an external entity, normally business consultancies, since they have all the invoices of their customers. Figure 5 shows the input screen, where the user must provide the input file in csv format (representing the debt graph), and then choose the date for debt calculation. This is important since the algorithm must take into consideration the due date of the invoice. Thus, only past due invoices with respect to the date provided by the user are considered. Let us remark that this tool was conceived with business consultancies in mind as they can receive succulent fees for advising debt compensation to their customers. Thus, the tool was developed in collaboration with the company IPI: Conocimiento y Flexibilidad. Once the input data are obtained, normally an Excel or text file, they are anonymized for privacy issues. This task is performed through bash scripts. When this process ends, our algorithm is executed to obtain the debt cycles in the graph (see Figure 6). The output is a text file with the current cycles, as well as the amount of debt that can be compensated. This text file is shown in the tool as HTML tables so that the user can check different cycles and make decisions accordingly (see also Figure 11). The required logic to create the stakeholders in the cycle compensation is included in the tool. Thus, companies are created inside the application by means of forms. In future versions, we can consider an automatic process to extract information from external platforms such as Metamask [42], integrating our tool so that companies can attach their ledger in the platform without any human interaction. In Figure 7, we show the input fields for company creation. As can be seen, participants must provide their name, CIF, and Ethereum address. CIF is the Spanish VAT number of a company, which allows it to be identified for taxation. Figure 6: Example of debt cycle. Figure 7: Company creation example. ```
Data: A set of invoices between companies Result: A set of cycles that can be compensated \(debts\leftarrow\infty\) path \(\leftarrow-1\) visited \(\leftarrow\) 0 debts [origin] = 0; path [0] = origin; min = origin; all_visited = 1; whileall_visited < tamdo for\(d=1\); \(d<\) tam; \(d++\)do ifexists_invoice(minimum, d) and \(invoice\_atly(minimum, d)< current_debt(minimum, d) and \(visited(d)=false\)then \(debt(minimum, d)=\) \(invoice(minimum, d)< minimum_debthen minimum_debthen minimum_debthen invoice_atly(minimum, d) end end end end end end end end end
```  
**Algorithm 1**Algorithm for finding debt cycles between companies.