Skip to content

Commit

Permalink
Merge pull request #101 from edenia/dev
Browse files Browse the repository at this point in the history
Merge dev to main
  • Loading branch information
AngeloCG97 authored Apr 21, 2023
2 parents f64cca5 + 6fdb5b2 commit e6c6c85
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 16 deletions.
9 changes: 9 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -e

mkdir -p build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_C_COMPILER_LAUNCHER=ccache ..
make -j $(nproc)
ctest -j10 -V --rerun-failed --output-on-failure
cd ..
1 change: 1 addition & 0 deletions contracts/eden/include/distributions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ namespace eden
uint32_t on_election_kick(eosio::name member, uint32_t max_steps);
void on_resign(const member& member);
void on_rename(eosio::name old_account, eosio::name new_account);
uint32_t on_collectfunds(uint32_t max_steps);
void clear_all();
};
} // namespace eden
4 changes: 4 additions & 0 deletions contracts/eden/include/eden.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ namespace eden
void givesbt(uint32_t max_steps);

void setdistpct(uint8_t pct);
void collectfunds(uint32_t max_steps);
void setcoltime(uint8_t months);

void fundtransfer(eosio::name from,
eosio::block_timestamp distribution_time,
Expand Down Expand Up @@ -297,6 +299,8 @@ namespace eden
action(distribute, max_steps),
action(givesbt, max_steps),
action(setdistpct, pct),
action(collectfunds, max_steps),
action(setcoltime, months),
action(inductdonate, payer, id, quantity, ricardian_contract(inductdonate_ricardian)),
eden_verb(inductcancel, 9, account, id, ricardian_contract(inductcancel_ricardian)),
action(inducted, inductee, ricardian_contract(inducted_ricardian)),
Expand Down
22 changes: 15 additions & 7 deletions contracts/eden/include/globals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace eden
active
};

struct global_data_v1;
struct global_data_v2;

struct global_data_v0
{
Expand All @@ -23,7 +23,7 @@ namespace eden
eosio::asset auction_starting_bid;
uint32_t auction_duration;
contract_stage_type stage;
global_data_v1 upgrade() const;
global_data_v2 upgrade() const;
};
EOSIO_REFLECT(global_data_v0,
community,
Expand All @@ -36,11 +36,18 @@ namespace eden
{
uint32_t election_start_time = 0xffffffffu; // seconds from the start of Sunday
uint32_t election_round_time_sec = 60 * 60;
auto upgrade() const { return *this; }
global_data_v2 upgrade() const;
};
EOSIO_REFLECT(global_data_v1, base global_data_v0, election_start_time, election_round_time_sec);

using global_variant = std::variant<global_data_v0, global_data_v1>;
struct global_data_v2 : global_data_v1
{
uint8_t max_month_withdraw = 3;
global_data_v2 upgrade() const { return *this; }
};
EOSIO_REFLECT(global_data_v2, base global_data_v1, max_month_withdraw);

using global_variant = std::variant<global_data_v0, global_data_v1, global_data_v2>;
using global_singleton = eosio::singleton<"global"_n, global_variant>;

global_singleton& get_global_singleton(eosio::name contract);
Expand All @@ -52,17 +59,18 @@ namespace eden
{
private:
eosio::name contract;
global_data_v1 data;
global_data_v2 data;

public:
explicit globals(eosio::name contract);
explicit globals(eosio::name contract, const global_data_v1& initial_value);
const global_data_v1& get() { return data; }
explicit globals(eosio::name contract, const global_data_v2& initial_value);
const global_data_v2& get() { return data; }
void check_active() const;
eosio::symbol default_token() const { return data.minimum_donation.symbol; }
void set_stage(contract_stage stage);
void set_election_start_time(uint32_t time);
void set_election_round_duration(uint32_t duration);
void set_minimum_donation_fee(eosio::asset new_minimum_donation);
void set_max_month_withdraw(uint8_t months);
};
} // namespace eden
19 changes: 18 additions & 1 deletion contracts/eden/src/actions/distributions.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <eden.hpp>
#include <accounts.hpp>
#include <distributions.hpp>
#include <eden.hpp>
#include <globals.hpp>

