> For the complete documentation index, see [llms.txt](https://docs.pinto.money/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.pinto.money/pinto-improvements/pi-9.md).

# PI-9: Helper Function Bug Fix

Executed on April 18, 2025 at 18:48 UTC.

## Summary <a href="#bd2e4019-aad5-82e9-beb1-012bc534f038" id="bd2e4019-aad5-82e9-beb1-012bc534f038"></a>

Update the `_setSortedDepositIds` function to properly set the [Deposit](https://docs.pinto.money/resources/glossary#deposit) ID-to-index mapping.

## Links <a href="#id-590e4019-aad5-8387-84ac-01bdd5d6c8f1" id="id-590e4019-aad5-8387-84ac-01bdd5d6c8f1"></a>

* [Implementation PR](https://github.com/pinto-org/protocol/pull/77)
* Implementation Commit: [452ee9638e5c86cfa73b1c25f5f1cdac80cd84ff](https://github.com/pinto-org/protocol/commit/452ee9638e5c86cfa73b1c25f5f1cdac80cd84ff)
* [Safe Transaction](https://app.safe.global/transactions/tx?safe=base:0x2cf82605402912C6a79078a9BBfcCf061CbfD507\&id=multisig_0x2cf82605402912C6a79078a9BBfcCf061CbfD507_0x616f091ed00ade24e93c084e4baea1fc2d129d81113d94fc87eda017745c5d96)
* [Diamond Cut Transaction](https://basescan.org/tx/0xe3f532ddf06b5a70bd8c1be8a2f3c54e3ec2de18aa16a3d1e4d1f2bdd9c208c9)

## Problem <a href="#a77e4019-aad5-837d-9359-81ff504127e2" id="a77e4019-aad5-837d-9359-81ff504127e2"></a>

The protocol stores a list of each [Farmer's](https://docs.pinto.money/resources/glossary#farmers) Deposits on-chain, which is used to allow clients like the [Pinto](https://docs.pinto.money/resources/glossary#pinto) UI to easily query Deposit data. The protocol also stores a mapping of Deposit IDs to index in order to efficiently look up a Deposit's index. [PI-8](https://github.com/pinto-org/protocol/pull/47) introduced an `updateSortedDepositIds` function that allows anyone to submit a Deposit ordering for one token, validated in ascending Stem order, but this function did not properly update the mapping to reflect the new ordering. As a result, a later full Deposit removal could use a stale index and remove the wrong ID from the list.

## Solution <a href="#id-75de4019-aad5-83ff-a19b-8114f2469209" id="id-75de4019-aad5-83ff-a19b-8114f2469209"></a>

Update `LibSilo._setSortedDepositIds` to properly update the mapping based on the new ordering. Each Deposit ID is mapped to its zero-based index before the ordered list is stored.

## Rationale <a href="#b62e4019-aad5-83ba-9ca5-8145349479c6" id="b62e4019-aad5-83ba-9ca5-8145349479c6"></a>

Keeping the index mapping consistent with the ordered list preserves correct Deposit index lookup and removal after reordering. The existing validation requires a nonempty, complete list in strictly ascending Stem order.

## Contract Changes <a href="#a62e4019-aad5-82b6-a412-814d3c300ddc" id="a62e4019-aad5-82b6-a412-814d3c300ddc"></a>

### Initialization <a href="#id-347e4019-aad5-838e-866e-01daa16df499" id="id-347e4019-aad5-838e-866e-01daa16df499"></a>

No initializer was called. The upgrade did not automatically rewrite existing account mappings. A successful call to `updateSortedDepositIds` updates the supplied account and token’s index mapping.

### Facet Changes <a href="#id-415e4019-aad5-8211-a186-018dbbc67b1c" id="id-415e4019-aad5-8211-a186-018dbbc67b1c"></a>

| Facet                  | Action  | Previous Implementation                                                                                                      | New Implementation                                                                                                           |
| ---------------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| `ApprovalFacet`        | Replace | [`0x61b9a20a5fc4a9965c2309c2feff56b5ebe855cd`](https://basescan.org/address/0x61b9a20a5fc4a9965c2309c2feff56b5ebe855cd#code) | [`0xa7c95ed70be8f91f8d8af91de193710fa7ab32bb`](https://basescan.org/address/0xa7c95ed70be8f91f8d8af91de193710fa7ab32bb#code) |
| `ClaimFacet`           | Replace | [`0x44df048857445cd8cba3b8cc26132816d827ff14`](https://basescan.org/address/0x44df048857445cd8cba3b8cc26132816d827ff14#code) | [`0xd38c3e61bf08734cd808aca15c944389968612e0`](https://basescan.org/address/0xd38c3e61bf08734cd808aca15c944389968612e0#code) |
| `ConvertFacet`         | Replace | [`0x1af9097b043e26f41d082c5adb89cd606528e526`](https://basescan.org/address/0x1af9097b043e26f41d082c5adb89cd606528e526#code) | [`0xdfa197784879926e2358a7c71c6a0bd487f919fd`](https://basescan.org/address/0xdfa197784879926e2358a7c71c6a0bd487f919fd#code) |
| `PipelineConvertFacet` | Replace | [`0xf26ab0c4a354f12c9e8fa86c423f463e2c72ddd8`](https://basescan.org/address/0xf26ab0c4a354f12c9e8fa86c423f463e2c72ddd8#code) | [`0xc54cd71bfa5d0fe05d887eabbec4dbbc94cf74ba`](https://basescan.org/address/0xc54cd71bfa5d0fe05d887eabbec4dbbc94cf74ba#code) |
| `SiloFacet`            | Replace | [`0xd954a3245a868ab3e991786d4e5e71892099a19d`](https://basescan.org/address/0xd954a3245a868ab3e991786d4e5e71892099a19d#code) | [`0xf6af001b8879a2e30db294b4c8deccbd0d168b34`](https://basescan.org/address/0xf6af001b8879a2e30db294b4c8deccbd0d168b34#code) |
| `SiloGettersFacet`     | Replace | [`0x6f9a49560a1db78cd32f1332e15fcaedc77aa66b`](https://basescan.org/address/0x6f9a49560a1db78cd32f1332e15fcaedc77aa66b#code) | [`0xadfd7fbe59ca2624cec1b04f9bc65c7f90efed36`](https://basescan.org/address/0xadfd7fbe59ca2624cec1b04f9bc65c7f90efed36#code) |

All six facets were replaced with unchanged external interfaces. The changed internal helper is called only by `SiloFacet.updateSortedDepositIds()`. The other replacements do not introduce additional function changes.

### Other Contract / Parameter Changes <a href="#c8de4019-aad5-8378-a287-81faf8242655" id="c8de4019-aad5-8378-a287-81faf8242655"></a>

The externally linked libraries below were redeployed. Only `LibSilo`’s own source changed. `LibTokenSilo`, `LibConvert` and `LibPipelineConvert` retained their own source. `ApprovalFacet` and `SiloGettersFacet` have no externally linked libraries. No protocol parameters were changed.

| Library              | Previous Implementation                                                                                                      | New Implementation                                                                                                           | Used By                                                           |
| -------------------- | ---------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------- |
| `LibSilo`            | [`0x49bfc93e96d467d79fc809c8f6da466f8ebb8975`](https://basescan.org/address/0x49bfc93e96d467d79fc809c8f6da466f8ebb8975#code) | [`0xefa4e6492966228a7af3d5d9045bd9c9e5dc72bf`](https://basescan.org/address/0xefa4e6492966228a7af3d5d9045bd9c9e5dc72bf#code) | `ClaimFacet`, `ConvertFacet`, `PipelineConvertFacet`, `SiloFacet` |
| `LibTokenSilo`       | [`0xe47c1d275ceb1554ce802a0755cc679294f1ae81`](https://basescan.org/address/0xe47c1d275ceb1554ce802a0755cc679294f1ae81#code) | [`0xf2a0e7fb0e9b1189b14274418e4ac78ae449b4ed`](https://basescan.org/address/0xf2a0e7fb0e9b1189b14274418e4ac78ae449b4ed#code) | `ClaimFacet`, `ConvertFacet`, `PipelineConvertFacet`, `SiloFacet` |
| `LibConvert`         | [`0xd10b907e2f8f452c4d43848a4f632a00eff9530b`](https://basescan.org/address/0xd10b907e2f8f452c4d43848a4f632a00eff9530b#code) | [`0x71e8ab0a025c0b96d64c34759880e6ba2ba0baa0`](https://basescan.org/address/0x71e8ab0a025c0b96d64c34759880e6ba2ba0baa0#code) | `ConvertFacet`                                                    |
| `LibPipelineConvert` | [`0x9b42c78bd19fdf3cf3b3c16c9184e1169bf2c7aa`](https://basescan.org/address/0x9b42c78bd19fdf3cf3b3c16c9184e1169bf2c7aa#code) | [`0xa4a2d1e394faac3e60297a55cdceeb38c0b6ca18`](https://basescan.org/address/0xa4a2d1e394faac3e60297a55cdceeb38c0b6ca18#code) | `ConvertFacet`, `PipelineConvertFacet`                            |

### Function Changes <a href="#d85e4019-aad5-8202-aa9b-814335cbf27d" id="d85e4019-aad5-8202-aa9b-814335cbf27d"></a>

* Updated `LibSilo._setSortedDepositIds()`, called by `SiloFacet.updateSortedDepositIds()`, to assign `idIndex[sortedDepositIds[i]] = i` for every validated Deposit ID before storing the array. This keeps the existing `getIndexForDepositId()` getter and index-based Deposit removal consistent with the new ordering. The external validation, return types and event interfaces were unchanged.
