Aj007 SmartContract (Aj007 coin)

Ethereum based Aj007 Smart Contract


pragma solidity 0.4.24;

contract admined{
	address public admin;

	constructor() public {
		admin = msg.sender;
	}
	
	modifier onlyAdmin() { 
		if(msg.sender != admin) revert();
		_;
	}

	function transferAdminship (address newAdmin) onlyAdmin public{
		admin = newAdmin;
	}
}

contract Aj007Base{

	mapping (address => uint256) public balanceOf;
	// allowing others to execute transactions on your behalf
	mapping (address => mapping ( address => uint256)) public allowance;
	
	// balanceOf[address] = 5;
	string public standard = "Aj007 v1.0";
	string public name;
	string public symbol;
	uint8 public decimal; // 1.23 Number of decimal
	uint256 public totalSupply;
	//indexed address passsed will be stored in log memory and not contract data
	event Transfer(address indexed from, address indexed to, uint256 _value);

	constructor (uint256 initialSupply, string tokenName, string tokenSymbol, uint8 decimalUnits) public{
		balanceOf[msg.sender] = initialSupply;
		totalSupply = initialSupply;
		decimal = decimalUnits;
		name = tokenName;
		symbol = tokenSymbol;

	} 

	function transfer (address _to,uint256 _value) public{
		if(balanceOf[msg.sender] < _value) revert();
		//0-255 8 bit
		// 250,10 = 4 Buffer overflow
		if(balanceOf[_to] + _value < balanceOf [_to]) revert();
		// Any number of conditions can be added here
		balanceOf[msg.sender] -= _value;
		balanceOf[_to] += _value;
		// Can be used for debugging values of smart contract and also act as an event for other clients
		// Technically a logging method
		emit Transfer(msg.sender,_to,_value);
	}

	function approve (address _spender, uint256 _value) public returns(bool res){
		allowance[msg.sender][_spender] = _value;
		return true;
	}
	
	function transferFrom (address _from, address _to, uint256 _value) public returns(bool res){
		if(balanceOf[_from] < _value) revert();
		if(balanceOf[_to] + _value < balanceOf[_to]) revert();
		if(_value > allowance[_from][msg.sender]) revert();

		balanceOf[_from] -= _value;
		balanceOf[_to] += _value;

		allowance[_from][msg.sender] -= _value;

		emit Transfer(_from,_to, _value);
		return true;
	}
	
}

contract Aj007 is admined, Aj007Base{

	uint256 public minimumBalanceForAccounts = 5 finney;
	uint256 public sellPrice;
	uint256 public buyPrice;
	mapping (address => bool) public frozenAccount;

	event FrozenFund(address target, bool frozenStatus);
	

	constructor (uint256 initialSupply, string tokenName, string tokenSymbol, uint8 decimalUnits, address centralAdmin) Aj007Base(0,tokenName,tokenSymbol,decimalUnits) public{
		totalSupply = initialSupply;
		if(centralAdmin != 0)
			admin = centralAdmin;
		else
			admin = msg.sender;
		balanceOf[admin]=initialSupply;
		totalSupply = initialSupply;	
	}

	function mintToken (address target, uint256 mintedAmount) onlyAdmin public returns(bool res) {
		balanceOf[target]+= mintedAmount;
		totalSupply += mintedAmount;
		// Notify the total amount is increased
		emit Transfer(0, this, mintedAmount);
		// Notify that the token is added to the given account
		emit Transfer(this, target, mintedAmount);
		return true;
	}

	function freezeAccount (address target, bool freeze) onlyAdmin public{
		frozenAccount[target] = freeze;
		emit FrozenFund(target, freeze);
	}
	
	function transfer (address _to,uint256 _value) public{
		if(msg.sender.balance < minimumBalanceForAccounts){
			//Ensures minimum balance is maintained in account. 
			//If ether amount goes to 0 the transaction cannot be processed further.
			sell((minimumBalanceForAccounts - msg.sender.balance)/sellPrice);
		}
		if(frozenAccount[msg.sender]) revert();
		if(balanceOf[msg.sender] < _value) revert();
		//0-255 8 bit
		// 250,10 = 4 Buffer overflow
		if(balanceOf[_to] + _value < balanceOf [_to]) revert();
		// Any number of conditions can be added here
		balanceOf[msg.sender] -= _value;
		balanceOf[_to] += _value;
		// Can be used for debugging values of smart contract and also act as an event for other clients
		// Technically a logging method
		emit Transfer(msg.sender,_to,_value);
	}

	// Ensure admin frozen accounts wont be able to transfer any coins anywhere
	function transferFrom (address _from, address _to, uint256 _value) public returns(bool res){
		if(frozenAccount[_from]) revert();
		if(balanceOf[_from] < _value) revert();
		if(balanceOf[_to] + _value < balanceOf[_to]) revert();
		if(_value > allowance[_from][msg.sender]) revert();

		balanceOf[_from] -= _value;
		balanceOf[_to] += _value;

		allowance[_from][msg.sender] -= _value;

		emit Transfer(_from,_to, _value);
		return true;
	}

	function setPrices (uint256 newSellPrice, uint256 newBuyPrice) onlyAdmin public returns(bool res){
		sellPrice = newSellPrice;
		buyPrice = newBuyPrice;
		return true;
	}
	
	//Sent Ether's contract gives the buyer tokens (For messageSender who wants to get the token)
	function buy () public payable{
		// convert to wei
		uint256 amount = (msg.value/(1 ether)) / buyPrice;
		// check if smart contract itself has those any coins to transfer
		if(balanceOf[this] < amount) revert();
		balanceOf[msg.sender] += amount;
		balanceOf[this] -= amount;
		emit Transfer(this, msg.sender, amount);
	}
	
	function sell (uint256 amount) public{
		
		// Check if the sender himself has those many tokens
		if (balanceOf[msg.sender] < amount) revert();
		balanceOf[this] += amount;
		// change coin balance of sender
		balanceOf[msg.sender] -= amount;
		if(!msg.sender.send(amount * sellPrice * 1 ether)){
			// revert() will revert all the changes which have been done above
			revert();
		}
		else{
			// Aj007 coin function to receive the coins back to the contract
			emit Transfer(msg.sender, this, amount);			
		}
	}
	
	// Function to have block rewards for mining
	function giveBlockreward () private {
		balanceOf[block.coinbase] += 1;
	}

	// Add the currentChallenge and set difficulty for the blockChain
	bytes32 public currentChallenge;
	uint public timeOfLastProof;
	uint public difficulty = 10**32;

	function proofOfWork (uint nonce) public{
		bytes32 n = keccak256(abi.encodePacked(nonce, currentChallenge));

		// revert() Exception as difficulty alsways has to be greater than the previous 
		if(n < bytes8(difficulty)) revert();

		uint timeSinceLastBlock = (now - timeOfLastProof);
		if(timeSinceLastBlock < 5 seconds) revert();

		// Reward given for every 1 minute here
		balanceOf[msg.sender] += timeSinceLastBlock / 60 seconds;

		//Ensures average mining time is 10 minutes
		difficulty = difficulty * 10 minutes / timeOfLastProof * 1;
		timeOfLastProof = now;

		currentChallenge = keccak256(abi.encodePacked(nonce, currentChallenge,blockhash(block.number-1)));
	}	
}

Leave a Reply