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

Automatic Setting of Negative Interest #2816

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 86 additions & 22 deletions src/dfi/govvariables/attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,33 +61,35 @@ const std::map<uint8_t, std::string> &ATTRIBUTES::displayVersions() {

const std::map<std::string, uint8_t> &ATTRIBUTES::allowedTypes() {
static const std::map<std::string, uint8_t> types{
{"locks", AttributeTypes::Locks },
{"oracles", AttributeTypes::Oracles },
{"params", AttributeTypes::Param },
{"poolpairs", AttributeTypes::Poolpairs },
{"token", AttributeTypes::Token },
{"gov", AttributeTypes::Governance},
{"transferdomain", AttributeTypes::Transfer },
{"evm", AttributeTypes::EVMType },
{"vaults", AttributeTypes::Vaults },
{"rules", AttributeTypes::Rules },
{"locks", AttributeTypes::Locks },
{"oracles", AttributeTypes::Oracles },
{"params", AttributeTypes::Param },
{"poolpairs", AttributeTypes::Poolpairs },
{"token", AttributeTypes::Token },
{"gov", AttributeTypes::Governance },
{"transferdomain", AttributeTypes::Transfer },
{"evm", AttributeTypes::EVMType },
{"vaults", AttributeTypes::Vaults },
{"rules", AttributeTypes::Rules },
{"negative_interest", AttributeTypes::NegativeInterst},
};
return types;
}

const std::map<uint8_t, std::string> &ATTRIBUTES::displayTypes() {
static const std::map<uint8_t, std::string> types{
{AttributeTypes::Live, "live" },
{AttributeTypes::Locks, "locks" },
{AttributeTypes::Oracles, "oracles" },
{AttributeTypes::Param, "params" },
{AttributeTypes::Poolpairs, "poolpairs" },
{AttributeTypes::Token, "token" },
{AttributeTypes::Governance, "gov" },
{AttributeTypes::Transfer, "transferdomain"},
{AttributeTypes::EVMType, "evm" },
{AttributeTypes::Vaults, "vaults" },
{AttributeTypes::Rules, "rules" },
{AttributeTypes::Live, "live" },
{AttributeTypes::Locks, "locks" },
{AttributeTypes::Oracles, "oracles" },
{AttributeTypes::Param, "params" },
{AttributeTypes::Poolpairs, "poolpairs" },
{AttributeTypes::Token, "token" },
{AttributeTypes::Governance, "gov" },
{AttributeTypes::Transfer, "transferdomain" },
{AttributeTypes::EVMType, "evm" },
{AttributeTypes::Vaults, "vaults" },
{AttributeTypes::Rules, "rules" },
{AttributeTypes::NegativeInterst, "negative_interest"},
};
return types;
}
Expand Down Expand Up @@ -226,6 +228,20 @@ const std::map<uint8_t, std::string> &ATTRIBUTES::displayRulesIDs() {
return params;
}

const std::map<std::string, uint8_t> &ATTRIBUTES::allowedNegativeInterestIDs() {
static const std::map<std::string, uint8_t> params{
{"automatic", NegativeInterestIDs::Automatic},
};
return params;
}

const std::map<uint8_t, std::string> &ATTRIBUTES::displayNegativeInterestIDs() {
static const std::map<uint8_t, std::string> params{
{NegativeInterestIDs::Automatic, "automatic"},
};
return params;
}

const std::map<uint8_t, std::map<std::string, uint8_t>> &ATTRIBUTES::allowedKeys() {
static const std::map<uint8_t, std::map<std::string, uint8_t>> keys{
{AttributeTypes::Token,
Expand Down Expand Up @@ -274,6 +290,7 @@ const std::map<uint8_t, std::map<std::string, uint8_t>> &ATTRIBUTES::allowedKeys
{"emission-unused-fund", DFIPKeys::EmissionUnusedFund},
{"mint-tokens-to-address", DFIPKeys::MintTokens},
{"transferdomain", DFIPKeys::TransferDomain},
{"auto-negative-interest", DFIPKeys::AutoNegativeInterest},
}},
{AttributeTypes::EVMType,
{
Expand Down Expand Up @@ -317,6 +334,11 @@ const std::map<uint8_t, std::map<std::string, uint8_t>> &ATTRIBUTES::allowedKeys
{"dvm_op_return_max_size_bytes", RulesKeys::DVMOPReturn},
{"evm_op_return_max_size_bytes", RulesKeys::EVMOPReturn},
}},
{AttributeTypes::NegativeInterst,
{
{"block_period", NegativeInterestKeys::BlockInterval},
{"burn_time_period", NegativeInterestKeys::BurnTimePeriod},
}},
};
return keys;
}
Expand Down Expand Up @@ -372,6 +394,7 @@ const std::map<uint8_t, std::map<uint8_t, std::string>> &ATTRIBUTES::displayKeys
{DFIPKeys::EmissionUnusedFund, "emission-unused-fund"},
{DFIPKeys::MintTokens, "mint-tokens-to-address"},
{DFIPKeys::TransferDomain, "transferdomain"},
{DFIPKeys::AutoNegativeInterest, "auto-negative-interest"},
}},
{AttributeTypes::EVMType,
{
Expand Down Expand Up @@ -434,6 +457,11 @@ const std::map<uint8_t, std::map<uint8_t, std::string>> &ATTRIBUTES::displayKeys
{RulesKeys::DVMOPReturn, "dvm_op_return_max_size_bytes"},
{RulesKeys::EVMOPReturn, "evm_op_return_max_size_bytes"},
}},
{AttributeTypes::NegativeInterst,
{
{NegativeInterestKeys::BlockInterval, "block_period"},
{NegativeInterestKeys::BurnTimePeriod, "burn_time_period"},
}},
};
return keys;
}
Expand Down Expand Up @@ -750,6 +778,7 @@ const std::map<uint8_t, std::map<uint8_t, std::function<ResVal<CAttributeValue>(
{DFIPKeys::EmissionUnusedFund, VerifyBool},
{DFIPKeys::MintTokens, VerifyBool},
{DFIPKeys::TransferDomain, VerifyBool},
{DFIPKeys::AutoNegativeInterest, VerifyBool},
}},
{AttributeTypes::Locks,
{
Expand Down Expand Up @@ -797,6 +826,11 @@ const std::map<uint8_t, std::map<uint8_t, std::function<ResVal<CAttributeValue>(
{RulesKeys::DVMOPReturn, VerifyUInt64},
{RulesKeys::EVMOPReturn, VerifyUInt64},
}},
{AttributeTypes::NegativeInterst,
{
{NegativeInterestKeys::BlockInterval, VerifyUInt64},
{NegativeInterestKeys::BurnTimePeriod, VerifyUInt64},
}},
};
return parsers;
}
Expand Down Expand Up @@ -914,7 +948,7 @@ static Res CheckValidAttrV0Key(const uint8_t type, const uint32_t typeId, const
typeKey != DFIPKeys::MNSetOwnerAddress && typeKey != DFIPKeys::GovernanceEnabled &&
typeKey != DFIPKeys::CFPPayout && typeKey != DFIPKeys::EmissionUnusedFund &&
typeKey != DFIPKeys::MintTokens && typeKey != DFIPKeys::EVMEnabled && typeKey != DFIPKeys::ICXEnabled &&
typeKey != DFIPKeys::TransferDomain) {
typeKey != DFIPKeys::TransferDomain && typeKey != DFIPKeys::AutoNegativeInterest) {
return DeFiErrors::GovVarVariableUnsupportedFeatureType(typeKey);
}
} else if (typeId == ParamIDs::Foundation) {
Expand Down Expand Up @@ -980,6 +1014,14 @@ static Res CheckValidAttrV0Key(const uint8_t type, const uint32_t typeId, const
} else {
return DeFiErrors::GovVarVariableUnsupportedGovType();
}
} else if (type == AttributeTypes::NegativeInterst) {
if (typeId == NegativeInterestIDs::Automatic) {
if (typeKey != NegativeInterestKeys::BlockInterval && typeKey != NegativeInterestKeys::BurnTimePeriod) {
return DeFiErrors::GovVarVariableUnsupportedRulesType(typeKey);
}
} else {
return DeFiErrors::GovVarVariableUnsupportedGovType();
}
}

