Skip to content

Commit

Permalink
DFIP2203 updates (#1163)
Browse files Browse the repository at this point in the history
* Add refund to listfutureswaphistory

* Update getburninfo to only display future swap balance after execution

* Allow future swap destination token to be set with name and int

* Add burn entry to Gov vars live

* Remove future swap history, add history to account history.

* tests: Reduce duplication
  • Loading branch information
Bushstar authored Mar 30, 2022
1 parent bbaedcb commit ad82928
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 337 deletions.
24 changes: 5 additions & 19 deletions src/masternodes/accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ uint32_t CAccountsView::GetBalancesHeight(CScript const & owner)

Res CAccountsView::StoreFuturesUserValues(const CFuturesUserKey& key, const CFuturesUserValue& futures)
{
if (!WriteBy<ByFuturesSourceKey>(key, futures)) {
if (!WriteBy<ByFuturesSwapKey>(key, futures)) {
return Res::Err("Failed to store futures");
}

Expand All @@ -110,12 +110,12 @@ Res CAccountsView::StoreFuturesUserValues(const CFuturesUserKey& key, const CFut

void CAccountsView::ForEachFuturesUserValues(std::function<bool(const CFuturesUserKey&, const CFuturesUserValue&)> callback, const CFuturesUserKey& start)
{
ForEach<ByFuturesSourceKey, CFuturesUserKey, CFuturesUserValue>(callback, start);
ForEach<ByFuturesSwapKey, CFuturesUserKey, CFuturesUserValue>(callback, start);
}

Res CAccountsView::EraseFuturesUserValues(const CFuturesUserKey& key)
{
if (!EraseBy<ByFuturesSourceKey>(key)) {
if (!EraseBy<ByFuturesSwapKey>(key)) {
return Res::Err("Failed to erase futures");
}

Expand All @@ -125,33 +125,19 @@ Res CAccountsView::EraseFuturesUserValues(const CFuturesUserKey& key)
boost::optional<uint32_t> CAccountsView::GetMostRecentFuturesHeight()
{
const CFuturesUserKey key{std::numeric_limits<uint32_t>::max(), {}, std::numeric_limits<uint32_t>::max()};
auto it = LowerBound<ByFuturesSourceKey>(key);
auto it = LowerBound<ByFuturesSwapKey>(key);
if (it.Valid()) {
return it.Key().height;
}

return {};
}

Res CAccountsView::StoreFuturesDestValues(const CFuturesUserKey& key, const CFuturesUserValue& destination)
{
if (!WriteBy<ByFuturesDestKey>(key, destination)) {
return Res::Err("Failed to store futures destination");
}

return Res::Ok();
}

ResVal<CFuturesUserValue> CAccountsView::GetFuturesUserValues(const CFuturesUserKey& key) {
CFuturesUserValue source;
if (!ReadBy<ByFuturesSourceKey>(key, source)) {
if (!ReadBy<ByFuturesSwapKey>(key, source)) {
return Res::Err("Failed to read futures source");
}

return {source, Res::Ok()};
}

void CAccountsView::ForEachFuturesDestValues(std::function<bool(const CFuturesUserKey&, const CFuturesUserValue&)> callback, const CFuturesUserKey& start)
{
ForEach<ByFuturesDestKey, CFuturesUserKey, CFuturesUserValue>(callback, start);
}
5 changes: 1 addition & 4 deletions src/masternodes/accounts.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,15 @@ class CAccountsView : public virtual CStorageView
Res UpdateBalancesHeight(CScript const & owner, uint32_t height);

Res StoreFuturesUserValues(const CFuturesUserKey& key, const CFuturesUserValue& futures);
Res StoreFuturesDestValues(const CFuturesUserKey& key, const CFuturesUserValue& destination);
ResVal<CFuturesUserValue> GetFuturesUserValues(const CFuturesUserKey& key);
Res EraseFuturesUserValues(const CFuturesUserKey& key);
boost::optional<uint32_t> GetMostRecentFuturesHeight();
void ForEachFuturesUserValues(std::function<bool(const CFuturesUserKey&, const CFuturesUserValue&)> callback, const CFuturesUserKey& start = {});
void ForEachFuturesDestValues(std::function<bool(const CFuturesUserKey&, const CFuturesUserValue&)> callback, const CFuturesUserKey& start = {});

// tags
struct ByBalanceKey { static constexpr uint8_t prefix() { return 'a'; } };
struct ByHeightKey { static constexpr uint8_t prefix() { return 'b'; } };
struct ByFuturesSourceKey { static constexpr uint8_t prefix() { return 'J'; } };
struct ByFuturesDestKey { static constexpr uint8_t prefix() { return 's'; } };
struct ByFuturesSwapKey { static constexpr uint8_t prefix() { return 'J'; } };

private:
Res SetBalance(CScript const & owner, CTokenAmount amount);
Expand Down
34 changes: 16 additions & 18 deletions src/masternodes/govvariables/attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

#include <masternodes/govvariables/attributes.h>

#include <core_io.h> /// ValueFromAmount
#include <masternodes/accountshistory.h> /// CAccountsHistoryWriter
#include <masternodes/masternodes.h> /// CCustomCSView
#include <masternodes/mn_checks.h> /// CustomTxType

#include <core_io.h> /// ValueFromAmount
#include <util/strencodings.h>

extern UniValue AmountsToJSON(TAmounts const & diffs);
Expand Down Expand Up @@ -146,7 +149,8 @@ const std::map<uint8_t, std::map<uint8_t, std::string>>& ATTRIBUTES::displayKeys
{
AttributeTypes::Live, {
{EconomyKeys::PaybackDFITokens, "dfi_payback_tokens"},
{EconomyKeys::DFIP2203Tokens, "dfip_tokens"},
{EconomyKeys::DFIP2203Current, "dfip2203_current"},
{EconomyKeys::DFIP2203Burned, "dfip2203_burned"},
}
},
};
Expand Down Expand Up @@ -370,14 +374,9 @@ Res ATTRIBUTES::RefundFuturesContracts(CCustomCSView &mnview, const uint32_t hei
return Res::Ok();
}

const uint32_t startHeight = height - (height % blockPeriod);
std::map<CFuturesUserKey, CFuturesUserValue> userFuturesValues;

mnview.ForEachFuturesUserValues([&](const CFuturesUserKey& key, const CFuturesUserValue& futuresValues) {
if (key.height <= startHeight) {
return false;
}

if (tokenID != std::numeric_limits<uint32_t>::max()) {
if (futuresValues.source.nTokenId.v == tokenID || futuresValues.destination == tokenID) {
userFuturesValues[key] = futuresValues;
Expand All @@ -394,18 +393,22 @@ Res ATTRIBUTES::RefundFuturesContracts(CCustomCSView &mnview, const uint32_t hei
return contractAddressValue;
}

CDataStructureV0 liveKey{AttributeTypes::Live, ParamIDs::Economy, EconomyKeys::DFIP2203Tokens};
CDataStructureV0 liveKey{AttributeTypes::Live, ParamIDs::Economy, EconomyKeys::DFIP2203Current};
auto balances = GetValue(liveKey, CBalances{});


CHistoryWriters writers{paccountHistoryDB.get(), nullptr, nullptr};
CAccountsHistoryWriter view(mnview, height, ~0u, {}, uint8_t(CustomTxType::FutureSwapRefund), &writers);

for (const auto& [key, value] : userFuturesValues) {
mnview.EraseFuturesUserValues(key);
view.EraseFuturesUserValues(key);

auto res = mnview.SubBalance(*contractAddressValue, value.source);
auto res = view.SubBalance(*contractAddressValue, value.source);
if (!res) {
return res;
}

res = mnview.AddBalance(key.owner, value.source);
res = view.AddBalance(key.owner, value.source);
if (!res) {
return res;
}
Expand All @@ -416,6 +419,8 @@ Res ATTRIBUTES::RefundFuturesContracts(CCustomCSView &mnview, const uint32_t hei
}
}

view.Flush();

attributes[liveKey] = balances;

return Res::Ok();
Expand Down Expand Up @@ -685,13 +690,6 @@ Res ATTRIBUTES::Apply(CCustomCSView & mnview, const uint32_t height)
if (GetValue(activeKey, false)) {
return Res::Err("Cannot set block period while DFIP2203 is active");
}

auto blockPeriod = boost::get<CAmount>(attribute.second);
const auto recentFuturesHeight = mnview.GetMostRecentFuturesHeight();

if (recentFuturesHeight && *recentFuturesHeight > height - (height % blockPeriod)) {
return Res::Err("Historical Futures contracts in this period");
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/masternodes/govvariables/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ enum ParamIDs : uint8_t {
enum EconomyKeys : uint8_t {
PaybackDFITokens = 'a',
PaybackTokens = 'b',
DFIP2203Tokens = 'c',
DFIP2203Current = 'c',
DFIP2203Burned = 'd',
};

enum DFIPKeys : uint8_t {
Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/masternodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ class CCustomCSView
CFoundationsDebtView :: Debt,
CAnchorRewardsView :: BtcTx,
CTokensView :: ID, Symbol, CreationTx, LastDctId,
CAccountsView :: ByBalanceKey, ByHeightKey, ByFuturesSourceKey, ByFuturesDestKey,
CAccountsView :: ByBalanceKey, ByHeightKey, ByFuturesSwapKey,
CCommunityBalancesView :: ById,
CUndosView :: ByUndoKey,
CPoolPairView :: ByID, ByPair, ByShare, ByIDPair, ByPoolSwap, ByReserves, ByRewardPct, ByRewardLoanPct,
Expand Down
12 changes: 5 additions & 7 deletions src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ std::string ToString(CustomTxType type) {
case CustomTxType::PaybackLoan: return "PaybackLoan";
case CustomTxType::PaybackLoanV2: return "PaybackLoan";
case CustomTxType::AuctionBid: return "AuctionBid";
case CustomTxType::FutureSwapExecution: return "FutureSwapExecution";
case CustomTxType::FutureSwapRefund: return "FutureSwapRefund";
case CustomTxType::Reject: return "Reject";
case CustomTxType::None: return "None";
}
Expand Down Expand Up @@ -162,6 +164,8 @@ CCustomTxMessage customTypeToMessage(CustomTxType txType) {
case CustomTxType::PaybackLoan: return CLoanPaybackLoanMessage{};
case CustomTxType::PaybackLoanV2: return CLoanPaybackLoanV2Message{};
case CustomTxType::AuctionBid: return CAuctionBidMessage{};
case CustomTxType::FutureSwapExecution: return CCustomTxMessageNone{};
case CustomTxType::FutureSwapRefund: return CCustomTxMessageNone{};
case CustomTxType::Reject: return CCustomTxMessageNone{};
case CustomTxType::None: return CCustomTxMessageNone{};
}
Expand Down Expand Up @@ -1520,19 +1524,13 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor
return contractAddressValue;
}

CDataStructureV0 liveKey{AttributeTypes::Live, ParamIDs::Economy, EconomyKeys::DFIP2203Tokens};
CDataStructureV0 liveKey{AttributeTypes::Live, ParamIDs::Economy, EconomyKeys::DFIP2203Current};
auto balances = attributes->GetValue(liveKey, CBalances{});

if (obj.withdraw) {
const auto blockPeriod = attributes->GetValue(blockKey, CAmount{});
const uint32_t startHeight = height - (height % blockPeriod);
std::map<CFuturesUserKey, CFuturesUserValue> userFuturesValues;

mnview.ForEachFuturesUserValues([&](const CFuturesUserKey& key, const CFuturesUserValue& futuresValues) {
if (key.height <= startHeight) {
return false;
}

if (key.owner == obj.owner &&
futuresValues.source.nTokenId == obj.source.nTokenId &&
futuresValues.destination == obj.destination) {
Expand Down
6 changes: 5 additions & 1 deletion src/masternodes/mn_checks.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ enum class CustomTxType : uint8_t
TakeLoan = 'X',
PaybackLoan = 'H',
PaybackLoanV2 = 'k',
AuctionBid = 'I'
AuctionBid = 'I',
FutureSwapExecution = 'q',
FutureSwapRefund = 'w',
};

inline CustomTxType CustomTxCodeToType(uint8_t ch) {
Expand Down Expand Up @@ -157,6 +159,8 @@ inline CustomTxType CustomTxCodeToType(uint8_t ch) {
case CustomTxType::PaybackLoan:
case CustomTxType::PaybackLoanV2:
case CustomTxType::AuctionBid:
case CustomTxType::FutureSwapExecution:
case CustomTxType::FutureSwapRefund:
case CustomTxType::Reject:
case CustomTxType::None:
return type;
Expand Down
4 changes: 2 additions & 2 deletions src/masternodes/mn_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ CWalletCoinsUnlocker GetWallet(const JSONRPCRequest& request) {
return CWalletCoinsUnlocker{std::move(wallet)};
}

std::optional<std::pair<CAmount, CAmount>> GetFuturesBlockAndReward()
std::optional<CAmount> GetFuturesBlock()
{
LOCK(cs_main);

Expand All @@ -474,7 +474,7 @@ std::optional<std::pair<CAmount, CAmount>> GetFuturesBlockAndReward()
return {};
}

return std::pair{attributes->GetValue(blockKey, CAmount{}), attributes->GetValue(rewardKey, CAmount{})};
return attributes->GetValue(blockKey, CAmount{});
}

UniValue setgov(const JSONRPCRequest& request) {
Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/mn_rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ CAccounts SelectAccountsByTargetBalances(const CAccounts& accounts, const CBalan
void execTestTx(const CTransaction& tx, uint32_t height, CTransactionRef optAuthTx = {});
CScript CreateScriptForHTLC(const JSONRPCRequest& request, uint32_t &blocks, std::vector<unsigned char>& image);
CPubKey PublickeyFromString(const std::string &pubkey);
std::optional<std::pair<CAmount, CAmount>> GetFuturesBlockAndReward();
std::optional<CAmount> GetFuturesBlock();

#endif // DEFI_MASTERNODES_MN_RPC_H
Loading

0 comments on commit ad82928

Please sign in to comment.