Project Ethereum coding 101

I put FatAssPonies on my GitHub.
It’s growing from here.
It’s kind of overkill maybe,
but I am adding one item at a time, making a note, then committing.
I think that is what GitHub is for,
to keep track of all the changes.

I’m super-stressed right now.
Breaking things down into bits
that barely tickle my brain is helping.

Oh, also, I always try to mess with something.
Even though I may “know” how the code goes
I might hink up a variable
or change arguments in kind, place, or number
just to see what happens.
I try to remember to note that.

PonyContract

The contract so far:

pragma solidity 0.5.12;

import "./Ownable.sol";
import "./Destroyable.sol";


/* Adding event and emit*/
/* Okay... now I'm adding asserts
I discovered that I don't have to use the entire struct in the hash, obviously.
I should be careful to use unique Identifiers.*/

/* Okay... all I'm doing in this contract is creating ponies using a pony struct,
mapping the creator address to the pony struct, then pushing the address to an array
and the structs to a separate array. As these are occurring simultaneously
the indices should be the same.*/

contract FatAssPonies is Ownable, Destroyable {
    
    struct Pony {
        string name;
        string breed;
        uint height;
        uint weight;
        string color;
        bool truePony;
        address stall;
        
    }
    
    event ponyCreated(string name, string breed, string color, bool truePony);
    
    
    Pony [] public deets;
    address [] public byAddy;
    mapping (address => Pony) public ponies;
    //Need to push the address to an array separate from the mapping...
    
    uint public balance;
   
    modifier costs(uint cost) {
        require(msg.value >= cost);
        _;
        
    }
    
    function createPony(string memory name, string memory breed, uint height, uint weight, string memory color) public payable costs(1 ether){
        address pony = msg.sender;
        Pony memory newPony;
        newPony.name = name;
        newPony.breed = breed;
        newPony.height = height;
        newPony.weight = weight;
        newPony.color = color;
        if(height<=56){
            newPony.truePony = true;
        }
        else {
            newPony.truePony = false;
        }
        newPony.stall = msg.sender;
        deets.push(newPony);
        byAddy.push(pony);
        ponies[pony]= newPony;
        
    
    
    
    assert(
            keccak256(
                abi.encodePacked(
                    ponies[msg.sender].name,
                    ponies[msg.sender].breed,
                    ponies[msg.sender].height
                )
            )
            ==
            keccak256(
                abi.encodePacked(
                    newPony.name,
                    newPony.breed,
                    newPony.height
                    
                )
            )
        );
       
        
        
    emit ponyCreated(newPony.name, newPony.breed, newPony.color, newPony.truePony ); 
    /*Needs to match event in arguments*/

    }

    
}

The Ownable:

pragma solidity 0.5.12;

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

And the Destroyable:

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

contract Destroyable is Ownable {
    
    function close() public onlyOwner { //onlyOwner is custom modifier
        selfdestruct(msg.sender);  // `owner` is the owners address
    }
    
    
}
1 Like

So the things I’m pleased with myself about here,
and I know there’s nothing in here Filip didn’t do in a video,
but I didn’t necessarily follow the vid when writing this
nor follow the other contracts exactly,

Is getting the boolean to work.
If the bool goes in the list of parameters for the function it expects you to enter an argument, so it doesn’t go there, even though it goes in instantiating the struct.

I was getting in my own way in circles of misreason
with the mapping and arrays creation.
When I resorted to sitting down with a pen and paper
and actually writing what I wanted
the contract to do
it became clear to me.

There’s more data we’ll want on the ponies,
but that’s no fun to write out over and over whilst
testing and writing code.
Pro-tip… Ponies don’t have to have a color, weight, or breed
to check the important functions.

SO

I need to fix it so only one pony can be at each address,
so different pony features can be changed,
and so the ponies automatically get a registration number that starts at 100.

I’d like to set something where the pony can be a choice of three things…
not sure how that will work yet…
and maybe so they have a leader board.

Eventually they’ll have avatars stored
I think in IFPS
and the registration will have GUI.

I’m planning to keep this project going and
adding to it throughout the program.

Looks like you’re starting to figure things out. Good stuff.

1 Like