Tables Programming - Discussion

I tried that…

1 Like

Are you sure you are deploying the contract after build?

When you click the “build” button which is a hammer, next to this one is the “deploy” button (like a boat with boxes).

Carlos Z

Okay so I did get a table but now im getting an error when I try to add things to the table.

1 Like

The OWNER should be an account that you have already created in you “Accounts” tab on EOS Studio, in the right top corner you are using the helloworldaccount, you should select the account in which you want to create a new pet.

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

Are you talking about the EOS account, I tried using both accounts and still get the same error. It says “Unknown action addpet in contract helloworld” for both accounts.

1 Like

Is there something I am doing wrong for this?

1 Like

There could be 2 issues here, or you have a tiny issue in your code, or your EOS Studio procedure is not correct, please do the following on EOS Studio always when deploying a contract:

After you finished coding the contract:

  • Click the hammer button which is used to “build” or compile the contract.
  • IF compile successful, click on the “Deploy” which is next to the “build”
  • Then in the contract tab you select the contract and in the accounts tab the account user that you deployed the contract.

Also please check the image of my i sent you days ago, the one I deployed your contract and show the table. Some times the EOS studio goes buggy and restarting the local network does the trick (just switch to off and on again).

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

I restarted EOS and I turned my local node on and off. When I’m building it, it’s successful but now when I try to deploy it, it says “Deployed failed
Unknown action set code in contract eosio”

What could be the issue in the code. @filip

1 Like

That case there is an error with the code, please share the code that you have compile but does not deploy correctly.

You can use the “Preformatted Text” Button to encapsulate any kind of code you want to show.


function formatText(){

let words = “I’m a preformatted Text box, Please use me wisely!”

}

prefromatted_text-animated

Carlos Z.

helloworld.hpp

#ifndef HELLOWORLD
#define HELLOWORLD

#include <eosio/eosio.hpp>

#include <pets.hpp>

CONTRACT helloworld : public eosio::contract
{
  public:
    using eosio::contract::contract;
    ACTION hi(eosio::name const & nm);
    ACTION addpet(uint64_t const id, eosio::name const & owner, eosio::name const & pets_name, uint64_t const age, eosio::name const & type);
    ACTION modifypet(uint64_t const id, eosio::name const & owner, eosio::name const & pets_name, uint64_t const age, eosio::name const & type);
    ACTION removepet(uint64_t const id);
};

#endif


helloworld.cpp:

#include <helloworld.hpp>

void helloworld::hi(eosio::name const & nm)
{
  eosio::print("Hello ", nm);
}

void helloworld::addpet(uint64_t const id, eosio::name const & owner, eosio::name const & pets_name, uint64_t const age, eosio::name const & type)
{
  pets_table pets(get_self(), get_self().value);  
  pets.emplace(get_self(), [&](auto & entry){
    entry = pet_t(id, owner, pets_name, age, type);
  }); 
}

void helloworld::modifypet(uint64_t const id, eosio::name const & owner, eosio::name const & pets_name, uint64_t const age, eosio::name const & type)
{
  pets_table pets(get_self(), get_self().value);
  auto pet_itr = pets.find(id); 
  pets.modify(pet_itr, get_self(), [&](auto & entry){
    entry = pet_t(id, owner, pets_name, age, type);
  }); 
}

void helloworld::removepet(uint64_t const id)
{
  pets_table pets(get_self(), get_self().value);
  auto pet_itr = pets.find(id);
  pets.erase(pet_itr);
}



pets.hpp:

#ifndef PETS
#define PETS

class [[eosio::table, eosio::contract("helloworld")]] pet_t 
{
    private:
        uint64_t id;
        eosio::name owner;
        eosio::name pets_name;
        uint64_t age;
        eosio::name type;
    public: 
        pet_t(){}

        pet_t(uint64_t const _id,
            eosio::name const & _owner,
            eosio::name const & _pets_name,
            uint64_t const _age,
            eosio::name const & _type) : id(_id), owner(_owner), pets_name(_pets_name), age (_age), type (_type) {}

