top of page
  • Writer's pictureRosalia Mazza

Cryptocurrency Creation via Smart Contracts

Updated: Apr 19

In this example we're simulating the creation of a BSC token with a tot. supply of 100,000 units, a cap of 5 tokens per BNB, and a max. daily minting limit of 100 units.







Understanding the Solidity Code for Crypto Token Creation


Let's break down this Solidity code snippet, which is a perfect starting point for our exploration.



pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

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

contract ECSample is ERC20, Ownable {

uint256 public constant MAX_SUPPLY = 100000 10 * decimals();

uint256 public constant MAX_MINT_PER_BNB = 5 10 * decimals();

uint256 public constant DAILY_MINT_LIMIT = 100 10 * decimals();

uint256 public lastMintTime;

uint256 public dailyMintedAmount;

constructor() ERC20("ECSample", "ECS") {

lastMintTime = block.timestamp;

}

function mint(address to, uint256 amount) public {

require(block.timestamp >= lastMintTime + 24 hours, "Minting only allowed once every 24 hours");

require(dailyMintedAmount + amount <= DAILY_MINT_LIMIT, "Exceeds daily minting limit");

uint256 userBNBBalance = msg.sender.balance; // BNB balance in wei

uint256 maxUserMintAmount = userBNBBalance * MAX_MINT_PER_BNB / 1 ether;

require(amount <= maxUserMintAmount, "Minting amount exceeds allowance per BNB owned");

if (block.timestamp >= lastMintTime + 24 hours) {

dailyMintedAmount = 0;

lastMintTime = block.timestamp;

}

dailyMintedAmount += amount;

require(totalSupply() + amount <= MAX_SUPPLY, "Max supply exceeded");

_mint(to, amount);

}

}




  • Solidity and OpenZeppelin: This code begins with declaring the version of Solidity - a key programming language for writing smart contracts. It then imports two essential components from OpenZeppelin, a trusted library in the blockchain community: ERC20 and Ownable.


  • Contract Declaration and Variables: The contract, named ECSample, inherits properties from both ERC20 and Ownable. We then see several constant variables defining the token's characteristics, such as MAX_SUPPLY, MAX_MINT_PER_BNB, and DAILY_MINT_LIMIT. These constants set the boundaries for token creation and ensure controlled distribution.


  • Constructor and Minting Function: The constructor initializes the token with a name and symbol. The minting function mint is where the magic happens. It includes checks to enforce rules like the 24-hour minting interval, daily minting limit, and the user's minting capacity based on their BNB balance. This function ensures that the token distribution is fair and within the defined limits.


  • Safety and Supply Checks: Throughout the contract, various require statements are used for validating conditions like the time elapsed since the last mint, the daily mint amount, and the maximum supply. These checks are crucial for maintaining the token's integrity and value.


Deploying the Contract on Binance Smart Chain


Deploying a smart contract on the Binance Smart Chain (BSC) is an exciting step in bringing your cryptocurrency to life. Here's how you can do it:


1. Prepare Your Environment: First, you'll need an environment like Remix, a popular web-based IDE for Solidity. Ensure you have a wallet like MetaMask installed and configured for BSC.


2. Compile the Contract: In Remix, paste your Solidity code and compile it. This process checks for errors and prepares the contract for deployment.


3. Connect to BSC: Switch your MetaMask wallet to the Binance Smart Chain network.


4. Deploy the Contract: In Remix, go to the deploy tab, connect your wallet, and deploy the contract. This will create your token on the BSC.


Interacting with the Contract to Mint Tokens


Once your contract is live on BSC, users can interact with it to mint tokens:


1. Access the Contract: Users need the contract address and a BSC-compatible wallet. They can use interfaces like BscScan or a custom frontend connected to the contract.


2. Minting Tokens: To mint tokens, users call the mint function, specifying the amount. They must adhere to the rules set in the contract, like the daily limit and BNB balance requirements.


3. Transaction Confirmation: After initiating the minting process, users must confirm the transaction in their wallet. Once confirmed and processed by the BSC network, the tokens will be minted and transferred to their specified address.


Final Thoughts


Creating a cryptocurrency using smart contracts on the Binance Smart Chain is a journey that combines technical skills with a deep understanding of blockchain principles.


From writing and deploying your contract to enabling users to mint tokens, every step is a learning opportunity. Remember, the blockchain world is about much more than just technology; it's about building communities and economies in a decentralized way.


Post: Blog2_Post
bottom of page