-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing the dToken restart (#2976)
* started test file TD not working yet, no idea why * fixed test setup * fmt * adapted test * added LockToken function to validation and trigger on fork height already doing paybackwithcollateral * added SC address for TokenLock * added DB entries for TokenLock UserBalances * split up ProcessTokenLock for easier reading * splitting loantokens leads to EVM-block hash mismatch. no idea why yet. * first shot on adding coinbase txs and pool splits. * got pool "splits" and tokens working EVM hash still wrong. no idea why. * added collateral to tokenSplit * added todos * locking away tokens from pools, collateral and balances * refactor: extract lockup method * added rpc calls, improved amount minted * added function to payback loans directly or from collateral DUSD is burned directly for dToken loans * refactor and added swapping of collateral if there is a pool * fmt * fmt * improved tests * fix payback in DUSD case with DUSD collateral * added further testcase, fix missing interest on paybackswaps * first shot on lock-release tx * tx working, tests not updated yet. need to figure out why locked tokens sometimes have fi difference * improved poolsplit logic fixed tests * added composite swaps on payback, also considering fees now * added cancelation of Auctions * added lock during TD * added handling of locks to stocksplits * fix TD lock to only lock when old token is transferred * fix: locked tokens must not be upgraded on EVM * refactor and fmt * improvements from review * review refactor, updated test values * prevent circular dep warning * refactor: remove dependency and mark circular as expected * reenable blockhash error still fails. still no idea why. * moved restart logic to DF25 * lint fixes * fmt * revert unneeded changes * Fix compilation error. Use snapshots. * remove new poolsplit logic into seperate PR * Miner logic, increment ID for each token and add rename for DUSD * adapted test data to seperated split logic * Remove code repetition * Remove copy pasted comment * Move USDD creation to be last miner side * Remove rename of DUSD. Unlock new tokens. * Update closing of vault auctions * Trigger dToken restart via attributes * Allow dToken restart to fail * Format CPP * added check to only allow lock if not locked before * Fix broken attribute logic * fix payback return value, added early returns on failure * Add lock ratio to attributes * added attribute check and improved logs and comments * Add missing setting of attributes. Format CPP. * Print subLoan without debug * Payback balance available * SubLoanToken after amount calculated * added check for owner balance at the start to reduce complexity later * fix handling of owner balance in case of multiple vaults per owner * improved logging * refactor to consolidate rewards before locking and cache pools and better logging * fix handling of payback if no DUSD collateral left * fixed floating errors on payback, removed unnecessary logging * fail if loans left after payback * fix calc of rewards before token split * add reward consolidation before loan payback * remove unnecessary log * move to DF24 * fix logs * Add missing forks to test * Add prefix checks and fix overlaps * Add another missing fork * fixed tests * lint: format CPP * added helper for fork params in tests added fixme with weird double-commission reference * fix: added ensure uniqueness of owners in ConsolidateRewards * fix cncurrency issue of ConsolidateRewards fixes #3001 * improve history: only 1 entry per address for tokenlock added tests for history entries * Post merge changes * Do not add EVM TXs if prices are invalid * added test checks for history. currently weird behaviour * fix comment in test * Update test * lint: format Python * lint: format CPP * Calculate static rewards by getting start balance from the block before * lint: format Python * lint: Format CPP * Migrate commission on split * Get tokens direct from attributes * Fix commission bug * Push failing test * fix rounding bug on collateral swap estimation * fix test values after rounding fix * Push failing minimal balances test * adapt locking logic: in case of rounding errors, add the extra fi to lock. * Add interest test * lint: remove unused import * Avoid clash with other pending changes * Update member outside of static function --------- Co-authored-by: Prasanna Loganathar <[email protected]> Co-authored-by: Bushstar <[email protected]>
- Loading branch information
1 parent
ca158f5
commit 8e1b75c
Showing
30 changed files
with
4,842 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright (c) 2023 The DeFi Blockchain Developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file LICENSE or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#include <consensus/params.h> | ||
#include <dfi/accountshistory.h> | ||
#include <dfi/consensus/tokenlock.h> | ||
#include <dfi/govvariables/attributes.h> | ||
#include <dfi/historywriter.h> | ||
#include <dfi/masternodes.h> | ||
#include <dfi/mn_checks.h> | ||
|
||
Res CTokenLockConsensus::operator()(const CReleaseLockMessage &obj) const { | ||
if (!HasFoundationAuth()) { | ||
return Res::Err("tx not from foundation member"); | ||
} | ||
if (obj.releasePart == 0) { | ||
return Res::Err("release ratio can not be 0"); | ||
} | ||
const auto &tx = txCtx.GetTransaction(); | ||
auto &mnview = blockCtx.GetView(); | ||
|
||
// get current ratio from attributes | ||
auto attributes = mnview.GetAttributes(); | ||
|
||
CDataStructureV0 releaseKey{AttributeTypes::Live, ParamIDs::Economy, EconomyKeys::TokenLockRatio}; | ||
auto currentRatio = attributes->GetValue(releaseKey, CAmount{}); | ||
if (currentRatio < obj.releasePart) { | ||
return Res::Err("can't release more than locked %s vs %s", | ||
GetDecimalString(currentRatio), | ||
GetDecimalString(obj.releasePart)); | ||
} | ||
|
||
auto newRatio = currentRatio - obj.releasePart; | ||
// part of locked funds to be released | ||
auto releaseAmount = [&](const CAmount &amount) { | ||
if (currentRatio <= obj.releasePart) { | ||
return amount; | ||
} else { | ||
return MultiplyDivideAmounts(amount, obj.releasePart, currentRatio); | ||
} | ||
}; | ||
LogPrintf("releasing locked tokens, current ratio %s, releasing %s, resulting ratio %s. \n", | ||
GetDecimalString(currentRatio), | ||
GetDecimalString(obj.releasePart), | ||
GetDecimalString(newRatio)); | ||
|
||
// calc part of current funds that should be released | ||
// cap to all funds | ||
// for each tokenlock: release funds and update values | ||
const auto contractAddressValue = blockCtx.GetConsensus().smartContracts.at(SMART_CONTRACT_TOKENLOCK); | ||
std::vector<CTokenLockUserKey> todelete{}; | ||
|
||
CBalances totalReleasedFunds; | ||
mnview.ForEachTokenLockUserValues([&](const auto &key, const auto &value) { | ||
const auto &owner = key.owner; | ||
auto newBalance = CTokenLockUserValue{}; | ||
bool gotNew = false; | ||
CAccountsHistoryWriter view( | ||
mnview, blockCtx.GetHeight(), txCtx.GetTxn(), tx.GetHash(), uint8_t(CustomTxType::TokenLockRelease)); | ||
|
||
for (const auto &[tokenId, amount] : value.balances) { | ||
const CTokenAmount moved = {tokenId, releaseAmount(amount)}; | ||
|
||
view.AddBalance(owner, moved); | ||
totalReleasedFunds.Add(moved); | ||
auto updated = amount - moved.nValue; | ||
if (updated > 0) { | ||
newBalance.Add({tokenId, updated}); | ||
gotNew = true; | ||
} | ||
} | ||
view.Flush(); | ||
if (gotNew) { | ||
mnview.StoreTokenLockUserValues(key, newBalance); | ||
} else { | ||
todelete.emplace_back(key); | ||
} | ||
return true; | ||
}); | ||
|
||
CAccountsHistoryWriter view( | ||
mnview, blockCtx.GetHeight(), txCtx.GetTxn(), tx.GetHash(), uint8_t(CustomTxType::TokenLockRelease)); | ||
for (const auto &[tokenId, amount] : totalReleasedFunds.balances) { | ||
view.SubBalance(contractAddressValue, {tokenId, amount}); | ||
} | ||
view.Flush(); | ||
|
||
attributes->SetValue(releaseKey, newRatio); | ||
mnview.SetVariable(*attributes); | ||
for (const auto &key : todelete) { | ||
mnview.EraseTokenLockUserValues(key); | ||
} | ||
|
||
return Res::Ok(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) 2023 The DeFi Blockchain Developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file LICENSE or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef DEFI_DFI_CONSENSUS_TOKENLOCK_H | ||
#define DEFI_DFI_CONSENSUS_TOKENLOCK_H | ||
|
||
#include <dfi/consensus/txvisitor.h> | ||
|
||
struct CReleaseLockMessage; | ||
|
||
class CTokenLockConsensus : public CCustomTxVisitor { | ||
public: | ||
using CCustomTxVisitor::CCustomTxVisitor; | ||
Res operator()(const CReleaseLockMessage &obj) const; | ||
}; | ||
|
||
#endif // DEFI_DFI_CONSENSUS_TOKENLOCK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.