Skip to content

Commit

Permalink
Rename DUSD loops to dusd-vault attribute key (#2187)
Browse files Browse the repository at this point in the history
* Implement new vaults gov attribute, shift dusd vaults

* Fixes to key id and key names

* Revert changes to test_runner

* New vault keys for dusd vault enabled

* Change naming
  • Loading branch information
sieniven authored Jul 19, 2023
1 parent 11a2c55 commit bf16610
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 27 deletions.
4 changes: 4 additions & 0 deletions src/masternodes/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ class DeFiErrors {
return Res::Err("Unsupported key for Transfer Domain {%d}", type);
}

static Res GovVarVariableUnsupportedVaultsType(const unsigned char type) {
return Res::Err("Unsupported key for Vaults {%d}", type);
}

static Res GovVarVariableUnsupportedParamType() {
return Res::Err("Unsupported Param ID");
}
Expand Down
62 changes: 56 additions & 6 deletions src/masternodes/govvariables/attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const std::map<std::string, uint8_t> &ATTRIBUTES::allowedTypes() {
{"gov", AttributeTypes::Governance},
{"consortium", AttributeTypes::Consortium},
{"transferdomain", AttributeTypes::Transfer },
{"vaults", AttributeTypes::Vaults },
};
return types;
}
Expand All @@ -74,6 +75,7 @@ const std::map<uint8_t, std::string> &ATTRIBUTES::displayTypes() {
{AttributeTypes::Governance, "gov" },
{AttributeTypes::Consortium, "consortium" },
{AttributeTypes::Transfer, "transferdomain"},
{AttributeTypes::Vaults, "vaults" },
};
return types;
}
Expand Down Expand Up @@ -158,6 +160,20 @@ const std::map<uint8_t, std::string> &ATTRIBUTES::displayTransferIDs() {
return params;
}

const std::map<std::string, uint8_t> &ATTRIBUTES::allowedVaultIDs() {
static const std::map<std::string, uint8_t> params{
{"dusd-vault", VaultIDs::DUSDVault},
};
return params;
}

