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

WIP Token splits changes from master #1259

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
6 changes: 4 additions & 2 deletions src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -953,9 +953,11 @@ bool IsVaultPriceValid(CCustomCSView& mnview, const CVaultId& vaultId, uint32_t
if (auto loans = mnview.GetLoanTokens(vaultId))
for (const auto& loan : loans->balances)
if (auto loanToken = mnview.GetLoanTokenByID(loan.first))
if (auto fixedIntervalPrice = mnview.GetFixedIntervalPrice(loanToken->fixedIntervalPriceId))
if (auto fixedIntervalPrice = mnview.GetFixedIntervalPrice(loanToken->fixedIntervalPriceId)) {
if (!fixedIntervalPrice.val->isLive(mnview.GetPriceDeviation()))
return false;

} else {
Copy link
Member

@prasannavl prasannavl May 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears correct, and probably should also branch more like this to short-circuit above as well. However, we might want to test this with a sync.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. Already mentioned this to Jeremy. He is performing a full sync of #1243 with the same change.

return false;
}
return true;
}
12 changes: 6 additions & 6 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4188,11 +4188,6 @@ static Res VaultSplits(CCustomCSView& view, CFutureSwapView& futureSwapView, ATT
});

for (auto& [vaultId, rate, schemeId] : loanInterestRates) {
auto amounts = view.GetLoanTokens(vaultId);
if (!amounts) {
return Res::Err("Failed to get loan token amounts.");
}

CAmount loanSchemeRate{0};
try {
loanSchemeRate = loanSchemes.at(schemeId);
Expand All @@ -4202,7 +4197,12 @@ static Res VaultSplits(CCustomCSView& view, CFutureSwapView& futureSwapView, ATT

view.EraseInterestDirect(vaultId, oldTokenId);
rate.interestToHeight = CalculateNewAmount(multiplier, rate.interestToHeight);
rate.interestPerBlock = InterestPerBlockCalculationV2(amounts->balances[newTokenId], loanToken->interest, loanSchemeRate);

auto amounts = view.GetLoanTokens(vaultId);
if (amounts) {
rate.interestPerBlock = InterestPerBlockCalculationV2(amounts->balances[newTokenId], loanToken->interest, loanSchemeRate);
}

view.WriteInterestRate(std::make_pair(vaultId, newTokenId), rate, height);
}

Expand Down
4 changes: 2 additions & 2 deletions test/functional/feature_token_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ def vault_lock(self):
self.nodes[0].generate(1)

# Try and take loan while token in vault locked
assert_raises_rpc_error(-32600, "Fixed interval price currently disabled due to locked token", self.nodes[0].takeloan, {'vaultId': self.vault, 'amounts': f'1@{self.symbolTSLA}'})
assert_raises_rpc_error(-32600, "Fixed interval price currently disabled due to locked token", self.nodes[0].takeloan, {'vaultId': self.vault, 'amounts': f'1@{self.symbolGOOGL}'})
assert_raises_rpc_error(-32600, "Cannot take loan while any of the asset's price in the vault is not live", self.nodes[0].takeloan, {'vaultId': self.vault, 'amounts': f'1@{self.symbolTSLA}'})
assert_raises_rpc_error(-32600, "Cannot take loan while any of the asset's price in the vault is not live", self.nodes[0].takeloan, {'vaultId': self.vault, 'amounts': f'1@{self.symbolGOOGL}'})

# Vault amounts should be zero while token locked
result = self.nodes[0].getvault(self.vault)
Expand Down
64 changes: 61 additions & 3 deletions test/functional/feature_token_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,38 @@ def setup_test_pools(self):
})
self.nodes[0].generate(1)

self.nodes[0].createpoolpair({
"tokenA": self.symbolDUSD,
"tokenB": self.symbolDFI,
"commission": Decimal('0.001'),
"status": True,
"ownerAddress": self.address
}, [])
self.nodes[0].generate(1)

self.nodes[0].createpoolpair({
"tokenA": self.symbolDUSD,
"tokenB": self.symbolNVDA,
"commission": Decimal('0.001'),
"status": True,
"ownerAddress": self.address
}, [])
self.nodes[0].generate(1)

# Fund address for pool
self.nodes[0].utxostoaccount({self.address: f'100@{self.symbolDFI}'})
self.nodes[0].minttokens([f'200@{self.idDUSD}', f'100@{self.idNVDA}'])
self.nodes[0].generate(1)

# Fund pools
self.nodes[0].addpoolliquidity({
self.address: [f'100@{self.symbolDUSD}', f'100@{self.symbolDFI}']
}, self.address)
self.nodes[0].addpoolliquidity({
self.address: [f'100@{self.symbolDUSD}', f'100@{self.symbolNVDA}']
}, self.address)
self.nodes[0].generate(1)

# Store pool ID
self.idGD = list(self.nodes[0].gettoken(self.symbolGD).keys())[0]

Expand Down Expand Up @@ -197,7 +229,7 @@ def setup_test_vaults(self):
# Deposit random collateral
collateral = round(random.uniform(1, 100), 8)
loan = truncate(str(collateral / 3), 8)
self.nodes[0].deposittovault(vault_id, self.address, f'{str(collateral)}@DFI')
self.nodes[0].deposittovault(vault_id, self.address, f'{str(collateral)}@{self.symbolDFI}')
self.nodes[0].generate(1)

# Take loan
Expand Down Expand Up @@ -490,8 +522,9 @@ def execute_vault_split(self, token_id, token_symbol, multiplier, suffix):
# Gather pre-split info to compare later
pre_get_interest = self.nodes[0].getinterest('LOAN0001', 'NVDA')[0]
for vault_info in self.nodes[0].listvaults():
vault = self.nodes[0].getvault(vault_info['vaultId'])
self.vault_balances.append([vault_info['vaultId'], vault])
if self.skip_vault != vault_info['vaultId']:
vault = self.nodes[0].getvault(vault_info['vaultId'])
self.vault_balances.append([vault_info['vaultId'], vault])

# Move to split block
self.nodes[0].generate(1)
Expand Down Expand Up @@ -532,6 +565,31 @@ def execute_vault_split(self, token_id, token_symbol, multiplier, suffix):
self.nodes[0].generate(1)

def vault_split(self):

# Create vault to test split after payback
self.skip_vault = self.nodes[0].createvault(self.address, '')
self.nodes[0].generate(1)

# Deposit to vault
self.nodes[0].deposittovault(self.skip_vault, self.address, f'100@{self.symbolDFI}')
self.nodes[0].generate(1)

# Take loan
self.nodes[0].takeloan({
'vaultId': self.skip_vault,
'amounts': f'100@{self.symbolNVDA}'
})
self.nodes[0].generate(1)

# Payback loan
result = self.nodes[0].getvault(self.skip_vault)
self.nodes[0].paybackloan({
'vaultId': self.skip_vault,
'from': self.address,
'amounts': result['loanAmounts'][0]
})
self.nodes[0].generate(1)

# Multiplier 2
self.execute_vault_split(self.idNVDA, self.symbolNVDA, 2, '/v1')

Expand Down