-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:JOJOexchange/smart-contract-EVM
- Loading branch information
Showing
2 changed files
with
70 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
Copyright 2022 JOJO Exchange | ||
SPDX-License-Identifier: BUSL-1.1 | ||
*/ | ||
pragma solidity ^0.8.19; | ||
|
||
import "@openzeppelin/contracts/access/Ownable.sol"; | ||
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; | ||
import "./DegenDealer.sol"; | ||
|
||
interface IWETH { | ||
function deposit() external payable; | ||
} | ||
|
||
contract DepositStableCoinToDegenDealer is Ownable { | ||
using SafeERC20 for IERC20; | ||
|
||
address public immutable degenDealer; | ||
address public immutable usdc; | ||
address public immutable weth; | ||
mapping(address => bool) public whiteListContract; | ||
|
||
constructor(address _degenDealer, address _weth) { | ||
degenDealer = _degenDealer; | ||
usdc = DegenDealer(degenDealer).primaryAsset(); | ||
weth = _weth; | ||
} | ||
|
||
function setWhiteListContract(address targetContract, bool isValid) public onlyOwner { | ||
whiteListContract[targetContract] = isValid; | ||
} | ||
|
||
function depositStableCoin( | ||
address asset, | ||
uint256 amount, | ||
address to, | ||
bytes calldata param, | ||
uint256 minReceive | ||
) | ||
external | ||
payable | ||
{ | ||
if (asset == weth && msg.value == amount) { | ||
IWETH(weth).deposit{ value: amount }(); | ||
} else { | ||
IERC20(asset).safeTransferFrom(msg.sender, address(this), amount); | ||
} | ||
(address approveTarget, address swapTarget, bytes memory data) = abi.decode(param, (address, address, bytes)); | ||
require(whiteListContract[approveTarget], "approve target is not in the whitelist"); | ||
require(whiteListContract[swapTarget], "swap target is not in the whitelist"); | ||
// if usdt | ||
IERC20(asset).safeApprove(approveTarget, 0); | ||
IERC20(asset).safeApprove(approveTarget, amount); | ||
(bool success,) = swapTarget.call(data); | ||
if (success == false) { | ||
assembly { | ||
let ptr := mload(0x40) | ||
let size := returndatasize() | ||
returndatacopy(ptr, 0, size) | ||
revert(ptr, size) | ||
} | ||
} | ||
|
||
uint256 usdcAmount = IERC20(usdc).balanceOf(address(this)); | ||
require(usdcAmount >= minReceive, "receive amount is too small"); | ||
IERC20(usdc).approve(degenDealer, usdcAmount); | ||
DegenDealer(degenDealer).deposit(msg.sender, to, usdcAmount); | ||
} | ||
} |