        uint64_t get_id() const { return id; }
        eosio::name get_owner() const { return owner; }
        eosio::name get_pets_name() const { return pets_name; }
        uint64_t get_age() const { return age; }
        eosio::name get_type() const { return type; }

        uint64_t primary_key() const { return get_id(); }

        EOSLIB_SERIALIZE( pet_t, (id)(owner)(pets_name)(age)(type) )
};

typedef eosio::multi_index< "pets"_n, pet_t > pets_table;

#endif
1 Like

Hey @inzhagey, hope you are great.

sorry for the delay,i manage to fix the contract, you forgot to define the modifypet and removepet to your header file (hpp).

Here is your contract fixed:

helloworld.cpp

#include <helloworld.hpp>;

void helloworld::hi(eosio::name const & nm)
{
  eosio::print("Hello ", nm);
}

void helloworld::addpet(uint64_t const id, eosio::name const & owner, eosio::name const & pets_name, uint64_t const age, eosio::name const & type)
{
  pets_table pets(get_self(), get_self().value);  
  pets.emplace(get_self(), [&](auto & entry){
    entry = pet_t(id, owner, pets_name, age, type);
  }); 
}

void helloworld::modifypet(uint64_t const id, eosio::name const & owner, eosio::name const & pets_name, uint64_t const age, eosio::name const & type)
{
  pets_table pets(get_self(), get_self().value);
  auto pet_itr = pets.find(id); 
  pets.modify(pet_itr, get_self(), [&](auto & entry){
    entry = pet_t(id, owner, pets_name, age, type);
  }); 
}

void helloworld::removepet(uint64_t const id)
{
  pets_table pets(get_self(), get_self().value);
  auto pet_itr = pets.find(id);
  pets.erase(pet_itr);
}

helloworld.hpp


#ifndef HELLOWORLD
#define HELLOWORLD

#include <eosio/eosio.hpp>

#include <pets.hpp>

CONTRACT helloworld : public eosio::contract
{
  public:
    using eosio::contract::contract;
    ACTION hi(eosio::name const & nm);
    ACTION addpet(uint64_t const id, eosio::name const & owner, eosio::name const & pets_name, uint64_t const age, eosio::name const & type);
    ACTION modifypet(uint64_t const id, eosio::name const & owner, eosio::name const & pets_name, uint64_t const age, eosio::name const & type);
    ACTION removepet(uint64_t const id);
};

#endif

pets.hpp

#ifndef PETS
#define PETS

class [[eosio::table, eosio::contract("helloworld")]] pet_t 
{
    private:
        uint64_t id;
        eosio::name owner;
        eosio::name pets_name;
        uint64_t age;
        eosio::name type;
    public: 
        pet_t(){}

        pet_t(uint64_t const _id,
            eosio::name const & _owner,
            eosio::name const & _pets_name,
            uint64_t const _age,
            eosio::name const & _type) : id(_id), owner(_owner), pets_name(_pets_name), age (_age), type (_type) {}

        uint64_t get_id() const { return id; }
        eosio::name get_owner() const { return owner; }
        eosio::name get_pets_name() const { return pets_name; }
        uint64_t get_age() const { return age; }
        eosio::name get_type() const { return type; }

        uint64_t primary_key() const { return get_id(); }

        EOSLIB_SERIALIZE( pet_t, (id)(owner)(pets_name)(age)(type) )
};

typedef eosio::multi_index< "pets"_n, pet_t > pets_table;

#endif

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

I fixed that but I’m still getting the error when I try to deploy it : Unknown action setcode in contract eosio. I think i might have done something in terminal that may be causing this issue.

here’s all my code:

helloworld.hpp:

#include <helloworld.hpp>

void helloworld::hi(eosio::name const & nm)
{
  eosio::print("Hello ", nm);
}

void helloworld::addpet(uint64_t const id, eosio::name const & owner, eosio::name const & pets_name, uint64_t const age, eosio::name const & type)
{
  require_auth(owner);

  pets_table pets(get_self(), get_self().value);  
  pets.emplace(get_self(), [&](auto & entry){
    entry = pet_t(id, owner, pets_name, age, type);
  }); 
}

