Skip to content

Instantly share code, notes, and snippets.

@SaundaryaSaurabh
Created December 11, 2022 15:18
Show Gist options
  • Save SaundaryaSaurabh/a61290a31ee4a7a0565e68372b582939 to your computer and use it in GitHub Desktop.
Save SaundaryaSaurabh/a61290a31ee4a7a0565e68372b582939 to your computer and use it in GitHub Desktop.
BASICERC20_New
pragma solidity ^0.5.1;
// SAFE Mathematical Operations
library SafeMath {
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a); //error handling condition
return a - b; //To Avoid Overflows
}
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}
}
contract IntelliContract {
string public constant name = "CustomEnergyToken"; // solidity automatically creates a getter function for public variables
string public constant symbol = "CET"; // getter function is a function used to retrive a specific value from ledger
uint8 public constant decimals = 18; // Similar to Ether
// Setter functions : Function Which Creates or Updates A Value of a variable on Legder.
// Getter Functions : Function Which Get or Fetches A Value of a variable From Legder.
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
event Transfer(address indexed from, address indexed to, uint tokens);
modifier onlyOwner {
require (msg.sender == ownerCon);
_;
}
mapping(address => uint256) balances;
// ----------------------------------------------------------------------------
mapping(address => mapping (address => uint256)) allowed; // nested mapping
// Nested Mapping Has 2 Keys & 1 Value
uint256 totalSupply_;
address ownerCon;
using SafeMath for uint256;
constructor(uint256 total) public { // special function , only called at time of deployemnet
totalSupply_ = total ;
balances[msg.sender] = total ; // To Deposit all the newly generated tokens in owner's account
ownerCon = msg.sender;
}
function totalSupply() public view returns (uint256) {
return totalSupply_;
}
// getter function
function balanceOf(address inputAddress) public view returns (uint) {
return balances[inputAddress];
}
// getter function
function transfer(address receiver, uint numTokens) public isAddressWhitelisted(receiver) returns (bool) {
require(numTokens <= balances[msg.sender], "Not Sufficient Balance");
balances[msg.sender] = balances[msg.sender].sub(numTokens);
balances[receiver] = balances[receiver].add(numTokens);
emit Transfer(msg.sender, receiver, numTokens); // logging these values using events
return true;
}
function approve(address approved_addr, uint numTokens) public returns (bool) {
allowed[msg.sender][approved_addr] = numTokens;
emit Approval(msg.sender, approved_addr, numTokens);
return true;
}
function allowance(address owner, address token_manger) public view returns (uint) {
return allowed[owner][token_manger];
}// what allowance has been provided by token_owner to Token_manager
function transferFrom(address owner, address buyer, uint numTokens) public returns (bool) {
require(numTokens <= balances[owner]);
require(numTokens <= allowed[owner][msg.sender]);
balances[owner] = balances[owner].sub(numTokens);
allowed[owner][msg.sender] = allowed[owner][msg.sender].sub(numTokens);
balances[buyer] = balances[buyer].add(numTokens);
emit Transfer(owner, buyer, numTokens);
return true;
}
//---------------------------------------------------------------------------------------//
// Adding Assignment Functions //
//----------------------------------------------------------------------------------------/
// create a feature to selfdestruct but it should only be called by owner .
// we want to increase the total supply of tokens (only be called by owner : Modifiers)
// we want to burn some of tokens out of total supply of tokens (only be called by owner)
//--------------------------------------------------------------------------//
// we want to transfer tokens only to whitelisted addresses ,firstly you will have to create a function to whitelist addresses , and then at time of transfer we will have to ensure that its one of whitelisted address
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment