Personal Solidity Projects

Since Filip told us to start programming on our own, after the end of the smart contract programming 101 course, I decided to work on my own little project.

I would love to hear what you guys did as a project and try to read your code. It took me 3 hours to build mine (is this fast/slow for the task I assigned to myself?), some parts were really frustrating, because we didn’t learn things that i needed in the course, but here it is:

I am a huge classical music fan and one of my favorite pianist is Evgeny Kissin. I created a smart contract about selling tickets, it is structured like this:

  • Only 100 tickets available
  • Price for one ticket is 0.36 Ether
  • Customer pays by registering with name and age
  • Refund is possible (allow owner to use a button to delete customer of the list, if for example the customer contacted, and the price gets transacted back to the customer)
  • Customers under the age of 25 get a discount for 0.14 Ether
  • There is a button for “tickets left”, so people know if it is sold out or not
  • Only the owner can get the information (like the name) of the customers listed
  • Owner can withdraw the money from the tickets
  • Contract can be destroyed (usefull for example if it only sells 95 tickets, and people cannot buy a ticket on accident if they didn’t realize the concert is already over)

I should’ve added comments to the contract, but to be honest I am too lazy for that now, sorry :joy:
Also, let me know if you find any bugs or can give any tips.

Evgeny.sol

import "./Ownable.sol";
import "./Destroyable.sol";
pragma solidity 0.5.12;

contract Evgeny is Ownable, Destroyable{
    
    struct Customer{
        address payable money;
        uint id;
        string lastName;
        string surName;
        uint age;
        bool young;
    }
    
    uint private balance;
    
    Customer [] private contacters;
    
    function createCustomer(string memory lastName, string memory surName, uint age) public payable{
        
        require(contacters.length<100);
        
        balance += msg.value;
        
        Customer memory newCustomer;
        newCustomer.lastName = lastName;
        newCustomer.surName = surName;
        newCustomer.age = age;
        
        if(age <= 25){
            newCustomer.young = true;
            require(msg.value >= 140000000000000000 wei);
        }
        else {
            newCustomer.young = false;
            require(msg.value >= 360000000000000000 wei);
        }
        newCustomer.money = msg.sender;
        
        contacters.push(newCustomer);
    
    }

  
    function getCustomer(uint index) public view onlyOwner returns(string memory lastName, string memory surName, uint age, address money){
        
        return (contacters[index].lastName, contacters[index].surName, contacters[index].age, contacters[index].money);
        
    }
    
    function withdrawAll() public onlyOwner{
        
        uint toTransfer = balance;
        balance = 0;
        msg.sender.transfer(toTransfer);
        
    }
    
    function ticketsLeft() public view returns(uint count) {
        
        count = 100 - contacters.length;
        return count;
        
    }

    function deleteCustomer (uint index) public onlyOwner {
        
        if (contacters[index].age <= 25){
            balance = balance - 140000000000000000 wei;
            contacters[index].money.transfer(140000000000000000 wei);
        }
        else {
            balance = balance - 360000000000000000 wei;
            contacters[index].money.transfer(360000000000000000 wei);
        }
        
        delete contacters[index];
        
    }
    
}

Ownable.sol

pragma solidity 0.5.12;

contract Ownable {
    
    address internal owner;
    
    modifier onlyOwner(){
      require(msg.sender == owner);
      _;
    }
    
    constructor() public {
      owner = msg.sender;
    }
    
}

Destroyable.sol

import "./Ownable.sol";
pragma solidity 0.5.12;

contract Destroyable is Ownable {
    
    
    function endContract() public onlyOwner {
      selfdestruct(msg.sender);
    }
    
}

Github link

3 Likes

Good idea to share code. Maybe, in addition you could also publish it on github. Who knows, maybe a reader could even submit a PR to improve something or fix a bug while reading the code :wink:

1 Like

Will do that, thanks :+1:

Great @illyushin your contract is simple and do the job well :+1:

Few recommendations, you are not using the id field in your Customer structure.

The young field is not necessary too. It will make you structure lighter and removing the young variable will make your createCustomer function cheaper in gas to execute.

As you are deleting a customer in your function deleteCustomer, you can just set the balance to 0. You will not have to use minus which is an operation and you will have to pay for it.

2 Likes