Skip to content

Latest commit

 

History

History
461 lines (401 loc) · 15.3 KB

SwapsExternal.md

File metadata and controls

461 lines (401 loc) · 15.3 KB

Swaps External contract.

  • (SwapsExternal.sol)

View Source: contracts/modules/SwapsExternal.sol

↗ Extends: VaultController, SwapsUser, ModuleCommonFunctionalities

SwapsExternal contract

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.

Functions


constructor

Empty public constructor.

function () public nonpayable
Source Code
constructor() public {}

constructor

Fallback function is to react to receiving value (rBTC).

function () external nonpayable
Source Code
function() external {
        revert("fallback not allowed");
    }

initialize

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");
    }

swapExternal

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
        );
    }

getSwapExpectedReturn

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);
    }

checkPriceDivergence

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");
    }

Contracts