return Res::Ok();
Expand Down Expand Up @@ -1067,6 +1109,12 @@ Res ATTRIBUTES::ProcessVariable(const std::string &key,
return DeFiErrors::GovVarVariableInvalidKey("rules", allowedRulesIDs());
}
typeId = id->second;
} else if (type == AttributeTypes::NegativeInterst) {
auto id = allowedNegativeInterestIDs().find(keys[2]);
if (id == allowedNegativeInterestIDs().end()) {
return DeFiErrors::GovVarVariableInvalidKey("negative_interest", allowedNegativeInterestIDs());
}
typeId = id->second;
} else {
auto id = VerifyInt32(keys[2]);
if (!id) {
Expand Down Expand Up @@ -1472,6 +1520,10 @@ Res ATTRIBUTES::CheckKeys() const {
if (!displayRulesIDs().count(attrV0->typeId)) {
return DeFiErrors::GovVarVariableInvalidKey("rules", displayRulesIDs());
}
} else if (attrV0->type == AttributeTypes::NegativeInterst) {
if (!displayNegativeInterestIDs().count(attrV0->typeId)) {
return DeFiErrors::GovVarVariableInvalidKey("negative_interest", displayNegativeInterestIDs());
}
}

// Check key - Locks and Oracles have height int keys so skip.
Expand Down Expand Up @@ -1518,6 +1570,8 @@ UniValue ATTRIBUTES::ExportFiltered(GovVarsFilter filter, const std::string &pre
id = displayVaultIDs().at(attrV0->typeId);
} else if (attrV0->type == AttributeTypes::Rules) {
id = displayRulesIDs().at(attrV0->typeId);
} else if (attrV0->type == AttributeTypes::NegativeInterst) {
id = displayNegativeInterestIDs().at(attrV0->typeId);
} else {
id = KeyBuilder(attrV0->typeId);
}
Expand Down Expand Up @@ -1893,6 +1947,10 @@ Res ATTRIBUTES::Validate(const CCustomCSView &view) const {
if (view.GetLastHeight() < Params().GetConsensus().DF22MetachainHeight) {
return Res::Err("Cannot be set before MetachainHeight");
}
} else if (attrV0->key == DFIPKeys::AutoNegativeInterest) {
if (view.GetLastHeight() < Params().GetConsensus().DF23Height) {
return Res::Err("Cannot be set before DF23Height");
}
}
} else if (attrV0->typeId == ParamIDs::Foundation) {
if (view.GetLastHeight() < Params().GetConsensus().DF20GrandCentralHeight) {
Expand Down Expand Up @@ -1974,6 +2032,12 @@ Res ATTRIBUTES::Validate(const CCustomCSView &view) const {
}
break;

case AttributeTypes::NegativeInterst:
if (view.GetLastHeight() < Params().GetConsensus().DF23Height) {
return Res::Err("Cannot be set before DF23Height");
}
break;

default:
return Res::Err("Unrecognised type (%d)", attrV0->type);
}
Expand Down
13 changes: 13 additions & 0 deletions src/dfi/govvariables/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ enum AttributeTypes : uint8_t {
EVMType = 'e',
Vaults = 'v',
Rules = 'r',
NegativeInterst = 'n',
};

enum ParamIDs : uint8_t {
Expand Down Expand Up @@ -78,6 +79,10 @@ enum RulesIDs : uint8_t {
TXRules = 'a',
};

enum NegativeInterestIDs : uint8_t {
Automatic = 'a',
};

enum EconomyKeys : uint8_t {
PaybackDFITokens = 'a',
PaybackTokens = 'b',
Expand Down Expand Up @@ -120,6 +125,7 @@ enum DFIPKeys : uint8_t {
EVMEnabled = 'u',
ICXEnabled = 'v',
TransferDomain = 'w',
AutoNegativeInterest = 'x',
};

enum GovernanceKeys : uint8_t {
Expand Down Expand Up @@ -183,6 +189,11 @@ enum RulesKeys : uint8_t {
EVMOPReturn = 'c',
};

enum NegativeInterestKeys : uint8_t {
BlockInterval = 'a',
BurnTimePeriod = 'b',
};

struct CDataStructureV0 {
uint8_t type;
uint32_t typeId;
Expand Down Expand Up @@ -504,6 +515,7 @@ class ATTRIBUTES : public GovVariable, public AutoRegistrator<GovVariable, ATTRI
static const std::map<uint8_t, std::string> &displayEVMIDs();
static const std::map<uint8_t, std::string> &displayVaultIDs();
static const std::map<uint8_t, std::string> &displayRulesIDs();
static const std::map<uint8_t, std::string> &displayNegativeInterestIDs();
static const std::map<uint8_t, std::map<uint8_t, std::string>> &displayKeys();

Res RefundFuturesContracts(CCustomCSView &mnview,
Expand All @@ -530,6 +542,7 @@ class ATTRIBUTES : public GovVariable, public AutoRegistrator<GovVariable, ATTRI
static const std::map<std::string, uint8_t> &allowedEVMIDs();
static const std::map<std::string, uint8_t> &allowedVaultIDs();
static const std::map<std::string, uint8_t> &allowedRulesIDs();
static const std::map<std::string, uint8_t> &allowedNegativeInterestIDs();
static const std::map<uint8_t, std::map<std::string, uint8_t>> &allowedKeys();
static const std::map<uint8_t, std::map<uint8_t, std::function<ResVal<CAttributeValue>(const std::string &)>>>
&parseValue();
Expand Down
73 changes: 56 additions & 17 deletions src/dfi/rpc_vault.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <dfi/accountshistory.h>
#include <dfi/auctionhistory.h>
#include <dfi/govvariables/attributes.h>
#include <dfi/mn_rpc.h>
#include <dfi/validation.h>
#include <dfi/vaulthistory.h>

extern UniValue AmountsToJSON(const TAmounts &diffs, AmountFormat format = AmountFormat::Symbol);
Expand Down Expand Up @@ -2197,26 +2199,63 @@ UniValue getloantokens(const JSONRPCRequest &request) {
return GetRPCResultCache().Set(request, ret);
}

UniValue estimatenegativeinterest(const JSONRPCRequest &request) {
RPCHelpMan{
"estimatenegativeinterest",
"Estimate what the negative interest rate should be, not what is currently set.\n",
{

},
RPCResult{"n (numeric) The calculated negative interest rate\n"},
RPCExamples{HelpExampleCli("estimatenegativeinterest", "") + HelpExampleRpc("estimatenegativeinterest", "")},
}
.Check(request);

if (auto res = GetRPCResultCache().TryGet(request)) {
return *res;
}

LOCK(cs_main);

auto attributes = pcustomcsview->GetAttributes();

const CDataStructureV0 burnTimeSampleKey{
AttributeTypes::NegativeInterst, NegativeInterestIDs::Automatic, NegativeInterestKeys::BurnTimePeriod};
const auto burnTimeSample = attributes->GetValue(burnTimeSampleKey, NEGATIVE_INT_BURN_TIME_SAMPLE);

const auto dusdBurned = GetDexBurnedDUSD(*pcustomcsview, *pburnHistoryDB, burnTimeSample);
const auto dusdLoaned = GetVaultLoanDUSD(*pcustomcsview);
const auto result = CalculateNegativeInterest(dusdBurned, dusdLoaned);

UniValue res(UniValue::VOBJ);
res.pushKV("dusdBurned", ValueFromAmount(dusdBurned));
res.pushKV("dusdLoaned", ValueFromAmount(dusdLoaned));
res.pushKV("negativeInterest", ValueFromAmount(result));

return GetRPCResultCache().Set(request, res);
}

static const CRPCCommand commands[] = {
// category name actor (function) params
// --------------- ---------------------- --------------------- ----------
{"vault", "createvault", &createvault, {"ownerAddress", "schemeId", "inputs"} },
{"vault", "closevault", &closevault, {"id", "returnAddress", "inputs"} },
{"vault", "listvaults", &listvaults, {"options", "pagination"} },
{"vault", "getvault", &getvault, {"id", "verbose"} },
{"vault", "listvaulthistory", &listvaulthistory, {"id", "options"} },
{"vault", "updatevault", &updatevault, {"id", "parameters", "inputs"} },
{"vault", "deposittovault", &deposittovault, {"id", "from", "amount", "inputs"} },
{"vault", "withdrawfromvault", &withdrawfromvault, {"id", "to", "amount", "inputs"} },
{"vault", "placeauctionbid", &placeauctionbid, {"id", "index", "from", "amount", "inputs"}},
{"vault", "listauctions", &listauctions, {"pagination"} },
{"vault", "listauctionhistory", &listauctionhistory, {"owner", "pagination"} },
{"vault", "estimateloan", &estimateloan, {"vaultId", "tokens", "targetRatio"} },
{"vault", "estimatecollateral", &estimatecollateral, {"loanAmounts", "targetRatio", "tokens"} },
{"vault", "estimatevault", &estimatevault, {"collateralAmounts", "loanAmounts"} },
{"hidden", "getstoredinterest", &getstoredinterest, {"vaultId", "token"} },
{"hidden", "logstoredinterests", &logstoredinterests, {} },
{"hidden", "getloantokens", &getloantokens, {"vaultId"} },
{"vault", "createvault", &createvault, {"ownerAddress", "schemeId", "inputs"} },
{"vault", "closevault", &closevault, {"id", "returnAddress", "inputs"} },
{"vault", "listvaults", &listvaults, {"options", "pagination"} },
{"vault", "getvault", &getvault, {"id", "verbose"} },
{"vault", "listvaulthistory", &listvaulthistory, {"id", "options"} },
{"vault", "updatevault", &updatevault, {"id", "parameters", "inputs"} },
{"vault", "deposittovault", &deposittovault, {"id", "from", "amount", "inputs"} },
{"vault", "withdrawfromvault", &withdrawfromvault, {"id", "to", "amount", "inputs"} },
{"vault", "placeauctionbid", &placeauctionbid, {"id", "index", "from", "amount", "inputs"}},
{"vault", "listauctions", &listauctions, {"pagination"} },
{"vault", "listauctionhistory", &listauctionhistory, {"owner", "pagination"} },
{"vault", "estimateloan", &estimateloan, {"vaultId", "tokens", "targetRatio"} },
{"vault", "estimatecollateral", &estimatecollateral, {"loanAmounts", "targetRatio", "tokens"} },
{"vault", "estimatevault", &estimatevault, {"collateralAmounts", "loanAmounts"} },
{"hidden", "getstoredinterest", &getstoredinterest, {"vaultId", "token"} },
{"hidden", "logstoredinterests", &logstoredinterests, {} },
{"hidden", "getloantokens", &getloantokens, {"vaultId"} },
{"hidden", "estimatenegativeinterest", &estimatenegativeinterest, {""} },
};

void RegisterVaultRPCCommands(CRPCTable &tableRPC) {
Expand Down
Loading
Loading