DEFI LIBRARY FOUNDATIONAL CONCEPTS

From Basics to Deployment: ERC-721 and ERC-1155 for DeFi Enthusiasts

8 min read
#DeFi #Smart Contracts #Blockchain #Token Standards #ERC-721
From Basics to Deployment: ERC-721 and ERC-1155 for DeFi Enthusiasts

Introduction to Token Standards in DeFi

Digital assets on Ethereum are built on a set of rules called token standards. These standards define how tokens behave, how they are stored, and how they interact with smart contracts. For developers and enthusiasts who want to explore the intersection of non‑fungible tokens (NFTs) and decentralized finance (DeFi), the most important standards to understand are ERC‑721 and ERC‑1155.

In this guide we start from the very basics, explain why NFTs matter in DeFi, compare the two standards side by side, and walk through a practical deployment example. By the end you will have a solid foundation to experiment with NFTs in your own DeFi projects.


What Makes a Token Standard?

A token standard is simply a specification that describes a set of functions and events. It guarantees that any contract that follows the standard will behave predictably. Think of it as a contract template that all applications can trust.

  • ERC‑20 is the standard for fungible tokens (e.g., USDC, DAI).
  • ERC‑721 is the standard for unique, indivisible tokens (the classic NFT).
  • ERC‑1155 is a newer standard that allows a single contract to hold both fungible and non‑fungible tokens.

The advantage of using a standard is that wallets, exchanges, and other contracts can interact with any token that implements the same interface without needing custom code.


NFTs in DeFi: Why They Matter

Non‑fungible tokens are usually associated with digital art, collectibles, or virtual real estate. However, DeFi ecosystems have begun to leverage the unique properties of NFTs in creative ways:

  1. Collateralization – NFTs can serve as collateral for loans on platforms like NFTfi or Aave’s NFT vaults.
  2. Tokenization of Physical Assets – Real‑world items can be represented as NFTs, enabling fractional ownership.
  3. Governance – Rare NFTs can confer voting power or exclusive rights within DAO structures.
  4. Yield Farming – Some protocols allow users to stake NFTs to earn fungible tokens.

Because NFTs are non‑fungible, they require specialized handling compared to fungible tokens. That’s where ERC‑721 and ERC‑1155 come into play.


ERC‑721: The Classic NFT Standard

Core Characteristics

  • Uniqueness – Every token has a distinct ID that cannot be replicated.
  • Indivisibility – A token cannot be split into smaller units.
  • Metadata URI – Each token can point to external metadata (images, attributes, etc.).
  • Ownership Transfer – Secure transfer via transferFrom or safeTransferFrom.

Typical Use Cases

  • Digital art platforms like OpenSea.
  • Collectible card games (e.g., CryptoKitties).
  • In‑game items that are truly unique.

Key Functions

Function Purpose
balanceOf(address) Number of NFTs owned by an address.
ownerOf(uint256) Owner of a specific token ID.
approve(address, uint256) Approve another address to transfer a token.
setApprovalForAll(address, bool) Grant or revoke operator approval for all tokens.
safeTransferFrom(...) Transfer while ensuring the recipient can handle ERC‑721.

Pros and Cons

Pros Cons
Simple to understand. Cannot batch operations efficiently.
Widespread support. Each NFT requires a separate contract if you want distinct behavior.
Strict ownership guarantees. Higher gas cost for minting many tokens.

ERC‑1155: Multi‑Token Flexibility

Core Characteristics

  • Batch Operations – Mint, transfer, and burn multiple token types in a single transaction.
  • Mixed Token Types – A single contract can contain fungible, semi‑fungible, and non‑fungible tokens.
  • Single Address for All Tokens – Easier to manage permissions and royalties.

Typical Use Cases

  • Game assets where some items are common (e.g., gold coins) and others rare (e.g., legendary weapons).
  • Decentralized exchanges that support token swaps for multiple token types simultaneously.
  • Protocols that want to offer NFT collateral and fungible rewards from the same contract.

Key Functions

