diff --git a/contract-shared-headers/eosdactokens_shared.hpp b/contract-shared-headers/eosdactokens_shared.hpp index 684267e5..c44b5203 100644 --- a/contract-shared-headers/eosdactokens_shared.hpp +++ b/contract-shared-headers/eosdactokens_shared.hpp @@ -198,6 +198,48 @@ namespace eosdac { return liquid; } + std::pair 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}); diff --git a/contracts/eosdactokens/eosdactokens.cpp b/contracts/eosdactokens/eosdactokens.cpp index 20bedb25..28fd7c12 100644 --- a/contracts/eosdactokens/eosdactokens.cpp +++ b/contracts/eosdactokens/eosdactokens.cpp @@ -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);