James Bachini

Solidity Examples

  • Creating A Solidity Escrow Smart Contract

    Creating A Solidity Escrow Smart Contract

    In this tutorial, we will create an Escrow contract using Solidity and deploy it to an Ethereum testnet. An Escrow contract is a financial agreement where a third party (the arbitrator) holds and regulates payment of the funds required for two parties involved in a given transaction. It helps make transactions more secure as it…

  • How To Create ERC404 Tokens Solidity Tutorial

    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

    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

    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. Upgradeable Proxy Contracts A proxy contract is a design pattern…

  • Hardhat Solidity Tutorial

    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…

  • Solidity Tutorial | Gas Paying NFT

    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.…

  • Chainlink CCIP | Cross-Chain Interoperability Protocol

    Chainlink CCIP | Cross-Chain Interoperability Protocol

    From all the announcements at ETHcc, the release of Chainlink CCIP as a direct competitor to LayerZero for cross-chain communications and bridging technology, is perhaps the most interesting. The inherent security risks associated with bridging technology means that Chainlink are in a good position to leverage their brand and security record to gain traction in…

  • Using Ethereum To Offset Bitcoin’s Carbon Footprint

    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

    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

    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,…

  • Introduction to Flash Loans | Unleashing Capital On Demand

    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

    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

    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

    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

    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

    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 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

    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

    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 | 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 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 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

    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 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 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

    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

    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

    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 | 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

    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 | 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 | 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

    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 | 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 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

    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

    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.…