DEV Community

angela300
angela300

Posted on

A SIMPLE SMART CONTRACT

This contract is a basic example of a cryptocurrency token contract, and it is written in Solidity, the programming language commonly used for Ethereum smart contracts.

This contract is a simple implementation of the ERC20 token standard, which is a widely adopted standard for fungible tokens on the Ethereum blockchain. It allows for basic token transfers, approvals, and allowance mechanisms.

Deploying smart contracts involves interacting with the Ethereum blockchain and requires a solid understanding of blockchain technology and smart contract development. Always ensure proper testing and auditing before deploying any smart contract to the mainnet or any public blockchain.

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

// Simple ERC20 Token Contract
contract SimpleToken {
    string public name;
    string public symbol;
    uint8 public decimals;
    uint256 public totalSupply;
    mapping(address => uint256) public balanceOf;
    mapping(address => mapping(address => uint256)) public allowance;

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);

    constructor(
        string memory _name,
        string memory _symbol,
        uint8 _decimals,
        uint256 _initialSupply
    ) {
        name = _name;
        symbol = _symbol;
        decimals = _decimals;
        totalSupply = _initialSupply * 10**uint256(_decimals);
        balanceOf[msg.sender] = totalSupply;
    }

    function transfer(address _to, uint256 _value) public returns (bool success) {
        require(_to != address(0), "Invalid recipient address");
        require(balanceOf[msg.sender] >= _value, "Insufficient balance");

        balanceOf[msg.sender] -= _value;
        balanceOf[_to] += _value;
        emit Transfer(msg.sender, _to, _value);
        return true;
    }

    function approve(address _spender, uint256 _value) public returns (bool success) {
        allowance[msg.sender][_spender] = _value;
        emit Approval(msg.sender, _spender, _value);
        return true;
    }

    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
        require(_to != address(0), "Invalid recipient address");
        require(balanceOf[_from] >= _value, "Insufficient balance");
        require(allowance[_from][msg.sender] >= _value, "Allowance exceeded");

        balanceOf[_from] -= _value;
        balanceOf[_to] += _value;
        allowance[_from][msg.sender] -= _value;
        emit Transfer(_from, _to, _value);
        return true;
    }
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)