# Ethernaut 题库闯关 #22 — Dex 2

Ethernaut题库闯关连载的第22篇

## 挑战# 22：Dex 2

``````// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import '@openzeppelin/contracts/math/SafeMath.sol';
import '@openzeppelin/contracts/access/Ownable.sol';

contract DexTwo is Ownable {
using SafeMath for uint;
constructor() public {}

token1 = _token1;
token2 = _token2;
}

}

require(IERC20(from).balanceOf(msg.sender) >= amount, "Not enough to swap");
uint swapAmount = getSwapAmount(from, to, amount);
}

}

function approve(address spender, uint amount) public {
SwappableTokenTwo(token1).approve(msg.sender, spender, amount);
SwappableTokenTwo(token2).approve(msg.sender, spender, amount);
}

return IERC20(token).balanceOf(account);
}
}

contract SwappableTokenTwo is ERC20 {
constructor(address dexInstance, string memory name, string memory symbol, uint initialSupply) public ERC20(name, symbol) {
_mint(msg.sender, initialSupply);
_dex = dexInstance;
}

require(owner != _dex, "InvalidApprover");
super._approve(owner, spender, amount);
}
}``````

## 研究合约

`DexTwo`合约与之前`Dex`挑战中的合约基本相同，唯一改变的是一些函数名称和`swap`函数的内容。

``````
function swap(
uint256 amount
) public {
require(IERC20(from).balanceOf(msg.sender) >= amount, "Not enough to swap");
uint256 swapAmount = getSwapAmount(from, to, amount);...``````

Ethernaut CTF