Skip to content

Tutorial Part 2 Issue #1

@NatPDeveloper

Description

@NatPDeveloper

Firstly, thank you for creating these tutorials. They are very well written and organized.

I have created a stack exchange post of the issue:

https://eosio.stackexchange.com/questions/1284/question-about-last-step-in-the-ultimate-end-to-end-eos-dapp-tutorial-part-2

Or I've included it here as well for your convenience. I didn't know where else to put this since infinite's site appears to be down.

EOSIO v1.07
IDE: clion
Hosting environment: PC w/ shared folder to vbox running Ubuntu 16

I am on the last step:

# Buy product from Marketplace
cleos push action market buy '["wade",1]' -p wade

Instead of getting the expected response, I get the following from cleos:

capture

And this from nodeos:

wade

To remedy this, I tried with -p wade, -p wade@active, -p wade@owner, and yet I still receive the same message. I tried the getplayer command using wade's permission to ensure that my wallet was passing keys appropriately.

After this, I deleted the chain and re added all of the users, their abilities, and the currency. When I came to this last command, I received the same response. Maybe I missed a step with my includes?

The asset and action pieces in the buy function may be throwing it off somehow. In the tutorial the account anorak has 2 contracts pushed to it. It appears that when the other is pushed, the other no longer responds.

Isolated Function:

void Marketplace::buy(account_name buyer, uint64_t productId) {
        productIndex products(_self, _self);

        auto iterator = products.find(productId);
        eosio_assert(iterator != products.end(), "The product not found");

        auto product = products.get(productId);
        eosio_assert(product.quantity > 0, "The product is out of stock");

        /*Create asset
        asset({amount}, string_to_symbol({precision}, {symbol}));*/
        asset productPrice = asset(product.price, string_to_symbol(4, "OAS"));

        /* Do inline trasfer
        action({permission_level}, {contract_deployer}, {contract_action}, {data_to_pass}).send();*/
        action(vector<permission_level>(), N(anorak), N(transfer), make_tuple(buyer, _self, productPrice, string(""))).send();

        /* Execute action from another contract
        action({permission_level}, {contract_deployer}, {contract_action}, {data_to_pass}).send();*/
        action(vector<permission_level>(), N(anorak), N(additem), make_tuple(buyer,
                                                                             product.product_id,
                                                                             product.name,
                                                                             product.power,
                                                                             product.health,
                                                                             product.ability,
                                                                             product.level_up
        )).send();

        update(buyer, product.product_id, -1);
    }

Marketplace.hpp:

#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
#include <eosiolib/asset.hpp>
#include <string>

namespace Oasis {
    using namespace eosio;
    using std::string;

    class Marketplace : public contract {
        using contract::contract;

    public:

        void buy(account_name buyer, uint64_t productId);

        //@abi action
        void getbyid(uint64_t productId);

        /**
         * Marketplace processes
        */

        //@abi table product i64
        struct product {
            uint64_t product_id;
            string name;
            uint64_t power;
            uint64_t health;
            string ability;
            uint64_t level_up;
            uint64_t quantity;
            uint64_t price;

            uint64_t primary_key() const { return product_id; }

            EOSLIB_SERIALIZE(product,
                    (product_id)
                    (name)
                    (power)(health)
                    (ability)
                    (level_up)
                    (quantity)
                    (price)
        )};

        typedef multi_index<N(product), product> productIndex;

        //@abi action
        void add(account_name account, product newProduct);

        //@abi action
        void update(account_name account, uint64_t product_id, uint64_t quantity);

        //@abi action
        void remove(account_name account, uint64_t productId);
    };

    EOSIO_ABI(Marketplace, (buy)(getbyid)(add)(update)(remove));
}

Marketplace.cpp:

#include "Marketplace.hpp"

