Skip to content
Verified contractExactMIT

MotoswapAdapter

Address0x7e4c8de291dea64969a6c1761d4d6d7ff7422367
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.17

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.17+commit.8df45f5f
Language
solidity
Optimization
Yes, 1 runs
EVM version
london
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,082,570
Creation transaction
0xb59fd8c5a8…3eecbe4619

src/adapters/motoswap/MotoswapAdapter.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

import {IAdapter} from "@interfaces/IAdapter.sol";
import {IERC20} from "@interfaces/IERC20.sol";
import {SafeERC20} from "@libraries/SafeERC20.sol";

/// @notice Motoswap FeeRouter token -> token entry. It pulls `amountIn` of `path[0]` from `msg.sender`,
///         takes the protocol fee (and any creator fee) on the quote leg, routes the rest through
///         MotoSwapRouter02 and delivers the net output to `to`.
interface IMotoswapFeeRouter {
    function swapExactTokensForTokens(
        uint256 amountIn,
        uint256 amountOutMin,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external returns (uint256[] memory amounts);
}

interface IMotoswapFactory {
    function getPair(address tokenA, address tokenB) external view returns (address pair);
}

interface IMotoswapPair {
    function token0() external view returns (address);

    function token1() external view returns (address);
}

/// @title MotoswapAdapter
/// @notice Adapter for Motoswap (Ethereum) V2-style pairs, including the TOKEN/WETH and TOKEN/MOTO pairs
///         seeded by moto.fun graduation.
/// @dev A direct `pair.swap` reverts (`SWAPPER_NOT_ALLOWED`: the factory only whitelists its perimeter
///      routers), so the swap goes through the public FeeRouter with a single-hop path. `pool` is the
///      Motoswap pair; it must be the factory's canonical pair for its tokens. `sellBase` sells token0 for
///      token1 and `sellQuote` sells token1 for token0; moreInfo is unused. The caller pre-transfers the
///      input to this adapter, which approves the FeeRouter for exactly its balance; the FeeRouter pulls
///      it and pays the net output straight to `to`. Slippage is checked by the caller (amountOutMin = 0).
contract MotoswapAdapter is IAdapter {
    using SafeERC20 for IERC20;

    address internal immutable FEE_ROUTER;
    address internal immutable FACTORY;

    constructor(address feeRouter, address factory) {
        FEE_ROUTER = feeRouter;
        FACTORY = factory;
    }

    function sellBase(address to, address pool, bytes memory) external override {
        _swap(to, pool, IMotoswapPair(pool).token0(), IMotoswapPair(pool).token1());
    }

    function sellQuote(address to, address pool, bytes memory) external override {
        _swap(to, pool, IMotoswapPair(pool).token1(), IMotoswapPair(pool).token0());
    }

    function _swap(address to, address pool, address tokenIn, address tokenOut) internal {
        require(IMotoswapFactory(FACTORY).getPair(tokenIn, tokenOut) == pool, "MotoswapAdapter: INVALID_POOL");

        uint256 amountIn = IERC20(tokenIn).balanceOf(address(this));
        require(amountIn > 0, "MotoswapAdapter: ZERO_AMOUNT_IN");

        address[] memory path = new address[](2);
        path[0] = tokenIn;
        path[1] = tokenOut;

        IERC20(tokenIn).forceApprove(FEE_ROUTER, amountIn);
        IMotoswapFeeRouter(FEE_ROUTER).swapExactTokensForTokens(amountIn, 0, path, to, block.timestamp);
    }
}

src/interfaces/IAdapter.sol

/// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
pragma abicoder v2;

interface IAdapter {
    function sellBase(address to, address pool, bytes memory data) external;

