1

I’m working on a card game. Right now it is really simple. There are orcs and cards. Both should be implemented as ERC 721.

What would be the most natural way to make the orc an ERC-721 token?

Actually the users should be allowed to do with it whatever they want. But only I or some other contract should be able to create new orcs.

Here is the code that I already have.

contract OrcBase is InterfaceOrcs {
    struct Orc {
        uint health;
        uint strenth;
    }

    Orc[] orcs;

    function createOrc(uint _health, uint _strenth) public returns(uint) {
        // TODO security checks and mapping similiar to orcBase & orcOwnership
        Orc memory _orc = Orc({
            health: _health,
            strenth: _strenth
        });
        uint256 newOrcId = orcs.push(_orc) - 1;

        return newOrcId;
    }

    /// @notice Returns the total number of Cards currently in existence.
    /// @dev Required for ERC-721 compliance.
    function totalSupply() public view returns (uint) {
        uint length = orcs.length;
        return length;
    }

    function getOrcHealth(uint id) public view returns (uint) {
        uint health = orcs[id].health;
        return health;
    }

    function getOrcStrenth(uint id) public view returns (uint) {
        uint strenth = orcs[id].strenth;
        return strenth;
    }
}
2

Yes, you are on the right track. Below is a fully usable contract which implements the feature you are requesting.

Fortunately, the reference implementation of ERC-721 from 0xcert happens to include a little extra code to implement Ownable (which is similar to the OpenZeppelin implementation).

I have made the subtle assumption that you will implement more than just orcs in this card game. If not, then you could rename GameCards to OrcCards to be more semantic.

pragma solidity 0.5.1;
import "https://github.com/0xcert/ethereum-erc721/src/contracts/tokens/nf-token.sol";
import "https://github.com/0xcert/ethereum-erc721/src/contracts/ownership/ownable.sol";

contract GameCards is NFToken, Ownable {

    struct Orc {
        uint health;
        uint strenth;
    }

    Orc[] orcs;

    function createOrc(uint health, uint strenth) 
        external
        onlyOwner
        returns(uint256 newOrcId)
    {
        newOrcId = orcs.length;

        orcs.push(Orc({
            health: health,
            strenth: strenth
        }));

        super._mint(msg.sender, newOrcId);
    }

    function getOrcHealth(uint id) external view returns (uint health) {
        health = orcs[id].health;
    }

    function getOrcStrenth(uint id) external view returns (uint strength) {
        strength = orcs[id].strenth;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.