namespace Oasis{
    void Marketplace::buy(account_name buyer, uint64_t productId) {
        productIndex products(_self, _self);

        auto iterator = products.find(productId);
        eosio_assert(iterator != products.end(), "The product not found");

        auto product = products.get(productId);
        eosio_assert(product.quantity > 0, "The product is out of stock");

        /*Create asset
        asset({amount}, string_to_symbol({precision}, {symbol}));*/
        asset productPrice = asset(product.price, string_to_symbol(4, "OAS"));

        /* Do inline trasfer
        action({permission_level}, {contract_deployer}, {contract_action}, {data_to_pass}).send();*/
        action(vector<permission_level>(), N(anorak), N(transfer), make_tuple(buyer, _self, productPrice, string(""))).send();

        /* Execute action from another contract
        action({permission_level}, {contract_deployer}, {contract_action}, {data_to_pass}).send();*/
        action(vector<permission_level>(), N(anorak), N(additem), make_tuple(buyer,
                                                                             product.product_id,
                                                                             product.name,
                                                                             product.power,
                                                                             product.health,
                                                                             product.ability,
                                                                             product.level_up
        )).send();

        update(buyer, product.product_id, -1);
    }

    void Marketplace::getbyid(uint64_t productId) {
        productIndex products(_self, _self);

        auto iterator = products.find(productId);
        eosio_assert(iterator != products.end(), "Product not found");

        auto product = products.get(productId);
        print("Id: ", product.product_id);
        print(" | Name: ", product.name.c_str());
        print(" | Power: ", product.power);
        print(" | Health: ", product.health);
        print(" | Ability: ", product.ability.c_str());
        print(" | Level up: ", product.level_up);
        print(" | Quantity: ", product.quantity);
        print(" | Price: ", product.price);
    }

    void Marketplace::add(account_name account, product newProduct) {
        require_auth(account);

        productIndex products(_self, _self);

        auto iterator = products.find(newProduct.product_id);
        eosio_assert(iterator == products.end(), "Product for this ID already exists");

        products.emplace(account, [&](auto& product) {
            product.product_id = newProduct.product_id;
            product.name = newProduct.name;
            product.power = newProduct.power;
            product.health = newProduct.health;
            product.ability = newProduct.ability;
            product.level_up = newProduct.level_up;
            product.quantity = newProduct.quantity;
            product.price = newProduct.price;
        });
    }

    void Marketplace::update(account_name account, uint64_t product_id, uint64_t quantity) {
        require_auth(account);

        productIndex products(_self, _self);

        auto iterator = products.find(product_id);
        eosio_assert(iterator != products.end(), "Product not found");

        products.modify(iterator, account, [&](auto& product) {
            product.quantity += quantity;
        });
    }

    void Marketplace::remove(account_name account, uint64_t productId) {
        require_auth(account);

        productIndex products(_self, _self);

        auto iterator = products.find(productId);
        eosio_assert(iterator != products.end(), "Product not found");

        products.erase(iterator);
    }
}

I'll also include the players.hpp/cpp

Players.hpp:

#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
#include <string>

namespace Oasis {
    using namespace eosio;
    using std::string;

    class Players : public contract {
        using contract::contract;

    public:

        Players(account_name self):contract(self) {}

        //@abi table item i64
        struct item {
            uint64_t item_id;
            string name;
            uint64_t power;
            uint64_t health;
            string ability;
            uint64_t level_up;

            uint64_t primary_key() const { return item_id; }

            EOSLIB_SERIALIZE(item, (item_id)(name)(power)(health)(ability)(level_up))
        };

        //@abi action
        void add(account_name account, string& username);

        //@abi action
        void update(account_name account, uint64_t level, int64_t healthPoints, int64_t energyPoints);

        //@abi action
        void getplayer(const account_name account);

        //@abi action
        void addability(const account_name account, string& ability);

        //@abi action
        void additem(const account_name account, item purchased_item);

        //@abi table player i64
        struct player {
            uint64_t account_name;
            string username;
            uint64_t level;
            int64_t health_points = 1000;
            int64_t energy_points = 1000;
            vector<string> abilities; // This is our new property
            vector<item> inventory;