namespace eden
{
Expand All @@ -11,4 +13,19 @@ namespace eden
set_distribution_pct(get_self(), pct);
}

void eden::collectfunds(uint32_t max_steps)
{
eosio::check(distributions{get_self()}.on_collectfunds(max_steps) != max_steps,
"Nothing to do");
}

void eden::setcoltime(uint8_t months)
{
require_auth(get_self());

eosio::check(months >= 1 && months <= 3,
"Proposed collecting time is out of the valid range");
globals{get_self()}.set_max_month_withdraw(months);
}

} // namespace eden
10 changes: 5 additions & 5 deletions contracts/eden/src/actions/genesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ namespace eden
migrations{get_self()}.init();

globals{get_self(),
{{.community = community,
.minimum_donation = minimum_donation,
.auction_starting_bid = auction_starting_bid,
.auction_duration = auction_duration,
.stage = contract_stage::genesis}}};
{{{.community = community,
.minimum_donation = minimum_donation,
.auction_starting_bid = auction_starting_bid,
.auction_duration = auction_duration,
.stage = contract_stage::genesis}}}};
members members{get_self()};
inductions inductions{get_self()};

Expand Down
2 changes: 1 addition & 1 deletion contracts/eden/src/badges.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace eden
eosio::action{{contract, "active"_n},
sbt_account,
"givesimple"_n,
std::tuple(contract, "epv"_n, contract, it->account(),
std::tuple(contract, "epvi"_n, contract, it->account(),
std::string("round " + std::to_string(it->round()) + ", " +
std::to_string(it->vote_time().sec_since_epoch())))}
.send();
Expand Down
38 changes: 38 additions & 0 deletions contracts/eden/src/distributions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <distributions.hpp>
#include <elections.hpp>
#include <events.hpp>
#include <globals.hpp>
#include <members.hpp>
#include <numeric>

Expand Down Expand Up @@ -501,6 +502,43 @@ namespace eden
}
}

uint32_t distributions::on_collectfunds(uint32_t max_steps)
{
uint32_t copy_max_steps = max_steps;
bool has_returned = false;
auto months_to_withdraw = globals{contract}.get().max_month_withdraw;

accounts owned_accounts{contract, "owned"_n};
setup_distribution(contract, owned_accounts);
for (auto iter = distribution_account_tb.begin(), end = distribution_account_tb.end();
max_steps > 0 && iter != end; --max_steps)
{
if (iter->distribution_time() <
eosio::current_time_point() - eosio::days(30 * months_to_withdraw))
{
push_event(
distribution_event_return{
.owner = iter->owner(),
.distribution_time = iter->distribution_time(),
.rank = iter->rank(),
.amount = iter->balance(),
.pool = "master"_n,
},
contract);
owned_accounts.add_balance("master"_n, iter->balance(), false);
iter = distribution_account_tb.erase(iter);

has_returned = true;
}
else
{
++iter;
}
}

return has_returned ? max_steps : copy_max_steps;
}

void distributions::clear_all()
{
clear_table(distribution_account_tb);
Expand Down
18 changes: 16 additions & 2 deletions contracts/eden/src/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@

namespace eden
{
global_data_v1 global_data_v0::upgrade() const { return {*this}; }
global_data_v2 global_data_v0::upgrade() const
{
return {{*this}};
}

global_data_v2 global_data_v1::upgrade() const
{
return {*this};
}

static std::optional<global_singleton> global_singleton_inst;

Expand All @@ -23,7 +31,7 @@ namespace eden
{
}

globals::globals(eosio::name contract, const global_data_v1& initial_value)
globals::globals(eosio::name contract, const global_data_v2& initial_value)
: contract(contract), data(initial_value)
{
auto& singleton = get_global_singleton(contract);
Expand Down Expand Up @@ -59,4 +67,10 @@ namespace eden
data.minimum_donation = new_minimum_donation;
get_global_singleton(contract).set(data, eosio::same_payer);
}

void globals::set_max_month_withdraw(uint8_t months)
{
data.max_month_withdraw = months;
get_global_singleton(contract).set(data, eosio::same_payer);
}
} // namespace eden
74 changes: 74 additions & 0 deletions contracts/eden/tests/test-eden.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,80 @@ TEST_CASE("clearall")
t.genesis();
}

