Skip to content

Commit

Permalink
Added Swap tests and removed 'receiverAddress'
Browse files Browse the repository at this point in the history
  • Loading branch information
smbsp committed Jun 30, 2021
1 parent 2db5c51 commit 0ebbf95
Show file tree
Hide file tree
Showing 7 changed files with 334 additions and 6 deletions.
25 changes: 25 additions & 0 deletions contracts/interfaces/ISovryn.sol
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,29 @@ contract ISovryn is
function getAffiliateRewardsHeld(address referrer) external view returns (uint256);

function getAffiliateTradingTokenFeePercent() external view returns (uint256 affiliateTradingTokenFeePercent);

////// Swaps External //////
function swapExternal(
address sourceToken,
address destToken,
address receiver,
address returnToSender,
uint256 sourceTokenAmount,
uint256 requiredDestTokenAmount,
uint256 minReturn,
bytes calldata swapData
) external returns (uint256 destTokenAmountReceived, uint256 sourceTokenAmountUsed);

function getSwapExpectedReturn(
address sourceToken,
address destToken,
uint256 sourceTokenAmount
) external view returns (uint256);

function checkPriceDivergence(
address sourceToken,
address destToken,
uint256 sourceTokenAmount,
uint256 minReturn
) public view;
}
15 changes: 14 additions & 1 deletion 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");
}
}
2 changes: 1 addition & 1 deletion contracts/swaps/ISwapsImpl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface ISwapsImpl {
function internalSwap(
address sourceTokenAddress,
address destTokenAddress,
address receiverAddress,
//address receiverAddress,
address returnToSenderAddress,
uint256 minSourceTokenAmount,
uint256 maxSourceTokenAmount,
Expand Down
1 change: 0 additions & 1 deletion contracts/swaps/SwapsUser.sol
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ contract SwapsUser is State, SwapsEvents, FeesHelper {
ISwapsImpl(swapsImpl).internalSwap.selector,
addrs[0], /// sourceToken
addrs[1], /// destToken
addrs[2], /// receiverAddress
addrs[3], /// returnToSenderAddress
vals[0], /// minSourceTokenAmount
vals[1], /// maxSourceTokenAmount
Expand Down
3 changes: 1 addition & 2 deletions contracts/swaps/connectors/SwapsImplSovrynSwap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ contract SwapsImplSovrynSwap is State, ISwapsImpl {
*
* @param sourceTokenAddress The address of the source tokens.
* @param destTokenAddress The address of the destination tokens.
* @param receiverAddress The address to receive the swapped tokens.
* @param returnToSenderAddress The address to return unspent tokens to (when called by the protocol, it's always the protocol contract).
* @param minSourceTokenAmount The minimum amount of source tokens to swapped (only considered if requiredDestTokens == 0).
* @param maxSourceTokenAmount The maximum amount of source tokens to swapped.
Expand All @@ -63,7 +62,7 @@ contract SwapsImplSovrynSwap is State, ISwapsImpl {
function internalSwap(
address sourceTokenAddress,
address destTokenAddress,
address receiverAddress,
//address receiverAddress,
address returnToSenderAddress,
uint256 minSourceTokenAmount,
uint256 maxSourceTokenAmount,
Expand Down
2 changes: 1 addition & 1 deletion contracts/swaps/connectors/testnet/SwapsImplLocal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ contract SwapsImplLocal is State, ISwapsImpl {
function internalSwap(
address sourceTokenAddress,
address destTokenAddress,
address, /*receiverAddress*/
//address, /*receiverAddress*/
address returnToSenderAddress,
uint256 minSourceTokenAmount,
uint256 maxSourceTokenAmount,
Expand Down
Loading

0 comments on commit 0ebbf95

Please sign in to comment.