Skip to content

Commit 4fd4460

Browse files
committed
fix: scripts and tests
1 parent 9c10c81 commit 4fd4460

5 files changed

Lines changed: 91 additions & 41 deletions

File tree

extension/packages/hardhat/deploy/00_deploy_contracts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export default deployScript(
8888
// Set deployer ETH balance
8989
await env.network.provider.request({
9090
method: "hardhat_setBalance",
91-
params: [deployer, `0x${parseEther("100000000000000000000").toString(16)}`],
91+
params: [deployer, `0x${parseEther("100000000000000000000").toString(16)}`] as never,
9292
});
9393

9494
// The deployer is going to provide liquidity to the DEX so that we can swap tokens

extension/packages/hardhat/scripts/interestRateController.ts

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
1-
import hre from "hardhat";
2-
import { DEX, RateController, MyUSDStaking, MyUSDEngine, Oracle } from "../typechain-types";
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
import { fileURLToPath } from "node:url";
4+
import { network } from "hardhat";
5+
import type {
6+
DEX,
7+
RateController,
8+
MyUSDStaking,
9+
MyUSDEngine,
10+
Oracle,
11+
} from "../types/ethers-contracts/index.js";
12+
import {
13+
DEX__factory,
14+
RateController__factory,
15+
MyUSDStaking__factory,
16+
MyUSDEngine__factory,
17+
Oracle__factory,
18+
} from "../types/ethers-contracts/index.js";
319

4-
const ethers = hre.ethers;
20+
const { ethers, networkName } = await network.create();
21+
22+
const deploymentsDir = path.join(
23+
path.dirname(fileURLToPath(import.meta.url)),
24+
"..",
25+
"deployments",
26+
networkName,
27+
);
28+
29+
function getDeployedAddress(name: string): string {
30+
const filePath = path.join(deploymentsDir, `${name}.json`);
31+
return JSON.parse(fs.readFileSync(filePath, "utf8")).address;
32+
}
533

634
// --- Config ---
735
const TARGET_PRICE = 1;
@@ -135,11 +163,11 @@ function checkPegHit(direction: "UP" | "DOWN" | "FLAT"): boolean {
135163

136164
async function main() {
137165
const [deployer] = await ethers.getSigners();
138-
const dex = await ethers.getContract<DEX>("DEX", deployer);
139-
const rateController = await ethers.getContract<RateController>("RateController", deployer);
140-
const engine = await ethers.getContract<MyUSDEngine>("MyUSDEngine", deployer);
141-
const staking = await ethers.getContract<MyUSDStaking>("MyUSDStaking", deployer);
142-
const oracle = await ethers.getContract<Oracle>("Oracle", deployer);
166+
const dex: DEX = DEX__factory.connect(getDeployedAddress("DEX"), deployer);
167+
const rateController: RateController = RateController__factory.connect(getDeployedAddress("RateController"), deployer);
168+
const engine: MyUSDEngine = MyUSDEngine__factory.connect(getDeployedAddress("MyUSDEngine"), deployer);
169+
const staking: MyUSDStaking = MyUSDStaking__factory.connect(getDeployedAddress("MyUSDStaking"), deployer);
170+
const oracle: Oracle = Oracle__factory.connect(getDeployedAddress("Oracle"), deployer);
143171
const ethPrice = await oracle.getETHUSDPrice();
144172

145173
const startBorrowRate = await engine.borrowRate();
@@ -235,7 +263,7 @@ async function main() {
235263
const { newRate, newState } = getNextRate(borrowState, direction, isPriceStable);
236264
logChange(
237265
`Price ${currentPriceEth.toFixed(6)} ${currentPriceEth > TARGET_PRICE ? "above" : "below"} peg, ` +
238-
`adjusting borrow rate to ${newRate}bps [${newState.searchBounds.low}, ${newState.searchBounds.high}]`,
266+
`adjusting borrow rate to ${newRate}bps [${newState.searchBounds.low}, ${newState.searchBounds.high}]`,
239267
);
240268
await rateController.setBorrowRate(newRate);
241269
Object.assign(borrowState, newState);
@@ -253,7 +281,7 @@ async function main() {
253281
const boundedRate = Math.min(Math.max(newRate, SAVINGS_RATE_MIN), maximumRate);
254282
logChange(
255283
`Price ${currentPriceEth.toFixed(6)} ${currentPriceEth > TARGET_PRICE ? "above" : "below"} peg, ` +
256-
`adjusting savings rate to ${boundedRate}bps [${newState.searchBounds.low}, ${maximumRate}]`,
284+
`adjusting savings rate to ${boundedRate}bps [${newState.searchBounds.low}, ${maximumRate}]`,
257285
);
258286
await rateController.setSavingsRate(boundedRate);
259287
Object.assign(savingsState, newState);

extension/packages/hardhat/scripts/marketSimulator.ts

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
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";
25
import { 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
1033
interface 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();

extension/packages/hardhat/test/MyUSDEngine.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,10 @@ describe("🚩 Stablecoin Challenge 🤓", function () {
2525
let collateralAmount: bigint;
2626
let borrowAmount: bigint;
2727

28-
before(async function () {
28+
beforeEach(async function () {
2929
({ ethers } = await network.create());
3030
collateralAmount = ethers.parseEther("10");
3131
borrowAmount = ethers.parseEther("5000");
32-
});
33-
34-
beforeEach(async function () {
35-
await ethers.provider.send("hardhat_reset", []);
3632
[owner, user1, user2] = await ethers.getSigners();
3733

3834
// For SRE Auto-grader - use the the downloaded contract instead of default contract
@@ -59,7 +55,10 @@ describe("🚩 Stablecoin Challenge 🤓", function () {
5955

6056
// Deploy RateController first
6157
const RateControllerFactory = await ethers.getContractFactory("RateController");
62-
rateController = (await RateControllerFactory.deploy(futureEngineAddress, futureStakingAddress)) as unknown as RateController;
58+
rateController = (await RateControllerFactory.deploy(
59+
futureEngineAddress,
60+
futureStakingAddress,
61+
)) as unknown as RateController;
6362

6463
// Deploy MyUSD with future addresses
6564
const MyUSDFactory = await ethers.getContractFactory("MyUSD");
@@ -434,8 +433,8 @@ describe("🚩 Stablecoin Challenge 🤓", function () {
434433
});
435434

436435
it("Should prevent setting borrow rate below savings rate", async function () {
437-
await rateController.setSavingsRate(0);
438-
expect(await rateController.setBorrowRate(300)).to.be.revertedWithCustomError(
436+
await rateController.setSavingsRate(350);
437+
await expect(rateController.setBorrowRate(300)).to.be.revertedWithCustomError(
439438
myUSDEngine,
440439
"Engine__InvalidBorrowRate",
441440
);

extension/packages/nextjs/app/_components/Modals/TokenSwapModal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export const TokenSwapModal = ({ tokenBalance, connectedAddress, ETHprice, modal
124124
<span className="text-sm pl-3">
125125
{tokenBalance} {tokenName}
126126
</span>
127-
<Balance address={connectedAddress as Address} className="min-h-0 h-auto" />
127+
<Balance address={connectedAddress as Address} style={{ minHeight: 0, height: "auto" }} />
128128
</div>
129129
</div>
130130
</div>

0 commit comments

Comments
 (0)