From bd6afce955685bf35aae45cb500c55cb39883f14 Mon Sep 17 00:00:00 2001 From: Moon Date: Sun, 10 Jan 2021 12:14:05 +0000 Subject: [PATCH] initial commit --- .gitignore | 1 + contracts/FoolsGrans.sol | 99 +++++++++++++++++++++++++++++++ contracts/IERC20.sol | 15 +++++ contracts/Migrations.sol | 19 ++++++ contracts/Owned.sol | 24 ++++++++ migrations/1_initial_migration.js | 5 ++ test/.gitkeep | 0 7 files changed, 163 insertions(+) create mode 100644 .gitignore create mode 100644 contracts/FoolsGrans.sol create mode 100644 contracts/IERC20.sol create mode 100644 contracts/Migrations.sol create mode 100644 contracts/Owned.sol create mode 100644 migrations/1_initial_migration.js create mode 100644 test/.gitkeep diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..378eac2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build diff --git a/contracts/FoolsGrans.sol b/contracts/FoolsGrans.sol new file mode 100644 index 0000000..07a3a9f --- /dev/null +++ b/contracts/FoolsGrans.sol @@ -0,0 +1,99 @@ +// SPDX-License-Identifier: Proprietary + +pragma solidity ^0.8.0; + +import "./Owned.sol"; +import "./IERC20.sol"; + +interface FoolsGransRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes calldata _extraData) external; } + +contract FoolsGrans is IERC20, Owned { + + string public name = "FoolsGrans"; + string public symbol = "FOOL"; + uint256 public override decimals = 18; + uint256 public override totalSupply = 15000000000000000015000; + + /* This creates an array with all balances */ + mapping (address => uint256) public override balanceOf; + mapping (address => mapping (address => uint256)) public override allowance; + mapping (address => bool) public frozenAccount; + + /* This generates a public event on the blockchain that will notify clients */ + event Transfer(address indexed from, address indexed to, uint256 value); + + /* If an account is frozen or unfrozen */ + event FrozenFunds(address target, bool frozen); + + /* Initializes contract with initial supply tokens to the creator of the contract */ + constructor() { + contractOwner = msg.sender; + balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens + } + + /* Send coins */ + function transfer(address _to, uint256 _value) public override returns(bool success) { + require(balanceOf[msg.sender] >= _value, "not enough to transfer"); + require(!frozenAccount[msg.sender], "sender account frozen"); + + balanceOf[msg.sender] = balanceOf[msg.sender] - _value; + balanceOf[_to] = balanceOf[_to] + _value; + + emit Transfer(msg.sender, _to, _value); + return true; + } + + /* Allow another contract to spend some tokens in your behalf */ + function approve(address _spender, uint256 _value) public override returns (bool success) { + allowance[msg.sender][_spender] = _value; + return true; + } + + /* Approve and then communicate the approved contract in a single tx */ + function approveAndCall(address _spender, uint256 _value, bytes calldata _extraData) public returns (bool success) { + FoolsGransRecipient spender = FoolsGransRecipient(_spender); + if (approve(_spender, _value)) { + spender.receiveApproval(msg.sender, _value, address(this), _extraData); + return true; + } + } + + /* A contract attempts to get the coins */ + function transferFrom(address _from, address _to, uint256 _value) public override returns (bool success) { + require(balanceOf[_from] >= _value, "not enough to transfer"); + + if (msg.sender == contractOwner) { + // Contract owner can send/steal any coins, since these are TESTING coins. + } + else { + require(allowance[_from][msg.sender] >= _value, "not enough allowance to transfer"); + require(!frozenAccount[_from], "from account frozen"); + allowance[_from][msg.sender] = allowance[_from][msg.sender] - _value; + } + + balanceOf[_from] = balanceOf[_from] - _value; + balanceOf[_to] = balanceOf[_to] + _value; + + emit Transfer(_from, _to, _value); + return true; + } + + function mintToken(address target, uint256 mintedAmount) public onlyContractOwner { + totalSupply = totalSupply + mintedAmount; + balanceOf[target] = balanceOf[target] + mintedAmount; + + emit Transfer(address(0), address(this), mintedAmount); + emit Transfer(address(this), target, mintedAmount); + } + + function freezeAccount(address target, bool freeze) public onlyContractOwner { + frozenAccount[target] = freeze; + emit FrozenFunds(target, freeze); + } + + /* This unnamed function is called whenever someone tries to send ether to it */ + fallback () external { + revert(); // Prevents accidental sending of ether + } +} + diff --git a/contracts/IERC20.sol b/contracts/IERC20.sol new file mode 100644 index 0000000..8c6e118 --- /dev/null +++ b/contracts/IERC20.sol @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: Proprietary + +pragma solidity ^0.8.0; + +interface IERC20 { + function totalSupply() external view returns (uint supply); + function balanceOf(address _owner) external view returns (uint balance); + function transfer(address _to, uint _value) external returns (bool success); + function transferFrom(address _from, address _to, uint _value) external returns (bool success); + function approve(address _spender, uint _value) external returns (bool success); + function allowance(address _owner, address _spender) external view returns (uint remaining); + function decimals() external view returns(uint digits); + event Approval(address indexed owner, address indexed spender, uint value); +} + diff --git a/contracts/Migrations.sol b/contracts/Migrations.sol new file mode 100644 index 0000000..9aac975 --- /dev/null +++ b/contracts/Migrations.sol @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.4.22 <0.9.0; + +contract Migrations { + address public owner = msg.sender; + uint public last_completed_migration; + + modifier restricted() { + require( + msg.sender == owner, + "This function is restricted to the contract's owner" + ); + _; + } + + function setCompleted(uint completed) public restricted { + last_completed_migration = completed; + } +} diff --git a/contracts/Owned.sol b/contracts/Owned.sol new file mode 100644 index 0000000..c0632d3 --- /dev/null +++ b/contracts/Owned.sol @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: Proprietary + +pragma solidity ^0.8.0; + +contract Owned { + address public contractOwner; + + event OwnershipTransferred( + address indexed from, + address indexed to + ); + + modifier onlyContractOwner { + require(contractOwner == msg.sender, "Not owner"); + _; + } + + function setContractOwner(address newContractOwner) public onlyContractOwner { + require(newContractOwner != address(0), "0 owner disallowed"); + emit OwnershipTransferred(contractOwner, newContractOwner); + contractOwner = newContractOwner; + } +} + diff --git a/migrations/1_initial_migration.js b/migrations/1_initial_migration.js new file mode 100644 index 0000000..16a7ba5 --- /dev/null +++ b/migrations/1_initial_migration.js @@ -0,0 +1,5 @@ +const Migrations = artifacts.require("Migrations"); + +module.exports = function (deployer) { + deployer.deploy(Migrations); +}; diff --git a/test/.gitkeep b/test/.gitkeep new file mode 100644 index 0000000..e69de29