-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81bf330
commit e05bd49
Showing
3 changed files
with
203 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; | ||
|
||
contract MockAggregatorV3 is AggregatorV3Interface { | ||
uint8 public decimals; | ||
string public description; | ||
uint256 public version; | ||
uint80 public latestRound; | ||
int256 public latestAnswer; | ||
uint256 public latestTimestamp; | ||
|
||
constructor( | ||
uint8 _decimals, | ||
string memory _description, | ||
uint256 _version, | ||
uint80 _latestRound, | ||
int256 _latestAnswer, | ||
uint256 _latestTimestamp | ||
) { | ||
decimals = _decimals; | ||
description = _description; | ||
version = _version; | ||
latestRound = _latestRound; | ||
latestAnswer = _latestAnswer; | ||
latestTimestamp = _latestTimestamp; | ||
} | ||
|
||
function getRoundData( | ||
uint80 _roundId | ||
) | ||
external | ||
view | ||
override | ||
returns ( | ||
uint80 roundId, | ||
int256 answer, | ||
uint256 startedAt, | ||
uint256 updatedAt, | ||
uint80 answeredInRound | ||
) | ||
{ | ||
return ( | ||
_roundId, | ||
latestAnswer, | ||
latestTimestamp, | ||
latestTimestamp, | ||
latestRound | ||
); | ||
} | ||
|
||
function latestRoundData() | ||
external | ||
view | ||
override | ||
returns ( | ||
uint80 roundId, | ||
int256 answer, | ||
uint256 startedAt, | ||
uint256 updatedAt, | ||
uint80 answeredInRound | ||
) | ||
{ | ||
return ( | ||
latestRound, | ||
latestAnswer, | ||
latestTimestamp, | ||
latestTimestamp, | ||
latestRound | ||
); | ||
} | ||
} |
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,92 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
import "./MockAggregatorV3.sol"; | ||
import "./MockERC20.sol"; | ||
import "../FundManager.sol"; | ||
|
||
contract MockUniswapRouter { | ||
FundManager public fundManager; | ||
address public stableToken; | ||
|
||
constructor(FundManager _fundManager, address _stableToken) { | ||
fundManager = _fundManager; | ||
stableToken = _stableToken; | ||
} | ||
|
||
function swapExactTokensForTokens( | ||
uint256 amountIn, | ||
uint256 /* amountOutMin */, | ||
address[] calldata path, | ||
address to, | ||
uint256 /* deadline */ | ||
) external returns (uint256[] memory amounts) { | ||
MockERC20 tokenIn = MockERC20(path[0]); | ||
MockERC20 tokenOut = MockERC20(path[1]); | ||
|
||
tokenIn.transferFrom(msg.sender, address(this), amountIn); | ||
|
||
if (address(tokenIn) != stableToken) { | ||
uint256 tokenValue = getTokenValue(address(tokenIn), amountIn); | ||
tokenOut.mint(to, tokenValue); | ||
} else { | ||
uint256 tokenValue = getTokenValueReverse( | ||
address(tokenOut), | ||
amountIn | ||
); | ||
tokenOut.mint(to, tokenValue); | ||
} | ||
} | ||
|
||
function getTokenValue( | ||
address tokenAddress, | ||
uint256 amount | ||
) public view returns (uint256) { | ||
uint256 id = fundManager.tokenAddressToId(tokenAddress); | ||
if (id == 0) revert("Token not found"); | ||
|
||
(, , , address chainlinkDataFeedAddress, ) = fundManager.tokens(id); | ||
|
||
AggregatorV3Interface chainlinkDataFeed = AggregatorV3Interface( | ||
chainlinkDataFeedAddress | ||
); | ||
(, int256 price, , , ) = chainlinkDataFeed.latestRoundData(); | ||
|
||
uint8 tokenDecimals = MockERC20(tokenAddress).decimals(); | ||
uint256 tokenUnit = 10 ** tokenDecimals; | ||
uint8 feedDecimals = chainlinkDataFeed.decimals(); | ||
uint256 feedUnit = 10 ** feedDecimals; | ||
uint8 stableTokenDecimals = MockERC20(stableToken).decimals(); | ||
uint256 stableTokenUnit = 10 ** stableTokenDecimals; | ||
|
||
return | ||
(stableTokenUnit * amount * uint256(price)) / | ||
(tokenUnit * feedUnit); | ||
} | ||
|
||
function getTokenValueReverse( | ||
address tokenAddress, | ||
uint256 amount | ||
) public view returns (uint256) { | ||
uint256 id = fundManager.tokenAddressToId(tokenAddress); | ||
if (id == 0) revert("Token not found"); | ||
|
||
(, , , address chainlinkDataFeedAddress, ) = fundManager.tokens(id); | ||
|
||
AggregatorV3Interface chainlinkDataFeed = AggregatorV3Interface( | ||
chainlinkDataFeedAddress | ||
); | ||
(, int256 price, , , ) = chainlinkDataFeed.latestRoundData(); | ||
|
||
uint8 tokenDecimals = MockERC20(tokenAddress).decimals(); | ||
uint256 tokenUnit = 10 ** tokenDecimals; | ||
uint8 feedDecimals = chainlinkDataFeed.decimals(); | ||
uint256 feedUnit = 10 ** feedDecimals; | ||
uint8 stableTokenDecimals = MockERC20(stableToken).decimals(); | ||
uint256 stableTokenUnit = 10 ** stableTokenDecimals; | ||
|
||
return | ||
(tokenUnit * amount * feedUnit) / | ||
(uint256(price) * stableTokenUnit); | ||
} | ||
} |
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