Solidity Tips
-
Building A Cross Chain Token Bridge With LayerZero v2
This tutorial will guide you through the process of building a token bridge using LayerZero v2, specifically employing the OFTAdapter on mainnet and the OFT.sol contract on all other chains. We’ll cover the setup and deployment workflow, ensuring you have a comprehensive understanding of the process. Before we delve into the deployment process, it’s crucial…
-
Practical Applications Of Merkle Trees
Merkle trees are a useful tool for blockchain developers offering a myriad of benefits, chief among them being the efficient verification of large data sets and proof of inclusion. By leveraging Merkle trees developers can significantly optimise gas costs and enhance the overall performance of their decentralised applications. In this article we will look at…
-
Implementing RBAC Patterns in Solidity
We can implement role based access control or RBAC patterns in Solidity using OpenZeppelin’s AccessControl.sol library. This allows developers to manage different roles and assign permissions dynamically. Here’s a step by step guide based on the information provided. Understanding Roles in Access Control The essence of RBAC in Solidity involves defining roles, where each role…
-
Creating MultiSig Wallets with Solidity
MultiSig wallets have become increasingly popular due to their enhanced security features. In this article, we will delve into the key components of a MultiSig wallet, how to write a MultiSig wallet contract using Solidity, deploying the wallet, and handling deposits and withdrawals. Note that the code provided here is for educational purposes only. If…
-
Prediction Markets Solidity
Prediction markets are decentralized platforms where participants can bet on the outcome of future events. For example, people could bet on the outcome of a presidential election or a sports game. In this tutorial, we will walk through a smart contract built on Ethereum, which allows users to create and participate in prediction markets. The…
-
Understanding ABI Encoding: A Guide to abi.encodeCall and encodeWithSelector
What Is ABI Encoding Application Binary Interface (ABI) encoding is a crucial concept in Ethereum smart contract development. It serves as a standardised method for encoding function calls and data, enabling seamless communication between different components of the Ethereum ecosystem. ABI encoding ensures that data is consistently formatted and interpreted across various platforms and programming…
-
Enums In Solidity
Enums are one of the most useful and underused data types in Solidity. I think because they aren’t widely used in Javascript and web dev, they often get overlooked in contract development. In this article I’ll make my case for why enums deserve a place in your tool kit and smart contracts. Enums in Solidity…
-
Pump.fun Clone In Solidity
Pump.fun is a token factory that lets users create and trade memecoins on Solana with dynamic pricing along a bonding curve. In this article I’ll show how I went about converting this concept to Solidity and deploying it on Ethereum. Full code for this is open source at: https://github.com/jamesbachini/Pump.sol Token Factory Contract At the core…
-
Script To Analyze Bytecode Size Of Smart Contracts
I was having trouble with the hardhat bytecode plugin so I wrote a little nodejs script which will print off the size of my compiled Solidity contracts. You’ll need nodejs installed and can then run it like this. You should get an ouput like this: Here’s the code to save to analyze_bytecode.js file in the…
-
How To Create ERC404 Tokens Solidity Tutorial
An ERC404 token is a digital asset that combines the characteristics of ERC20 fungible tokens and ERC721 non-fungible tokens (aka NFT’s) to enable fractional ownership of an NFT. In essence, ERC404 tokens represent divisible parts of an NFT, allowing multiple individuals to own shares of a single NFT. This approach is designed to enhance the…
-
Sell Me This Pen.sol
What happens when you ask a blockchain developer to sell you a pen? Etch your words permanently on the Ethereum blockchain where they will outlive you & create an everlasting record of your thoughts, contemplations & predictions. This is your chance to leave an eternal mark and express your presence in the digital age while…
-
Solidity Proxy Contract Tutorial With Example Code
This Solidity proxy contract tutorial delves into the concept of upgradeable proxy contracts in Solidity, specifically utilizing OpenZeppelin’s proxy contract template. We’ll start with an understanding of how these contracts work, the compromise of immutable decentralization vs upgradeability and then dive into practical code examples. Proxy Contract Video Tutorial In this video I provide an…
-
Hardhat Solidity Tutorial
Hardhat is an extensive framework for developing, testing and deploying solidity smart contracts. In this Hardhat Solidity tutorial you will learn how to set it up, how to create hardhat unit test tricks, working with hardhat scripts and finally some Hardhat tips and tricks for Solidity developers. Hardhat Easy Setup Hardhat is built on NodeJS…
-
The Essential Checklist For Deploying A New Token
In this article we are going to look at all the things that need doing when launching a new token to create a token deployment checklist for developers and founders. At the end there is a big list of all the token listing sites that you can submit your new token to. Unit Tests &…
-
Solidity Tutorial | Gas Paying NFT
The challenge is to create a NFT contract that charges 1 ETH to mint but then stores the entire amount as collateral in a liquid staking token. As staking rewards come in they get distributed to the holders of the NFTs. At any time a user can burn the NFT to reclaim the 1 ETH.…
-
Using Ethereum To Offset Bitcoin’s Carbon Footprint
Never have I sounded more like an Ethereum maxi, but there is a point beyond the provocative title. In this post I’m going to explore the potential for investors to use carbon credit tokens on Ethereum to offset the electrical consumption of BTC mining on their Bitcoin holdings. This novel idea provides the opportunity to…
-
How To Shuffle A Deck Of Cards In Solidity
In this post I explore how to simulate the shuffling of a deck of cards, addressing the challenges of randomness and predictability in a solidity smart contract environment where every input and output is part of the public record. Whether you’re looking to create an on-chain poker game, a collectible card game, or you’re simply…
-
Automate Solidity With MEV
There are no cron jobs in Solidity or native to the Ethereum blockchain. To automate Solidity code we can either use an external service/oracle or we can incentivise MEV searchers to complete tasks efficiently on our behalf. This type of Solidity automation is very common and used widely across liquidation systems which require reliable, fast,…
-
3 Tips For Gas Efficient Solidity Smart Contracts
Here are 3 tips which could be considered the low hanging fruit of gas efficient solidity smart contracts. James On YouTube Watch On YouTube: https://youtu.be/xfNHzqkMWEQ |Subscribe Use Correct Modifiers & Declarations This is the simplest way to get some small wins with gas optimisation. Go through your contract and define anything that shouldn’t change as…
-
Introduction to Flash Loans | Unleashing Capital On Demand
In this tutorial on flash loans we will be creating a Solidity smart contract which takes a flash loan from Uniswap v3. This allows you to borrow huge amounts of capital with the catch that you have to pay it back in the same block or the whole transaction is reverted. James On YouTube Watch…
-
Sign In With Ethereum Tutorial
To sign in with Ethereum using Metamask we will be using the ERC4361 specification that allows Ethereum accounts to authenticate with off-chain services using a standard message format. This technology offer users a self-custodial alternative to centralized identity providers that generally use email:password credentials. In Web3 applications we can improve user experiences and establish a…
-
Solidity Callback | Solidity Tips & Examples
A callback function in Solidity allows us to pass a function reference from one contract to another and have it execute as part of an atomic transaction. James On YouTube Watch On YouTube: https://youtu.be/pjm0eOelPXc |Subscribe Let’s first take a look at an example from the Solidity Snippets Github repo. Full code at: https://github.com/jamesbachini/Solidity-Snippets/blob/main/contracts/Callback.sol This code…
-
Strings in Solidity | Solidity Tips & Examples
A string in Solidity is a data type used to store text. Working with strings in Solidity can be challenging due to the limitations and efficiency required by the Ethereum virtual machine. This article aims to provide a comprehensive guide on how to use strings in Solidity, covering their properties, limitations, and best practices for…
-
Timestamp in Solidity | Solidity Tips & Examples
A timestamp is a numerical value that represents a specific moment in time. It’s often used to track when a certain event occurred or when a piece of data was created or modified. The most common way to represent a timestamp is by using the number of seconds that have elapsed since a specific moment…
-
encodeCall in Solidity | Solidity Tips & Examples
From Solidity version 0.8.11 we can use an interface to define a function to pass to encodeCall parameters. encodeCall is a function that allows you to encode a function call with its parameters into a single byte array. This byte array can then be used to make a low-level call to another contract’s function. Here…
-
Custom Error Handling in Solidity | Solidity Tips & Examples
Custom error handling enables Solidity developers to efficiently handle reverts and failed transactions including logging parameters. Let’s first take a look at an example from the Solidity Snippets Github repo: Note that the Solidity compiler version must be greater than 0.8.4 to use custom errors. Customer errors are supported by Etherscan and most other block…
-
Solidity Libraries | Solidity Tips & Examples
A solidity library is a reusable contract that contains functions that can be called by other contracts. When you import a Solidity library into your contract, you can use the functions provided by the library. How To Import Solidity Libraries In your smart contract, you can import the library by using the import keyword followed…
-
Random Numbers in Solidity | Solidity Tips & Examples
Generating a random number in Solidity is not as straightforward as in some other programming languages, because the Ethereum blockchain is deterministic, meaning that multiple execution clients or nodes must come to the same conclusion about the state of the blockchain. For many use cases using the new prevrandao global variable provides “good enough” randomness…
-
Conditional Statements & Loops in Solidity | Solidity Tips & Examples
Conditional statements & loops in Solidity are fairly intuitive and follow the conventions of other languages such as Javascript. In this article we will go through some examples of each before looking at the relational operators we can use within these statements. if statement The “if” statement is used to execute a block of code…
-
Natspec in Solidity | Solidity Tips & Examples
Natspec comments are important for Solidity developers to understand because sooner or later you will come across a code base that requires you work with them. Proponents will say Natspec makes the code more readable and understandable. While I personally disagree with this and prefer to separate code and documentation, the format is popular and…
-
ERC1155 Token Contract | Solidity Tips & Examples
ERC1155 offers a more flexible and efficient way of managing fungible and non-fungible tokens, making it an attractive option for developers who want to create complex digital assets with multiple use cases. In this blog post, we’ll dive into the details of ERC1155 and explore why it’s becoming a preferred choice for Solidity developers looking…
-
ERC721 Token Contract | Solidity Tips & Examples
The ERC721 Token contract is the original industry standard for NFTs (Non-fungible tokens). In this article we will look at a simple example and talk about some of the best practices around building ERC721 Tokens. James On YouTube Watch On YouTube: https://youtu.be/B64VBGt3tMU |Subscribe Why Use ERC721 Token ERC721 is a standard interface for NFTs on…
-
ERC20 Token Contract | Solidity Tips & Examples
ERC20 tokens have become the de facto standard for creating tokens on Ethereum and are widely used in many DeFi protocols for things like governance and utility tokens. Why Use ERC20 Token Solidity developers use ERC20 tokens because they are the industry standard for fungible tokens (where every token is equal to another one). The…
-
Merkle Tree in Solidity | Solidity Tips & Examples
Merkle Trees provide an efficient way to verify data in Solidity. This reduces the gas cost for on-chain storage when validating large data sets such as a large list of addresses. James On YouTube Watch On YouTube: https://youtu.be/NTPpyL4pJG0 |Subscribe How Do Merkle Trees Work Merkle trees are a data structure that allow efficient and secure…
-
How To Emit Events in Solidity | Solidity Tips & Examples
A Solidity Event is a way for smart contracts to communicate with the outside world by providing a mechanism for emitting messages that can be observed by external applications. It’s like a signalling message that is broadcast when a certain condition is met within the smart contract. Emitting Events In Solidity Here is a simple…
-
Bulk Send Tokens & ETH Contract | Solidity Tips & Examples
Sending payments in bulk can be a tedious and time-consuming process, especially when dealing with a large number of recipients. However, with a Solidity smart contract, bulk payments can be executed in a matter of seconds, providing a more efficient and cost-effective solution for managing bulk transfers. In this article, I will walk you through…
-
Multiple Return Values | Solidity Tips & Examples
In Solidity we can multiple return values from a function. This enables us to efficiently move data about within the application flow of a smart contract. In this example we are returning three variables enclosed within brackets. Note that the data types of these variables are defined at the top of the function in the…
-
Modifiers in Solidity | Solidity Tips & Examples
Modifiers in Solidity are a way to add a certain condition to functions within a smart contract. They allow developers to define a piece of code that can be reused across multiple functions and contracts, making your code more modular and easier to read. A modifier is defined using the modifier keyword, followed by a…
-
Struct in Solidity | Solidity Tips & Examples
A struct in Solidity is a data type that allows you to group together multiple variables of different data types into a single unit. This is useful because it gives us a convenient container to package structured data, it enables us to pack variables into memory slots more efficiently and it allows us to get…
-
Ownable Contracts | Solidity Tips & Examples
Ownable contracts in Solidity are used to implement access control for certain functions. The idea is that only the contract owner, who is typically the deployer of the contract, can execute these protected functions. To do this we will first import the OpenZeppelin library Inside our contract we can then add the onlyOwner modifier to…
-
Payable Transfers | Solidity Tips & Examples
Payable transfers are one of a number of ways to send ether from a Solidity smart contract to an external address. In this next example we create a fallback function so that any user can send this contract ether and it will get split between the different addresses. Full code at: https://github.com/jamesbachini/Solidity-Snippets/blob/main/contracts/PaymentDivider.sol Payable fails if…
-
Solidity Interface | Solidity Tips & Examples
A Solidity interface is code that provides a set of function declarations without any implementation details. Interfaces are used to interact with 3rd party contracts or external systems by defining a common set of functions that both parties agree to implement. Interfaces are used to define a contract’s external-facing functions, which is the only part…
-
OpenZeppelin Libraries | Solidity Tips & Examples
OpenZeppelin libraries are popular for building secure smart contracts on top of pre-audited code in Solidity. The framework includes a number of reusable smart contract components, such as token contracts, access control contracts, and math libraries. In this article we will explore the various Libraries available within the OpenZeppelin framework to give you a tool…
-
Arrays in Solidity | Solidity Tips & Examples
Arrays are a fundamental data structure in programming, and Solidity is no exception. In Solidity, there are several types of arrays available, including dynamic arrays, fixed-size arrays, and multi-dimensional arrays. Dynamic Arrays Dynamic arrays are the most common type of array in Solidity. These are arrays whose length can be changed during runtime. To declare…
-
Get Contract Balance | Solidity Tips & Examples
To get the contract balance in solidity there are a few different methods depending on if you want the Ether balance or an ERC20 token balance. Contract Balance Eth You can use the address type’s balance property to get the balance of any address, including the contract’s own address. The this value is used to…
-
Mapping in Solidity | Solidity Tips & Examples
This article explores how mapping in Solidity is used to for storing key->value pairs, where the keys are of a specific data type and the values can be of any data type. Mappings are declared using the mapping keyword, followed by the key data type in parentheses and the value data type after the parentheses.…
-
ERC721 vs ERC1155 | Solidity Tips & Examples
Today we will discuss the differences between ERC721 vs ERC1155 smart contracts and the use cases where each is best suited. These are the two most popular types of smart contracts used for the creation of NFTs (non-fungible tokens). James On YouTube Watch On YouTube: https://youtu.be/B64VBGt3tMU |Subscribe ERC721 Smart Contract ERC721 is the original standard…