Skip to content

Commit

Permalink
Mod genperiods to update all DAO's sharing calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerard097 committed Aug 30, 2023
1 parent 723c8b8 commit 13c9aac
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 21 deletions.
3 changes: 3 additions & 0 deletions include/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ namespace hypha::common
inline constexpr auto TESTNET_CONTRACT_NAME = name("mtdhoxhyphaa");
#endif

inline constexpr auto CALENDAR_WEEK = "weekly";
inline constexpr auto CALENDAR_LUNAR = "lunar";

inline constexpr auto PEG_TOKEN = "peg_token";
inline constexpr auto VOICE_TOKEN = "voice_token";
inline constexpr auto REWARD_TOKEN = "reward_token";
Expand Down
1 change: 1 addition & 0 deletions include/dao.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ namespace pricing {
//ACTION remsetting(const string &key);

ACTION genperiods(uint64_t dao_id, int64_t period_count/*, int64_t period_duration_sec*/);
ACTION initperiods(uint64_t dao_id, int64_t next_period);

ACTION claimnextper(uint64_t assignment_id);
// ACTION simclaimall(name account, uint64_t dao_id, bool only_ids);
Expand Down
4 changes: 4 additions & 0 deletions include/period.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <document_graph/document.hpp>

#include <optional>

namespace hypha
{
class dao;
Expand Down Expand Up @@ -36,6 +38,8 @@ namespace hypha

Period next();

std::optional<Period> nextOpt();

Period getNthPeriodAfter(int64_t count) const;
int64_t getPeriodCountTo(Period& other);
Period getPeriodUntil(eosio::time_point moment);
Expand Down
91 changes: 80 additions & 11 deletions src/dao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1811,18 +1811,27 @@ void dao::createdao(ContentGroups& config)
else {
readDaoSettings(daoDoc.getID(), dao, configCW, false);
}

// Create start & end edges
// Just point to the initial period of the selected calendar
std::string_view calendar = common::CALENDAR_WEEK;

if (auto [_, calendarType] = configCW.get(DETAILS, "calendary_type"); calendarType){
calendar = calendarType->getAs<std::string>();
}

//Create start period
Period newPeriod(this, eosio::current_time_point(), to_str(dao, " start period"));

//Create start & end edges
Edge(get_self(), get_self(), daoDoc.getID(), newPeriod.getID(), common::START);
Edge(get_self(), get_self(), daoDoc.getID(), newPeriod.getID(), common::END);
Edge(get_self(), get_self(), daoDoc.getID(), newPeriod.getID(), common::PERIOD);
Edge(get_self(), get_self(), newPeriod.getID(), daoDoc.getID(), common::DAO);
EOS_CHECK(
calendar == common::CALENDAR_WEEK ||
calendar == common::CALENDAR_LUNAR,
to_str("Unkonw calendar type: ", calendar)
);

uint64_t startPeriodId = Edge::get(get_self(), getRootID(), name(calendar)).getToNode();

Edge(get_self(), get_self(), daoDoc.getID(), startPeriodId, common::START);

#ifdef USE_TREASURY
//Create Treasury
// Create Treasury
eosio::action(
eosio::permission_level{ get_self(), name("active") },
get_self(),
Expand All @@ -1842,6 +1851,15 @@ void dao::createdao(ContentGroups& config)
)
).send();

eosio::action(
eosio::permission_level{ get_self(), name("active") },
get_self(),
name("initperiods"),
std::make_tuple(
daoDoc.getID(), startPeriodId
)
).send();

auto onboarder = getSettingsDocument(daoDoc.getID())->getOrFail<name>(common::ONBOARDER_ACCOUNT);

//Init core members should happen after scheduling setupdefs action
Expand Down Expand Up @@ -2775,7 +2793,7 @@ void dao::checkAdminsAuth(uint64_t dao_id)
void dao::genPeriods(uint64_t dao_id, int64_t period_count/*, int64_t period_duration_sec*/)
{
//Max number of periods that should be created in one call
const int64_t MAX_PERIODS_PER_CALL = 30;
const int64_t MAX_PERIODS_PER_CALL = 10;

//Get last period
//Document daoDoc(get_self(), dao_id);
Expand All @@ -2787,6 +2805,9 @@ void dao::genPeriods(uint64_t dao_id, int64_t period_count/*, int64_t period_dur
name daoName = settings->getOrFail<eosio::name>(DAO_NAME);

auto lastEdge = Edge::get(get_self(), dao_id, common::END);

auto updateDaos = getGraph().getEdgesFrom(lastEdge.getToNode(), common::DAO);

lastEdge.erase();

uint64_t lastPeriodID = lastEdge.getToNode();
Expand All @@ -2805,7 +2826,11 @@ void dao::genPeriods(uint64_t dao_id, int64_t period_count/*, int64_t period_dur
);

Edge(get_self(), get_self(), lastPeriodID, nextPeriod.getID(), common::NEXT);
Edge(get_self(), get_self(), dao_id, nextPeriod.getID(), common::PERIOD);

for (auto& updateDao : updateDaos) {
Edge(get_self(), get_self(), updateDao.getToNode(), nextPeriod.getID(), common::PERIOD);
}

Edge(get_self(), get_self(), nextPeriod.getID(), dao_id, common::DAO);

lastPeriodStartSecs = nextPeriodStart.sec_since_epoch();
Expand All @@ -2814,6 +2839,13 @@ void dao::genPeriods(uint64_t dao_id, int64_t period_count/*, int64_t period_dur

Edge(get_self(), get_self(), dao_id, lastPeriodID, common::END);

for (auto& updateDao : updateDaos) {
if (updateDao.getToNode() != dao_id) {
Edge(get_self(), get_self(), lastPeriodID, updateDao.getToNode(), common::DAO);
updateDao.erase();
}
}

//Check if there are more periods to created

if (period_count > MAX_PERIODS_PER_CALL) {
Expand Down Expand Up @@ -2849,6 +2881,43 @@ void dao::createVoiceToken(const eosio::name& daoName,
).send();
}

void dao::initperiods(uint64_t dao_id, int64_t next_period)
{
verifyDaoType(dao_id);

checkAdminsAuth(dao_id);

auto period = std::make_optional<Period>(this, next_period);

const size_t MAX_PERIODS_PER_CALL = 15;

int it = 0;

while (period && it++ < MAX_PERIODS_PER_CALL) {
Edge(get_self(), get_self(), dao_id, period->getID(), common::PERIOD);

auto prev = std::move(period);

//If next period is empty, then we reached the end of the period list
if (!(period = prev->next())) {
//Only the last period will create a reference to the DAO
Edge(get_self(), get_self(), prev->getID(), dao_id, common::DAO);
}
}

//We didn't finish
if (period) {
eosio::action(
eosio::permission_level{ get_self(), name("active") },
get_self(),
name("initperiods"),
std::make_tuple(
dao_id, period->getID()
)
).send();
}
}

void dao::createToken(const std::string& contractType, name issuer, const asset& token)
{
auto dhoSettings = getSettingsDocument();
Expand Down
15 changes: 13 additions & 2 deletions src/period.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,20 @@ namespace hypha
Period Period::next()
{
TRACE_FUNCTION()
auto nextPeriod = nextOpt();
EOS_CHECK(nextPeriod, "End of calendar has been reached. Contact administrator to add more time periods.");
return *nextPeriod;
}

std::optional<Period> Period::nextOpt()
{
auto [exists, period] = Edge::getIfExists(m_dao->get_self(), getID (), common::NEXT);
EOS_CHECK(exists, "End of calendar has been reached. Contact administrator to add more time periods.");
return Period(m_dao, period.getToNode());

if (exists) {
return std::make_optional<Period>(m_dao, period.getToNode());
}

return std::nullopt;
}

Period Period::getNthPeriodAfter(int64_t count) const
Expand Down
8 changes: 4 additions & 4 deletions src/proposals/assignment_proposal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ namespace hypha
// )

//TODO Period: Remove since period refactor will no longer point to DAO
EOS_CHECK(
Edge::exists(m_dao.get_self(), period.getID(), m_daoID, common::DAO),
"Period must belong to the DAO"
);
// EOS_CHECK(
// Edge::exists(m_dao.get_self(), period.getID(), m_daoID, common::DAO),
// "Period must belong to the DAO"
// );
} else {
// default START_PERIOD to next period
ContentWrapper::insertOrReplace(
Expand Down
8 changes: 4 additions & 4 deletions src/proposals/badge_assignment_proposal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ namespace hypha
);

//TODO Period: Remove since period refactor will no longer point to DAO
EOS_CHECK(
Edge::exists(m_dao.get_self(), period.getID(), m_daoID, common::DAO),
"Period must belong to the DAO"
);
// EOS_CHECK(
// Edge::exists(m_dao.get_self(), period.getID(), m_daoID, common::DAO),
// "Period must belong to the DAO"
// );
}
else {
// default START_PERIOD to next period
Expand Down

0 comments on commit 13c9aac

Please sign in to comment.