const std::map<uint8_t, std::string> &ATTRIBUTES::displayVaultIDs() {
static const std::map<uint8_t, std::string> params{
{VaultIDs::DUSDVault, "dusd-vault"},
};
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 @@ -212,7 +228,6 @@ const std::map<uint8_t, std::map<std::string, uint8_t>> &ATTRIBUTES::allowedKeys
{"gov-payout", DFIPKeys::CFPPayout},
{"emission-unused-fund", DFIPKeys::EmissionUnusedFund},
{"mint-tokens-to-address", DFIPKeys::MintTokens},
{"allow-dusd-loops", DFIPKeys::AllowDUSDLoops},
{"transferdomain", DFIPKeys::TransferDomain},
}},
{AttributeTypes::Governance,
Expand All @@ -235,6 +250,10 @@ const std::map<uint8_t, std::map<std::string, uint8_t>> &ATTRIBUTES::allowedKeys
{"evm-dvm", TransferKeys::EVM_DVM},
{"dvm-evm", TransferKeys::DVM_EVM},
}},
{AttributeTypes::Vaults,
{
{"enabled", VaultKeys::DUSDVaultEnabled},
}},
};
return keys;
}
Expand Down Expand Up @@ -296,7 +315,6 @@ const std::map<uint8_t, std::map<uint8_t, std::string>> &ATTRIBUTES::displayKeys
{DFIPKeys::CFPPayout, "gov-payout"},
{DFIPKeys::EmissionUnusedFund, "emission-unused-fund"},
{DFIPKeys::MintTokens, "mint-tokens-to-address"},
{DFIPKeys::AllowDUSDLoops, "allow-dusd-loops"},
{DFIPKeys::TransferDomain, "transferdomain"},
}},
{AttributeTypes::Live,
Expand Down Expand Up @@ -338,6 +356,10 @@ const std::map<uint8_t, std::map<uint8_t, std::string>> &ATTRIBUTES::displayKeys
{TransferKeys::EVM_DVM, "evm-dvm"},
{TransferKeys::DVM_EVM, "dvm-evm"},
}},
{AttributeTypes::Vaults,
{
{VaultKeys::DUSDVaultEnabled, "enabled"},
}},
};
return keys;
}
Expand Down Expand Up @@ -631,7 +653,6 @@ const std::map<uint8_t, std::map<uint8_t, std::function<ResVal<CAttributeValue>(
{DFIPKeys::CFPPayout, VerifyBool},
{DFIPKeys::EmissionUnusedFund, VerifyBool},
{DFIPKeys::MintTokens, VerifyBool},
{DFIPKeys::AllowDUSDLoops, VerifyBool},
{DFIPKeys::TransferDomain, VerifyBool},
}},
{AttributeTypes::Locks,
Expand Down Expand Up @@ -662,6 +683,10 @@ const std::map<uint8_t, std::map<uint8_t, std::function<ResVal<CAttributeValue>(
{TransferKeys::EVM_DVM, VerifyBool},
{TransferKeys::DVM_EVM, VerifyBool},
}},
{AttributeTypes::Vaults,
{
{VaultKeys::DUSDVaultEnabled, VerifyBool},
}},
};
return parsers;
}
Expand Down Expand Up @@ -787,7 +812,14 @@ Res ATTRIBUTES::ProcessVariable(const std::string &key,
return DeFiErrors::GovVarVariableInvalidKey("transferdomain", allowedTransferIDs());
}
typeId = id->second;
} else {
} else if (type == AttributeTypes::Vaults) {
auto id = allowedVaultIDs().find(keys[2]);
if (id == allowedVaultIDs().end()) {
return DeFiErrors::GovVarVariableInvalidKey("vaults", allowedVaultIDs());
}
typeId = id->second;
}
else {
auto id = VerifyInt32(keys[2]);
if (!id) {
return id;
Expand Down Expand Up @@ -855,7 +887,7 @@ Res ATTRIBUTES::ProcessVariable(const std::string &key,
typeKey != DFIPKeys::ConsortiumEnabled && typeKey != DFIPKeys::CFPPayout &&
typeKey != DFIPKeys::EmissionUnusedFund && typeKey != DFIPKeys::MintTokens &&
typeKey != DFIPKeys::EVMEnabled && typeKey != DFIPKeys::ICXEnabled &&
typeKey != DFIPKeys::AllowDUSDLoops && typeKey != DFIPKeys::TransferDomain) {
typeKey != DFIPKeys::TransferDomain) {
return DeFiErrors::GovVarVariableUnsupportedFeatureType(typeKey);
}
} else if (typeId == ParamIDs::Foundation) {
Expand Down Expand Up @@ -884,6 +916,14 @@ Res ATTRIBUTES::ProcessVariable(const std::string &key,
} else {
return DeFiErrors::GovVarVariableUnsupportedGovType();
}
} else if (type == AttributeTypes::Vaults) {
if (typeId == VaultIDs::DUSDVault) {
if (typeKey != VaultKeys::DUSDVaultEnabled) {
return DeFiErrors::GovVarVariableUnsupportedVaultsType(typeKey);
}
} else {
return DeFiErrors::GovVarVariableUnsupportedGovType();
}
}

attrV0 = CDataStructureV0{type, typeId, typeKey};
Expand Down Expand Up @@ -1192,8 +1232,10 @@ UniValue ATTRIBUTES::ExportFiltered(GovVarsFilter filter, const std::string &pre
id = displayOracleIDs().at(attrV0->typeId);
} else if (attrV0->type == AttributeTypes::Governance) {
id = displayGovernanceIDs().at(attrV0->typeId);
} else if (attrV0->type == AttributeTypes::Transfer) {
} else if (attrV0->type == AttributeTypes::Transfer) {
id = displayTransferIDs().at(attrV0->typeId);
} else if (attrV0->type == AttributeTypes::Vaults) {
id = displayVaultIDs().at(attrV0->typeId);
} else {
id = KeyBuilder(attrV0->typeId);
}
Expand Down Expand Up @@ -1611,6 +1653,14 @@ Res ATTRIBUTES::Validate(const CCustomCSView &view) const {
}
break;

case AttributeTypes::Vaults:
if (attrV0->typeId == VaultIDs::DUSDVault && attrV0->key == VaultKeys::DUSDVaultEnabled) {
if (view.GetLastHeight() < Params().GetConsensus().NextNetworkUpgradeHeight) {
return Res::Err("Cannot be set before NextNetworkUpgrade");
}
}
break;

default:
return Res::Err("Unrecognised type (%d)", attrV0->type);
}
Expand Down
14 changes: 12 additions & 2 deletions src/masternodes/govvariables/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ enum AttributeTypes : uint8_t {
Governance = 'g',
Consortium = 'c',
Transfer = 'b',
Vaults = 'v',
};

