Solidity Mapping: Complete Guide to Key-Value Data Structures

What is a Mapping in Solidity? 🗺️ A mapping in Solidity is a powerful key-value data structure that functions like a hash table or dictionary in traditional programming languages. It’s the go-to solution for associating wallet addresses with balances and managing relationships between different data types on the blockchain. Mappings are essential building blocks in Solidity, particularly for storing and managing critical data such as user balances, permissions, and any other relationship between two types of data. ...

September 30, 2024 · 2 min · 249 words · 0xuki

Understanding Solidity Modifiers: A Practical Guide

Modifiers in Solidity can be confusing for newcomers. This guide explains their purpose and usage in simple terms. Modifiers act as function decorators that let you add reusable preconditions to functions. They are commonly used for checks like ownership validation before executing a function’s core logic. What is a Modifier? A modifier injects reusable code that runs before a function executes. It is often used for: Access control (e.g., restricting functions to contract owners) Input validation Reusable preconditions The onlyOwner pattern is a classic example, restricting function access to a specific address. ...

September 19, 2024 · 2 min · 297 words · 0xuki

Quick Introdution of hardhat

When developing smart contracts using Solidity, efficient development processes and reliable testing are crucial. Therefore, in this article, we’ll introduce and explore Hardhat, a handy tool for Solidity developers. What is Hardhat? Hardhat is a development tool for building, testing, and deploying smart contracts on the Ethereum blockchain. Formerly known as Buidler, it was rebranded as Hardhat in 2020. Here are some key features of Hardhat: Customizable Task Runner: Hardhat provides a flexible task runner to execute various tasks required for Ethereum development. This allows for automation of tasks such as building, compiling, testing, and deploying contracts. ...

March 9, 2024 · 2 min · 332 words · 0xuki

Solidity Functions 🛠️

Solidity functions are the building blocks of smart contracts, allowing you to define the behavior and logic of your decentralized applications. They can be called internally or externally, and their visibility and state permissions can be controlled through modifiers. Understanding how to properly implement functions is crucial for creating efficient and secure smart contracts. Here are some basic function examples: Addition ➕ function add(uint a, uint b) public pure returns (uint){ return a + b; } Message 📝 function add(uint a, uint b) public pure returns (uint){ return a + b; } Solidity Visibility Modifiers 👁️ private 🔒 : Only accessible within the current contract internal 🔐 : Accessible within the current contract and inherited contracts external 🌐 : Only callable from outside the contract (via transactions or other contracts) public 🔓 : Accessible from anywhere (no restrictions) State Permission Modifiers ⚡ If you don’t specify pure/view, the function will consume gas. Keep this in mind! ...

October 15, 2022 · 1 min · 180 words · 0xuki

Creating Your First Simple Solidity Smart Contract with Remix IDE

Learn how to create your first simple Solidity smart contract using the popular Remix IDE. This tutorial covers the most basic contract that’s often introduced in Solidity learning resources. The contract functionality is straightforward: set a number and retrieve it. Even this simple operation consumes gas fees, which we’ll verify in a local EVM test environment. Creating Your First Smart Contract with Remix IDE 1. Access Remix IDE Visit https://remix.ethereum.org/ Ensure the Environment is set to [Remix VM (London)] for local testing. ...

September 19, 2022 · 1 min · 199 words · 0xuki

Solidity SafeMath Library: Secure Arithmetic Operations

Solidity SafeMath Library: Secure Arithmetic Operations 🔒 The SafeMath library provides secure arithmetic operations that prevent overflow and underflow vulnerabilities using assert statements. /** * @title SafeMath * @dev Math operations with safety checks that throw on error */ library SafeMath { /** * @dev Multiplies two numbers, throws on overflow. */ function mul(uint256 a, uint256 b) internal pure returns (uint256) { if (a == 0) { return 0; } uint256 c = a * b; assert(c / a == b); return c; } /** * @dev Integer division of two numbers, truncating the quotient. */ function div(uint256 a, uint256 b) internal pure returns (uint256) { // assert(b > 0); // Solidity automatically throws when dividing by 0 uint256 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; } /** * @dev Substracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend). */ function sub(uint256 a, uint256 b) internal pure returns (uint256) { assert(b <= a); return a - b; } /** * @dev Adds two numbers, throws on overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; assert(c >= a); return c; } } /** * @title SafeMath32 * @dev SafeMath library implemented for uint32 */ library SafeMath32 { function mul(uint32 a, uint32 b) internal pure returns (uint32) { if (a == 0) { return 0; } uint32 c = a * b; assert(c / a == b); return c; } function div(uint32 a, uint32 b) internal pure returns (uint32) { // assert(b > 0); // Solidity automatically throws when dividing by 0 uint32 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; } function sub(uint32 a, uint32 b) internal pure returns (uint32) { assert(b <= a); return a - b; } function add(uint32 a, uint32 b) internal pure returns (uint32) { uint32 c = a + b; assert(c >= a); return c; } } /** * @title SafeMath16 * @dev SafeMath library implemented for uint16 */ library SafeMath16 { function mul(uint16 a, uint16 b) internal pure returns (uint16) { if (a == 0) { return 0; } uint16 c = a * b; assert(c / a == b); return c; } function div(uint16 a, uint16 b) internal pure returns (uint16) { // assert(b > 0); // Solidity automatically throws when dividing by 0 uint16 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; } function sub(uint16 a, uint16 b) internal pure returns (uint16) { assert(b <= a); return a - b; } function add(uint16 a, uint16 b) internal pure returns (uint16) { uint16 c = a + b; assert(c >= a); return c; } } Usage Best Practices When using this library, it’s recommended to replace increment operators like ++ with SafeMath’s .add(1) method. ...

September 11, 2022 · 3 min · 571 words · 0xuki

Understanding ERC20 Token Standard in Solidity

Understanding ERC20 Token Standard in Solidity The ERC20 standard is a specification for tokens that operate on the Ethereum blockchain. It defines a common set of rules that all Ethereum-based tokens must follow, ensuring compatibility across different platforms and applications. What is ERC20? 🤔 ERC20 is a technical standard used for smart contracts on the Ethereum blockchain that implements a common list of rules for Ethereum tokens. This standard allows developers to create tokens that are compatible with the broader Ethereum ecosystem. ...

September 11, 2022 · 1 min · 116 words · 0xuki