Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yashgo0018 committed Dec 6, 2023
1 parent 81bf330 commit e05bd49
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 16 deletions.
73 changes: 73 additions & 0 deletions contracts/test/MockAggregatorV3.sol
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
);
}
}
92 changes: 92 additions & 0 deletions contracts/test/MockUniswapRouter.sol
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);
}
}
54 changes: 38 additions & 16 deletions test/Vault.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Abi,
Account,
Chain,
GetContractReturnType,
Expand All @@ -12,25 +13,27 @@ import { viem } from "hardhat";
import { MockERC20$Type } from "../artifacts/contracts/test/MockERC20.sol/MockERC20";
import { IERC20$Type } from "../artifacts/@openzeppelin/contracts/token/ERC20/IERC20.sol/IERC20";
import { expect } from "chai";
import { startSimulator } from "./chainlink-functions-simulators";
import { FundManager$Type } from "../artifacts/contracts/FundManager.sol/FundManager";
import { MockUniswapRouter$Type } from "../artifacts/contracts/test/MockUniswapRouter.sol/MockUniswapRouter";

type Contract<TAbi extends Abi | readonly unknown[]> = GetContractReturnType<
TAbi,
PublicClient<Transport, Chain>,
WalletClient<Transport, Chain, Account>
>;

describe("Vault", function () {
let wallets: WalletClient[];
let publicClient: PublicClient;
let vault: GetContractReturnType<
Vault$Type["abi"],
PublicClient<Transport, Chain>,
WalletClient<Transport, Chain, Account>
>;
let usdc: GetContractReturnType<
MockERC20$Type["abi"],
PublicClient<Transport, Chain>,
WalletClient<Transport, Chain, Account>
>;
let vaultShare: GetContractReturnType<
IERC20$Type["abi"],
PublicClient<Transport, Chain>,
WalletClient<Transport, Chain, Account>
>;
let functionsRouterAddress: `0x${string}`;

let vault: Contract<Vault$Type["abi"]>;
let usdc: Contract<MockERC20$Type["abi"]>;
let vaultShare: Contract<IERC20$Type["abi"]>;
let fundManager: Contract<FundManager$Type["abi"]>;
let mockUniswapRouter: Contract<MockUniswapRouter$Type["abi"]>;

const accounts: Account[] = [];

this.beforeAll(async () => {
Expand All @@ -43,7 +46,26 @@ describe("Vault", function () {

usdc = await viem.deployContract("MockERC20", []);

vault = await viem.deployContract("Vault", [usdc.address]);
functionsRouterAddress = await startSimulator({
secrets: {
pinataAPIKey: process.env.PINATA_API_KEY || "",
},
});

fundManager = await viem.deployContract("FundManager", [
functionsRouterAddress,
]);

mockUniswapRouter = await viem.deployContract("MockUniswapRouter", [
fundManager.address,
usdc.address,
]);

vault = await viem.deployContract("Vault", [
usdc.address,
fundManager.address,
mockUniswapRouter.address,
]);

const shareTokenAddress = await vault.read.shareToken();

Expand Down

0 comments on commit e05bd49

Please sign in to comment.