# LibMulticall

The purpose of this library is to enable the execution of multiple delegatecalls to different facets of a Diamond contract in a single transaction.

### multicall():

The function begins by accessing the `LibDiamond.DiamondStorage` storage struct from the Diamond contract using the `LibDiamond.diamondStorage()` function. This allows the function to access the storage variables of the Diamond contract.

The function checks if the length of `selectors` is equal to the length of `data`. If the lengths are not equal, it reverts the transaction by calling `revert` with the error `SelectorsLengthInvalid()`. This ensures that each selector has a corresponding data item.

The function then iterates over each element in the `data` array using a `for` loop. It retrieves the facet address associated with the selector from the `selectorToFacetAndPosition` mapping stored in the Diamond contract's storage.

The `delegatecall` function is invoked on the `facet` address, passing the corresponding `data[i]` as the delegatecall data. Delegatecall allows the called contract (facet) to execute the function within the context of the calling contract (the current contract). The `delegatecall` returns a tuple `(bool success, bytes memory returnData)`.

If the `delegatecall` was not successful (i.e., `success` is `false`), the function reverts the transaction by calling `revert` with the error `InvalidMulticall()`.

**Input**:

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">selectors
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes4[]
</code></pre></td><td>an array of function selectors</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">data
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes[]
</code></pre></td><td>an array of data that needs to be fed into the selectors</td></tr></tbody></table>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.fly.trade/developers/deprecated-magpie-contracts/magpieaggregator-diamond-proxy/multicall/libmulticall.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
