Skip to content

Commit

Permalink
reviewed: treasurer in the scope #314
Browse files Browse the repository at this point in the history
  • Loading branch information
kaynarov committed Mar 10, 2020
1 parent b3b939b commit c2b2031
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 34 deletions.
1 change: 1 addition & 0 deletions common/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ inline eosio::chain::name operator ""_n() {

#define CYBER_TOKEN "cyber.token"
#define CYBER_STAKE "cyber.stake"
#define CYBER_BOX "cyber.box"

namespace cyber { namespace config {

Expand Down
11 changes: 5 additions & 6 deletions cyber.box/include/cyber.box/cyber.box.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ struct structures {
struct box {
uint64_t id;
name contract;
name treasurer;
name title;
name owner;
bool empty;
uint64_t primary_key()const { return id; }
using key_t = std::tuple<name, name, name>;
key_t by_key()const { return std::make_tuple(contract, treasurer, title); }
using key_t = std::tuple<name, name>;
key_t by_key()const { return std::make_tuple(contract, title); }
};
};
using box_key_index [[using eosio: order("contract"), order("treasurer"), order("title")]] =
using box_key_index [[using eosio: order("contract"), order("title")]] =
eosio::indexed_by<"bykey"_n, eosio::const_mem_fun<structures::box, structures::box::key_t, &structures::box::by_key> >;
using boxes [[eosio::order("id")]] = eosio::multi_index<"box"_n, structures::box, box_key_index>;

Expand All @@ -36,9 +35,9 @@ struct structures {
[[eosio::action]] void burn(name contract, name treasurer, name title);
[[eosio::action]] void transfer(name contract, name treasurer, name title, name from, name to, std::string memo);
static inline name get_owner(name box_contract_account, name contract, name treasurer, name title) {
boxes boxes_table(box_contract_account, box_contract_account.value);
boxes boxes_table(box_contract_account, treasurer.value);
auto boxes_idx = boxes_table.get_index<"bykey"_n>();
return boxes_idx.get({contract, treasurer, title}, "box does not exist").owner;
return boxes_idx.get({contract, title}, "box does not exist").owner;
}
//do we need to add the ability to put boxes in a box?
};
Expand Down
20 changes: 10 additions & 10 deletions cyber.box/src/cyber.box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
namespace cyber {

void box::create(name contract, name treasurer, name title) {
eosio::check(is_account(contract), "contract account does not exist");
require_auth(treasurer);
boxes boxes_table(_self, _self.value);
boxes boxes_table(_self, treasurer.value);
auto boxes_idx = boxes_table.get_index<"bykey"_n>();
eosio::check(boxes_idx.find({contract, treasurer, title}) == boxes_idx.end(), "such a box already exists");
eosio::check(boxes_idx.find({contract, title}) == boxes_idx.end(), "such a box already exists");
boxes_table.emplace(treasurer, [&](auto& b) { b = {
.id = boxes_table.available_primary_key(),
.contract = contract,
.treasurer = treasurer,
.title = title,
.owner = treasurer,
.empty = true
Expand All @@ -18,19 +18,19 @@ void box::create(name contract, name treasurer, name title) {

void box::packup(name contract, name treasurer, name title) {
require_auth(contract);
boxes boxes_table(_self, _self.value);
boxes boxes_table(_self, treasurer.value);
auto boxes_idx = boxes_table.get_index<"bykey"_n>();
auto box_itr = boxes_idx.find({contract, treasurer, title});
auto box_itr = boxes_idx.find({contract, title});
eosio::check(box_itr != boxes_idx.end(), "box does not exist");
eosio::check(box_itr->empty, "the box is not empty");
eosio::check(box_itr->treasurer == box_itr->owner, "SYSTEM: invalid box owner");
eosio::check(treasurer == box_itr->owner, "SYSTEM: invalid box owner");
boxes_idx.modify(box_itr, name(), [&](auto& b) { b.empty = false; });
}

void box::erase_box(name contract, name treasurer, name title, bool release) {
boxes boxes_table(_self, _self.value);
boxes boxes_table(_self, treasurer.value);
auto boxes_idx = boxes_table.get_index<"bykey"_n>();
auto box_itr = boxes_idx.find({contract, treasurer, title});
auto box_itr = boxes_idx.find({contract, title});
eosio::check(box_itr != boxes_idx.end(), "box does not exist");
require_auth(box_itr->owner);
if (!box_itr->empty && release) {
Expand All @@ -53,9 +53,9 @@ void box::burn(name contract, name treasurer, name title) {

void box::transfer(name contract, name treasurer, name title, name from, name to, std::string memo) {
eosio::check(is_account(to), "to account does not exist");
boxes boxes_table(_self, _self.value);
boxes boxes_table(_self, treasurer.value);
auto boxes_idx = boxes_table.get_index<"bykey"_n>();
auto box_itr = boxes_idx.find({contract, treasurer, title});
auto box_itr = boxes_idx.find({contract, title});
eosio::check(box_itr != boxes_idx.end(), "box does not exist");
require_auth(from);
eosio::check(box_itr->owner == from, "only the owner can do it");
Expand Down
10 changes: 2 additions & 8 deletions cyber.stake/include/cyber.stake/cyber.stake.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,9 @@ struct structures {
};

struct box {
uint64_t id;
name treasurer;
name title;
asset quantity;
uint64_t primary_key()const { return id; }
using key_t = std::tuple<name, name>;
key_t by_key()const { return std::make_tuple(treasurer, title); }
uint64_t primary_key()const { return title.value; }
};
};

Expand Down Expand Up @@ -246,9 +242,7 @@ struct structures {

using losses_singleton [[eosio::order("id","asc")]] = eosio::singleton<"losses"_n, structures::losses>;

using box_key_index [[eosio::order("treasurer"), eosio::order("title")]] =
eosio::indexed_by<"bykey"_n, eosio::const_mem_fun<structures::box, structures::box::key_t, &structures::box::by_key> >;
using boxes [[eosio::order("id")]] = eosio::multi_index<"box"_n, structures::box, box_key_index>;
using boxes [[eosio::order("title")]] = eosio::multi_index<"box"_n, structures::box>;

void update_stake_proxied(symbol_code token_code, name agent_name) {
eosio::update_stake_proxied(token_code, agent_name, true);
Expand Down
16 changes: 6 additions & 10 deletions cyber.stake/src/cyber.stake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -867,13 +867,10 @@ void stake::constrain(name treasurer, name title, asset quantity) {
agents_idx.modify(treasurer_itr, name(), [&](auto& a) { a.provided += quantity.amount; });
send_checkstake(treasurer, token_code);

boxes boxes_table(_self, _self.value);
auto boxes_idx = boxes_table.get_index<"bykey"_n>();
eosio::check(boxes_idx.find({treasurer, title}) == boxes_idx.end(), "non-unique title");
boxes boxes_table(_self, treasurer.value);
eosio::check(boxes_table.find(title.value) == boxes_table.end(), "non-unique title");

boxes_table.emplace(treasurer, [&](auto& b) { b = {
.id = boxes_table.available_primary_key(),
.treasurer = treasurer,
.title = title,
.quantity = quantity
};});
Expand All @@ -888,10 +885,9 @@ void stake::constrain(name treasurer, name title, asset quantity) {
void stake::release(name treasurer, name title, name owner) {
require_auth(config::box_name);

boxes boxes_table(_self, _self.value);
auto boxes_idx = boxes_table.get_index<"bykey"_n>();
auto box_itr = boxes_idx.find({treasurer, title});
eosio::check(box_itr != boxes_idx.end(), "SYSTEM: nothing to release");
boxes boxes_table(_self, treasurer.value);
auto box_itr = boxes_table.find(title.value);
eosio::check(box_itr != boxes_table.end(), "SYSTEM: nothing to release");

auto quantity = box_itr->quantity;
auto token_code = quantity.symbol.code();
Expand All @@ -907,7 +903,7 @@ void stake::release(name treasurer, name title, name owner) {
agents_idx.modify(treasurer_itr, name(), [&](auto& a) { a.provided -= amount; });
}

boxes_idx.erase(box_itr);
boxes_table.erase(box_itr);

if (treasurer == owner) {
return;
Expand Down

0 comments on commit c2b2031

Please sign in to comment.