> For the complete documentation index, see [llms.txt](https://docs.fly.trade/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.fly.trade/developers/deprecated-magpie-contracts/magpieaggregator-diamond-proxy/multicall/libmulticall.md).

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