Function Purpose
balanceOf(address, uint256) Balance of a specific token ID for an address.
balanceOfBatch(address[], uint256[]) Batch balances for multiple addresses and IDs.
setApprovalForAll(address, bool) Grant or revoke operator approval.
safeTransferFrom(...) Transfer a single token ID.
safeBatchTransferFrom(...) Transfer multiple token IDs in one call.

Pros and Cons

Pros Cons
Gas efficient for large token sets. Slightly more complex logic.
Unified interface for multiple token types. Less established tooling compared to ERC‑721.
Ideal for games and DeFi protocols that require both types. Some legacy platforms may not support it yet.

Choosing Between ERC‑721 and ERC‑1155

Scenario Recommended Standard
You need a single, immutable digital asset with a lot of external metadata. ERC‑721
You have many items, some fungible, some non‑fungible, and want efficient batch transfers. ERC‑1155
You anticipate future upgrades or cross‑token interactions. ERC‑1155
You need maximum compatibility with existing NFT marketplaces. ERC‑721

A pragmatic strategy is to start with ERC‑1155 if you foresee scaling or mixed token types, and then create a wrapper or bridge if you later need to expose specific assets to purely ERC‑721 marketplaces.


Step‑by‑Step Deployment: From Concept to Live Contract

Below is a concise guide to deploying an ERC‑1155 contract that can be used for both fungible and non‑fungible assets. We assume you are familiar with Solidity and the Hardhat development environment.

1. Project Setup

mkdir nft-defi-demo
cd nft-defi-demo
npm init -y
npm install --save-dev hardhat @nomiclabs/hardhat-ethers ethers
npx hardhat

Choose “Create an empty hardhat.config.js” when prompted.

2. Install OpenZeppelin Contracts

OpenZeppelin provides battle‑tested implementations for ERC‑1155.

npm install @openzeppelin/contracts

3. Write the Contract

Create contracts/DeFiNFT.sol:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract DeFiNFT is ERC1155, Ownable {
    // Mapping from token ID to its supply (for fungible tokens)
    mapping(uint256 => uint256) private _supply;

    constructor(string memory uri_) ERC1155(uri_) {}

    // Mint new tokens
    function mint(
        address to,
        uint256 id,
        uint256 amount,
        bytes memory data
    ) external onlyOwner {
        _mint(to, id, amount, data);
        _supply[id] += amount;
    }

    // Mint batch
    function mintBatch(
        address to,
        uint256[] memory ids,
        uint256[] memory amounts,
        bytes memory data
    ) external onlyOwner {
        _mintBatch(to, ids, amounts, data);
        for (uint i = 0; i < ids.length; i++) {
            _supply[ids[i]] += amounts[i];
        }
    }

    // Burn tokens
    function burn(
        address from,
        uint256 id,
        uint256 amount
    ) external {
        require(msg.sender == from || isApprovedForAll(from, msg.sender), "Not approved");
        _burn(from, id, amount);
        _supply[id] -= amount;
    }

    // Get total supply for a token ID
    function totalSupply(uint256 id) external view returns (uint256) {
        return _supply[id];
    }
}

4. Configure Hardhat

Edit hardhat.config.js:

require("@nomiclabs/hardhat-ethers");

module.exports = {
  solidity: "0.8.20",
  networks: {
    goerli: {
      url: "https://goerli.infura.io/v3/<YOUR_INFURA_KEY>",
      accounts: [`0x${process.env.PRIVATE_KEY}`]
    }
  }
};

Add your Infura key and private key to .env and load it using dotenv.

5. Compile

npx hardhat compile

6. Write a Deployment Script

Create scripts/deploy.js:

