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 divergence check and max slippage #105

Merged
merged 22 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from 10 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
393 changes: 241 additions & 152 deletions contracts/connectors/loantoken/LoanTokenLogicStandard.sol

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions contracts/feeds/PriceFeeds.sol
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ contract PriceFeeds is Constants, Ownable {
if (globalPricingPaused) {
return 0;
}

(uint256 rate, uint256 precision) = _queryRate(sourceToken, destToken);

destAmount = sourceAmount.mul(rate).div(precision);
Expand Down Expand Up @@ -155,11 +156,9 @@ contract PriceFeeds is Constants, Ownable {

sourceToDestSwapRate = destAmount.mul(precision).div(sourceAmount);

uint256 spreadValue = sourceToDestSwapRate > rate ? sourceToDestSwapRate - rate : rate - sourceToDestSwapRate;

if (spreadValue != 0) {
if (rate > sourceToDestSwapRate) {

Choose a reason for hiding this comment

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

Why is the case where rate < sourceToDestSwapRate is not considered anymore?

uint256 spreadValue = rate - sourceToDestSwapRate;
spreadValue = spreadValue.mul(10**20).div(sourceToDestSwapRate);

require(spreadValue <= maxSlippage, "price disagreement");
}
}
Expand Down Expand Up @@ -353,9 +352,11 @@ contract PriceFeeds is Constants, Ownable {
* @param isPaused The new status of pause (true/false).
* */
function setGlobalPricingPaused(bool isPaused) external onlyOwner {
globalPricingPaused = isPaused;
if (globalPricingPaused != isPaused) {
globalPricingPaused = isPaused;

emit GlobalPricingPaused(msg.sender, isPaused);
emit GlobalPricingPaused(msg.sender, isPaused);
}
}

/*
Expand Down
13 changes: 13 additions & 0 deletions contracts/modules/SwapsExternal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ contract SwapsExternal is VaultController, SwapsUser {
function initialize(address target) external onlyOwner {
_setTarget(this.swapExternal.selector, target);
_setTarget(this.getSwapExpectedReturn.selector, target);
_setTarget(this.checkPriceDivergence.selector, target);
}

/**
Expand All @@ -66,9 +67,11 @@ contract SwapsExternal is VaultController, SwapsUser {
address returnToSender,
uint256 sourceTokenAmount,
uint256 requiredDestTokenAmount,
uint256 minReturn,
bytes memory swapData
) public payable nonReentrant returns (uint256 destTokenAmountReceived, uint256 sourceTokenAmountUsed) {
require(sourceTokenAmount != 0, "sourceTokenAmount == 0");
checkPriceDivergence(sourceToken, destToken, sourceTokenAmount, minReturn);

/// @dev Get payed value, be it rBTC or tokenized.
if (msg.value != 0) {
Expand Down Expand Up @@ -138,4 +141,14 @@ contract SwapsExternal is VaultController, SwapsUser {
) external view returns (uint256) {
return _swapsExpectedReturn(sourceToken, destToken, sourceTokenAmount);
}

function checkPriceDivergence(
address sourceToken,
address destToken,
uint256 sourceTokenAmount,
uint256 minReturn
) public view {
uint256 destTokenAmount = _swapsExpectedReturn(sourceToken, destToken, sourceTokenAmount);
require(destTokenAmount >= minReturn, "destTokenAmountReceived too low");
}
}
Loading