This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #998 from EOSIO/fix_kv_todo
fix kv_todo
- Loading branch information
Showing
6 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
include(ExternalProject) | ||
# if no cdt root is given use default path | ||
if(EOSIO_CDT_ROOT STREQUAL "" OR NOT EOSIO_CDT_ROOT) | ||
find_package(eosio.cdt) | ||
endif() | ||
|
||
ExternalProject_Add( | ||
kv_todo_project | ||
SOURCE_DIR ${CMAKE_SOURCE_DIR}/src | ||
BINARY_DIR ${CMAKE_BINARY_DIR}/kv_todo | ||
CMAKE_ARGS -DCMAKE_TOOLCHAIN_FILE=${EOSIO_CDT_ROOT}/lib/cmake/eosio.cdt/EosioWasmToolchain.cmake | ||
UPDATE_COMMAND "" | ||
PATCH_COMMAND "" | ||
TEST_COMMAND "" | ||
INSTALL_COMMAND "" | ||
BUILD_ALWAYS 1 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- kv_todo Project --- | ||
|
||
-- How to Build with CMake and Make -- | ||
- cd into the 'build' directory | ||
- run the command 'cmake ..' | ||
- run the command 'make' | ||
|
||
- After build - | ||
- The built smart contract is under the 'kv_todo' directory in the 'build' directory | ||
- You can then do a 'set contract' action with 'cleos' and point to the './build/kv_todo' directory | ||
|
||
- Additions to CMake should be done to the CMakeLists.txt in the './src' directory and not in the top level CMakeLists.txt | ||
|
||
-- How to build with eosio-cpp -- | ||
- cd into the 'build' directory | ||
- run the command 'eosio-cpp -abigen ../src/kv_todo.cpp -o kv_todo.wasm -I ../include/' | ||
|
||
- After build - | ||
- The built smart contract is in the 'build' directory | ||
- You can then do a 'set contract' action with 'cleos' and point to the 'build' directory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <eosio/eosio.hpp> | ||
#include <eosio/system.hpp> | ||
using namespace eosio; | ||
|
||
struct todo_entry { | ||
std::string uuid; | ||
eosio::non_unique<eosio::name, std::string> account_name; | ||
eosio::non_unique<std::string, std::string> task; | ||
eosio::non_unique<bool, std::string> checked; | ||
eosio::non_unique<uint32_t, std::string> created; | ||
|
||
std::string get_uuid() const { return uuid; } | ||
eosio::name get_account_name() const { return std::get<0>(account_name); } | ||
std::string get_task() const { return std::get<0>(task); } | ||
bool get_checked() const { return std::get<0>(checked); } | ||
int get_created() const { return std::get<0>(created); } | ||
}; | ||
|
||
class [[eosio::contract]] kv_todo : public contract { | ||
struct [[eosio::table]] todo_table : eosio::kv::table<todo_entry, "todo"_n> { | ||
KV_NAMED_INDEX("uuid"_n, uuid); | ||
KV_NAMED_INDEX("accname"_n, account_name); | ||
KV_NAMED_INDEX("task"_n, task) | ||
KV_NAMED_INDEX("checked"_n, checked) | ||
KV_NAMED_INDEX("created"_n, created) | ||
|
||
// constructor for our `kvtodo` table to setup and initialize it | ||
todo_table(eosio::name contract_name) { init(contract_name, uuid, account_name, task, checked, created); } | ||
}; | ||
public: | ||
using contract::contract; | ||
|
||
[[eosio::action]] | ||
std::vector<todo_entry> getbyaccname(name account_name); | ||
|
||
[[eosio::action]] | ||
size_t cntbyaccname(name account_name); | ||
|
||
[[eosio::action]] | ||
std::vector<todo_entry> getbytask(std::string task); | ||
|
||
[[eosio::action]] | ||
size_t cntbytask(std::string name); | ||
|
||
[[eosio::action]] | ||
std::vector<todo_entry> getbychecked(bool value); | ||
|
||
[[eosio::action]] | ||
size_t cntbychecked(bool checked); | ||
|
||
[[eosio::action]] | ||
todo_entry upsert(const std::string& uuid, | ||
eosio::name account_name, | ||
const std::string& task, | ||
bool checked); | ||
|
||
[[eosio::action]] | ||
void del(const std::string& uuid); | ||
|
||
using get_by_account_name_action = action_wrapper<"getbyaccname"_n, &kv_todo::getbyaccname>; | ||
using cnt_by_account_name_action = action_wrapper<"cntbyaccname"_n, &kv_todo::cntbyaccname>; | ||
using get_by_task_action = action_wrapper<"getbytask"_n, &kv_todo::getbytask>; | ||
using cnt_by_task_action = action_wrapper<"cntbytask"_n, &kv_todo::cntbytask>; | ||
using get_by_checked_action = action_wrapper<"getbychecked"_n, &kv_todo::getbychecked>; | ||
using cnt_by_checked_action = action_wrapper<"cntbychecked"_n, &kv_todo::cntbychecked>; | ||
|
||
using upsert_action = action_wrapper<"upsert"_n, &kv_todo::upsert>; | ||
using del_action = action_wrapper<"del"_n, &kv_todo::del>; | ||
|
||
private: | ||
todo_table todo_entries{"todo"_n}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<h1 class="contract"> hi </h1> | ||
|
||
Stub for hi action's ricardian contract |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
project(kv_todo) | ||
|
||
set(EOSIO_WASM_OLD_BEHAVIOR "Off") | ||
find_package(eosio.cdt) | ||
|
||
add_contract( kv_todo kv_todo kv_todo.cpp ) | ||
target_include_directories( kv_todo PUBLIC ${CMAKE_SOURCE_DIR}/../include ) | ||
target_ricardian_directory( kv_todo ${CMAKE_SOURCE_DIR}/../ricardian ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#include <kv_todo.hpp> | ||
|
||
// example) cleos push action todo getbyaccname '["todo"]' -p todo@active | ||
[[eosio::action]] | ||
std::vector<todo_entry> kv_todo::getbyaccname(name account_name) { | ||
std::string min_uuid = "00000000-0000-0000-0000-000000000000"; | ||
std::string max_uuid = "ffffffff-ffff-ffff-ffff-ffffffffffff"; | ||
|
||
return todo_entries.account_name.range({account_name, min_uuid}, {account_name, max_uuid}); | ||
} | ||
|
||
// example) cleos push action todo cntbyaccname '["todo"]' -p todo@active | ||
[[eosio::action]] | ||
size_t kv_todo::cntbyaccname(name account_name) { | ||
std::string min_uuid = "00000000-0000-0000-0000-000000000000"; | ||
std::string max_uuid = "ffffffff-ffff-ffff-ffff-ffffffffffff"; | ||
|
||
return todo_entries.account_name.range({account_name, min_uuid}, {account_name, max_uuid}).size(); | ||
} | ||
|
||
// example) cleos push action todo getbytask '["Start Blockchain"]' -p todo@active | ||
[[eosio::action]] | ||
std::vector<todo_entry> kv_todo::getbytask(std::string task) { | ||
std::string min_uuid = "00000000-0000-0000-0000-000000000000"; | ||
std::string max_uuid = "ffffffff-ffff-ffff-ffff-ffffffffffff"; | ||
|
||
return todo_entries.task.range({task, min_uuid}, {task, max_uuid}); | ||
} | ||
|
||
// example) cleos push action todo cntbytask '["Start Blockchain"]' -p todo@active | ||
[[eosio::action]] | ||
size_t kv_todo::cntbytask(std::string task) { | ||
std::string min_uuid = "00000000-0000-0000-0000-000000000000"; | ||
std::string max_uuid = "ffffffff-ffff-ffff-ffff-ffffffffffff"; | ||
|
||
return todo_entries.task.range({task, min_uuid}, {task, max_uuid}).size(); | ||
} | ||
|
||
// example) cleos push action todo getbychecked '["false"]' -p todo@active | ||
[[eosio::action]] | ||
std::vector<todo_entry> kv_todo::getbychecked(bool checked) { | ||
std::string min_uuid = "00000000-0000-0000-0000-000000000000"; | ||
std::string max_uuid = "ffffffff-ffff-ffff-ffff-ffffffffffff"; | ||
|
||
return todo_entries.checked.range({checked, min_uuid}, {checked, max_uuid}); | ||
} | ||
|
||
// example) cleos push action todo cntbychecked '["false"]' -p todo@active | ||
[[eosio::action]] | ||
size_t kv_todo::cntbychecked(bool checked) { | ||
std::string min_uuid = "00000000-0000-0000-0000-000000000000"; | ||
std::string max_uuid = "ffffffff-ffff-ffff-ffff-ffffffffffff"; | ||
|
||
return todo_entries.checked.range({checked, min_uuid}, {checked, max_uuid}).size(); | ||
} | ||
|
||
/** | ||
examples) | ||
cleos push action todo upsert '["ac8acfe7-cd4e-4d22-8400-218b697a4517", "todo", "Deploy Hello World Contract", false]' -p todo@active | ||
cleos push action todo upsert '["b7b0d09d-a82b-44d9-b067-3bae2d02917e", "todo", "Start Blockchain", false]' -p todo@active | ||
cleos push action todo upsert '["bf581bee-9f2c-447b-94ad-78e4984b6f51", "todo", "Write Hello World Contract", false]' -p todo@active | ||
*/ | ||
[[eosio::action]] | ||
todo_entry kv_todo::upsert(const std::string& uuid, | ||
eosio::name account_name, | ||
const std::string& task, | ||
bool checked) { | ||
|
||
require_auth(account_name); | ||
|
||
auto itr = todo_entries.uuid.find(uuid); | ||
if (itr != todo_entries.uuid.end()) { | ||
check(account_name == itr.value().get_account_name(), "Unauthorized"); | ||
todo_entry todo_entry_update = {itr.value().get_uuid(), {itr.value().get_account_name(), uuid}, {task, uuid}, {checked, uuid}, {itr.value().get_created(), uuid}}; | ||
todo_entries.put(todo_entry_update, get_self()); | ||
} else { | ||
uint32_t created = eosio::current_time_point().sec_since_epoch(); | ||
todo_entry todo_entry_insert = {uuid, {account_name, uuid}, {task,uuid}, {checked, uuid}, {created, uuid}}; | ||
todo_entries.put(todo_entry_insert, get_self()); | ||
} | ||
|
||
itr = todo_entries.uuid.find(uuid); | ||
return itr.value(); | ||
} | ||
|
||
[[eosio::action]] | ||
void kv_todo::del(const std::string& uuid) { | ||
auto itr = todo_entries.uuid.find(uuid); | ||
|
||
if (itr != todo_entries.uuid.end()) { | ||
todo_entries.erase(itr.value()); | ||
eosio::print_f("todo_entry was successfully deleted from table."); | ||
} else { | ||
eosio::print_f("todo_entry not found in table."); | ||
} | ||
} |