Skip to content

Commit

Permalink
Add import call when setting tokens (#1275)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bushstar authored May 24, 2022
1 parent 06efc01 commit 576503a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 18 deletions.
60 changes: 51 additions & 9 deletions src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ATTRIBUTES>(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)
Expand Down Expand Up @@ -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<ATTRIBUTES>(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)
Expand Down Expand Up @@ -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<ATTRIBUTES>(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)
Expand Down
20 changes: 11 additions & 9 deletions test/functional/feature_loan_setcollateraltoken.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 576503a

Please sign in to comment.