Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DFI2203 Futures #1155

Merged
merged 12 commits into from
Mar 28, 2022
4 changes: 4 additions & 0 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ class CMainParams : public CChainParams {

consensus.smartContracts.clear();
consensus.smartContracts[SMART_CONTRACT_DFIP_2201] = GetScriptForDestination(CTxDestination(WitnessV0KeyHash(std::vector<unsigned char>{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0})));
consensus.smartContracts[SMART_CONTRACT_DFIP_2203] = GetScriptForDestination(CTxDestination(WitnessV0KeyHash(std::vector<unsigned char>{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1})));

// owner base58, operator base58
vMasternodes.push_back({"8PuErAcazqccCVzRcc8vJ3wFaZGm4vFbLe", "8J846CKFF83Jcj5m4EReJmxiaJ6Jy1Y6Ea"});
Expand Down Expand Up @@ -462,6 +463,7 @@ class CTestNetParams : public CChainParams {

consensus.smartContracts.clear();
consensus.smartContracts[SMART_CONTRACT_DFIP_2201] = GetScriptForDestination(CTxDestination(WitnessV0KeyHash(std::vector<unsigned char>{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0})));
consensus.smartContracts[SMART_CONTRACT_DFIP_2203] = GetScriptForDestination(CTxDestination(WitnessV0KeyHash(std::vector<unsigned char>{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1})));

// owner base58, operator base58
vMasternodes.push_back({"7LMorkhKTDjbES6DfRxX2RiNMbeemUkxmp", "7KEu9JMKCx6aJ9wyg138W3p42rjg19DR5D"});
Expand Down Expand Up @@ -644,6 +646,7 @@ class CDevNetParams : public CChainParams {

consensus.smartContracts.clear();
consensus.smartContracts[SMART_CONTRACT_DFIP_2201] = GetScriptForDestination(CTxDestination(WitnessV0KeyHash(std::vector<unsigned char>{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0})));
consensus.smartContracts[SMART_CONTRACT_DFIP_2203] = GetScriptForDestination(CTxDestination(WitnessV0KeyHash(std::vector<unsigned char>{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1})));

// owner base58, operator base58
vMasternodes.push_back({"7M3g9CSERjLdXisE5pv2qryDbURUj9Vpi1", "7Grgx69MZJ4wDKRx1bBxLqTnU9T3quKW7n"});
Expand Down Expand Up @@ -832,6 +835,7 @@ class CRegTestParams : public CChainParams {

consensus.smartContracts.clear();
consensus.smartContracts[SMART_CONTRACT_DFIP_2201] = GetScriptForDestination(CTxDestination(WitnessV0KeyHash(std::vector<unsigned char>{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0})));
consensus.smartContracts[SMART_CONTRACT_DFIP_2203] = GetScriptForDestination(CTxDestination(WitnessV0KeyHash(std::vector<unsigned char>{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1})));

// owner base58, operator base58
vMasternodes.push_back({"mwsZw8nF7pKxWH8eoKL9tPxTpaFkz7QeLU", "mswsMVsyGMj1FzDMbbxw2QW3KvQAv2FKiy"});
Expand Down
1 change: 1 addition & 0 deletions src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class CChainParams
};

const auto SMART_CONTRACT_DFIP_2201 = "DFIP2201";
const auto SMART_CONTRACT_DFIP_2203 = "DFIP2203";

/**
* Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
Expand Down
1 change: 1 addition & 0 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ const CLogCategoryDesc LogCategories[] =
{BCLog::ORACLE, "oracle"},
{BCLog::LOAN, "loan"},
{BCLog::ACCOUNTCHANGE, "accountchange"},
{BCLog::FUTURESWAP, "futureswap"},
{BCLog::ALL, "1"},
{BCLog::ALL, "all"},
};
Expand Down
1 change: 1 addition & 0 deletions src/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ namespace BCLog {
ORACLE = (1 << 24),
LOAN = (1 << 25),
ACCOUNTCHANGE = (1 << 26),
FUTURESWAP = (1 << 27),
ALL = ~(uint32_t)0,
};

Expand Down
57 changes: 57 additions & 0 deletions src/masternodes/accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,60 @@ uint32_t CAccountsView::GetBalancesHeight(CScript const & owner)
bool ok = ReadBy<ByHeightKey>(owner, height);
return ok ? height : 0;
}

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

return Res::Ok();
}

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

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

return Res::Ok();
}

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);
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)) {
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);
}
52 changes: 52 additions & 0 deletions src/masternodes/accounts.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,48 @@
#include <amount.h>
#include <script/script.h>

struct CFuturesUserKey {
uint32_t height;
CScript owner;
uint32_t txn;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
if (ser_action.ForRead()) {
READWRITE(WrapBigEndian(height));
height = ~height;
READWRITE(owner);
READWRITE(WrapBigEndian(txn));
txn = ~txn;
} else {
uint32_t height_ = ~height;
READWRITE(WrapBigEndian(height_));
READWRITE(owner);
uint32_t txn_ = ~txn;
READWRITE(WrapBigEndian(txn_));
}
}

bool operator<(const CFuturesUserKey& o) const {
return std::tie(height, owner, txn) < std::tie(o.height, o.owner, o.txn);
}
};

struct CFuturesUserValue {
CTokenAmount source{};
uint32_t destination{};

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(source);
READWRITE(destination);
}
};

class CAccountsView : public virtual CStorageView
{
public:
Expand All @@ -27,9 +69,19 @@ class CAccountsView : public virtual CStorageView
uint32_t GetBalancesHeight(CScript const & owner);
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'; } };

private:
Res SetBalance(CScript const & owner, CTokenAmount amount);
Expand Down
17 changes: 17 additions & 0 deletions src/masternodes/balances.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,23 @@ struct CSmartContractMessage {
}
};

struct CFutureSwapMessage {
CScript owner;
CTokenAmount source{};
uint32_t destination{};
bool withdraw{};

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(owner);
READWRITE(source);
READWRITE(destination);
READWRITE(withdraw);
}
};

inline CBalances SumAllTransfers(CAccounts const & to) {
CBalances sum;
for (const auto& kv : to) {
Expand Down
Loading