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

Add import call when setting tokens #1275

Merged
merged 2 commits into from
May 24, 2022
Merged
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
60 changes: 51 additions & 9 deletions src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
@@ -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)
@@ -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)
@@ -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)
20 changes: 11 additions & 9 deletions test/functional/feature_loan_setcollateraltoken.py
Original file line number Diff line number Diff line change
@@ -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,