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

Invert DFIP2203 token flag. Fix bug in attrs to refund disabled token. #1168

Merged
merged 1 commit into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/masternodes/govvariables/attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const std::map<uint8_t, std::map<std::string, uint8_t>>& ATTRIBUTES::allowedKeys
{"loan_payback_fee_pct",TokenKeys::LoanPaybackFeePCT},
{"dex_in_fee_pct", TokenKeys::DexInFeePct},
{"dex_out_fee_pct", TokenKeys::DexOutFeePct},
{"dfip2203_disabled", TokenKeys::DFIP2203Disabled},
{"dfip2203", TokenKeys::DFIP2203Enabled},
}
},
{
Expand Down Expand Up @@ -128,7 +128,7 @@ const std::map<uint8_t, std::map<uint8_t, std::string>>& ATTRIBUTES::displayKeys
{TokenKeys::LoanPaybackFeePCT,"loan_payback_fee_pct"},
{TokenKeys::DexInFeePct, "dex_in_fee_pct"},
{TokenKeys::DexOutFeePct, "dex_out_fee_pct"},
{TokenKeys::DFIP2203Disabled, "dfip2203_disabled"},
{TokenKeys::DFIP2203Enabled, "dfip2203"},
}
},
{
Expand Down Expand Up @@ -212,7 +212,7 @@ const std::map<uint8_t, std::map<uint8_t,
{TokenKeys::LoanPaybackFeePCT,VerifyPct},
{TokenKeys::DexInFeePct, VerifyPct},
{TokenKeys::DexOutFeePct, VerifyPct},
{TokenKeys::DFIP2203Disabled, VerifyBool},
{TokenKeys::DFIP2203Enabled, VerifyBool},
}
},
{
Expand Down Expand Up @@ -550,7 +550,7 @@ Res ATTRIBUTES::Validate(const CCustomCSView & view) const
return Res::Err("No such token (%d)", attrV0->typeId);
}
break;
case TokenKeys::DFIP2203Disabled:
case TokenKeys::DFIP2203Enabled:
if (view.GetLastHeight() < Params().GetConsensus().FortCanningRoadHeight) {
return Res::Err("Cannot be set before FortCanningRoad");
}
Expand Down Expand Up @@ -633,7 +633,7 @@ Res ATTRIBUTES::Apply(CCustomCSView & mnview, const uint32_t height)
return res;
}
}
if (attrV0->key == TokenKeys::DFIP2203Disabled) {
if (attrV0->key == TokenKeys::DFIP2203Enabled) {

// Skip on block period change to avoid refunding and erasing entries.
// Block period change will check for conflicting entries, deleting them
Expand All @@ -642,6 +642,11 @@ Res ATTRIBUTES::Apply(CCustomCSView & mnview, const uint32_t height)
continue;
}

auto value = boost::get<bool>(attribute.second);
if (value) {
continue;
}

const auto token = mnview.GetLoanTokenByID(DCT_ID{attrV0->typeId});
if (!token) {
return Res::Err("No such loan token (%d)", attrV0->typeId);
Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/govvariables/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ enum TokenKeys : uint8_t {
LoanPaybackFeePCT = 'd',
DexInFeePct = 'e',
DexOutFeePct = 'f',
DFIP2203Disabled = 'g',
DFIP2203Enabled = 'g',
};

enum PoolKeys : uint8_t {
Expand Down
12 changes: 6 additions & 6 deletions src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1497,9 +1497,9 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor
}

if (source->symbol == "DUSD") {
CDataStructureV0 tokenKey{AttributeTypes::Token, obj.destination, TokenKeys::DFIP2203Disabled};
const auto disabled = attributes->GetValue(tokenKey, false);
if (disabled) {
CDataStructureV0 tokenKey{AttributeTypes::Token, obj.destination, TokenKeys::DFIP2203Enabled};
const auto enabled = attributes->GetValue(tokenKey, true);
if (!enabled) {
return Res::Err("DFIP2203 currently disabled for token %d", obj.destination);
}

Expand All @@ -1512,9 +1512,9 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor
return Res::Err("Destination should not be set when source amount is a dToken");
}

CDataStructureV0 tokenKey{AttributeTypes::Token, obj.source.nTokenId.v, TokenKeys::DFIP2203Disabled};
const auto disabled = attributes->GetValue(tokenKey, false);
if (disabled) {
CDataStructureV0 tokenKey{AttributeTypes::Token, obj.source.nTokenId.v, TokenKeys::DFIP2203Enabled};
const auto enabled = attributes->GetValue(tokenKey, true);
if (!enabled) {
return Res::Err("DFIP2203 currently disabled for token %s", obj.source.nTokenId.ToString());
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3321,12 +3321,12 @@ void CChainState::ProcessFutures(const CBlockIndex* pindex, CCustomCSView& cache
const auto premium{COIN + rewardPct};

std::map<DCT_ID, CFuturesPrice> futuresPrices;
CDataStructureV0 tokenKey{AttributeTypes::Token, 0, TokenKeys::DFIP2203Disabled};
CDataStructureV0 tokenKey{AttributeTypes::Token, 0, TokenKeys::DFIP2203Enabled};

cache.ForEachLoanToken([&](const DCT_ID& id, const CLoanView::CLoanSetLoanTokenImpl& loanToken) {
tokenKey.typeId = id.v;
const auto disabled = attributes->GetValue(tokenKey, false);
if (disabled) {
const auto enabled = attributes->GetValue(tokenKey, true);
if (!enabled) {
return true;
}

Expand Down
28 changes: 24 additions & 4 deletions test/functional/feature_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,12 @@ def futures_setup(self):
assert_equal(result['v0/params/dfip2203/block_period'], str(self.futures_interval))

# Disable DUSD
self.nodes[0].setgov({"ATTRIBUTES":{f'v0/token/{str(self.idDUSD)}/dfip2203_disabled':'true'}})
self.nodes[0].setgov({"ATTRIBUTES":{f'v0/token/{str(self.idDUSD)}/dfip2203':'false'}})
self.nodes[0].generate(1)

# Verify Gov vars
result = self.nodes[0].getgov('ATTRIBUTES')['ATTRIBUTES']
assert_equal(result[f'v0/token/{self.idDUSD}/dfip2203_disabled'], 'true')
assert_equal(result[f'v0/token/{self.idDUSD}/dfip2203'], 'false')

# Check futures block
next_futures_block = self.nodes[0].getblockcount() + (self.futures_interval - (self.nodes[0].getblockcount() % self.futures_interval))
Expand Down Expand Up @@ -832,7 +832,7 @@ def check_gov_var_change(self):
self.nodes[0].generate(1)

# Disable GOOGL
self.nodes[0].setgov({"ATTRIBUTES":{f'v0/token/{str(self.idGOOGL)}/dfip2203_disabled':'true'}})
self.nodes[0].setgov({"ATTRIBUTES":{f'v0/token/{str(self.idGOOGL)}/dfip2203':'false'}})
self.nodes[0].generate(1)

# Only TSLA contract should remain
Expand All @@ -850,6 +850,26 @@ def check_gov_var_change(self):
result = self.nodes[0].getaccount(address_tsla)
assert_equal(result, [])

# Enable GOOGL
self.nodes[0].setgov({"ATTRIBUTES":{f'v0/token/{str(self.idGOOGL)}/dfip2203':'true'}})
self.nodes[0].generate(1)

# Create user futures contracts
self.nodes[0].futureswap(address_googl, f'{self.prices[1]["premiumPrice"]}@{self.symbolDUSD}', int(self.idGOOGL))
self.nodes[0].generate(1)

# GOOGL balance should be empty
result = self.nodes[0].getaccount(address_googl)
assert_equal(result, [])

# Disable GOOGL
self.nodes[0].setgov({"ATTRIBUTES":{f'v0/token/{str(self.idGOOGL)}/dfip2203':'false'}})
self.nodes[0].generate(1)

# Balance should be restored
result = self.nodes[0].getaccount(address_googl)
assert_equal(result, [f'{self.prices[1]["premiumPrice"]}@{self.symbolDUSD}'])

# Move to next futures block
next_futures_block = self.nodes[0].getblockcount() + (self.futures_interval - (self.nodes[0].getblockcount() % self.futures_interval))
self.nodes[0].generate(next_futures_block - self.nodes[0].getblockcount())
Expand Down Expand Up @@ -940,7 +960,7 @@ def rpc_history(self):

# Check all swap refunds present
result = self.nodes[0].listaccounthistory('all', {'txtype':'w'})
assert_equal(len(result), 7)
assert_equal(len(result), 9)

# Check swap by specific address
result = self.nodes[0].listaccounthistory(self.list_history[0]['swaps'][0]['address'], {'txtype':'q'})
Expand Down
12 changes: 6 additions & 6 deletions test/functional/feature_setgov.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def run_test(self):
assert_raises_rpc_error(-5, "Empty value", self.nodes[0].setgov, {"ATTRIBUTES":{'v0/token/15/payback_dfi':''}})
assert_raises_rpc_error(-5, "Incorrect key for <type>. Object of ['<version>/<type>/ID/<key>','value'] expected", self.nodes[0].setgov, {"ATTRIBUTES":{'v0/token/payback_dfi':'true'}})
assert_raises_rpc_error(-5, "Unrecognised type argument provided, valid types are: params, poolpairs, token,", self.nodes[0].setgov, {"ATTRIBUTES":{'v0/unrecognised/5/payback_dfi':'true'}})
assert_raises_rpc_error(-5, "Unrecognised key argument provided, valid keys are: dex_in_fee_pct, dex_out_fee_pct, dfip2203_disabled, loan_payback, loan_payback_fee_pct, payback_dfi, payback_dfi_fee_pct,", self.nodes[0].setgov, {"ATTRIBUTES":{'v0/token/5/unrecognised':'true'}})
assert_raises_rpc_error(-5, "Unrecognised key argument provided, valid keys are: dex_in_fee_pct, dex_out_fee_pct, dfip2203, loan_payback, loan_payback_fee_pct, payback_dfi, payback_dfi_fee_pct,", self.nodes[0].setgov, {"ATTRIBUTES":{'v0/token/5/unrecognised':'true'}})
assert_raises_rpc_error(-5, "Value must be a positive integer", self.nodes[0].setgov, {"ATTRIBUTES":{'v0/token/not_a_number/payback_dfi':'true'}})
assert_raises_rpc_error(-5, 'Boolean value must be either "true" or "false"', self.nodes[0].setgov, {"ATTRIBUTES":{'v0/token/5/payback_dfi':'not_a_number'}})
assert_raises_rpc_error(-5, 'Boolean value must be either "true" or "false"', self.nodes[0].setgov, {"ATTRIBUTES":{'v0/token/5/payback_dfi':'unrecognised'}})
Expand All @@ -465,7 +465,7 @@ def run_test(self):
assert_raises_rpc_error(-32600, "ATTRIBUTES: Cannot be set before FortCanningRoad", self.nodes[0].setgov, {"ATTRIBUTES":{'v0/params/dfip2203/active':'true'}})
assert_raises_rpc_error(-32600, "ATTRIBUTES: Cannot be set before FortCanningRoad", self.nodes[0].setgov, {"ATTRIBUTES":{'v0/params/dfip2203/reward_pct':'0.05'}})
assert_raises_rpc_error(-32600, "ATTRIBUTES: Cannot be set before FortCanningRoad", self.nodes[0].setgov, {"ATTRIBUTES":{'v0/params/dfip2203/block_period':'2880'}})
assert_raises_rpc_error(-32600, "ATTRIBUTES: Cannot be set before FortCanningRoad", self.nodes[0].setgov, {"ATTRIBUTES":{'v0/token/5/dfip2203_disabled':'true'}})
assert_raises_rpc_error(-32600, "ATTRIBUTES: Cannot be set before FortCanningRoad", self.nodes[0].setgov, {"ATTRIBUTES":{'v0/token/5/dfip2203':'true'}})

# Setup for loan related tests
oracle_address = self.nodes[0].getnewaddress("", "legacy")
Expand Down Expand Up @@ -566,13 +566,13 @@ def run_test(self):
assert_raises_rpc_error(-5, "Percentage exceeds 100%", self.nodes[0].setgov, {"ATTRIBUTES":{'v0/params/dfip2203/reward_pct':'2'}})
assert_raises_rpc_error(-5, "Value must be a positive integer", self.nodes[0].setgov, {"ATTRIBUTES":{'v0/params/dfip2203/block_period':'not_a_number'}})
assert_raises_rpc_error(-5, "Value must be a positive integer", self.nodes[0].setgov, {"ATTRIBUTES":{'v0/params/dfip2203/block_period':'-1'}})
assert_raises_rpc_error(-5, "Boolean value must be either \"true\" or \"false\"", self.nodes[0].setgov, {"ATTRIBUTES":{'v0/token/5/dfip2203_disabled':'not_a_bool'}})
assert_raises_rpc_error(-32600, "No such loan token", self.nodes[0].setgov, {"ATTRIBUTES":{'v0/token/4/dfip2203_disabled':'true'}})
assert_raises_rpc_error(-5, "Boolean value must be either \"true\" or \"false\"", self.nodes[0].setgov, {"ATTRIBUTES":{'v0/token/5/dfip2203':'not_a_bool'}})
assert_raises_rpc_error(-32600, "No such loan token", self.nodes[0].setgov, {"ATTRIBUTES":{'v0/token/4/dfip2203':'true'}})
assert_raises_rpc_error(-5, "Unsupported type for DFIP2203", self.nodes[0].setgov, {"ATTRIBUTES":{'v0/params/dfip2203/premium': '0.025'}})
assert_raises_rpc_error(-5, "Unsupported type for DFIP2203", self.nodes[0].setgov, {"ATTRIBUTES":{'v0/params/dfip2203/minswap': '0.025'}})

# Test setting FCR ATTRBIUTES
self.nodes[0].setgov({"ATTRIBUTES":{'v0/params/dfip2203/reward_pct':'0.05','v0/params/dfip2203/block_period':'20160','v0/token/5/dfip2203_disabled':'true'}})
self.nodes[0].setgov({"ATTRIBUTES":{'v0/params/dfip2203/reward_pct':'0.05','v0/params/dfip2203/block_period':'20160','v0/token/5/dfip2203':'true'}})
self.nodes[0].setgov({"ATTRIBUTES":{'v0/params/dfip2203/active':'true'}})
self.nodes[0].generate(1)

Expand All @@ -581,7 +581,7 @@ def run_test(self):
assert_equal(result['v0/params/dfip2203/active'], 'true')
assert_equal(result['v0/params/dfip2203/reward_pct'], '0.05')
assert_equal(result['v0/params/dfip2203/block_period'], '20160')
assert_equal(result['v0/token/5/dfip2203_disabled'], 'true')
assert_equal(result['v0/token/5/dfip2203'], 'true')

if __name__ == '__main__':
GovsetTest ().main ()