            uint64_t primary_key() const { return account_name; }

            EOSLIB_SERIALIZE(player,
                    (account_name)
                    (username)
                    (level)
                    (health_points)
                    (energy_points)
                    (abilities)
                    (inventory)
        )};

        typedef multi_index<N(player), player> playerIndex;
    };

    EOSIO_ABI(Players, (add)(update)(getplayer)(addability)(additem));
}

Players.cpp:

#include "Players.hpp"

namespace Oasis{

    void Players::add(account_name account, string& username){

        require_auth(account);

        playerIndex players(_self, _self);

        auto iterator = players.find(account);
        eosio_assert(iterator == players.end(), "Address for account already exists");

        players.emplace(account, [&](auto& player) {
            player.account_name = account;
            player.username = username;
            player.level = 1;
            player.health_points = 1000;
            player.energy_points = 1000;
        });

    }

    void Players::update(account_name account,
                         uint64_t level,
                         int64_t healthPoints,
                         int64_t energyPoints){

        require_auth(account);

        playerIndex players (_self, _self);

        auto iterator = players.find(account);
        eosio_assert(iterator != players.end(), "Address for account not found");

        players.modify(iterator, account, [&](auto& player) {
            player.level = level;
            if ((player.health_points - healthPoints) < 0) {
                player.health_points = 0;
            } else {
                player.health_points -= healthPoints;
            }

            if ((player.energy_points - energyPoints) < 0){
                player.energy_points = 0;
            } else {
                player.energy_points -= energyPoints;
            }
        });
    }

    void Players::getplayer(const account_name account){
        require_auth(account);

        playerIndex players (_self, _self);

        auto iterator = players.find(account);
        eosio_assert(iterator != players.end(), "Address for account not found");

        auto currentPlayer = players.get(account);

        print("Username: ", currentPlayer.username.c_str());
        print(" Level: ", currentPlayer.level);
        print(" Health: ", currentPlayer.health_points);
        print(" Energy: ", currentPlayer.energy_points);

        if (currentPlayer.abilities.size() > 0) {
            print(" Abilities: ");

            for (uint32_t i = 0; i < currentPlayer.abilities.size(); i++) {
                print(currentPlayer.abilities.at(i).c_str(), " ");
            }
        } else {
            print(" No Abilities");
        }
        if (currentPlayer.inventory.size() > 0) {
            print(" Items: ");

            for (uint32_t i = 0; i < currentPlayer.inventory.size(); i++) {
                item currentItem = currentPlayer.inventory.at(i);
                print(currentItem.name.c_str(), " == ");
            }
        } else {
            print(" Empty inventory");
        }
    }

    void Players::addability(const account_name account, string& ability) {
        require_auth(account);

        playerIndex players(_self, _self);

        auto iterator = players.find(account);
        eosio_assert(iterator != players.end(), "Address for account not found");

        players.modify(iterator, account, [&](auto& player) {
            player.abilities.push_back(ability);
        });
    }

    void Players::additem(const account_name account, item purchased_item) {
        playerIndex players(_self, _self);

        auto iterator = players.find(account);
        eosio_assert(iterator != players.end(), "Address for account not found");

        players.modify(iterator, account, [&](auto& player) {
            player.energy_points += purchased_item.power;
            player.health_points += purchased_item.health;
            player.level += purchased_item.level_up;
            player.abilities.push_back(purchased_item.ability);
            player.inventory.push_back(item{
                    purchased_item.item_id,
                    purchased_item.name,
                    purchased_item.power,
                    purchased_item.health,
                    purchased_item.ability,
                    purchased_item.level_up
            });
        });

        print("Item Id: ", purchased_item.item_id);
        print(" | Name: ", purchased_item.name.c_str());
        print(" | Power: ", purchased_item.power);
        print(" | Health: ", purchased_item.health);
        print(" | Ability: ", purchased_item.ability.c_str());
        print(" | Level up: ", purchased_item.level_up);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions