Skip to content

Instantly share code, notes, and snippets.

@eca20
Created February 15, 2019 15:29
Show Gist options
  • Save eca20/8f4d21d57e3dce9baf211b9722c46b74 to your computer and use it in GitHub Desktop.
Save eca20/8f4d21d57e3dce9baf211b9722c46b74 to your computer and use it in GitHub Desktop.

Revisions

  1. eca20 created this gist Feb 15, 2019.
    322 changes: 322 additions & 0 deletions ABI.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,322 @@
    var PittCoinABI =[
    {
    "constant": true,
    "inputs": [],
    "name": "name",
    "outputs": [
    {
    "name": "",
    "type": "string"
    }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
    },
    {
    "constant": false,
    "inputs": [
    {
    "name": "_spender",
    "type": "address"
    },
    {
    "name": "_value",
    "type": "uint256"
    }
    ],
    "name": "approve",
    "outputs": [
    {
    "name": "success",
    "type": "bool"
    }
    ],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
    },
    {
    "constant": true,
    "inputs": [],
    "name": "totalSupply",
    "outputs": [
    {
    "name": "",
    "type": "uint256"
    }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
    },
    {
    "constant": false,
    "inputs": [
    {
    "name": "_from",
    "type": "address"
    },
    {
    "name": "_to",
    "type": "address"
    },
    {
    "name": "_value",
    "type": "uint256"
    }
    ],
    "name": "transferFrom",
    "outputs": [
    {
    "name": "success",
    "type": "bool"
    }
    ],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
    },
    {
    "constant": true,
    "inputs": [],
    "name": "decimals",
    "outputs": [
    {
    "name": "",
    "type": "uint8"
    }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
    },
    {
    "constant": false,
    "inputs": [
    {
    "name": "_value",
    "type": "uint256"
    }
    ],
    "name": "burn",
    "outputs": [
    {
    "name": "success",
    "type": "bool"
    }
    ],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
    },
    {
    "constant": true,
    "inputs": [
    {
    "name": "",
    "type": "address"
    }
    ],
    "name": "balanceOf",
    "outputs": [
    {
    "name": "",
    "type": "uint256"
    }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
    },
    {
    "constant": false,
    "inputs": [
    {
    "name": "_from",
    "type": "address"
    },
    {
    "name": "_value",
    "type": "uint256"
    }
    ],
    "name": "burnFrom",
    "outputs": [
    {
    "name": "success",
    "type": "bool"
    }
    ],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
    },
    {
    "constant": true,
    "inputs": [],
    "name": "symbol",
    "outputs": [
    {
    "name": "",
    "type": "string"
    }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
    },
    {
    "constant": false,
    "inputs": [
    {
    "name": "_to",
    "type": "address"
    },
    {
    "name": "_value",
    "type": "uint256"
    }
    ],
    "name": "transfer",
    "outputs": [
    {
    "name": "success",
    "type": "bool"
    }
    ],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
    },
    {
    "constant": false,
    "inputs": [
    {
    "name": "_spender",
    "type": "address"
    },
    {
    "name": "_value",
    "type": "uint256"
    },
    {
    "name": "_extraData",
    "type": "bytes"
    }
    ],
    "name": "approveAndCall",
    "outputs": [
    {
    "name": "success",
    "type": "bool"
    }
    ],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
    },
    {
    "constant": true,
    "inputs": [
    {
    "name": "",
    "type": "address"
    },
    {
    "name": "",
    "type": "address"
    }
    ],
    "name": "allowance",
    "outputs": [
    {
    "name": "",
    "type": "uint256"
    }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
    },
    {
    "inputs": [
    {
    "name": "initialSupply",
    "type": "uint256"
    },
    {
    "name": "tokenName",
    "type": "string"
    },
    {
    "name": "tokenSymbol",
    "type": "string"
    }
    ],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "constructor"
    },
    {
    "anonymous": false,
    "inputs": [
    {
    "indexed": true,
    "name": "from",
    "type": "address"
    },
    {
    "indexed": true,
    "name": "to",
    "type": "address"
    },
    {
    "indexed": false,
    "name": "value",
    "type": "uint256"
    }
    ],
    "name": "Transfer",
    "type": "event"
    },
    {
    "anonymous": false,
    "inputs": [
    {
    "indexed": true,
    "name": "_owner",
    "type": "address"
    },
    {
    "indexed": true,
    "name": "_spender",
    "type": "address"
    },
    {
    "indexed": false,
    "name": "_value",
    "type": "uint256"
    }
    ],
    "name": "Approval",
    "type": "event"
    },
    {
    "anonymous": false,
    "inputs": [
    {
    "indexed": true,
    "name": "from",
    "type": "address"
    },
    {
    "indexed": false,
    "name": "value",
    "type": "uint256"
    }
    ],
    "name": "Burn",
    "type": "event"
    }
    ]
    160 changes: 160 additions & 0 deletions PittCoin.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,160 @@
    pragma solidity >=0.4.22 <0.6.0;

    //interface tokenRecipient {
    // function receiveApproval(address _from, uint256 _value, address _token, bytes calldata _extraData) external;
    //}

    contract PittCoin {
    // Public variables of the token
    string public name;
    string public symbol;
    uint8 public decimals = 18;
    // 18 decimals is the strongly suggested default, avoid changing it
    uint256 public totalSupply;

    // This creates an array with all balances
    mapping (address => uint256) public balanceOf;
    mapping (address => mapping (address => uint256)) public allowance;

    // This generates a public event on the blockchain that will notify clients
    event Transfer(address indexed from, address indexed to, uint256 value);

    // This generates a public event on the blockchain that will notify clients
    event Approval(address indexed _owner, address indexed _spender, uint256 _value);

    // This notifies clients about the amount burnt
    event Burn(address indexed from, uint256 value);

    /**
    * Constructor function
    *
    * Initializes contract with initial supply tokens to the creator of the contract
    */
    constructor(
    uint256 initialSupply,
    string memory tokenName,
    string memory tokenSymbol
    ) public {
    totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount
    balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens
    name = tokenName; // Set the name for display purposes
    symbol = tokenSymbol; // Set the symbol for display purposes
    }

    /**
    * Internal transfer, only can be called by this contract
    */
    function _transfer(address _from, address _to, uint _value) internal {
    // Prevent transfer to 0x0 address. Use burn() instead
    require(_to != address(0x0));
    // Check if the sender has enough
    require(balanceOf[_from] >= _value);
    // Check for overflows
    require(balanceOf[_to] + _value >= balanceOf[_to]);
    // Save this for an assertion in the future
    uint previousBalances = balanceOf[_from] + balanceOf[_to];
    // Subtract from the sender
    balanceOf[_from] -= _value;
    // Add the same to the recipient
    balanceOf[_to] += _value;
    emit Transfer(_from, _to, _value);
    // Asserts are used to use static analysis to find bugs in your code. They should never fail
    assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
    }

    /**
    * Transfer tokens
    *
    * Send `_value` tokens to `_to` from your account
    *
    * @param _to The address of the recipient
    * @param _value the amount to send
    */
    function transfer(address _to, uint256 _value) public returns (bool success) {
    _transfer(msg.sender, _to, _value);
    return true;
    }

    /**
    * Transfer tokens from other address
    *
    * Send `_value` tokens to `_to` on behalf of `_from`
    *
    * @param _from The address of the sender
    * @param _to The address of the recipient
    * @param _value the amount to send
    */
    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
    require(_value <= allowance[_from][msg.sender]); // Check allowance
    allowance[_from][msg.sender] -= _value;
    _transfer(_from, _to, _value);
    return true;
    }

    /**
    * Set allowance for other address
    *
    * Allows `_spender` to spend no more than `_value` tokens on your behalf
    *
    * @param _spender The address authorized to spend
    * @param _value the max amount they can spend
    */
    function approve(address _spender, uint256 _value) public
    returns (bool success) {
    allowance[msg.sender][_spender] = _value;
    emit Approval(msg.sender, _spender, _value);
    return true;
    }

    /**
    * Set allowance for other address and notify
    *
    * Allows `_spender` to spend no more than `_value` tokens on your behalf, and then ping the contract about it
    *
    * @param _spender The address authorized to spend
    * @param _value the max amount they can spend
    * @param _extraData some extra information to send to the approved contract
    */
    function approveAndCall(address _spender, uint256 _value, bytes memory _extraData)
    public
    returns (bool success) {
    // tokenRecipient spender = tokenRecipient(_spender);
    if (approve(_spender, _value)) {
    // spender.receiveApproval(msg.sender, _value, address(this), _extraData);
    return true;
    }
    }

    /**
    * Destroy tokens
    *
    * Remove `_value` tokens from the system irreversibly
    *
    * @param _value the amount of money to burn
    */
    function burn(uint256 _value) public returns (bool success) {
    require(balanceOf[msg.sender] >= _value); // Check if the sender has enough
    balanceOf[msg.sender] -= _value; // Subtract from the sender
    totalSupply -= _value; // Updates totalSupply
    emit Burn(msg.sender, _value);
    return true;
    }

    /**
    * Destroy tokens from other account
    *
    * Remove `_value` tokens from the system irreversibly on behalf of `_from`.
    *
    * @param _from the address of the sender
    * @param _value the amount of money to burn
    */
    function burnFrom(address _from, uint256 _value) public returns (bool success) {
    require(balanceOf[_from] >= _value); // Check if the targeted balance is enough
    require(_value <= allowance[_from][msg.sender]); // Check allowance
    balanceOf[_from] -= _value; // Subtract from the targeted balance
    allowance[_from][msg.sender] -= _value; // Subtract from the sender's allowance
    totalSupply -= _value; // Update totalSupply
    emit Burn(_from, _value);
    return true;
    }
    }
    51 changes: 51 additions & 0 deletions PittCoinStackOverflow.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    pragma solidity >=0.4.22 <0.6.0;
    import "./PittCoin.sol";
    contract PittCoinStackOverflow is PittCoin {

    event NewQuestion(uint questionId, string title);

    struct User{
    string pittUsername;
    uint amount;
    // uint accountNumber;
    }

    struct Question{
    uint bounty;
    string qText;
    }

    struct Course {
    uint catalogNumber; //5 digit
    string courseTitle; // actual title
    string courseNumber; // CS 1699
    uint semester; // 4 digit semester
    address instructor; //instructor PittCoin address
    }

    mapping(uint => address) public questionToAsker;
    mapping(address=> uint) userAnsweredCount;
    mapping(uint=> address) userToWallet;
    Question[] public questions;
    Course[] public courses;
    User[] public users;

    function _addUser(string memory _uName, uint _amount) internal{
    uint id = users.push(User(_uName, _amount)) -1;
    userToWallet[id] = msg.sender;
    }

    function _generateRandomID(string memory _str) private view returns (uint){
    uint rand = uint(keccak256(abi.encodePacked(_str)));
    return rand;
    }

    function _createQuestion(uint _amount, string memory _qText) internal {
    uint randID = _generateRandomID(_qText);
    uint id = questions.push(Question(_amount, _qText)) - 1;
    questionToAsker[id] = msg.sender;
    questions.push(Question(_amount, _qText)) - 1;
    questionToAsker[id] = msg.sender;
    emit NewQuestion(randID, _qText);
    }
    }
    5 changes: 5 additions & 0 deletions QuestionContract.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    pragma solidity >=0.4.22 <0.6.0;
    contract QuestionContract {


    }
    2 changes: 2 additions & 0 deletions frontEnd.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    alert( 'Hello, world!' );

    24 changes: 24 additions & 0 deletions test_test.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    pragma solidity >=0.5.0 <0.6.0;
    import "remix_tests.sol"; // this import is automatically injected by Remix.
    import "./PittCoin.sol";

    // file name has to end with '_test.sol'
    contract test_test {

    PittCoin coinToTest;


    function beforeAll() public {
    // here should instantiate tested contract
    coinToTest = new PittCoin(10,"PittCoin", "PIT");
    }

    function CheckName() public view returns (bool) {
    // use the return value (true or false) to test the contract
    string memory name = coinToTest.name();
    return keccak256(abi.encode("PittCoin")) == keccak256(abi.encode(name));
    }



    }