📜  eosio require_find - C++ (1)

📅  最后修改于: 2023-12-03 14:40:59.606000             🧑  作者: Mango

eosio require_find - Introduction for C++ Developers

If you're developing a contract for the EOSIO blockchain using C++, you may come across the require_find function. This function is often used to check whether an entry exists in a table based on a provided key.

How require_find Works

The require_find function takes three parameters:

require_find(iterator_type it, const std::string& error_message, const std::string& message_suffix = {})
  • iterator_type it: The iterator returned by a table's find or lower_bound function.
  • const std::string& error_message: The error message to display if the iterator points to the end of the table (i.e. the key is not found).
  • const std::string& message_suffix: Optional suffix to add to the error message.

If it points to the end of the table, require_find will assert with the provided error message (and suffix, if present). If it points to a valid entry in the table, require_find will do nothing and allow the contract to continue execution.

Here's an example usage:

auto my_table = some_contract::my_table{};
auto itr = my_table.find(some_key);
eosio::require_find(itr, "Error: Key not found");

This code will check if my_table contains an entry with the key some_key. If so, itr will point to that entry. If not, itr will be set to the end of the table. The require_find function will then assert with the error message "Error: Key not found" if itr points to the end of the table.

Conclusion

require_find is a useful function for checking the existence of entries in a table in EOSIO smart contracts. By providing a clear error message, it can help prevent unexpected behavior and improve the overall user experience of your contract.