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

Negative interest rate support #1423

Merged
merged 92 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
e0ec4c3
Fixes type check on verbose parameter
dcorral Jun 13, 2022
66f2707
Merge branch 'master' of github.com:DeFiCh/ain into fix-getvault-verbose
dcorral Jul 6, 2022
4700496
Add high precission string for getvault verbose
dcorral Jul 7, 2022
35b00eb
Fixes error, return -1 when there are locked tokens
dcorral Jul 7, 2022
40d2fda
Remove Boost Thread and Chrono (#1349)
Bushstar Jul 11, 2022
16bc19e
Separate undo database from account database (#1362)
Bushstar Jul 11, 2022
e9ebb04
updatemasternode (#1343)
Bushstar Jul 11, 2022
e3fe62a
Revert "Separate undo database from account database (#1362)"
Bushstar Jul 11, 2022
2df6f72
TX version and expiration (#1346)
Bushstar Jul 11, 2022
67e12c8
Migrate undos to dedicated DB (#1381)
Bushstar Jul 12, 2022
5b91dc6
Merge branch 'master' into fix-getvault-verbose
dcorral Jul 13, 2022
6d5d5a9
Revert "Migrate undos to dedicated DB (#1381)"
Bushstar Jul 17, 2022
f224c4d
Negative Interest
Bushstar Jul 18, 2022
7b6148a
Merge branch 'fix-getvault-verbose' of github.com:DeFiCh/ain into neg…
dcorral Jul 18, 2022
304b7c0
Allows negative interests on loan tokens
dcorral Jul 18, 2022
d3851d8
tests: change error text
Bushstar Jul 18, 2022
8a92bcd
Order getmasternodeblocks results (#1388)
Bushstar Jul 19, 2022
21d0701
Revert "TX version and expiration (#1346)"
Bushstar Jul 25, 2022
bef9c14
Add testing to negative interest
dcorral Jul 28, 2022
e39414b
Remove breakpoint from failing test
dcorral Jul 28, 2022
2a329b1
Allow fallthrough from LoanMintingInterest
Bushstar Aug 2, 2022
14a0857
Update test to fail as expected
Bushstar Aug 2, 2022
ae92630
Apply token interest to all vaults immediately
Bushstar Aug 2, 2022
dc5066c
Partial negative vault interest
Bushstar Aug 4, 2022
6eb99b4
lint: Fix error
Bushstar Aug 4, 2022
3e815b5
Adds tests for negative interests
dcorral Aug 17, 2022
67a92e2
Add failing tests for discussion
dcorral Aug 17, 2022
4ce146a
Fix tests for correct behaviour
dcorral Aug 17, 2022
2425c53
Further negative interest rate updates
Bushstar Aug 18, 2022
79e8847
Check correct height value for incorrect amount. Restores behaviour.
Bushstar Aug 18, 2022
52ef2fc
TotalInterestCalculation use higher precision
Bushstar Aug 18, 2022
3f95ce1
Fix getinterest RPC when token param is passed
Jouzo Aug 18, 2022
cf46254
Add tests for loan paid by negative interest
dcorral Aug 18, 2022
b755727
Do not sub minted token
Bushstar Aug 19, 2022
801789e
Store negative interest the same as positive interest was stored
Bushstar Aug 19, 2022
400582d
Set negative bool in all branches
Bushstar Aug 19, 2022
4c16685
Remove unused variable
Bushstar Aug 19, 2022
da65598
Call EraseInterest on take loan
Bushstar Aug 19, 2022
715366d
Fixes tests to match new implementation
dcorral Aug 19, 2022
acc0e61
Total loan should not wrap around
Bushstar Aug 20, 2022
b20d3f3
Add getstoredinterest
Bushstar Aug 20, 2022
84ac0b1
Simplify interest addition lambda
Bushstar Aug 20, 2022
cd127ea
Add TotalInterestCalculation unit test
Jouzo Aug 20, 2022
439a451
Fix interest cal on tests after new updates
dcorral Aug 22, 2022
2164a44
Add new suite of functional test for getstoredinterest
dcorral Aug 22, 2022
1724978
Introduce helper functions and function template
dcorral Aug 22, 2022
2060940
Add helper functions
dcorral Aug 22, 2022
140a78b
Add stored interest test
Bushstar Aug 22, 2022
3cd816c
Add negative ITH positive IPB cases
Bushstar Aug 22, 2022
2f97316
Combine duplicate interest additions
Bushstar Aug 22, 2022
1a4e140
Update vault failing test
dcorral Aug 22, 2022
1ebf931
Pass new scheme to StoreInterest
Bushstar Aug 22, 2022
b910ffb
Add updatevault complete tests
dcorral Aug 22, 2022
d3da038
Remove subInterest assignation
dcorral Aug 22, 2022
be83d04
Adds test for takeloan
dcorral Aug 22, 2022
9c8a994
Wipe interest on take loan with negative interest
Bushstar Aug 23, 2022
bb3d90d
Test full payback
Bushstar Aug 23, 2022
a4edbd0
On take loan negate existing loan amount only
Bushstar Aug 24, 2022
1c8eeeb
Update negative interest test
Bushstar Aug 24, 2022
2c2c4d6
Do not sub balance on payback if loan negated
Bushstar Aug 24, 2022
0179613
Negative interest pays loan back first
Bushstar Aug 24, 2022
d2c134c
Add payback loan functional tests
Jouzo Aug 24, 2022
896f4e0
Adds remainning tests for takeloan
dcorral Aug 24, 2022
4f9950f
Merge branch 'master' into epic/v3
prasannavl Aug 25, 2022
227bbe8
Merge branch 'epic/v3' of ssh://github.com/DeFiCh/ain into negative-i…
prasannavl Aug 25, 2022
648e4f7
Revert "updatemasternode (#1343)"
prasannavl Aug 25, 2022
fcc8dd8
Checked expected ITH
Bushstar Aug 25, 2022
cd10158
Fix lint error
dcorral Aug 25, 2022
8ce856f
Extend basic negative interest tests
Bushstar Aug 25, 2022
3dd47d4
Add failing case for paid loan and take loan again
dcorral Aug 25, 2022
c32398a
Merge branch 'master' into feature/negative_interest
Jouzo Aug 25, 2022
d3939b7
Merge branch 'master' into feature/negative_interest
prasannavl Aug 26, 2022
ea4f6a2
Fix tests
dcorral Aug 26, 2022
457c9e1
Merge branch 'feature/negative_interest' of github.com:DeFiCh/ain int…
dcorral Aug 26, 2022
81eb92b
Reduce or remove loan amounts from auctions
Bushstar Aug 26, 2022
78ed2ab
Add takeloan input guards
prasannavl Aug 26, 2022
748e2a6
Add payback input guards
prasannavl Aug 26, 2022
4894d20
Fix string format
prasannavl Aug 26, 2022
1b91268
Reset interest on migration with recalculations
prasannavl Aug 26, 2022
6743c04
Bring payback amount checks in line with takeloan checks
prasannavl Aug 26, 2022
14d679c
Add fork guard to new amount guards
Bushstar Aug 26, 2022
f049adf
Floor negative interest
Bushstar Aug 26, 2022
b0886a0
Merge branch 'master' into feature/negative_interest
Bushstar Aug 26, 2022
d8ebeab
Do not store negative zero
Bushstar Aug 26, 2022
1780f33
Addapt test for rounding in negative interest amounts
dcorral Aug 26, 2022
9461e58
Merge branch 'master' into feature/negative_interest
prasannavl Aug 26, 2022
4010f1e
Handle zero "positive" interest before minimum interest introduced
Bushstar Aug 28, 2022
5d9f40c
Rename functions
Bushstar Aug 28, 2022
8c532fb
Update negative interest on withdraw from vault
Bushstar Aug 28, 2022
2ad9a91
Move fork guard
Bushstar Aug 29, 2022
a3da303
Sub full loan amount. Wipe negative interest in liquidation.
Bushstar Aug 29, 2022
e075fb7
Adds test for auction and negative interests
dcorral Aug 29, 2022
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
58 changes: 50 additions & 8 deletions src/masternodes/govvariables/attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ const std::map<std::string, uint8_t>& ATTRIBUTES::allowedParamIDs() {
{"dfip2201", ParamIDs::DFIP2201},
{"dfip2203", ParamIDs::DFIP2203},
{"dfip2206a", ParamIDs::DFIP2206A},
// Note: DFIP2206F is currently in beta testing
// for testnet. May not be enabled on mainnet until testing is complete.
// Note: DFIP2206F is currently in beta testing
// for testnet. May not be enabled on mainnet until testing is complete.
{"dfip2206f", ParamIDs::DFIP2206F},
};
return params;
Expand All @@ -98,8 +98,8 @@ const std::map<uint8_t, std::string>& ATTRIBUTES::displayParamsIDs() {
{ParamIDs::DFIP2201, "dfip2201"},
{ParamIDs::DFIP2203, "dfip2203"},
{ParamIDs::DFIP2206A, "dfip2206a"},
// Note: DFIP2206F is currently in beta testing
// for testnet. May not be enabled on mainnet until testing is complete.
// Note: DFIP2206F is currently in beta testing
// for testnet. May not be enabled on mainnet until testing is complete.
{ParamIDs::DFIP2206F, "dfip2206f"},
{ParamIDs::TokenID, "token"},
{ParamIDs::Economy, "economy"},
Expand Down Expand Up @@ -245,6 +245,14 @@ static ResVal<CAttributeValue> VerifyInt64(const std::string& str) {
}

static ResVal<CAttributeValue> VerifyFloat(const std::string& str) {
CAmount amount = 0;
if (!ParseFixedPoint(str, 8, &amount)) {
return Res::Err("Amount must be a valid number");
}
return {amount, Res::Ok()};
}

ResVal<CAttributeValue> VerifyPositiveFloat(const std::string& str) {
CAmount amount = 0;
if (!ParseFixedPoint(str, 8, &amount) || amount < 0) {
return Res::Err("Amount must be a positive value");
Expand All @@ -253,7 +261,7 @@ static ResVal<CAttributeValue> VerifyFloat(const std::string& str) {
}

static ResVal<CAttributeValue> VerifyPct(const std::string& str) {
auto resVal = VerifyFloat(str);
auto resVal = VerifyPositiveFloat(str);
if (!resVal) {
return resVal;
}
Expand Down Expand Up @@ -359,7 +367,7 @@ const std::map<uint8_t, std::map<uint8_t,
AttributeTypes::Param, {
{DFIPKeys::Active, VerifyBool},
{DFIPKeys::Premium, VerifyPct},
{DFIPKeys::MinSwap, VerifyFloat},
{DFIPKeys::MinSwap, VerifyPositiveFloat},
{DFIPKeys::RewardPct, VerifyPct},
{DFIPKeys::BlockPeriod, VerifyInt64},
{DFIPKeys::DUSDInterestBurn, VerifyBool},
Expand Down Expand Up @@ -708,6 +716,8 @@ Res ATTRIBUTES::Import(const UniValue & val) {

SetValue(attribute, *splitValue);
return Res::Ok();
} else if (attrV0->type == AttributeTypes::Token && attrV0->key == TokenKeys::LoanMintingInterest) {
interestTokens.insert(attrV0->typeId);
}

// apply DFI via old keys
Expand Down Expand Up @@ -907,10 +917,17 @@ Res ATTRIBUTES::Validate(const CCustomCSView & view) const
return Res::Err("No such token (%d)", attrV0->typeId);
}
break;
case TokenKeys::LoanMintingInterest:
if (view.GetLastHeight() < Params().GetConsensus().GreatWorldHeight) {
const auto amount = std::get_if<CAmount>(&value);
if (amount && *amount < 0) {
return Res::Err("Amount must be a positive value");
}
}
[[fallthrough]];
case TokenKeys::LoanCollateralEnabled:
case TokenKeys::LoanCollateralFactor:
case TokenKeys::LoanMintingEnabled:
case TokenKeys::LoanMintingInterest: {
case TokenKeys::LoanMintingEnabled: {
if (view.GetLastHeight() < Params().GetConsensus().FortCanningCrunchHeight) {
return Res::Err("Cannot be set before FortCanningCrunch");
}
Expand Down Expand Up @@ -1135,6 +1152,31 @@ Res ATTRIBUTES::Apply(CCustomCSView & mnview, const uint32_t height)
if (!res) {
return res;
}
} else if (attrV0->key == TokenKeys::LoanMintingInterest) {
if (height >= static_cast<uint32_t>(Params().GetConsensus().GreatWorldHeight) && interestTokens.count(attrV0->typeId)) {
const auto tokenInterest = std::get_if<CAmount>(&attribute.second);
if (!tokenInterest) {
return Res::Err("Unexpected type");
}

std::set<CVaultId> affectedVaults;
mnview.ForEachLoanTokenAmount([&](const CVaultId& vaultId, const CBalances& balances){
for (const auto& [tokenId, discarded] : balances.balances) {
if (tokenId.v == attrV0->typeId) {
affectedVaults.insert(vaultId);
}
}
return true;
});

for (const auto& vaultId : affectedVaults) {
const auto vault = mnview.GetVault(vaultId);
assert(vault);

// Updated stored interest with new interest rate.
mnview.IncreaseInterest(height, vaultId, vault->schemeId, {attrV0->typeId}, *tokenInterest, 0);
}
}
}
} else if (attrV0->type == AttributeTypes::Param) {
if (attrV0->typeId == ParamIDs::DFIP2203) {
Expand Down
1 change: 1 addition & 0 deletions src/masternodes/govvariables/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ class ATTRIBUTES : public GovVariable, public AutoRegistrator<GovVariable, ATTRI
bool futureUpdated{};
bool futureDUSDUpdated{};
std::set<uint32_t> tokenSplits{};
std::set<uint32_t> interestTokens{};
std::set<CAttributeType> changed;
std::map<CAttributeType, CAttributeValue> attributes;

Expand Down
Loading