From 576503a12b02f430dda4190f2d8c93c7e00f2fde Mon Sep 17 00:00:00 2001 From: Peter John Bushnell Date: Tue, 24 May 2022 11:45:05 +0100 Subject: [PATCH] Add import call when setting tokens (#1275) --- src/masternodes/mn_checks.cpp | 60 ++++++++++++++++--- .../feature_loan_setcollateraltoken.py | 20 ++++--- 2 files changed, 62 insertions(+), 18 deletions(-) diff --git a/src/masternodes/mn_checks.cpp b/src/masternodes/mn_checks.cpp index bfed0291dd6..a89d8acf59f 100644 --- a/src/masternodes/mn_checks.cpp +++ b/src/masternodes/mn_checks.cpp @@ -2319,9 +2319,23 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor CDataStructureV0 collateralFactor{AttributeTypes::Token, tokenId, TokenKeys::LoanCollateralFactor}; CDataStructureV0 pairKey{AttributeTypes::Token, tokenId, TokenKeys::FixedIntervalPriceId}; - attributes->SetValue(collateralEnabled, true); - attributes->SetValue(collateralFactor, obj.factor); - attributes->SetValue(pairKey, obj.fixedIntervalPriceId); + auto gv = GovVariable::Create("ATTRIBUTES"); + if (!gv) { + return Res::Err("Failed to create ATTRIBUTES Governance variable"); + } + + auto var = std::dynamic_pointer_cast(gv); + if (!var) { + return Res::Err("Failed to convert ATTRIBUTES Governance variable"); + } + + var->SetValue(collateralEnabled, true); + var->SetValue(collateralFactor, obj.factor); + var->SetValue(pairKey, obj.fixedIntervalPriceId); + + res = attributes->Import(var->Export()); + if (!res) + return res; res = attributes->Validate(mnview); if (!res) @@ -2407,9 +2421,23 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor CDataStructureV0 mintInterest{AttributeTypes::Token, id, TokenKeys::LoanMintingInterest}; CDataStructureV0 pairKey{AttributeTypes::Token, id, TokenKeys::FixedIntervalPriceId}; - attributes->SetValue(mintEnabled, obj.mintable); - attributes->SetValue(mintInterest, obj.interest); - attributes->SetValue(pairKey, obj.fixedIntervalPriceId); + auto gv = GovVariable::Create("ATTRIBUTES"); + if (!gv) { + return Res::Err("Failed to create ATTRIBUTES Governance variable"); + } + + auto var = std::dynamic_pointer_cast(gv); + if (!var) { + return Res::Err("Failed to convert ATTRIBUTES Governance variable"); + } + + var->SetValue(mintEnabled, obj.mintable); + var->SetValue(mintInterest, obj.interest); + var->SetValue(pairKey, obj.fixedIntervalPriceId); + + res = attributes->Import(var->Export()); + if (!res) + return res; res = attributes->Validate(mnview); if (!res) @@ -2498,9 +2526,23 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor CDataStructureV0 mintInterest{AttributeTypes::Token, id, TokenKeys::LoanMintingInterest}; CDataStructureV0 pairKey{AttributeTypes::Token, id, TokenKeys::FixedIntervalPriceId}; - attributes->SetValue(mintEnabled, obj.mintable); - attributes->SetValue(mintInterest, obj.interest); - attributes->SetValue(pairKey, obj.fixedIntervalPriceId); + auto gv = GovVariable::Create("ATTRIBUTES"); + if (!gv) { + return Res::Err("Failed to create ATTRIBUTES Governance variable"); + } + + auto var = std::dynamic_pointer_cast(gv); + if (!var) { + return Res::Err("Failed to convert ATTRIBUTES Governance variable"); + } + + var->SetValue(mintEnabled, obj.mintable); + var->SetValue(mintInterest, obj.interest); + var->SetValue(pairKey, obj.fixedIntervalPriceId); + + res = attributes->Import(var->Export()); + if (!res) + return res; res = attributes->Validate(mnview); if (!res) diff --git a/test/functional/feature_loan_setcollateraltoken.py b/test/functional/feature_loan_setcollateraltoken.py index 758d4c8acd0..da0048ec622 100755 --- a/test/functional/feature_loan_setcollateraltoken.py +++ b/test/functional/feature_loan_setcollateraltoken.py @@ -8,7 +8,7 @@ from test_framework.test_framework import DefiTestFramework from test_framework.authproxy import JSONRPCException -from test_framework.util import assert_equal +from test_framework.util import assert_equal, assert_raises_rpc_error from decimal import Decimal import calendar @@ -96,14 +96,10 @@ def run_test(self): self.nodes[0].setoracledata(oracle_id1, timestamp, oracle1_prices) self.nodes[0].generate(1) - try: - self.nodes[0].setcollateraltoken({ - 'token': idDFI, - 'factor': 2, - 'fixedIntervalPriceId': "DFI/USD"}) - except JSONRPCException as e: - errorString = e.error['message'] - assert("setCollateralToken factor must be lower or equal than 1" in errorString) + assert_raises_rpc_error(-32600, "setCollateralToken factor must be lower or equal than 1", self.nodes[0].setcollateraltoken, { + 'token': idDFI, + 'factor': 2, + 'fixedIntervalPriceId': "DFI/USD"}) try: self.nodes[0].setcollateraltoken({ @@ -211,6 +207,12 @@ def run_test(self): # Move to fork height self.nodes[0].generate(150 - self.nodes[0].getblockcount()) + # Check errors + assert_raises_rpc_error(-32600, "Percentage exceeds 100%", self.nodes[0].setcollateraltoken, { + 'token': idDFI, + 'factor': 1.01, + 'fixedIntervalPriceId': "DFI/USD"}) + # Create collateral token self.nodes[0].setcollateraltoken({ 'token': idGOOGL,