TEST_CASE("migrate to include max_month_withdraw")
{
eden_tester t;
t.genesis();

const uint8_t default_max_month_withdraw = 3;

CHECK(get_globals().max_month_withdraw == default_max_month_withdraw);
}

TEST_CASE("change the max month to transfer funds")
{
eden_tester t;
t.genesis();

const uint8_t default_max_month_withdraw = 3;
const uint8_t new_max_month_withdraw = 1;

CHECK(get_globals().max_month_withdraw == default_max_month_withdraw);
expect(t.eden_gm.trace<actions::setcoltime>(0),
"Proposed collecting time is out of the valid range");
expect(t.eden_gm.trace<actions::setcoltime>(4),
"Proposed collecting time is out of the valid range");
t.eden_gm.act<actions::setcoltime>(new_max_month_withdraw);
CHECK(get_globals().max_month_withdraw == new_max_month_withdraw);
}

TEST_CASE("return funds to master by collecting them")
{
eden_tester t;
t.genesis();
t.set_balance(s2a("36.0000 EOS"));

t.run_election();

t.eden_gm.act<actions::setcoltime>(2);
t.egeon.act<actions::distribute>(250);
t.skip_to("2020-05-04T15:30:00.000");
t.egeon.act<actions::distribute>(250);
t.skip_to("2020-07-04T15:30:00.000");
t.egeon.act<actions::distribute>(250);

std::map<eosio::block_timestamp, eosio::asset> expected{
{s2t("2020-04-04T15:30:00.000"), s2a("1.8000 EOS")},
{s2t("2020-05-04T15:30:00.000"), s2a("1.7100 EOS")},
{s2t("2020-06-03T15:30:00.000"), s2a("1.6245 EOS")},
{s2t("2020-07-03T15:30:00.000"), s2a("0.0514 EOS")},
{s2t("2020-07-04T15:30:00.000"), s2a("1.5407 EOS")}};

// remove because the max time to collect the funds is 2 months
t.egeon.act<actions::collectfunds>(100);
t.chain.start_block();
expect(t.egeon.trace<actions::collectfunds>(100), "Nothing to do");
expected.erase(s2t("2020-04-04T15:30:00.000"));
expected.erase(s2t("2020-05-04T15:30:00.000"));
// validate funds are returned to master: 29.2734 EOS + 1.8000 EOS + 1.7100 EOS = 32.7834 EOS
CHECK(t.get_budgets_by_period() == expected);
CHECK(accounts{"eden.gm"_n, "owned"_n}.get_account("master"_n)->balance() == s2a("32.7834 EOS"));

t.skip_to("2020-08-03T15:30:00.000");
t.egeon.act<actions::collectfunds>(100);
// from 2020-06-03T15:30:00.000 to 2020-08-03T15:30:00.000 there are 61 days
expected.erase(s2t("2020-06-03T15:30:00.000"));
expected[s2t("2020-08-03T15:30:00.000")] = s2a("1.6391 EOS");
CHECK(t.get_budgets_by_period() == expected);
CHECK(accounts{"eden.gm"_n, "owned"_n}.get_account("master"_n)->balance() == s2a("32.7688 EOS"));

t.eden_gm.act<actions::setcoltime>(3);
t.skip_to("2020-09-02T15:30:00.000");
expect(t.egeon.trace<actions::collectfunds>(100), "Nothing to do");
// no deletion is required since the max month to withdraw funds has changed to 90 days
// and no distribution is done since no funds to return are available
}

TEST_CASE("account migration")
{
eden_tester t;
Expand Down

0 comments on commit e6c6c85

Please sign in to comment.