Skip to content

Instantly share code, notes, and snippets.

@xoriole
Last active April 30, 2018 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xoriole/4a99d6389e6b47aab7564cf9756ad911 to your computer and use it in GitHub Desktop.
Save xoriole/4a99d6389e6b47aab7564cf9756ad911 to your computer and use it in GitHub Desktop.
Token example 2 - Alumni Credits
pragma solidity ^0.4.12;
contract AlumniCredits {
string public name = "EIT Digital Alumni Credits";
string public symbol = "EITDAC";
uint8 public decimals = 2;
uint256 public totalSupply;
// Foundation address
address public foundation;
// This creates an array with all balances
mapping (address => uint256) public balanceOf;
// All registered/valid alumni addresses approved by the foundation.
mapping (address => bool) registeredAlumni;
// This generates a public event on the blockchain that will notify clients
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* Constructor function.
* Initializes Alumni contract with some initial supply tokens.
*/
function AlumniCredits( uint256 initialSupply) public {
foundation = msg.sender; // contract creator is the foundation
totalSupply = initialSupply * 10 ** uint256(decimals);
balanceOf[foundation] = totalSupply;
registeredAlumni[foundation] = true;
}
/**
* Ensures the function can only be excuted by the foundation.
*/
modifier onlyFoundation(){
require(msg.sender == foundation);
_;
}
/**
* Ensures that the function can only be executed by registered alumni.
*/
modifier onlyAlumni(){
require(registeredAlumni[msg.sender] == true);
_;
}
/**
* Transfer credits
*/
function transfer(address _alumni, uint _value) public {
// Prevent transfer to 0x0 address.
require(_alumni != 0x0);
// Receiver should also be an alumni
require(registeredAlumni[_alumni] == true);
// Check if the sender has enough funds
require(balanceOf[msg.sender] >= _value);
// Check for overflows
require(balanceOf[_alumni] + _value >= balanceOf[_alumni]);
// Save this for an assertion in the future
uint previousBalances = balanceOf[msg.sender] + balanceOf[_alumni];
// Subtract from the sender
balanceOf[msg.sender] -= _value;
// Add the same to the alumni
balanceOf[_alumni] += _value;
emit Transfer(msg.sender, _alumni, _value);
// Make sure math is correct. Assertions should never fail
assert(balanceOf[msg.sender] + balanceOf[_alumni] == previousBalances);
}
/**
* Register an alumni address
* Can be done by foundation only.
*/
function registerAlumni(address _address) public onlyFoundation {
registeredAlumni[_address] = true;
}
/**
* Check if an address belongs to a registered alumni.
*/
function isRegistered(address _address) public view returns (bool registered){
return registeredAlumni[_address] == true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment