22
33pragma solidity ^ 0.8.24 ;
44
5- import {Test, console} from "forge-std/Test.sol " ;
5+ import {Test, console, console2 } from "forge-std/Test.sol " ;
66
77import {DeployDSC} from "script/DeployDSC.s.sol " ;
88import {DecentralizedStableCoin} from "../../src/DecentralizedStableCoin.sol " ;
@@ -44,10 +44,20 @@ contract DSCEngineTest is Test {
4444 uint256 public constant MINT_DSC_AMOUNT = 100e18 ; // 100 USD
4545 uint256 public constant COLLATERAL_TO_COVER = 20 ether ;
4646
47+ uint256 constant WBTC_PRECISION = 1e8 ;
48+ uint256 constant WETH_PRECISION = 1e18 ;
49+ uint256 constant PRICEFEED_PRECISION = 1e8 ;
50+ uint256 public currentEthPrice;
51+ uint256 public currentBtcPrice;
52+
4753 function setUp () public {
4854 deployer = new DeployDSC ();
4955 (dsc, engine, helperConfig) = deployer.run ();
5056 (ethUsdPriceFeed, btcUsdPriceFeed, weth, wbtc, deployerKey) = helperConfig.activeNetworkConfig ();
57+ (, int256 ethPrice ,,,) = MockV3Aggregator (ethUsdPriceFeed).latestRoundData ();
58+ currentEthPrice = uint256 (ethPrice);
59+ (, int256 btcPrice ,,,) = MockV3Aggregator (btcUsdPriceFeed).latestRoundData ();
60+ currentBtcPrice = uint256 (btcPrice);
5161 ERC20Mock (weth).mint (USER, STARTING_ERC20_BALANCE); // 100 weth
5262 ERC20Mock (wbtc).mint (USER, STARTING_ERC20_BALANCE); // 100 wbtc
5363
@@ -70,20 +80,22 @@ contract DSCEngineTest is Test {
7080 //////////////////////////////////////////////
7181 /// View Function Tests ///
7282 //////////////////////////////////////////////
73- function test_GetUsdValue () public view {
74- uint256 ethAmount = 2 ;
75- uint256 expectedValue = 2 * 2400 ;
76- uint256 actualValue = engine.getUsdValue (weth, ethAmount);
77- console.log ("Value in USD: %d " , actualValue);
83+ function test_getUsdValue () public view {
84+ uint256 btcAmount = 2e8 ;
85+ uint256 expectedValue = btcAmount * currentBtcPrice * 1e18 / (WBTC_PRECISION * PRICEFEED_PRECISION);
86+ // / wbtc decimals * priceFeedDecimals
87+ uint256 actualValue = engine.getUsdValue (wbtc, btcAmount);
88+ console.log ("expected value : " , expectedValue);
89+ console.log ("actual value : " , actualValue);
7890 assert (expectedValue == actualValue);
7991 }
8092
8193 function test_getTokenAmountFromUsd () public view {
82- uint256 usdAmount = 2400 * 1e18 ;
83- uint256 expectedValue = ( 2400 * 1e18 ) / 2400 ;
84- uint256 actualValue = engine.getTokenAmountFromUsd (weth , usdAmount);
94+ uint256 usdAmount = 80_000 * 1e18 ;
95+ uint256 expectedBtc = usdAmount * WBTC_PRECISION * PRICEFEED_PRECISION / (currentBtcPrice * 1e18 ) ;
96+ uint256 actualValue = engine.getTokenAmountFromUsd (wbtc , usdAmount);
8597 console.log ("Value in ETH: %d " , actualValue);
86- assert (expectedValue == actualValue);
98+ assert (expectedBtc == actualValue);
8799 }
88100
89101 function test_getAccountCollateralValue () public view {}
0 commit comments