How to blacklist an address in solidity, Code for blacklist Explained #solidity

28q5...GL9P
17 Jan 2024
67


In Solidity, you can implement a blacklist functionality to restrict certain addresses from performing specific actions within your smart contract. Below is a simple example of how you might implement a blacklist in a contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract BlacklistExample {
address public owner;
mapping(address => bool) public isBlacklisted;
event AddressBlacklisted(address indexed _address, bool _isBlacklisted);
modifier onlyOwner() {
require(msg.sender == owner, “Not the owner”);
_;
}
modifier notBlacklisted(address _address) {
require(!isBlacklisted[_address], “Address is blacklisted”);
_;
}
constructor() {
owner = msg.sender;
}
function blacklistAddress(address _address) external onlyOwner {
isBlacklisted[_address] = true;
emit AddressBlacklisted(_address, true);
}
function unblacklistAddress(address _address) external onlyOwner {
isBlacklisted[_address] = false;
emit AddressBlacklisted(_address, false);
}
function someFunction() external notBlacklisted(msg.sender) {
// Your logic here
}
}
YT: https://youtu.be/XsH3RIacjuY
Explanation:

  1. owner: The owner is the address that has the authority to blacklist or unblacklist addresses. This is typically set during contract deployment.
  2. isBlacklisted mapping: This mapping associates each address with a boolean value indicating whether it is blacklisted or not.
  3. AddressBlacklisted event: This event is emitted when an address is blacklisted or unblacklisted, providing information about the affected address and its blacklist status.
  4. onlyOwner modifier: This modifier restricts certain functions to be callable only by the owner of the contract.
  5. notBlacklisted modifier: This modifier ensures that a function can only be executed if the caller’s address is not blacklisted.
  6. blacklistAddress and unblacklistAddress functions: These functions allow the owner to blacklist or unblacklist an address.
  7. someFunction: This is just an example of a function that uses the notBlacklisted modifier. You would replace this with the actual logic you want to restrict.

Remember to thoroughly test your smart contract and ensure that it meets your specific requirements before deploying it on the Ethereum blockchain or any other blockchain platform.
Follow me for more, Thanks



Blacklist
Solidity
Solidity Tutorial
Smart Contracts
Smart Contract Blockchain





Follow


Written by Crypto Beast

22 Followers
·
Writer for
Coinmonks
Welcome to the Era of Metaverse & AI. Revolutionizing the Digital World with AI, Web3 and Blockchain Innovation. To know more about blockchain & AI follow me.

More from Crypto Beast and Coinmonks

Crypto Beast
How to Connect MetaMask to a Flutter App
MetaMask is a popular cryptocurrency wallet that allows users to store, send, and receive Ethereum and other ERC-20 tokens. Flutter is a…
3 min read
·
Oct 6, 2023

6




Shantanu Gupta
in
Coinmonks
Which Crypto Will Explode in 2024? Here Are Some Of MyTop Picks.
As we approach the end of the year, investors worldwide are gearing up for the anticipated bull run in the crypto market, eyeing the next…
4 min read
·
Dec 19, 2023

668

2



Velvet.Capital
in
Coinmonks
🚨Velvet.Capital Token Distribution (Airdrop)🚨
🚨 Airdrop Alert🚨: DeFi Asset Management Done right! Everything you need to know for Velvet.Capital’s Token Distribution & Airdrop!
4 min read
·
Dec 30, 2022

39K

1028



Crypto Beast
Understanding and Resolving the “useState is not a function” or “Return Value is not Iterable”
ReactJS has revolutionized the way developers build user interfaces by introducing a declarative and efficient approach to building UI…
3 min read
·
Nov 19, 2023

3



See all from Crypto Beast

See all from Coinmonks

Recommended from Medium

nagrarohit
How to become a Smart Contract Auditor
In this article, you will learn the complete roadmap to becoming a smart contract auditor.
10 min read
·
Sep 25, 2023

1




Securr
Complete Guide to Rust: Zero to One to Hacking!
In the fast-evolving WEB3 landscape, security is paramount. With the rise of blockchain technology, decentralized applications, and smart…
4 min read
·
Oct 2, 2023

68



Lists

Staff Picks558 stories
·
642
 
saves
Stories to Help You Level-Up at Work19 stories
·
420
 
saves
Self-Improvement 10120 stories
·
1217
 
saves
Productivity 10120 stories
·
1112
 
saves
Kristaps Grinbergs
in
Block Magnates
Understanding tx.origin and msg.sender in Solidity
Developers need to understand the difference between tx.origin and msg.sender in Solidity. These two global variables often need…

·
3 min read
·
Jan 6

5




Johnny Time
Why you should ALWAYS use SafeERC20
So… I recently participated in the Codehawks decentralized stablecoin contest, and I reported a valid medium risk issue, you can also watch…
3 min read
·
Sep 6, 2023

29




Kaan Kaçar
in
Coinmonks
How to Create Upgradable Smart Contracts
I loved the idea of upgradable smart contracts, until I used one. Nevertheless, any web3 developer should know them.
2 min read
·
Dec 23, 2023

5




Heuss
Unprotected Swap() Function: A ERC777 Reentrancy Vulnerability
This article sheds light on a critical vulnerability associated with the swap() function in a bridge/DEX. This vulnerability exposes a…
2 min read
·
Aug 9, 2023

60



See more recommendations


Write & Read to Earn with BULB

Learn More

Enjoy this blog? Subscribe to cobaltt4

1 Comment

B
No comments yet.
Most relevant comments are displayed, so some may have been filtered out.