# 13. Reentrancy Attack

## Problem

* If we call a function of another contract in the middle of a **withdraw**, the other contract may have malicious function which may recalls the withdraw function again and again resulting in draining of all the funds
* <https://solidity-by-example.org/hacks/re-entrancy/>

## Solution (Easy Way)

* Always call another contract in the last step of transaction
* Updated balances (**change state**) before calling another contract

## Solution (Mutex)

* Use a mutex
* Openzepplin provides a way to safeguard i.e a Modifier
  * Contract name is [**ReentrancyGuard**](https://docs.openzeppelin.com/contracts/4.x/api/security#ReentrancyGuard)

***


---

# 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://notes.nomanaziz.me/development/blockchain/freecodecamp-course/13.-reentrancy-attack.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.
