eos icon indicating copy to clipboard operation
eos copied to clipboard

what's the difference between '_self' and '_code' in smart contract?

Open bytehi opened this issue 8 years ago • 1 comments

Hi, all. In the official example, I found these codes. ` [[eosio::action]] void erase(name user) { require_auth(user);

address_index addresses(_self, _code.value);

auto iterator = addresses.find(user.value);
eosio_assert(iterator != addresses.end(), "Record does not exist");
addresses.erase(iterator);

} `

What is the difference between '_self' and '_code'? In contract.hpp: _self: receiver - The name of this contract _code - The code name of the action this contract is processing.

/** 24 * Construct a new contract given the contract name 25 * 26 * @brief Construct a new contract object. 27 * @param receiver - The name of this contract 28 * @param code - The code name of the action this contract is processing. 29 * @param ds - The datastream used 30 */ 31 contract( name receiver, name code, datastream<const char*> ds ):_self(receiver),_code(code),_ds(ds) {}

And i did some testing,bug i found _self and _code always the contract name...

bytehi avatar Nov 19 '18 02:11 bytehi

      /**
       * The name of this contract
       *
       * @brief The name of this contract.
       */
      name _self;

      /**
       * The code name of the action this contract is processing.
       *
       * @brief The code name of the action this contract is processing.
       */
      name _code;

So normally in the main action self is equal to code. However, in the notifications and inline actions, code refers to the main action.

Please ask https://eosio.stackexchange.com/ if you have further questions.

taokayan avatar Nov 19 '18 09:11 taokayan