Introduction
Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.
I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.
I also build and publish a variety of tools, libraries, and open-source projects.
This article covers reentrancy attacks, one of the best-known classes of smart contract vulnerabilities.
Smart contract developers need to recognize reentrancy when designing their contracts. The subject is also essential for bug bounty researchers.
If you are unfamiliar with bug bounties, start here.
[Solidity-Bug Bounty] A Beginner's Guide to Bug Bounties
Reentrancy attacks

First, consider how contracts interact.
Calling one contract from another
A prerequisite for understanding reentrancy is that one contract can call another contract.
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.13;
contract ContractA {
bool public check = false;
address public user;
/// @notice checkの値をtrueにし、userの値をcheckOk()を呼び出したアドレスに設定。
function checkOk() public {
check = true;
user = msg.sender;
}
}
contract ContractB {
ContractA public contractA;
// ContractAをデプロイしたアドレスを指定。
constructor(address _contractAAddress) {
contractA = ContractA(_contractAAddress);
}
/// @notice ContractA内のcheckOk()を呼び出し。
function callCheckOk() external {
contractA.checkOk();
}
}The example defines ContractA and ContractB.
ContractA
Calling checkOk sets check to true and stores the caller's address in user.
ContractB
ContractB is deployed with ContractA's address. Calling callCheckOk invokes ContractA's checkOk function.
The original demonstration shows this interaction.

The call succeeds. You can inspect the example in a local test environment using Remix.
What is reentrancy?
Reentrancy means entering a function or contract again before a previous invocation has completed.
For example, ContractB calls ContractA, and an external interaction during ContractA's execution allows another call into ContractA before the original invocation finishes.
Why is this dangerous? Suppose a function performs the following operations.
Sequence
- Read the variable
priceand transfer funds. - After the transfer finishes, set
priceto zero.
If the function is entered again after the transfer but before the update, it may still observe the old value and transfer funds repeatedly, subject to available funds and execution limits.
The following example demonstrates why this ordering matters.
Reentrancy example

The original article uses the following example contracts.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Bank {
// 預けた資金を記録する。
mapping(address => uint) public balances;
/// @notice 一定額資金を預ける関数。
function deposit() public payable {
balances[msg.sender] += msg.value;
}
/// @notice 預けている資金を引き出し、balancesの値を0にする。
function withdraw() public {
uint balance = balances[msg.sender];
// 預けた資金があるか確認。
require(balance > 0, "There are no funds available to withdraw.");
(bool sent, ) = msg.sender.call{value: balance}("");
// 資金を遅れた確認。
require(sent, "Failed to send Ether");
balances[msg.sender] = 0;
}
/// @notice 特定アドレスの預けた資金を確認する関数。
function getBalance() public view returns (uint) {
return address(this).balance;
}
}
contract ReentrancyAttack {
Bank public bank;
constructor(address _bankAddress) {
bank = Bank(_bankAddress);
}
/// @notice 資金を受け取ったり、実行した関数が存在しないときに呼ばれる。
/// 預けた資金が存在すれば引き出す処理をする。
fallback() external payable {
if (address(bank).balance >= 1 ether) {
bank.withdraw();
}
}
/// @notice 1 etherを預けて、その後預けた資金を引き出す関数。
function attack() external payable {
require(msg.value >= 1 ether);
bank.deposit{value: 1 ether}();
bank.withdraw();
}
/// @notice 特定アドレスの預けた資金を確認する関数。
function getBalance() public view returns (uint) {
return address(this).balance;
}
}Bank
Calling deposit deposits the specified amount. The amount is recorded in balances, keyed by the user's address.
The withdraw function allows withdrawal of deposited funds, while the balance getter returns the total funds held by the contract.
ReentrancyAttack
This demonstration contract is deployed with Bank's address. Its attack function deposits funds through Bank and requests a withdrawal. Its balance getter, like Bank's, returns the amount of funds held by the contract.
The recorded demonstration illustrates the unexpected result.

The two users' combined deposits of 5 ether end up in the demonstration contract.
The important component is the fallback function, which can run when a call does not match a declared function, including certain Ether transfers. During the vulnerable withdrawal, the external transfer allows the recipient to call Bank again before the original withdrawal updates its accounting.
Call sequence in the original demonstration
- ReentrancyAttack deposits 1 ether into Bank.
- The initial withdrawal requests the deposited 1 ether.
- Bank transfers funds before updating
balances, allowing the recipient's fallback function to run. - The nested withdrawal observes the old recorded balance.
- The recorded demonstration repeats this interaction while sufficient funds remain.
This explains the loss of the Bank balance in the isolated example. You can study the behavior in a local test environment with Remix.
Causes and mitigations

Cause
The vulnerable contract did not update balances before transferring funds.
Mitigation
Approach 1
Because withdraw uses the balance recorded in balances, clearing the balance before making the external transfer prevents the nested invocation from using that same balance.
function withdraw() public {
uint balance = balances[msg.sender];
// 預けた資金があるか確認。
require(balance > 0, "There are no funds available to withdraw.");
// 資金を引き出す前にbalancesのデータを更新。
balances[msg.sender] = 0;
(bool sent, ) = msg.sender.call{value: balance}("");
// 資金を遅れた確認。
require(sent, "Failed to send Ether");
}After withdrawal, the recorded balance is zero until a new deposit is made.
This is known as the checks-effects-interactions (CEI) pattern.
Checks validate the conditions for the operation, effects update internal state, and interactions invoke external contracts or transfer funds.
The key principle is to complete validation and the necessary state changes before handing control to external code.
Approach 2
Another useful technique is a reentrancy guard that checks whether a protected operation is already in progress.
The following variable and modifier implement a simple guard.
// 他の処理が実行されているかチェックするbool値。
bool internal locked;
// lockedがtrueの時は処理を行わない。
// lockedがfalseの時はtrueにして、一連の処理が終わったのちfalseに変更。
modifier noReentrant() {
require(!locked, "No re-entrancy");
locked = true;
_;
locked = false;
}The Boolean locked indicates whether a protected invocation is in progress.
When locked is true, noReentrant rejects another protected invocation. Otherwise, it sets the lock to true, executes the function body at _;, and resets the lock to false after the function completes.
This prevents another protected withdrawal from running before the original operation finishes. In production, the protection must cover all entry points that share the affected invariants.
function withdraw() public noReentrant {
uint balance = balances[msg.sender];
// 預けた資金があるか確認。
require(balance > 0, "There are no funds available to withdraw.");
(bool sent, ) = msg.sender.call{value: balance}("");
// 資金を遅れた確認。
require(sent, "Failed to send Ether");
balances[msg.sender] = 0;
}Historical reentrancy incidents

Uniswap and Lendf.me
The cited report describes losses of more than $25 million in cryptocurrency across incidents involving Uniswap and Lendf.me.
Beosin-Eagle Eye
The cited report describes an attack on the XSURGE DeFi protocol on BSC with losses of $4 million.
https://beosin.medium.com/a-sweet-blow-fb0a5e08657d
The DAO
The original article reports losses of approximately ¥5.2 billion and notes the Ethereum hard fork that resulted in the Ethereum and Ethereum Classic networks.
Conclusion

This article explained reentrancy, how it can affect contracts, and approaches to mitigation.
The topic can be difficult at first, so studying the example in an isolated environment and reviewing the references can help.
If you have questions, feel free to contact me on Twitter. I regularly write about Solidity, blockchain, and Web3 and share interesting articles there.
References
https://medium.com/@shub.sharma350/secure-transfer-in-smart-contracts-reentrancy-attack-9aa835d6aba9




