Learn how Stablecoins work and Create Your own Version Similar to USDC

BuildBearBuildBear
5 min read

In this tutorial, we’ll delve into the workings of stablecoins, focusing on the example of USDC, and guide you in creating a simplified version with similar features.

Understanding Stablecoins

Stablecoins are cryptocurrencies typically pegged to real-world assets like the U.S. dollar or gold. Their primary purpose is to provide a cryptocurrency with a value that remains stable over time.

Introduction to USDC

USDC, short for USD Coin, is a stablecoin designed to maintain a value pegged to the U.S. dollar. Each USDC aims to hold a value close to $1.

Mechanism Behind USDC’s Peg

USDC relies on Circle and Centre’s open-source fiat stablecoin infrastructure. It operates on a 1:1 ratio, meaning each USDC in circulation is entirely backed by reserves and redeemable for USD. This collateralization method ensures that for every $1 worth of USDC, there is an equivalent $1 held in a bank account. This stability mechanism is crucial for its functioning and can be observed on various crypto trading platforms.

In this tutorial, we’ll focus on the smart contract aspect of StableCoin operations, providing insights into its functioning. We’ve already set up a repository containing all the necessary requirements for a quick start.

Before you begin, install the following tools:

Clone this repo & install dependencies:

git clone <https://github.com/BuildBearLabs/StableCoin.git>
cd StableCoin
yarn install

Create your private Sandbox:

yarn fork-bb

After running the command, choose the chain you want to fork. Enter the block number from which you wish to fork the chain. Wait for 10 seconds, and your sandbox will be live. The sandbox details like RPC, Faucet, and Explorer URL are stored in packages/buildbear/sandbox.json.

Deploy the contracts:

Before proceeding with the deployment, ensure to make a minor adjustment in packages/hardhat/deploy/00_deploy_your_stableCoin.ts. The final constructor argument passed to our stableCoin contract is the owner's address, so make sure to use your wallet address as the owner.

yarn deploy

This command deploys and verifies smart contracts to the BuildBear Sandbox, located in packages/hardhat/contracts. Customize the deploy script in packages/hardhat/deploy to suit your needs.

Start your NextJS app:

yarn start

Check out your app at: http://localhost:3000

Use the BuildBear faucet to mint Native and ERC20 Tokens. As shown below mint an unlimited number of Native and ERC20 Tokens directly from Scaffold-eth x BuildBear UI.

Now, let's interact with our StableCoin and explore its different features.

Navigate to the "Debug Contract" page to interact with the deployed contracts.

Currently, we are owners of the StableCoin but not the minter. Let’s make ourselves the minter using the setMinter function, as shown below.

Next, let’s mint tokens using the mint function. Enter the receiver's address and the value, then confirm the transaction on the explorer.

Use the balance function to check your balance.

Now, transfer the tokens to a second address using the transfer function.

Let’s blacklist an address to see how it works.

Now, switch to the blacklisted address and try to transfer tokens. Because the address is blacklisted, you won’t be able to transfer tokens, and it is locked for everyone.

Let’s pause the contract.

Now that the contract is paused, no users can perform transactions.

Now, let's learn about the smart contract.

Contract Features:

Stablecoin Properties:

  • name, symbol, decimals: These variables store the name, symbol, and decimal places of the stablecoin, providing basic information about the token.

  • totalSupply: Tracks the total supply of the stablecoin.

Ownership and Access Control:

  • owner: Stores the address of the owner of the smart contract.

  • paused: Indicates whether certain functions are currently paused.

  • minters: Maps addresses to their minting privileges.

  • blacklist: Maps addresses to their blacklisted status.

Modifiers:

  • notBlacklisted: Ensures that the caller's address is not blacklisted.

  • onlyOwner: Restricts access to functions to only the owner of the contract.

  • onlyMinter: Restricts access to minting functions to only addresses with minting privileges.

  • notPaused: Ensures that certain functions are not executed when the contract is paused.

Functions:

  • changeOwner: Allows the owner to change the contract owner.

  • transfer, approve, transferFrom: Implement standard ERC-20 token transfer functions with additional checks.

  • mint: Allows minting new tokens, restricted to addresses with minting privileges.

  • setPaused: Allows the owner to pause/unpause certain functions.

  • setMinter: Allows the owner to grant/revoke minting privileges.

  • setBlacklist: Allows the owner to blacklist/unblacklist addresses.

Explanation:

This smart contract provides a basic foundation for a stablecoin with features like transfer, approve, mint, and access control. The owner can control various aspects, such as pausing functions, managing minting privileges, and blacklisting addresses. The modifiers ensure that certain conditions are met before allowing the execution of functions. It's a flexible and extensible contract that can be further customized based on specific requirements.

🎉 Congratulations!

We have successfully crafted a fundamental StableCoin, granting the owner the power to oversee the minter, blacklist addresses, and halt token transfers when necessary. 🚀

Conclusion: With Scaffold-ETH-2 x BuildBear, you can streamline Dapp development and testing, reduce development time, and fast-track to production. Leave behind the limitations of public and local Hardhat Testnets.

  • Effortlessly fork EVM and EVM-compatible chains directly from the CLI, creating your own Sandbox. Test your application as if you’re on the Mainnet without real-world consequences, saving you time from searching for working RPCs.

  • Mint unlimited native and ERC20 tokens directly from the CLI or your personal Sandbox faucet. No more wasting time searching for working faucets.

  • Effortlessly debug transactions within your sandbox using the built-in explorer and transaction tracer.

  • Experience rapid transactions on BuildBear, completing in under 3 seconds.

About BuildBear:

BuildBear is a platform tailored for DApp development and testing. Developers gain the freedom to construct a personalized Private Testnet sandbox across various blockchain networks. The liberty to mint unlimited Native and ERC20 tokens and rapid transaction times on BuildBear (under 3 seconds!) enhances the DApp development lifecycle manifold. The platform comes equipped with tools designed for real-time testing and debugging, ensuring developers can keep tabs on intricate blockchain transactions with unparalleled ease.

Connect with us on Twitter | LinkedIn | Telegram | GitHub

Author: chandan

0
Subscribe to my newsletter

Read articles from BuildBear directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

BuildBear
BuildBear

BuildBear is a platform for testing dApps at scale, for teams. It provides users with their own private Testnet to test their smart contracts and dApps, which can be forked from any EVM chain. It also provides a Faucet, Explorer, and RPC for testing purposes.