0

Problem

I'm trying to emplace a new row in a multi-index table, but it tells me that I don't have the authotity of the row key name to do it.

Code

  1. Multi-index table definition
struct [[eosio::table]] election {
    name        election_name;
    election_info    election_data;  // simple struct

    uint64_t primary_key() const { return election_name.value; }
};

typedef eosio::multi_index<name("election"), election> elections_table;
  1. Multi-index table instantiation in upsert action
void evote::upsert(name election_name) {
   require_auth(name("evote"));
   print("About to create new election...");

   // Instantiate multi-index table aka: Get the first row
   // (1) Owner of table. (2) Account name this contract is deployed to
   //      get_self() gets the name of the contract (evote)
   elections_table elections(get_self(), get_first_receiver().value);

   // Next, query the table for the election.
   auto row = elections.find(election_name.value);

   // Check if election doesn't exists
   if ( row == elections.end() ) {
       print("Creating a smoking new election");

       // Create election info
       election_info info;

       elections.emplace(election_name, [&]( auto& row){
           row.election_name = election_name;
           row.election_data = info;
       });
   }

   // Table exists
   else {
       print("The election: ", election_name, " exists");

   }
}

Environment

I'm running this in a local testnet with a wallet with the keys for two accounts.

  • evote: The one setting the contract
  • tse: Doesn't do nothing here

This is how I setted the contract

cleos set contract evote ./ -p evote@active

This is the action that I push

cleos push action evote upsert '["eleccion1"]' -p evote@active

Error

It asks for eleccion1 authority, but eleccion1 isn't even an account.

Error 3090004: Missing required authority
Ensure that you have the related authority inside your transaction!;
If you are currently using 'cleos push action' command, try to add the relevant authority using -p option.
Error Details:
missing authority of eleccion1
pending console output: About to create new election...Creating a smoking new election

0

Please refer to the documentation for the emplace function.

Specifically, the first parameter. In your example, you are sending election_name as the first parameter of the emplace function. This is incorrect. You should be sending get_self() as the first parameter to refer to the smart contract, as the smart contract is the RAM payer. Alternatively, if you wish for the caller of the function to pay for the RAM storage, you could send name("evote") as you have done on line 2. See below for an amended example:

elections.emplace(get_self(), [&]( auto& row){
    row.election_name = election_name;
    row.election_data = info;
});
|improve this answer|||||

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.