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 1 commit
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
4 changes: 2 additions & 2 deletions cyber.box/include/cyber.box/cyber.box.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ struct structures {
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, bool release);
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);
[[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) {
Expand Down
18 changes: 7 additions & 11 deletions cyber.box/src/cyber.box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,24 @@ void box::packup(name contract, name treasurer, name title) {
boxes_idx.modify(box_itr, name(), [&](auto& b) { b.empty = false; });
}

void box::erase_box(name contract, name treasurer, name title, bool release) {
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");
require_auth(box_itr->owner);
if (!box_itr->empty && release) {
eosio::action(
eosio::permission_level{_self, config::active_name},
contract, "release"_n,
std::make_tuple(treasurer, title, box_itr->owner)
).send();
}
boxes_idx.erase(box_itr);
}

void box::unpack(name contract, name treasurer, name title) {
erase_box(contract, treasurer, title, true);
void box::unpack(name contract, name treasurer, name title, name owner) {
require_recipient(contract);
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, false);
erase_box(contract, treasurer, title);
}

void box::transfer(name contract, name treasurer, name title, name from, name to, std::string memo) {
Expand Down
2 changes: 1 addition & 1 deletion cyber.stake/include/cyber.stake/cyber.stake.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,6 @@ struct structures {
[[eosio::action]] void setautorcmode(symbol_code token_code, bool enabled);

[[eosio::action]] void constrain(name treasurer, name title, asset quantity);
[[eosio::action]] void release(name treasurer, name title, name owner);
[[eosio::on_notify(CYBER_BOX"::unpack")]] void on_unpack(name contract, name treasurer, name title, name owner);
};
} /// namespace cyber
8 changes: 4 additions & 4 deletions cyber.stake/src/cyber.stake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,12 +882,12 @@ void stake::constrain(name treasurer, name title, asset quantity) {
).send();
}

void stake::release(name treasurer, name title, name owner) {
require_auth(config::box_name);
void stake::on_unpack(name contract, name treasurer, name title, name owner) {
eosio::check(contract == _self, "SYSTEM: incorrect contract");

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");
eosio::check(box_itr != boxes_table.end(), "SYSTEM: nothing to unpack");

auto quantity = box_itr->quantity;
auto token_code = quantity.symbol.code();
Expand Down Expand Up @@ -953,7 +953,7 @@ void stake::release(name treasurer, name title, name owner) {
eosio::check(eosio::token::balance_exist(::cyber::config::token_name, owner, token_code), "owner balance does not exist");

INLINE_ACTION_SENDER(eosio::token, transfer)(config::token_name, {_self, config::active_name},
{_self, owner, quantity, "released tokens"});
{_self, owner, quantity, "unpacked tokens"});
}

} /// namespace cyber
4 changes: 2 additions & 2 deletions tests/cyber.box_test_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ struct cyber_box_api: base_contract_api {
return push(N(packup), signer ? signer : contract,
args()("contract", contract)("treasurer", treasurer)("title", title));
}
action_result unpack(name contract, name treasurer, name title, name signer) {
action_result unpack(name contract, name treasurer, name title, name owner, name signer) {
return push(N(unpack), signer,
args()("contract", contract)("treasurer", treasurer)("title", title));
args()("contract", contract)("treasurer", treasurer)("title", title)("owner", owner));
}
action_result burn(name contract, name treasurer, name title, name signer) {
return push(N(burn), signer,
Expand Down
42 changes: 24 additions & 18 deletions tests/cyber.box_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,22 @@ class cyber_box_tester : public golos_tester {

struct errors: contract_error_messages {
const string no_staking = amsg("no staking for token");
const string no_box = amsg("box does not exist");
const string box_already_exists = amsg("such a box already exists");
const string not_empty = amsg("the box is not empty");
const string not_enough = amsg("not enough staked tokens");
const string no_box = amsg("box does not exist");
const string box_already_exists = amsg("such a box already exists");
const string not_empty = amsg("the box is not empty");
const string not_enough = amsg("not enough staked tokens");

const string non_unique = amsg("non-unique title");
const string non_unique = amsg("non-unique title");

const string empty_transfer = amsg("cannot transfer an empty box");
const string empty_transfer = amsg("cannot transfer an empty box");
const string empty_unpack = amsg("cannot unpack an empty box");
const string self_transfer = amsg("cannot transfer to self");
const string no_to_acc = amsg("to account does not exist");
const string no_to_acc = amsg("to account does not exist");

const string no_balance = amsg("owner balance does not exist");
const string not_owner = amsg("only the owner can do it");
const string incorrect_owner = amsg("incorrect owner");

const string no_balance = amsg("owner balance does not exist");
const string not_owner = amsg("only the owner can do it");

string missing_authority(name acc) { return "missing authority of " + std::string(acc); };

Expand Down Expand Up @@ -117,9 +120,11 @@ BOOST_FIXTURE_TEST_CASE(stake_in_box_tests, cyber_box_tester) try {
BOOST_CHECK_EQUAL(err.box_already_exists, box.create(stake_account_name, _whale, N(whalebox)));

BOOST_CHECK_EQUAL(err.no_box, box.burn(stake_account_name, _whale, N(whalefox), _whale));
BOOST_CHECK_EQUAL(err.no_box, box.unpack(stake_account_name, _whale, N(whalefox), _whale));
BOOST_CHECK_EQUAL(err.missing_authority(_whale), box.unpack(stake_account_name, _whale, N(whalebox), _alice));
BOOST_CHECK_EQUAL(success(), box.unpack(stake_account_name, _whale, N(whalebox), _whale));
BOOST_CHECK_EQUAL(err.no_box, box.unpack(stake_account_name, _whale, N(whalefox), _whale, _whale));

BOOST_CHECK_EQUAL(err.missing_authority(_whale), box.unpack(stake_account_name, _whale, N(whalebox), _whale, _alice));
BOOST_CHECK_EQUAL(err.incorrect_owner, box.unpack(stake_account_name, _whale, N(whalebox), _alice, _whale));
BOOST_CHECK_EQUAL(success(), box.unpack(stake_account_name, _whale, N(whalebox), _whale, _whale));

produce_block();
BOOST_CHECK_EQUAL(success(), stake.delegateuse(_whale, _alice, token.from_amount(provided)));
Expand All @@ -139,6 +144,7 @@ BOOST_FIXTURE_TEST_CASE(stake_in_box_tests, cyber_box_tester) try {
BOOST_CHECK_EQUAL(success(), box.create(stake_account_name, _whale, N(whalebox)));

BOOST_CHECK_EQUAL(err.empty_transfer, box.transfer(stake_account_name, _whale, N(alicebox), _whale, _alice, "", _whale));
BOOST_CHECK_EQUAL(err.empty_unpack, box.unpack(stake_account_name, _whale, N(alicebox), _whale, _whale));

BOOST_CHECK_EQUAL(success(), stake.constrain(_whale, N(alicebox), asset(alice_box, token._symbol)));
BOOST_CHECK_EQUAL(success(), stake.constrain(_whale, N(bobbox), asset(bob_box, token._symbol)));
Expand All @@ -161,9 +167,9 @@ BOOST_FIXTURE_TEST_CASE(stake_in_box_tests, cyber_box_tester) try {
BOOST_CHECK_EQUAL(success(), box.transfer(stake_account_name, _whale, N(carolbox), _whale, _carol, "", _whale));
BOOST_CHECK_EQUAL(err.self_transfer, box.transfer(stake_account_name, _whale, N(whalebox), _whale, _whale, "", _whale));

BOOST_CHECK_EQUAL(err.missing_authority(_alice), box.unpack(stake_account_name, _whale, N(alicebox), _whale));
BOOST_CHECK_EQUAL(err.missing_authority(_alice), box.unpack(stake_account_name, _whale, N(alicebox), _alice, _whale));

BOOST_CHECK_EQUAL(err.no_balance, box.unpack(stake_account_name, _whale, N(alicebox), _alice));
BOOST_CHECK_EQUAL(err.no_balance, box.unpack(stake_account_name, _whale, N(alicebox), _alice, _alice));

BOOST_CHECK_EQUAL(success(), token.open(_alice, token._symbol, _alice));
BOOST_CHECK_EQUAL(success(), token.open(_bob, token._symbol, _bob));
Expand All @@ -172,22 +178,22 @@ BOOST_FIXTURE_TEST_CASE(stake_in_box_tests, cyber_box_tester) try {
BOOST_CHECK_EQUAL(stake.get_agent(_whale, token._symbol)["balance"], balance);
BOOST_CHECK_EQUAL(stake.get_agent(_whale, token._symbol)["proxied"], staked - balance);

BOOST_CHECK_EQUAL(success(), box.unpack(stake_account_name, _whale, N(alicebox), _alice));
BOOST_CHECK_EQUAL(success(), box.unpack(stake_account_name, _whale, N(alicebox), _alice, _alice));

BOOST_CHECK_EQUAL(alice_box, token.get_account(_alice)["balance"].as<asset>().get_amount());
BOOST_CHECK_EQUAL(stake.get_agent(_whale, token._symbol)["balance"], 0);
BOOST_CHECK_EQUAL(stake.get_agent(_whale, token._symbol)["proxied"], staked - alice_box);

BOOST_CHECK_EQUAL(success(), box.unpack(stake_account_name, _whale, N(bobbox), _bob));
BOOST_CHECK_EQUAL(success(), box.unpack(stake_account_name, _whale, N(bobbox), _bob, _bob));
BOOST_CHECK_EQUAL(bob_box, token.get_account(_bob)["balance"].as<asset>().get_amount());
BOOST_CHECK_EQUAL(stake.get_agent(_whale, token._symbol)["balance"], 0);
BOOST_CHECK_EQUAL(stake.get_agent(_whale, token._symbol)["proxied"], staked - alice_box - bob_box);

BOOST_CHECK_EQUAL(success(), box.unpack(stake_account_name, _whale, N(whalebox), _whale));
BOOST_CHECK_EQUAL(success(), box.unpack(stake_account_name, _whale, N(whalebox), _whale, _whale));
BOOST_CHECK_EQUAL(stake.get_agent(_whale, token._symbol)["balance"], 0);
BOOST_CHECK_EQUAL(stake.get_agent(_whale, token._symbol)["proxied"], staked - alice_box - bob_box);

BOOST_CHECK_EQUAL(success(), box.unpack(stake_account_name, _whale, N(carolbox), _carol));
BOOST_CHECK_EQUAL(success(), box.unpack(stake_account_name, _whale, N(carolbox), _carol, _carol));
BOOST_CHECK_EQUAL(carol_box, token.get_account(_carol)["balance"].as<asset>().get_amount());
BOOST_CHECK_EQUAL(stake.get_agent(_whale, token._symbol)["balance"], 0);
BOOST_CHECK_EQUAL(stake.get_agent(_whale, token._symbol)["proxied"], staked - alice_box - bob_box - carol_box);
Expand Down