11/* eslint-disable @typescript-eslint/no-unused-vars */
2+ import fs from "node:fs" ;
3+ import path from "node:path" ;
4+ import { fileURLToPath } from "node:url" ;
25import { HDNodeWallet } from "ethers" ;
3- import hre from "hardhat" ;
4- import { DEX , MyUSDEngine , MyUSD , MyUSDStaking , Oracle } from "../typechain-types" ;
5- import * as blessed from "blessed" ;
6- import * as contrib from "blessed-contrib" ;
7- const ethers = hre . ethers ;
6+ import { network } from "hardhat" ;
7+ import type { DEX , MyUSDEngine , MyUSD , MyUSDStaking , Oracle } from "../types/ethers-contracts/index.js" ;
8+ import {
9+ DEX__factory ,
10+ MyUSDEngine__factory ,
11+ MyUSD__factory ,
12+ MyUSDStaking__factory ,
13+ Oracle__factory ,
14+ } from "../types/ethers-contracts/index.js" ;
15+ import blessed from "blessed" ;
16+ import contrib from "blessed-contrib" ;
17+
18+ const { ethers, networkName } = await network . create ( ) ;
19+
20+ const deploymentsDir = path . join (
21+ path . dirname ( fileURLToPath ( import . meta. url ) ) ,
22+ ".." ,
23+ "deployments" ,
24+ networkName ,
25+ ) ;
26+
27+ function getDeployedAddress ( name : string ) : string {
28+ const filePath = path . join ( deploymentsDir , `${ name } .json` ) ;
29+ return JSON . parse ( fs . readFileSync ( filePath , "utf8" ) ) . address ;
30+ }
831
932// Account types and preferences
1033interface BorrowerProfile {
@@ -171,7 +194,7 @@ function getBorrowerStatus(
171194 const rateFactor =
172195 1 -
173196 ( Math . min ( currentBorrowRate , borrower . maxAcceptableRate ) / borrower . maxAcceptableRate ) *
174- ( borrower . rateSensitivity / 100 ) ;
197+ ( borrower . rateSensitivity / 100 ) ;
175198
176199 const borrowingWillingness = ( borrower . debtTolerance / 100 ) * rateFactor ;
177200
@@ -273,9 +296,9 @@ async function updateUI(
273296 try {
274297 systemInfoBox . setContent (
275298 `MyUSD Price: {yellow-fg}${ myUSDPriceInUSD . toFixed ( 6 ) } {/yellow-fg} | ` +
276- `ETH Price: {cyan-fg}${ ethToMyUSDPriceNum . toFixed ( 1 ) } MyUSD{/cyan-fg} | ` +
277- `Savings Rate: {cyan-fg}${ savingsRate > 0 ? savingsRate / 100 : 0 } % {/cyan-fg} | ` +
278- `Borrow Rate: {magenta-fg}${ borrowRate > 0 ? borrowRate / 100 : 0 } % {/magenta-fg}` ,
299+ `ETH Price: {cyan-fg}${ ethToMyUSDPriceNum . toFixed ( 1 ) } MyUSD{/cyan-fg} | ` +
300+ `Savings Rate: {cyan-fg}${ savingsRate > 0 ? savingsRate / 100 : 0 } % {/cyan-fg} | ` +
301+ `Borrow Rate: {magenta-fg}${ borrowRate > 0 ? borrowRate / 100 : 0 } % {/magenta-fg}` ,
279302 ) ;
280303 } catch ( error : any ) {
281304 console . log ( `Error updating system info: ${ error } ` ) ;
@@ -451,7 +474,7 @@ async function simulateBorrowing(
451474
452475 logActivity (
453476 `Borrower ${ borrower . wallet . address . slice ( 0 , 6 ) } ... repaid ${ ethers . formatEther ( amountToBurn ) . slice ( 0 , 6 ) } MyUSD ` +
454- `(keeping ${ ethers . formatEther ( amountToKeep ) . slice ( 0 , 6 ) } MyUSD)` ,
477+ `(keeping ${ ethers . formatEther ( amountToKeep ) . slice ( 0 , 6 ) } MyUSD)` ,
455478 ) ;
456479 } catch ( error : any ) {
457480 logActivity ( `Failed to repay debt for ${ borrower . wallet . address . slice ( 0 , 6 ) } ...` ) ;
@@ -470,7 +493,7 @@ async function simulateBorrowing(
470493
471494 logActivity (
472495 `Borrower ${ borrower . wallet . address . slice ( 0 , 6 ) } ... swapped ${ ethers . formatEther ( safeEthToSwap ) . slice ( 0 , 6 ) } ETH for MyUSD ` +
473- `to repay debt (rate: ${ currentBorrowRate } > ${ borrower . maxAcceptableRate } )` ,
496+ `to repay debt (rate: ${ currentBorrowRate } > ${ borrower . maxAcceptableRate } )` ,
474497 ) ;
475498 continue ;
476499 } catch ( error : any ) {
@@ -486,7 +509,7 @@ async function simulateBorrowing(
486509 const rateFactor =
487510 1 -
488511 ( Math . min ( currentBorrowRate , borrower . maxAcceptableRate ) / borrower . maxAcceptableRate ) *
489- ( borrower . rateSensitivity / 100 ) ;
512+ ( borrower . rateSensitivity / 100 ) ;
490513
491514 // Higher tolerance + lower rate sensitivity = more borrowing
492515 const borrowingWillingness = ( borrower . debtTolerance / 100 ) * rateFactor ;
@@ -583,7 +606,7 @@ async function executeBorrowing(
583606
584607 logActivity (
585608 `Borrower ${ borrower . wallet . address . slice ( 0 , 6 ) } ... leveraged borrowed ${ ethers . formatEther ( borrowAmount ) . slice ( 0 , 6 ) } MyUSD ` +
586- `(rate: ${ currentBorrowRate } bps, willingness: ${ ( borrowingWillingness * 100 ) . toFixed ( 1 ) } %)` ,
609+ `(rate: ${ currentBorrowRate } bps, willingness: ${ ( borrowingWillingness * 100 ) . toFixed ( 1 ) } %)` ,
587610 ) ;
588611 } catch ( error : any ) {
589612 logActivity ( `Leveraged borrowing failed for ${ borrower . wallet . address . slice ( 0 , 6 ) } ... Error: ${ error } ` ) ;
@@ -617,7 +640,7 @@ async function simulateStaking(
617640
618641 logActivity (
619642 `Staker ${ staker . wallet . address . slice ( 0 , 6 ) } ... unstaked ALL shares ` +
620- `(rate: ${ currentSavingsRate } bps < min rate: ${ staker . minAcceptableRate } bps)` ,
643+ `(rate: ${ currentSavingsRate } bps < min rate: ${ staker . minAcceptableRate } bps)` ,
621644 ) ;
622645 }
623646 } catch ( error : any ) {
@@ -637,7 +660,7 @@ async function simulateStaking(
637660
638661 logActivity (
639662 `Staker ${ staker . wallet . address . slice ( 0 , 6 ) } ... sold ALL MyUSD (${ ethers . formatEther ( sellableBalance ) . slice ( 0 , 6 ) } ) for ETH ` +
640- `(rate too low: ${ currentSavingsRate } < ${ staker . minAcceptableRate } bps)` ,
663+ `(rate too low: ${ currentSavingsRate } < ${ staker . minAcceptableRate } bps)` ,
641664 ) ;
642665 }
643666 } catch ( error : any ) {
@@ -685,7 +708,7 @@ async function simulateStaking(
685708 await stakingWithStaker . stake ( amountToStake ) ;
686709 logActivity (
687710 `Staker ${ staker . wallet . address . slice ( 0 , 6 ) } ... staked ${ ethers . formatEther ( amountToStake ) . slice ( 0 , 6 ) } MyUSD ` +
688- `(rate: ${ currentSavingsRate } bps, willingness: ${ ( stakingWillingness * 100 ) . toFixed ( 1 ) } %)` ,
711+ `(rate: ${ currentSavingsRate } bps, willingness: ${ ( stakingWillingness * 100 ) . toFixed ( 1 ) } %)` ,
689712 ) ;
690713 } catch ( error : any ) {
691714 logActivity ( `Failed to stake for ${ staker . wallet . address . slice ( 0 , 6 ) } ...` ) ;
@@ -754,12 +777,12 @@ async function main() {
754777 logActivity ( "Initializing simulator..." ) ;
755778
756779 const [ deployer ] = await ethers . getSigners ( ) ;
757- const dex = await ethers . getContract < DEX > ( "DEX" , deployer ) ;
758- const oracle = await ethers . getContract < Oracle > ( "Oracle" , deployer ) ;
780+ const dex : DEX = DEX__factory . connect ( getDeployedAddress ( "DEX" ) , deployer ) ;
781+ const oracle : Oracle = Oracle__factory . connect ( getDeployedAddress ( "Oracle" ) , deployer ) ;
759782 const ethPrice = await oracle . getETHUSDPrice ( ) ;
760- const engine = await ethers . getContract < MyUSDEngine > ( "MyUSDEngine" , deployer ) ;
761- const myUSD = await ethers . getContract < MyUSD > ( "MyUSD" , deployer ) ;
762- const staking = await ethers . getContract < MyUSDStaking > ( "MyUSDStaking" , deployer ) ;
783+ const engine : MyUSDEngine = MyUSDEngine__factory . connect ( getDeployedAddress ( "MyUSDEngine" ) , deployer ) ;
784+ const myUSD : MyUSD = MyUSD__factory . connect ( getDeployedAddress ( "MyUSD" ) , deployer ) ;
785+ const staking : MyUSDStaking = MyUSDStaking__factory . connect ( getDeployedAddress ( "MyUSDStaking" ) , deployer ) ;
763786
764787 logActivity ( "Connected to deployed contracts" ) ;
765788 const accounts = await setupAccounts ( ) ;
0 commit comments