Skip to content

Commit

Permalink
Merge pull request #73 from Alien-Worlds/better-error-message
Browse files Browse the repository at this point in the history
eosdactokens::stake improve error message
  • Loading branch information
angelol authored Oct 19, 2022
2 parents fabd912 + ef503af commit 756bc8f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
42 changes: 42 additions & 0 deletions contract-shared-headers/eosdactokens_shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,48 @@ namespace eosdac {
return liquid;
}

std::pair<asset, string> get_liquid_debug(name owner, name code, symbol sym) {

std::string debug_output = "";

// Hardcoding a precision of 4, it doesnt matter because the index ignores precision
dacdir::dac dac = dacdir::dac_for_symbol(extended_symbol{sym, code});

stakes_table stakes(code, dac.dac_id.value);
unstakes_table unstakes(code, dac.dac_id.value);
auto unstakes_idx = unstakes.get_index<"byaccount"_n>();

asset liquid = get_balance(owner, code, sym.code());
debug_output += fmt("get_balance(%s, %s, %s) = %s | ", owner, code, sym.code(), liquid);
auto existing_stake = stakes.find(owner.value);
if (existing_stake != stakes.end()) {
debug_output += fmt("reducing by existing_stake->stake: %s | ", existing_stake->stake);
liquid -= existing_stake->stake;
}
debug_output += fmt("liquid is now: %s | ", liquid);

auto unstakes_itr = unstakes_idx.find(owner.value);
while (unstakes_itr != unstakes_idx.end()) {
if (unstakes_itr->released()) {
print("this is already released, erasing");
// if this unstake is already released, it can be safely deleted
debug_output += fmt("deleting stake %s | ", unstakes_itr->stake);
unstakes_itr = unstakes_idx.erase(unstakes_itr);
} else {
print("NOT yet released");

// otherwise it still negatively impacts the liquid balance
debug_output += fmt("reducing by unstakes_itr->stake: %s | ", unstakes_itr->stake);
liquid -= unstakes_itr->stake;
debug_output += fmt("liquid is now: %s | ", liquid);
unstakes_itr++;
}
}
debug_output += fmt("while loop ended liquid is now: %s", liquid);

return {liquid, debug_output};
}

asset get_staked(name owner, name code, symbol sym) {
// Hardcoding a precision of 4, it doesnt matter because the index ignores precision
dacdir::dac dac = dacdir::dac_for_symbol(extended_symbol{sym, code});
Expand Down
6 changes: 4 additions & 2 deletions contracts/eosdactokens/eosdactokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,11 @@ namespace eosdac {
check(quantity.is_valid(), "ERR::STAKE_INVALID_QTY::Invalid quantity supplied");
check(quantity.amount > 0, "ERR::STAKE_NON_POSITIVE_QTY::Stake amount must be greater than 0");

asset liquid = eosdac::get_liquid(account, get_self(), quantity.symbol);
auto [liquid, debug_output] = eosdac::get_liquid_debug(account, get_self(), quantity.symbol);

check(liquid >= quantity, "ERR::STAKE_MORE_LIQUID::Attempting to stake more than your liquid balance");
check(liquid >= quantity,
"ERR::STAKE_MORE_LIQUID::Attempting to stake %s but your liquid balance is only %s | %s", quantity, liquid,
debug_output);

add_stake(account, quantity, dac.dac_id, account);

Expand Down

0 comments on commit 756bc8f

Please sign in to comment.