Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stake in a box #314 #315 #316

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ add_subdirectory(cyber.stake)
add_subdirectory(cyber.govern)
add_subdirectory(cyber.rejector)
add_subdirectory(cyber.incomereject)
add_subdirectory(cyber.box)

add_subdirectory(scripts/base-genesis)

Expand Down
3 changes: 3 additions & 0 deletions common/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,20 @@ 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 {

// contracts
static const auto token_name = "cyber.token"_n;
static const auto stake_name = "cyber.stake"_n;
static const auto govern_name = "cyber.govern"_n;
static const auto box_name = "cyber.box"_n;
static const auto worker_name = "cyber.worker"_n;
static const auto names_name = "cyber.names"_n;
static const auto producers_name = "cyber.prods"_n;
static const auto internal_name = "cyber"_n;
static const auto null_name = "cyber.null"_n;

// permissions
static const auto code_name = "cyber.code"_n;
Expand Down
9 changes: 4 additions & 5 deletions cyber.bios/include/cyber.bios/cyber.bios.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace cyber {
using eosio::time_point_sec;
using eosio::contract;
using eosio::asset;
using eosio::symbol_code;

struct permission_level_weight {
permission_level permission;
Expand Down Expand Up @@ -110,7 +111,6 @@ namespace cyber {
eosio::multi_index<"stake.autorc"_n, auto_recall, autorc_key_index>;
void autorcs_dummy() { autorcs autorcs_tabl(_self, _self.value); } // an ugly way to make abi appear

void check_stake(name account);
public:
using contract::contract;
[[eosio::action]]
Expand Down Expand Up @@ -183,10 +183,9 @@ namespace cyber {

[[eosio::action]]
void providebw(name provider, name account) {} // defined in cyberway/libraries/chain/cyberway/cyberway_contract.cpp

[[eosio::on_notify(CYBER_STAKE "::withdraw")]] void on_stake_withdraw(name account, asset quantity);
[[eosio::on_notify(CYBER_STAKE "::provide")]] void on_stake_provide(name provider_name, name consumer_name, asset quantity);


[[eosio::action]]
void checkstake(name account, symbol_code token_code);
};

} /// namespace cyber
15 changes: 2 additions & 13 deletions cyber.bios/src/cyber.bios.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,23 +156,12 @@ void bios::newaccount(name creator, name newact, ignore<authority> owner, ignore
}
}

void bios::check_stake(name account) {
auto token_code = system_token.code();
void bios::checkstake(name account, symbol_code token_code) {
eosio::check(token_code == system_token.code(), "unsupported token_code");
auto cost = eosio::get_used_resources_cost(account);
auto effective_stake = stake::get_effective_stake(account, token_code);
eosio::check(!stake::enabled(token_code) || (effective_stake >= cost),
"no staked tokens available due to resource usage");
}

void bios::on_stake_withdraw(name account, asset quantity) {
(void)quantity;
check_stake(account);
}

void bios::on_stake_provide(name provider_name, name consumer_name, asset quantity) {
(void)quantity;
(void)consumer_name;
check_stake(provider_name);
}

}
12 changes: 12 additions & 0 deletions cyber.box/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
add_contract(cyber.box cyber.box ${CMAKE_CURRENT_SOURCE_DIR}/src/cyber.box.cpp)
install_contract(cyber.box)

target_include_directories(cyber.box.wasm
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/../cyber.token/include
${CMAKE_CURRENT_SOURCE_DIR}/..)

set_target_properties(cyber.box.wasm
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
44 changes: 44 additions & 0 deletions cyber.box/include/cyber.box/cyber.box.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once
#include <eosio/eosio.hpp>
#include <eosio/time.hpp>
#include <eosio/singleton.hpp>
#include <tuple>
#include <eosio/privileged.hpp>
#include <cyber.token/cyber.token.hpp>
#include <common/dispatchers.hpp>

namespace cyber {
using eosio::name;
class [[eosio::contract("cyber.box")]] box : public eosio::contract {
struct structures {
struct box {
uint64_t id;
name contract;
name title;
name owner;
bool empty;
uint64_t primary_key()const { return id; }
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("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>;

void erase_box(name contract, name treasurer, name title, name owner = name());
public:
using contract::contract;
[[eosio::action]] void create(name contract, name treasurer, name title);
[[eosio::action]] void packup(name contract, name treasurer, name title);
[[eosio::action]] void unpack(name contract, name treasurer, name title, name owner);
[[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, treasurer.value);
auto boxes_idx = boxes_table.get_index<"bykey"_n>();
return boxes_idx.get({contract, title}, "box does not exist").owner;
}
//do we need to add the ability to put boxes in a box?
};
} /// namespace cyber
68 changes: 68 additions & 0 deletions cyber.box/src/cyber.box.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <cyber.box/cyber.box.hpp>
namespace cyber {

void box::create(name contract, name treasurer, name title) {
eosio::check(is_account(contract), "contract account does not exist");
require_auth(treasurer);
zxcat marked this conversation as resolved.
Show resolved Hide resolved
boxes boxes_table(_self, treasurer.value);
auto boxes_idx = boxes_table.get_index<"bykey"_n>();
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,
.title = title,
.owner = treasurer,
.empty = true
};});
}

void box::packup(name contract, name treasurer, name title) {
require_auth(contract);
boxes boxes_table(_self, treasurer.value);
auto boxes_idx = boxes_table.get_index<"bykey"_n>();
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(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, name owner) {
boxes boxes_table(_self, treasurer.value);
auto boxes_idx = boxes_table.get_index<"bykey"_n>();
auto box_itr = boxes_idx.find({contract, title});
eosio::check(box_itr != boxes_idx.end(), "box does not exist");
eosio::check(!owner || owner == box_itr->owner, "incorrect owner");
eosio::check(!owner || !box_itr->empty, "cannot unpack an empty box");
if (!box_itr->empty) {
require_recipient(contract);
}
require_auth(box_itr->owner);
boxes_idx.erase(box_itr);
}

void box::unpack(name contract, name treasurer, name title, name owner) {
eosio::check(static_cast<bool>(owner), "owner not specified");
erase_box(contract, treasurer, title, owner);
zxcat marked this conversation as resolved.
Show resolved Hide resolved
}

void box::burn(name contract, name treasurer, name title) {
erase_box(contract, treasurer, 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, treasurer.value);
auto boxes_idx = boxes_table.get_index<"bykey"_n>();
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");
eosio::check(from != to, "cannot transfer to self");
eosio::check(!box_itr->empty, "cannot transfer an empty box");
require_recipient(from);
require_recipient(to);
zxcat marked this conversation as resolved.
Show resolved Hide resolved
boxes_idx.modify(box_itr, name(), [&](auto& b) { b.owner = to; });
}

} /// namespace cyber
17 changes: 16 additions & 1 deletion cyber.stake/include/cyber.stake/cyber.stake.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ struct structures {
symbol_code token_code;
int64_t total;
};

struct box {
name title;
asset quantity;
uint64_t primary_key()const { return title.value; }
};
};

using agent_id_index = eosio::indexed_by<"agentid"_n, eosio::const_mem_fun<structures::agent, uint64_t, &structures::agent::primary_key> >;
Expand Down Expand Up @@ -235,6 +241,8 @@ struct structures {
using susps_idx_t = decltype(susps(_self, _self.value).get_index<"bykey"_n>());

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

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 All @@ -248,7 +256,7 @@ struct structures {
void add_proxy(symbol_code token_code, grants& grants_table, const structures::agent& grantor_as_agent, const structures::agent& agent,
int16_t pct, int64_t share, int16_t break_fee = -1, int64_t break_min_own_staked = -1);

void change_balance(name account, asset quantity);
void sub_own_funds(name account, asset quantity);

static inline void staking_exists(symbol_code token_code) {
params params_table(table_owner, table_owner.value);
Expand Down Expand Up @@ -281,6 +289,9 @@ struct structures {

void check_suspense(susps& susps_table, susps_idx_t& susps_idx, symbol_code token_code, name account, name action_name);
void set_suspense(name ram_payer, susps& susps_table, susps_idx_t& susps_idx, symbol_code token_code, name account, name action_name, int delay);

void send_checkstake(name account, symbol_code token_code);
void erase_box(name contract, name treasurer, name title, name owner);

public:

Expand Down Expand Up @@ -431,5 +442,9 @@ struct structures {

[[eosio::action]] void setautorc(name account, symbol_code token_code, bool break_fee_enabled, bool break_min_stake_enabled);
[[eosio::action]] void setautorcmode(symbol_code token_code, bool enabled);

[[eosio::action]] void constrain(name treasurer, name title, asset quantity);
[[eosio::on_notify(CYBER_BOX"::unpack")]] void on_unpack(name contract, name treasurer, name title, name owner);
[[eosio::on_notify(CYBER_BOX"::burn")]] void on_burn(name contract, name treasurer, name title);
};
} /// namespace cyber
Loading