Secure Smart Contract library providing battle-tested implementations of industry-standard Solidity contracts including ERC20, ERC721, ERC1155 tokens, access control mechanisms, proxy patterns, and governance systems for Ethereum and EVM-compatible blockchains.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
OpenZeppelin Contracts provides essential security mechanisms to protect smart contracts from common vulnerabilities including reentrancy attacks, unauthorized access during emergency situations, and payment-related exploits.
Import security contracts using Solidity import statements:
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/security/PullPayment.sol";Protects against reentrancy attacks by preventing nested calls to functions marked with the nonReentrant modifier.
abstract contract ReentrancyGuard {
modifier nonReentrant();
}pragma solidity ^0.8.0;
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
contract SecureWithdrawal is ReentrancyGuard {
mapping(address => uint256) private balances;
function withdraw() external nonReentrant {
uint256 balance = balances[msg.sender];
require(balance > 0, "No funds to withdraw");
balances[msg.sender] = 0;
(bool success, ) = msg.sender.call{value: balance}("");
require(success, "Transfer failed");
}
}Provides emergency pause functionality that can halt contract operations when security issues are detected.
abstract contract Pausable is Context {
function paused() public view virtual returns (bool);
modifier whenNotPaused();
modifier whenPaused();
function _pause() internal virtual;
function _unpause() internal virtual;
}event Paused(address account);
event Unpaused(address account);pragma solidity ^0.8.0;
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract EmergencyPausableToken is Pausable, Ownable {
mapping(address => uint256) private balances;
function transfer(address to, uint256 amount) public whenNotPaused {
require(balances[msg.sender] >= amount, "Insufficient balance");
balances[msg.sender] -= amount;
balances[to] += amount;
}
function emergencyPause() external onlyOwner {
_pause();
}
function emergencyUnpause() external onlyOwner {
_unpause();
}
}Implements the pull payment pattern to avoid reentrancy risks and failed transfers by allowing payees to withdraw their funds themselves.
abstract contract PullPayment {
function payments(address dest) public view returns (uint256);
function withdrawPayments(address payable payee) public virtual;
function _asyncTransfer(address dest, uint256 amount) internal;
}pragma solidity ^0.8.0;
import "@openzeppelin/contracts/security/PullPayment.sol";
contract Auction is PullPayment {
address public highestBidder;
uint256 public highestBid;
function bid() public payable {
require(msg.value > highestBid, "Bid not high enough");
if (highestBid != 0) {
// Previous highest bidder can withdraw their bid
_asyncTransfer(highestBidder, highestBid);
}
highestBidder = msg.sender;
highestBid = msg.value;
}
function withdrawPreviousBid() public {
withdrawPayments(payable(msg.sender));
}
}When using OpenZeppelin security patterns:
nonReentrant modifier for functions that make external calls or transfer fundsnonReentrant and whenNotPaused modifiersSecurity contracts may revert with the following errors:
ReentrancyGuardReentrantCall() - When reentrancy is detectedInstall with Tessl CLI
npx tessl i tessl/npm-openzeppelin-solidity