22pragma solidity ^ 0.8.0 ;
33
44import "@openzeppelin/contracts/access/Ownable.sol " ;
5-
6- interface IERC20 {
7- function transferFrom (address _from , address _to , uint256 _amount ) external returns (bool );
8- function transfer (address _to , uint256 _amount ) external returns (bool );
9- }
5+ import "@openzeppelin/contracts/token/ERC20/IERC20.sol " ;
6+ import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol " ;
107
118contract YakuSwap is Ownable {
9+ using SafeERC20 for IERC20 ;
1210
1311 // Uninitialized - Default status (if swaps[index] doesn't exist, status will get this value)
1412 // Created - the swap was created, but the mone is still in the contract
@@ -79,7 +77,7 @@ contract YakuSwap is Ownable {
7977 );
8078
8179 require (swaps[swapHash] == SwapStatus.Uninitialized, "Invalid swap status " );
82- require ( IERC20 (tokenAddress).transferFrom (msg .sender , address (this ), amount), " Could not transfer tokens " );
80+ IERC20 (tokenAddress).safeTransferFrom (msg .sender , address (this ), amount);
8381
8482 swaps[swapHash] = SwapStatus.Created;
8583 emit SwapCreated (
@@ -118,7 +116,7 @@ contract YakuSwap is Ownable {
118116 uint swapAmount = amount * 993 / 1000 ;
119117 totalFees[tokenAddress] += amount - swapAmount;
120118
121- require ( IERC20 (tokenAddress).transfer (toAddress, swapAmount), " Transfer failed " );
119+ IERC20 (tokenAddress).safeTransfer (toAddress, swapAmount);
122120 }
123121
124122 function cancelSwap (
@@ -141,13 +139,13 @@ contract YakuSwap is Ownable {
141139 require (block .number >= blockNumber + MAX_BLOCK_HEIGHT, "MAX_BLOCK_HEIGHT not exceeded " );
142140
143141 swaps[swapHash] = SwapStatus.Cancelled;
144- require ( IERC20 (tokenAddress).transfer (msg .sender , amount), " Transfer failed " );
142+ IERC20 (tokenAddress).safeTransfer (msg .sender , amount);
145143 }
146144
147145 function withdrawFees (address tokenAddress ) external onlyOwner {
148146 uint feesToWithdraw = totalFees[tokenAddress];
149147 totalFees[tokenAddress] = 0 ;
150148
151- require ( IERC20 (tokenAddress).transfer (msg .sender , feesToWithdraw), " Transfer failed " );
149+ IERC20 (tokenAddress).safeTransfer (msg .sender , feesToWithdraw);
152150 }
153151}
0 commit comments