# Credit Score Calculation

### *Data fetching via Calimero*

Calimero’s SDK is used to interact with the private shard. Payment data is securely fetched from the company’s systems through API endpoints.

```
const payments = await client.get('/api/payments', {
    companyId: 'COMPANY_ID',
});

```

### *Data Aggregation and Proof Generation*

The payment data is processed to calculate:

* **Total Due**: Sum of all payment dues.
* **Total Paid**: Sum of all payments made.
* **On-Time Payments**: Count of payments made on or before the due date.

Since this is ZKP-based workflow, a proof is generated for the calculated results to ensure privacy-preserving verification.

```
const totalDue = payments.reduce((sum, payment) => sum + payment.amountDue, 0);
const totalPaid = payments.reduce((sum, payment) => sum + payment.amountPaid, 0);

const proof = await client.post('/api/generate-proof', {
    inputs: [totalDue, totalPaid],
});

```

### *Submitting Data to StarkNet*

The aggregated data (and proof, if applicable) is submitted to the StarkNet smart contract. The `add_payments` method is used for standard data submission, and the `update_credit_score_with_proof` method is used for ZKP-based workflows.

```
await contract.add_payment([
    [paymentDate, dueDate, amountDue, amountPaid],
]);

await contract.update_credit_score_with_proof(proof.data, [publicInputs]);

```

### *Credit Score Calculation*

The smart contract computes the credit score based on:

* **On-Time Payment Percentage**: Proportion of on-time payments.
* **Payment Ratio**: Ratio of total paid to total due. These metrics are weighted using configurable parameters stored in the contract.
