- (SwapsExternal.sol)
View Source: contracts/modules/SwapsExternal.sol
↗ Extends: VaultController, SwapsUser, ModuleCommonFunctionalities
This contract code comes from bZx. bZx is a protocol for tokenized margin trading and lending https://bzx.network similar to the dYdX protocol.
- This contract contains functions to calculate and execute swaps.
- constructor()
- constructor()
- initialize(address target)
- swapExternal(address sourceToken, address destToken, address receiver, address returnToSender, uint256 sourceTokenAmount, uint256 requiredDestTokenAmount, uint256 minReturn, bytes swapData)
- getSwapExpectedReturn(address sourceToken, address destToken, uint256 sourceTokenAmount)
- checkPriceDivergence(address sourceToken, address destToken, uint256 sourceTokenAmount, uint256 minReturn)
Empty public constructor.
function () public nonpayable
Source Code
constructor() public {}
Fallback function is to react to receiving value (rBTC).
function () external nonpayable
Source Code
function() external {
revert("fallback not allowed");
}
Set function selectors on target contract. *
function initialize(address target) external nonpayable onlyOwner
Arguments
Name | Type | Description |
---|---|---|
target | address | The address of the target contract. |
Source Code
function initialize(address target) external onlyOwner {
address prevModuleContractAddress = logicTargets[this.swapExternal.selector];
_setTarget(this.swapExternal.selector, target);
_setTarget(this.getSwapExpectedReturn.selector, target);
_setTarget(this.checkPriceDivergence.selector, target);
emit ProtocolModuleContractReplaced(prevModuleContractAddress, target, "SwapsExternal");
}
Perform a swap w/ tokens or rBTC as source currency. *
function swapExternal(address sourceToken, address destToken, address receiver, address returnToSender, uint256 sourceTokenAmount, uint256 requiredDestTokenAmount, uint256 minReturn, bytes swapData) public payable nonReentrant whenNotPaused
returns(destTokenAmountReceived uint256, sourceTokenAmountUsed uint256)
Arguments
Name | Type | Description |
---|---|---|
sourceToken | address | The address of the source token instance. |
destToken | address | The address of the destiny token instance. |
receiver | address | The address of the recipient account. |
returnToSender | address | The address of the sender account. |
sourceTokenAmount | uint256 | The amount of source tokens. |
requiredDestTokenAmount | uint256 | The amount of required destiny tokens. |
minReturn | uint256 | Minimum amount (position size) in the collateral tokens. |
swapData | bytes | Additional swap data (not in use yet). * |
Returns
destTokenAmountReceived The amount of destiny tokens sent.
Source Code
function swapExternal(
address sourceToken,
address destToken,
address receiver,
address returnToSender,
uint256 sourceTokenAmount,
uint256 requiredDestTokenAmount,
uint256 minReturn,
bytes memory swapData
)
public
payable
nonReentrant
whenNotPaused
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) {
if (sourceToken == address(0)) {
sourceToken = address(wrbtcToken);
}
require(sourceToken == address(wrbtcToken), "sourceToken mismatch");
require(msg.value == sourceTokenAmount, "sourceTokenAmount mismatch");
/// @dev Update wrBTC balance for this contract.
wrbtcToken.deposit.value(sourceTokenAmount)();
} else {
if (address(this) != msg.sender) {
IERC20(sourceToken).safeTransferFrom(msg.sender, address(this), sourceTokenAmount);
}
}
/// @dev Perform the swap w/ tokens.
(destTokenAmountReceived, sourceTokenAmountUsed) = _swapsCall(
[
sourceToken,
destToken,
receiver,
returnToSender,
msg.sender /// user
],
[
sourceTokenAmount, /// minSourceTokenAmount
sourceTokenAmount, /// maxSourceTokenAmount
requiredDestTokenAmount
],
0, /// loanId (not tied to a specific loan)
false, /// bypassFee
swapData,
true // the flag for swapExternal (so that it will use the swapExternalFeePercent)
);
emit ExternalSwap(
msg.sender, /// user
sourceToken,
destToken,
sourceTokenAmountUsed,
destTokenAmountReceived
);
}
Get the swap expected return value. *
function getSwapExpectedReturn(address sourceToken, address destToken, uint256 sourceTokenAmount) external view
returns(uint256)
Arguments
Name | Type | Description |
---|---|---|
sourceToken | address | The address of the source token instance. |
destToken | address | The address of the destiny token instance. |
sourceTokenAmount | uint256 | The amount of source tokens. * |
Returns
The expected return value.
Source Code
function getSwapExpectedReturn(
address sourceToken,
address destToken,
uint256 sourceTokenAmount
) external view returns (uint256) {
return _swapsExpectedReturn(sourceToken, destToken, sourceTokenAmount);
}
Check the slippage based on the swapExpectedReturn. *
function checkPriceDivergence(address sourceToken, address destToken, uint256 sourceTokenAmount, uint256 minReturn) public view
Arguments
Name | Type | Description |
---|---|---|
sourceToken | address | The address of the source token instance. |
destToken | address | The address of the destiny token instance. |
sourceTokenAmount | uint256 | The amount of source tokens. |
minReturn | uint256 | The amount (max slippage) that will be compared to the swapsExpectedReturn. |
Source Code
function checkPriceDivergence(
address sourceToken,
address destToken,
uint256 sourceTokenAmount,
uint256 minReturn
) public view {
uint256 destTokenAmount = _swapsExpectedReturn(sourceToken, destToken, sourceTokenAmount);
require(destTokenAmount >= minReturn, "destTokenAmountReceived too low");
}
- Address
- Administered
- AdminRole
- AdvancedToken
- AdvancedTokenStorage
- Affiliates
- AffiliatesEvents
- ApprovalReceiver
- BProPriceFeed
- CheckpointsShared
- Constants
- Context
- DevelopmentFund
- DummyContract
- EnumerableAddressSet
- EnumerableBytes32Set
- EnumerableBytes4Set
- ERC20
- ERC20Detailed
- ErrorDecoder
- Escrow
- EscrowReward
- FeedsLike
- FeesEvents
- FeeSharingCollector
- FeeSharingCollectorProxy
- FeeSharingCollectorStorage
- FeesHelper
- FourYearVesting
- FourYearVestingFactory
- FourYearVestingLogic
- FourYearVestingStorage
- GenericTokenSender
- GovernorAlpha
- GovernorVault
- IApproveAndCall
- IChai
- IContractRegistry
- IConverterAMM
- IERC1820Registry
- IERC20_
- IERC20
- IERC777
- IERC777Recipient
- IERC777Sender
- IFeeSharingCollector
- IFourYearVesting
- IFourYearVestingFactory
- IFunctionsList
- ILiquidityMining
- ILiquidityPoolV1Converter
- ILoanPool
- ILoanToken
- ILoanTokenLogicBeacon
- ILoanTokenLogicModules
- ILoanTokenLogicProxy
- ILoanTokenModules
- ILoanTokenWRBTC
- ILockedSOV
- IMoCState
- IModulesProxyRegistry
- Initializable
- InterestUser
- IPot
- IPriceFeeds
- IPriceFeedsExt
- IProtocol
- IRSKOracle
- ISovryn
- ISovrynSwapNetwork
- IStaking
- ISwapsImpl
- ITeamVesting
- ITimelock
- IV1PoolOracle
- IVesting
- IVestingFactory
- IVestingRegistry
- IWrbtc
- IWrbtcERC20
- LenderInterestStruct
- LiquidationHelper
- LiquidityMining
- LiquidityMiningConfigToken
- LiquidityMiningProxy
- LiquidityMiningStorage
- LoanClosingsEvents
- LoanClosingsLiquidation
- LoanClosingsRollover
- LoanClosingsShared
- LoanClosingsWith
- LoanClosingsWithoutInvariantCheck
- LoanInterestStruct
- LoanMaintenance
- LoanMaintenanceEvents
- LoanOpenings
- LoanOpeningsEvents
- LoanParamsStruct
- LoanSettings
- LoanSettingsEvents
- LoanStruct
- LoanToken
- LoanTokenBase
- LoanTokenLogicBeacon
- LoanTokenLogicLM
- LoanTokenLogicProxy
- LoanTokenLogicStandard
- LoanTokenLogicStorage
- LoanTokenLogicWrbtc
- LoanTokenSettingsLowerAdmin
- LockedSOV
- MarginTradeStructHelpers
- Medianizer
- ModuleCommonFunctionalities
- ModulesCommonEvents
- ModulesProxy
- ModulesProxyRegistry
- MultiSigKeyHolders
- MultiSigWallet
- Mutex
- Objects
- OrderStruct
- OrigingVestingCreator
- OriginInvestorsClaim
- Ownable
- Pausable
- PausableOz
- PreviousLoanToken
- PreviousLoanTokenSettingsLowerAdmin
- PriceFeedRSKOracle
- PriceFeeds
- PriceFeedsLocal
- PriceFeedsMoC
- PriceFeedV1PoolOracle
- ProtocolAffiliatesInterface
- ProtocolLike
- ProtocolSettings
- ProtocolSettingsEvents
- ProtocolSettingsLike
- ProtocolSwapExternalInterface
- ProtocolTokenUser
- Proxy
- ProxyOwnable
- ReentrancyGuard
- RewardHelper
- RSKAddrValidator
- SafeERC20
- SafeMath
- SafeMath96
- setGet
- SharedReentrancyGuard
- SignedSafeMath
- SOV
- sovrynProtocol
- StakingAdminModule
- StakingGovernanceModule
- StakingInterface
- StakingProxy
- StakingRewards
- StakingRewardsProxy
- StakingRewardsStorage
- StakingShared
- StakingStakeModule
- StakingStorageModule
- StakingStorageShared
- StakingVestingModule
- StakingWithdrawModule
- State
- SwapsEvents
- SwapsExternal
- SwapsImplLocal
- SwapsImplSovrynSwap
- SwapsUser
- TeamVesting
- Timelock
- TimelockHarness
- TimelockInterface
- TokenSender
- UpgradableProxy
- USDTPriceFeed
- Utils
- VaultController
- Vesting
- VestingCreator
- VestingFactory
- VestingLogic
- VestingRegistry
- VestingRegistry2
- VestingRegistry3
- VestingRegistryLogic
- VestingRegistryProxy
- VestingRegistryStorage
- VestingStorage
- WeightedStakingModule
- WRBTC