void helloworld::modifypet(uint64_t const id, eosio::name const & owner, eosio::name const & pets_name, uint64_t const age, eosio::name const & type)
{
  pets_table pets(get_self(), get_self().value);
  auto pet_itr = pets.find(id); 
  pets.modify(pet_itr, get_self(), [&](auto & entry){
    entry = pet_t(id, owner, pets_name, age, type);
  }); 
}

void helloworld::removepet(uint64_t const id)
{
  pets_table pets(get_self(), get_self().value);
  auto pet_itr = pets.find(id);

  require_auth(pet_itr->get_owner());

  pets.erase(pet_itr);
}

pets.hpp:

#ifndef PETS
#define PETS

class [[eosio::table, eosio::contract("helloworld")]] pet_t 
{
    private:
        uint64_t id;
        eosio::name owner;
        eosio::name pets_name;
        uint64_t age;
        eosio::name type;
    public: 
        pet_t(){}

        pet_t(uint64_t const _id,
            eosio::name const & _owner,
            eosio::name const & _pets_name,
            uint64_t const _age,
            eosio::name const & _type) : id(_id), owner(_owner), pets_name(_pets_name), age (_age), type (_type) {}

        uint64_t get_id() const { return id; }
        eosio::name get_owner() const { return owner; }
        eosio::name get_pets_name() const { return pets_name; }
        uint64_t get_age() const { return age; }
        eosio::name get_type() const { return type; }

        uint64_t primary_key() const { return get_id(); }

        EOSLIB_SERIALIZE( pet_t, (id)(owner)(pets_name)(age)(type) )
};

typedef eosio::multi_index< "pets"_n, pet_t > pets_table;

#endif

helloworld.cpp:

#include <helloworld.hpp>

void helloworld::hi(eosio::name const & nm)
{
  eosio::print("Hello ", nm);
}

void helloworld::addpet(uint64_t const id, eosio::name const & owner, eosio::name const & pets_name, uint64_t const age, eosio::name const & type)
{
  require_auth(owner);

  pets_table pets(get_self(), get_self().value);  
  pets.emplace(get_self(), [&](auto & entry){
    entry = pet_t(id, owner, pets_name, age, type);
  }); 
}

void helloworld::modifypet(uint64_t const id, eosio::name const & owner, eosio::name const & pets_name, uint64_t const age, eosio::name const & type)
{
  pets_table pets(get_self(), get_self().value);
  auto pet_itr = pets.find(id); 
  pets.modify(pet_itr, get_self(), [&](auto & entry){
    entry = pet_t(id, owner, pets_name, age, type);
  }); 
}

void helloworld::removepet(uint64_t const id)
{
  pets_table pets(get_self(), get_self().value);
  auto pet_itr = pets.find(id);

  require_auth(pet_itr->get_owner());

  pets.erase(pet_itr);
}
1 Like

Are you deploying the contract to EOSIO account? you should deploy it on a different account (bob, alice, weirdName…).

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

I tried to do it on helloworld account and I got the same error, then I got this error when trying to make a new account.

1 Like

Try to create a new public key, the one you have selected is for the eosio account to work properly.

Carlos Z

I created a new public key and tried to make bob account and got this.

Foe my eosio and helloworld account my public keys look like this, is that normal or should I delete one.

1 Like

Apparently you have a problem with eosio contracts, please check if you have access to this projects, there should be deployed in the eosio account so it can be able to create new accounts.

In case you dont have those projects, you might have to reset the entire app:

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

I checked and all the projects showed up like the picture you had, so I’m not sure what the problem is.

1 Like

Mmmm, last solution could be to reinstall the EOS Studio, if you install all the modules that is need it at the beginning properly, it should deploy the eosio contracts to the eosio account.

Carlos Z

I deleted and reinstalled EOS sand got the same error when I try to deploy it I get the same error Unknown action setcode in contract eosio. i can’t make a new account either.

1 Like