async function main() {
  const [deployer] = await ethers.getSigners();
  console.log("Deploying contracts with account:", deployer.address);

  const DeFiNFT = await ethers.getContractFactory("DeFiNFT");
  const nft = await DeFiNFT.deploy("https://api.example.com/metadata/{id}.json");

  await nft.deployed();
  console.log("DeFiNFT deployed at:", nft.address);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

7. Deploy to Goerli

npx hardhat run scripts/deploy.js --network goerli

The script will output the contract address. Verify on Etherscan and test minting.

8. Interacting with the Contract

Use Hardhat console or a front‑end framework to call mint, mintBatch, or burn. For example:

const tx = await nft.mint(deployer.address, 1, 100, "0x");
await tx.wait();

This mints 100 units of fungible token ID 1 to the deployer. For a non‑fungible token, call mint with amount = 1.

9. Integrating with DeFi Protocols

  • Collateralization – Wrap the NFT contract into a vault contract that accepts any ERC‑1155 token as collateral.
  • Yield Farming – Allow users to stake specific token IDs to earn rewards.
  • Cross‑chain Bridges – Use ChainBridge or similar to transfer ERC‑1155 tokens between networks.

Security Considerations

Issue Mitigation
Reentrancy attacks Use the Checks-Effects-Interactions pattern or ReentrancyGuard.
Overflow/underflow Solidity 0.8 automatically reverts on overflow.
Unauthorized minting Restrict mint functions to the contract owner or a role‑based access control.
Token enumeration If you expose an allTokens function, make sure it is gas‑efficient.
Metadata tampering Store immutable metadata on IPFS or use a pinned gateway.

OpenZeppelin’s libraries already include many of these safeguards. Always audit your contract or use a reputable third‑party auditor before deploying to mainnet.


Tooling and Ecosystem Support

  • Hardhat & Truffle – Local development and testing.
  • OpenZeppelin Defender – Deploys, monitors, and secures contracts.
  • Chainlink Keepers – Automate periodic actions (e.g., auto‑bidding on NFTs).
  • The Graph – Index ERC‑1155 events for off‑chain analytics.
  • Third‑web – Front‑end SDK that simplifies wallet interactions.

Community Resources

  • OpenZeppelin Blog – Guides on token standards.
  • Ethereum Stack Exchange – Question‑answering for Solidity nuances.
  • DeFi Pulse & Nansen – Analytics on NFT collateralization trends.
  • Discord Communities@defifarm, @nftcatalyst, and others share real‑world use cases.

Conclusion

ERC‑721 and ERC‑1155 each bring unique strengths to the DeFi landscape. ERC‑721 remains the gold standard for truly unique digital assets, while ERC‑1155 offers a powerful, gas‑efficient approach for handling multiple token types within a single contract. Understanding these standards is crucial for building robust, interoperable DeFi solutions that leverage the full potential of digital ownership.

By following the deployment steps outlined above, you can prototype an ERC‑1155 contract that fits both fungible and non‑fungible use cases. From there, the path opens to innovative collateralization mechanisms, yield farming strategies, and cross‑chain integrations that bring NFT utility to the broader DeFi ecosystem.

Happy hacking, and may your smart contracts stay secure and your yields stay high!

JoshCryptoNomad
Written by

JoshCryptoNomad

CryptoNomad is a pseudonymous researcher traveling across blockchains and protocols. He uncovers the stories behind DeFi innovation, exploring cross-chain ecosystems, emerging DAOs, and the philosophical side of decentralized finance.

Discussion (7)

MA
Marco 1 month ago
Nice walkthrough, the breakdown of the differences between ERC‑721 and ERC‑1155 really helped. I especially liked the part about batch transfers; it could cut gas costs for DeFi projects significantly.
IV
Ivan 1 month ago
Sure thing but you forget the trade off: ERC‑1155 can be more complex to audit. Those shared IDs confuse a lot of developers. Not all projects care about batch ops, so why adopt it just because it looks cool?
DM
Dmitri 1 month ago
Good point Ivan, but think about liquidity pools for NFTs. ERC‑1155 allows fractional ownership of items, which opens doors for liquidity that ERC‑721 can’t match. Anyone willing to roll the dice with auditing is usually in a good position.
LU
Lucia 1 month ago
The article made a quick pass over the interface compatibility issue. In reality, many DeFi protocols have hard‑coded ERC‑721 assumptions. Switching to ERC‑1155 might require rewriting a chunk of the front‑end logic, especially when handling approvals and transfer hooks.
AL
Alex 1 month ago
What about the gas cost for a simple ERC‑1155 single transfer? I was under the impression that ERC‑721 was cheaper for single movements. Any real numbers?
LI
Livia 1 month ago
It depends on the minting process, Alex. If you already have a batch minted, the gas is lower. But if you have to mint on the fly, ERC‑1155 can be a bit more expensive due to the extra data handling. I’d say for one‑off transfers ERC‑721 wins, but for high volume, ERC‑1155 is stronger.
AL
Alex 1 month ago
Look, my team ran a small test last week. A single ERC‑1155 transfer actually cost ~35k gwei while ERC‑721 spent ~30k gwei. Not a huge gap, but still noticeable. If you’re scaling, the numbers add up.
ET
Ethan 1 month ago
All good points from everyone. That said, DeFi users rarely care about the tech; they want liquidity and yield. An NFT project that integrates staking or lending has to pick the standard that gives it the smoothest integration. That’s where ERC‑1155’s flexibility shines after your early onboarding.
MA
Marta 1 month ago
Yo, so for real I’m like 90% sure the future is ERC‑1155. Think about all them 0x, 1of1, multi‑token stuff. Nobody wants to write two contracts for every token they launch. Let the devs choose, we’ll just get more flex.
AU
Aurelia 3 weeks ago
I appreciate the balanced view here. ERC‑1155’s batch transfer feature is a game changer for DeFi protocols that need to move many tokens in a single transaction. The only caveat is that the community still lags in tooling, but that’ll improve over time.

Join the Discussion

Contents

Aurelia I appreciate the balanced view here. ERC‑1155’s batch transfer feature is a game changer for DeFi protocols that need to... on From Basics to Deployment: ERC-721 and E... Oct 02, 2025 |
Marta Yo, so for real I’m like 90% sure the future is ERC‑1155. Think about all them 0x, 1of1, multi‑token stuff. Nobody wants... on From Basics to Deployment: ERC-721 and E... Sep 25, 2025 |
Ethan All good points from everyone. That said, DeFi users rarely care about the tech; they want liquidity and yield. An NFT p... on From Basics to Deployment: ERC-721 and E... Sep 23, 2025 |
Alex What about the gas cost for a simple ERC‑1155 single transfer? I was under the impression that ERC‑721 was cheaper for s... on From Basics to Deployment: ERC-721 and E... Sep 20, 2025 |
Lucia The article made a quick pass over the interface compatibility issue. In reality, many DeFi protocols have hard‑coded ER... on From Basics to Deployment: ERC-721 and E... Sep 15, 2025 |
Ivan Sure thing but you forget the trade off: ERC‑1155 can be more complex to audit. Those shared IDs confuse a lot of develo... on From Basics to Deployment: ERC-721 and E... Sep 13, 2025 |
Marco Nice walkthrough, the breakdown of the differences between ERC‑721 and ERC‑1155 really helped. I especially liked the pa... on From Basics to Deployment: ERC-721 and E... Sep 12, 2025 |
Aurelia I appreciate the balanced view here. ERC‑1155’s batch transfer feature is a game changer for DeFi protocols that need to... on From Basics to Deployment: ERC-721 and E... Oct 02, 2025 |
Marta Yo, so for real I’m like 90% sure the future is ERC‑1155. Think about all them 0x, 1of1, multi‑token stuff. Nobody wants... on From Basics to Deployment: ERC-721 and E... Sep 25, 2025 |
Ethan All good points from everyone. That said, DeFi users rarely care about the tech; they want liquidity and yield. An NFT p... on From Basics to Deployment: ERC-721 and E... Sep 23, 2025 |
Alex What about the gas cost for a simple ERC‑1155 single transfer? I was under the impression that ERC‑721 was cheaper for s... on From Basics to Deployment: ERC-721 and E... Sep 20, 2025 |
Lucia The article made a quick pass over the interface compatibility issue. In reality, many DeFi protocols have hard‑coded ER... on From Basics to Deployment: ERC-721 and E... Sep 15, 2025 |
Ivan Sure thing but you forget the trade off: ERC‑1155 can be more complex to audit. Those shared IDs confuse a lot of develo... on From Basics to Deployment: ERC-721 and E... Sep 13, 2025 |
Marco Nice walkthrough, the breakdown of the differences between ERC‑721 and ERC‑1155 really helped. I especially liked the pa... on From Basics to Deployment: ERC-721 and E... Sep 12, 2025 |