Voucher Based Ethereum System
Tags: ethereum, solidity, blockchain
Published: 2021-01-18 10:58:34



Yello guys, hope you all are doing great and out of a hollow because of a mellow kicking you to euphoria. No pun intended 🥴.

We all get inspired to initiate an amount of creativity by some sort of muse. Well, this post was inspired by a list of groovy thoughts:

- Create an Ethereum Token (Swac Coin 😜).
- Using Ethereum tokens to replace Vouchers in schools.
- Thinking further of creating a custom blockchain payment system using NFC's.


So to kick off, I have a little story to tell. I once attended a boarding school for some years during part of my secondary education. 

The school I attended made use of a voucher payment system for within school purchases. It required students to pay in money( real cash) to the bursary office during resumption. 
The cash paid is converted to vouchers for the student to utilize for daily school expenses. The voucher was a printed paper with a text written on it to denote its value. 

Well, there are some advantages that this system has:

- The school's grocery vendors receive money in voucher currency. In this way, the school accounting department tries to prevent the school vendors from keeping away some cash during stock taking.
- It also prevents students from purchasing things that are off the radar of the school's stores inventory.
- It allows students to receive vouchers on a timely basis until their initial paid cash equivalent is exhausted.

Well in reality people still smuggle in real cash but see an outline of its disadvantages below:

-  Vouchers can still be stolen like real cash
-  Counterfeit Vouchers can be made by students
-  They can easily be torn (printed vouchers)
-  High burden of security consciousness

So after delving a little bit into the Ethereum planet. I began to imagine a custom replacement to the voucher-based system in my school.

## My Thought 

Instead of having a voucher-based system, a custom Ethereum token can be used. Students would be given an NFC card during their registration. 

The NFC card would store a token address which would be unique to each registered student. The NFC card would be deemed to be a  physical wallet, a proxy for payment ( juxtaposed to an ATM card). 

During the registration process, the students would need to create a one time password that would be used to permit transactions using their NFC card. 

Since this is a custom payment system, a custom Point of Sale (POS) machine would need to be created to permit students to make payment in school shops.

If the Ethereum contract is deployed on the main-net,  parents of students can transfer money to the contract of the custom Ethereum token, with this students can easily call their parents for financial support.

Parents sending money to the main-net isn't the only way to buy more tokens for his/child. It all depends on what the school wants. 

## Scenario

Student A gets registered as a student of School B. During the registration process, Student A's parent pays in money to the bursary department of school B, the school gives student A an NFC payment card which serves as a blockchain wallet for student A. 

The student is requested to create a one time password for the NFC payment card. The password would be used in the POS machine of the school's shop to permit transactions.

The wallet can permit transactions up to the equivalent amount deposited by student A's parent. 

Student A goes to School B's shop to purchase snacks. The student is requested to place his/her card on the POS machine with a request to insert student A's password to permit the transaction. Let's say student A has 10 token coins before the transaction. If the snacks cost 2 token coins, then after the transaction, student A would have 8 token coins available for future purchase. 

In the future, after student A has consumed his/her token, student A can contact his/her parent to transfer more money to his/her account. This can be achieved by student A's parent transferring some ethers to the contract's address of the token on Ethereum's mainnet.

An alternative method of abstraction could still be used to prevent parents from dealing with creating an Ethereum account. A payment gateway company can be used. 

## Benefits of using the custom Ethereum blockchain token

- Unlike using vouchers that can be torn into pieces, using the NFC card prevents that, and even if misplaced, the student can request for a new NFC card with the same address. 
- Students can't use a students NFC card if stolen without knowing its access password.
- Counterfeit tokens can't be made since it is secured by the blockchain architecture.
- School doesn't need to worry about printing vouchers, a single card can handle all transactions
- The school's shop seller doesn't need to count the cash because a custom POS system is provided.

Adopting this system would need the school to set up some new infrastructure to support it but I believe it has more benefits to offer than its downside.

## Swac Token

To make clients and developers easily build tokens (eg USDT) on top of the Ethereum blockchain, a standard was created to help make this possible.
It is called the ERC-20 standard. 
It details the interface that an Ethereum token should adhere to. You can see it [here](https://eips.ethereum.org/EIPS/eip-20).
So using the standard I decided to create my custom token. Just to try and see 😂. The code below is the contract for my Swac token.

```js
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface ERC20 {
    function totalSupply() external view returns (uint256);

    function balanceOf(address _owner) external view returns (uint256 balance);

    function transfer(address _to, uint256 _value)
        external
        returns (bool success);

    function transferFrom(
        address _from,
        address _to,
        uint256 _value
    ) external returns (bool success);

    function approve(address _spender, uint256 _value)
        external
        returns (bool success);

    function allowance(address _owner, address _spender)
        external
        view
        returns (uint256 remaining);

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(
        address indexed owner,
        address indexed spender,
        uint256 value
    );
}

contract Swac is ERC20 {
    string public name = "SWAC COIN";
    string public symbol = "SWC";
    uint256 public decimals = 18;
    uint256 public override totalSupply;
    uint256 ethToSwac = 10;
    address payable contractOwner;
    mapping(address => uint256) balances;
    mapping(address => mapping(address => uint256)) allowed;

    constructor(uint256 initialAmount) {
        totalSupply = initialAmount;
        balances[msg.sender] = initialAmount;
        contractOwner = payable(msg.sender);
    }

    function balanceOf(address _owner) public view override returns (uint256) {
        return balances[_owner];
    }

    function transfer(address _to, uint256 _value)
        public
        override
        returns (bool)
    {
        if (_value > balances[msg.sender]) return false;
        balances[msg.sender] -= _value;
        balances[_to] += _value;
        return true;
    }

    function transferFrom(
        address _from,
        address _to,
        uint256 _value
    ) public override returns (bool) {
        if (balances[_from] >= _value && allowed[_from][msg.sender] >= _value) {
            balances[_from] -= _value;
            balances[_to] += _value;
            allowed[_from][msg.sender] -= _value;
            emit Transfer(_from, _to, _value);
            return true;
        }
        return false;
    }


    function approve(address _spender, uint256 _value)
        public
        override
        returns (bool)
    {
        allowed[msg.sender][_spender] = _value;
        emit Approval(msg.sender, _spender, _value);
        return true;
    }

    function allowance(address _owner, address _spender)
        public
        view
        override
        returns (uint256)
    {
        return allowed[_owner][_spender];
    }

    receive() external payable {
        uint256 token = msg.value * ethToSwac;
        if (balances[contractOwner] < token) {
            payable(msg.sender).transfer(msg.value);
            return;
        }
        balances[msg.sender] += token;
        balances[contractOwner] -= token;
        contractOwner.transfer(msg.value);
        emit Transfer(msg.sender, contractOwner, token);
    }
}

```

*NB: My contract doesn't handle overflow situations of uint and a little adjustment can be made to the approve function to prevent [this](https://docs.google.com/document/d/1YLPtQxZu1UAvO9cZ1O2RPXBbT0mooh4DYKjA_jp-RLM/edit) attack*
![swaccoin](https://user-images.githubusercontent.com/36219292/104900357-45fda680-597c-11eb-9b27-fd3c84737493.png)


I might consider building the NFC payment machine as a school project. I think it is cool.
If you like my thought on this please do share this post and if you have a comment to make please do 😁.
Thanks