    function sellQuote(address to, address pool, bytes memory data) external;
}

src/interfaces/IDaiLikePermit.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface IDaiLikePermit {
    function permit(
        address holder,
        address spender,
        uint256 nonce,
        uint256 expiry,
        bool allowed,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external;
}

src/interfaces/IERC20.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface IERC20 {
    event Approval(address indexed owner, address indexed spender, uint256 value);
    event Transfer(address indexed from, address indexed to, uint256 value);

    function name() external view returns (string memory);

    function symbol() external view returns (string memory);

    function decimals() external view returns (uint8);

    function totalSupply() external view returns (uint256);

    function balanceOf(address owner) external view returns (uint256);

    function allowance(address owner, address spender) external view returns (uint256);

    function approve(address spender, uint256 value) external returns (bool);

    function transfer(address to, uint256 value) external returns (bool);

    function transferFrom(address from, address to, uint256 value) external returns (bool);
}

src/interfaces/IERC20Permit.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface IERC20Permit {
    function permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s)
        external;

    function nonces(address owner) external view returns (uint256);

    // solhint-disable-next-line func-name-mixedcase
    function DOMAIN_SEPARATOR() external view returns (bytes32);
}

src/libraries/RevertReasonForwarder.sol

/// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

library RevertReasonForwarder {
    function reRevert() internal pure {
        /// @solidity memory-safe-assembly
        assembly {
            // solhint-disable-line no-inline-assembly
            let ptr := mload(0x40)
            returndatacopy(ptr, 0, returndatasize())
            revert(ptr, returndatasize())
        }
    }
}

src/libraries/SafeERC20.sol

/// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./RevertReasonForwarder.sol";
import "@interfaces/IDaiLikePermit.sol";
import "@interfaces/IERC20.sol";
import "@interfaces/IERC20Permit.sol";

library SafeERC20 {
    error SafeTransferFailed();
    error SafeTransferFromFailed();
    error ForceApproveFailed();
    error SafeIncreaseAllowanceFailed();
    error SafeDecreaseAllowanceFailed();
    error SafePermitBadLength();

    function safeTransferFrom(IERC20 token, address from, address to, uint256 amount) internal {
        bytes4 selector = token.transferFrom.selector;
        bool success;
        /// @solidity memory-safe-assembly
        assembly {
            // solhint-disable-line no-inline-assembly
            let data := mload(0x40)

            mstore(data, selector)
            mstore(add(data, 0x04), from)
            mstore(add(data, 0x24), to)
            mstore(add(data, 0x44), amount)
            success := call(gas(), token, 0, data, 100, 0x0, 0x20)
            if success {
                switch returndatasize()
                case 0 { success := gt(extcodesize(token), 0) }
                default { success := and(gt(returndatasize(), 31), eq(mload(0), 1)) }
            }
        }
        if (!success) revert SafeTransferFromFailed();
    }

    function safeTransfer(IERC20 token, address to, uint256 value) internal {
        if (!_makeCall(token, token.transfer.selector, to, value)) {
            revert SafeTransferFailed();
        }
    }

    function safeApprove(IERC20 token, address spender, uint256 value) internal {
        forceApprove(token, spender, value);
    }

    function forceApprove(IERC20 token, address spender, uint256 value) internal {
        if (!_makeCall(token, token.approve.selector, spender, value)) {
            if (
                !_makeCall(token, token.approve.selector, spender, 0)
                    || !_makeCall(token, token.approve.selector, spender, value)
            ) {
                revert ForceApproveFailed();
            }
        }
    }

    function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
        uint256 allowance = token.allowance(address(this), spender);
        if (value > type(uint256).max - allowance) revert SafeIncreaseAllowanceFailed();
        forceApprove(token, spender, allowance + value);
    }

    function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
        uint256 allowance = token.allowance(address(this), spender);
        if (value > allowance) revert SafeDecreaseAllowanceFailed();
        forceApprove(token, spender, allowance - value);
    }

    function safePermit(IERC20 token, bytes calldata permit) internal {
        bool success;
        if (permit.length == 32 * 7) {
            success = _makeCalldataCall(token, IERC20Permit.permit.selector, permit);
        } else if (permit.length == 32 * 8) {
            success = _makeCalldataCall(token, IDaiLikePermit.permit.selector, permit);
        } else {
            revert SafePermitBadLength();
        }
        if (!success) RevertReasonForwarder.reRevert();
    }

    function _makeCall(IERC20 token, bytes4 selector, address to, uint256 amount) private returns (bool success) {
        /// @solidity memory-safe-assembly
        assembly {
            // solhint-disable-line no-inline-assembly
            let data := mload(0x40)

            mstore(data, selector)
            mstore(add(data, 0x04), to)
            mstore(add(data, 0x24), amount)
            success := call(gas(), token, 0, data, 0x44, 0x0, 0x20)
            if success {
                switch returndatasize()
                case 0 { success := gt(extcodesize(token), 0) }
                default { success := and(gt(returndatasize(), 31), eq(mload(0), 1)) }
            }
        }
    }

    function _makeCalldataCall(IERC20 token, bytes4 selector, bytes calldata args) private returns (bool success) {
        /// @solidity memory-safe-assembly
        assembly {
            // solhint-disable-line no-inline-assembly
            let len := add(4, args.length)
            let data := mload(0x40)

            mstore(data, selector)
            calldatacopy(add(data, 0x04), args.offset, args.length)
            success := call(gas(), token, 0, data, len, 0x0, 0x20)
            if success {
                switch returndatasize()
                case 0 { success := gt(extcodesize(token), 0) }
                default { success := and(gt(returndatasize(), 31), eq(mload(0), 1)) }
            }
        }
    }
}
Compiler settingsShow
{
  "viaIR": false,
  "metadata": {
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 1,
    "enabled": true
  },
  "evmVersion": "london",
  "remappings": [
    "forge-std/=lib/forge-std/src/",
    "@adapters/=src/adapters/",
    "@interfaces/=src/interfaces/",
    "@libraries/=src/libraries/",
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "openzeppelin/=lib/openzeppelin-contracts/contracts/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/contracts/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.