enum ParamIDs : uint8_t {
Expand Down Expand Up @@ -51,6 +52,10 @@ enum TransferIDs : uint8_t {
Edges = 'a',
};

enum VaultIDs : uint8_t {
DUSDVault = 'a',
};

enum EconomyKeys : uint8_t {
PaybackDFITokens = 'a',
PaybackTokens = 'b',
Expand Down Expand Up @@ -93,8 +98,7 @@ enum DFIPKeys : uint8_t {
MintTokens = 't',
EVMEnabled = 'u',
ICXEnabled = 'v',
AllowDUSDLoops = 'w',
TransferDomain = 'x',
TransferDomain = 'w',
};

enum GovernanceKeys : uint8_t {
Expand Down Expand Up @@ -149,6 +153,10 @@ enum TransferKeys : uint8_t {
EVM_DVM = 'b',
};

enum VaultKeys : uint8_t {
DUSDVaultEnabled = 'w',
};

struct CDataStructureV0 {
uint8_t type;
uint32_t typeId;
Expand Down Expand Up @@ -436,6 +444,7 @@ class ATTRIBUTES : public GovVariable, public AutoRegistrator<GovVariable, ATTRI
static const std::map<uint8_t, std::string> &displayConsortiumIDs();
static const std::map<uint8_t, std::string> &displayGovernanceIDs();
static const std::map<uint8_t, std::string> &displayTransferIDs();
static const std::map<uint8_t, std::string> &displayVaultIDs();
static const std::map<uint8_t, std::map<uint8_t, std::string>> &displayKeys();

Res RefundFuturesContracts(CCustomCSView &mnview,
Expand All @@ -460,6 +469,7 @@ class ATTRIBUTES : public GovVariable, public AutoRegistrator<GovVariable, ATTRI
static const std::map<std::string, uint8_t> &allowedConsortiumIDs();
static const std::map<std::string, uint8_t> &allowedGovernanceIDs();
static const std::map<std::string, uint8_t> &allowedTransferIDs();
static const std::map<std::string, uint8_t> &allowedVaultIDs();
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
6 changes: 3 additions & 3 deletions src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3043,11 +3043,11 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor {
auto isPostNext = static_cast<int>(height) >= consensus.ChangiIntermediateHeight2; // Change to NextNetworkUpgradeHeight on mainnet release

if(isPostNext) {
const CDataStructureV0 enabledKey{AttributeTypes::Param, ParamIDs::Feature, DFIPKeys::AllowDUSDLoops};
const CDataStructureV0 enabledKey{AttributeTypes::Vaults, VaultIDs::DUSDVault, VaultKeys::DUSDVaultEnabled};
auto attributes = mnview.GetAttributes();
assert(attributes);
auto DUSDLoopsAllowed= attributes->GetValue(enabledKey, false);
if(DUSDLoopsAllowed && hasDUSDColl && !hasOtherColl) {
auto DUSDVaultsAllowed = attributes->GetValue(enabledKey, false);
if(DUSDVaultsAllowed && hasDUSDColl && !hasOtherColl) {
return Res::Ok(); //every loan ok when DUSD loops allowed and 100% DUSD collateral
}
}
Expand Down
22 changes: 6 additions & 16 deletions test/functional/feature_dusd_loans.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,30 +535,21 @@ def check_looped_dusd(self):
ERR_STRING_MIN_COLLATERAL_DFI_PCT,
self.takeloan_withdraw, vault_id, "1.00000000@DUSD", 'takeloan')

self.nodes[0].setgov({"ATTRIBUTES": {'v0/params/feature/allow-dusd-loops': 'true'}})
self.nodes[0].generate(1)

#still not allowed before hardfork
assert_raises_rpc_error(-32600,
ERR_STRING_MIN_COLLATERAL_DFI_PCT,
self.takeloan_withdraw, vault_id, "1.00000000@DUSD", 'takeloan')
self.goto_next_height()

# min coll ratio still applies
assert_raises_rpc_error(-32600,
"Vault does not have enough collateralization ratio defined by loan scheme - 148 < 150",
self.takeloan_withdraw, vault_id, "67.00000000@DUSD", 'takeloan')


self.goto_next_height()
self.nodes[0].setgov({"ATTRIBUTES": {'v0/vaults/dusd-vault/enabled': 'true'}})
self.nodes[0].generate(1)

self.takeloan_withdraw(vault_id, "1.00000000@DUSD", 'takeloan')
self.nodes[0].generate(1)
self.takeloan_withdraw(vault_id, "1.00000000@DUSD", 'withdraw')
self.nodes[0].generate(1)

# not sure why this is needed like this. but it works
self.update_oracle_price()
#also fails with other crypto in
self.nodes[0].deposittovault(vault_id, self.account0, "100.00000000@BTC")
self.nodes[0].generate(1)
assert_raises_rpc_error(-32600,
Expand All @@ -569,15 +560,14 @@ def check_looped_dusd(self):
ERR_STRING_MIN_COLLATERAL_DFI_PCT,
self.takeloan_withdraw, vault_id, "5.00000000@BTC", 'withdraw')

# full withdrawal (go to 100% DUSD) should work
# full withdrawal to go back to 100% DUSD vault
self.takeloan_withdraw(vault_id, "100.00000000@BTC", 'withdraw')
self.nodes[0].generate(1)


self.nodes[0].setgov({"ATTRIBUTES": {'v0/params/feature/allow-dusd-loops': 'false'}})
self.nodes[0].setgov({"ATTRIBUTES": {'v0/vaults/dusd-vault/enabled': 'false'}})
self.nodes[0].generate(1)

#not allowed if attribute false
# not allowed if attribute false

assert_raises_rpc_error(-32600,
ERR_STRING_MIN_COLLATERAL_DFI_PCT,
Expand Down

0 comments on commit bf16610

Please sign in to comment.