Skip to content

Commit

Permalink
simplify redeem - add dao id to the memo
Browse files Browse the repository at this point in the history
  • Loading branch information
n13 committed May 27, 2024
1 parent b9242f0 commit 113158f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 55 deletions.
12 changes: 10 additions & 2 deletions include/dao.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,15 @@ namespace pricing {
to == get_self() &&
from != get_self()) {
if (memo == "redeem") {
onCashTokenTransfer(from, to, quantity, memo);
EOS_CHECK(false, "redeem must have a dao id specified like this: redeem,[daoId]");
}

auto prefix_length = 7;

if (memo.substr(0, prefix_length) == "redeem,") {
string number_str = memo.substr(prefix_length);
uint64_t daoId = stoi(number_str);
onCashTokenTransfer(daoId, from, to, quantity, memo);
}
else {
EOS_CHECK(
Expand Down Expand Up @@ -555,7 +563,7 @@ namespace pricing {
std::optional<TimeShare>& lastUsedTimeShare,
int64_t initTimeShare);

void onCashTokenTransfer(const name& from, const name& to, const asset& quantity, const string& memo);
void onCashTokenTransfer(uint64_t dao_id, const name& from, const name& to, const asset& quantity, const string& memo);

void onNativeTokenTransfer(const name& from, const name& to, const asset& quantity, const string& memo);

Expand Down
57 changes: 4 additions & 53 deletions src/treasury/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ void dao::onNativeTokenTransfer(const name& from, const name& to, const asset& q
#endif
}

void dao::onCashTokenTransfer(const name& from, const name& to, const asset& quantity, const string& memo)
void dao::onCashTokenTransfer(uint64_t dao_id, const name& from, const name& to, const asset& quantity, const string& memo)
{
#ifndef USE_TREASURY
EOS_CHECK(
Expand All @@ -486,65 +486,16 @@ void dao::onCashTokenTransfer(const name& from, const name& to, const asset& qua
EOS_CHECK(quantity.amount > 0, "quantity must be > 0");
EOS_CHECK(quantity.is_valid(), "quantity invalid");

//Since the symbols must be unique for each Cash token we can use the
//raw value as the edge name
name lookupEdgeName = name(quantity.symbol.raw());

//This would be a very weird scenario where the symbol raw value
//equals the edge name 'dao' which would cause unexpected behaviour
if (lookupEdgeName == common::DAO) {
// auto settings = getSettingsDocument();
// settings->setSetting(
// "errors",
// Content{ "cash_critital_error", to_str("Symbol raw value colapses with 'dao' edge name:", quantity) }
// );
EOS_CHECK(
false,
to_str("Symbol raw value colapses with 'dao' edge name:", quantity)
)

return;
}

uint64_t daoID = 0;

auto rootID = getRootID();
//Fast lookup
if (auto [exists, edge] = Edge::getIfExists(get_self(), rootID, lookupEdgeName); exists) {
daoID = edge.getToNode();
}
//We have to find which DAO this token belongs to (if any)
else {
auto daoEdges = getGraph().getEdgesFrom(rootID, common::DAO);

for (auto& edge : daoEdges) {
auto daoSettings = getSettingsDocument(edge.getToNode());

//Some DAO's might not have a peg token so let's use the default asset{}
if (daoSettings->getSettingOrDefault<asset>(common::PEG_TOKEN).symbol == quantity.symbol) {
//If we find it, let's create a lookup edge for the next time we get this symbol
daoID = edge.getToNode();
Edge(get_self(), get_self(), rootID, daoID, lookupEdgeName);
break;
}
}
}

EOS_CHECK(
daoID != 0,
"No DAO uses the transfered token"
);

verifyDaoType(daoID);
verifyDaoType(dao_id);

EOS_CHECK(
Member::isMember(*this, daoID, from),
Member::isMember(*this, dao_id, from),
"Sender is not a member of the DAO"
)

auto member = Member(*this, getMemberID(from));

Balance memberBal = Balance::getOrCreate(*this, daoID, member.getID());
Balance memberBal = Balance::getOrCreate(*this, dao_id, member.getID());

memberBal.add(quantity);

Expand Down

0 comments on commit 113158f

Please sign in to comment.