deploy.js
1 const { ethers } = require("hardhat"); 2 const fs = require("fs"); 3 const path = require("path"); 4 5 async function main() { 6 const [deployer] = await ethers.getSigners(); 7 console.log("Deploying contract with account:", deployer.address); 8 9 const OrderBook = await ethers.getContractFactory("OrderBook"); 10 const orderBook = await OrderBook.deploy(); 11 await orderBook.waitForDeployment(); 12 13 const deployedAddress = await orderBook.getAddress(); 14 console.log("OrderBook deployed to:", deployedAddress); 15 16 const filePath = path.join(__dirname, "../../dapp/src/contract-address.json"); 17 fs.writeFileSync(filePath, JSON.stringify({ address: deployedAddress }, null, 2)); 18 console.log("Wrote contract address to", filePath); 19 20 console.log("Seeding sample orders..."); 21 22 const prices = { 23 0: 204.07, // AAPL 24 1: 269.26, // TSLA 25 2: 858.36 // NVDA 26 }; 27 28 const priceWei = (price) => ethers.parseUnits(price.toFixed(2).toString(), 18); // Round to 2 decimals 29 30 for (let securityId = 0; securityId < 3; securityId++) { 31 const basePrice = prices[securityId]; 32 33 // ±1% for quantity 5 34 let tx = await orderBook.seedOrder( 35 deployedAddress, 36 securityId, 37 true, // Buy 38 priceWei(basePrice * 0.99), // -1% 39 5 40 ); 41 await tx.wait(); 42 43 tx = await orderBook.seedOrder( 44 deployedAddress, 45 securityId, 46 false, // Sell 47 priceWei(basePrice * 1.01), // +1% 48 5 49 ); 50 await tx.wait(); 51 52 // ±2% for quantity 20 53 tx = await orderBook.seedOrder( 54 deployedAddress, 55 securityId, 56 true, // Buy 57 priceWei(basePrice * 0.98), // -2% 58 20 59 ); 60 await tx.wait(); 61 62 tx = await orderBook.seedOrder( 63 deployedAddress, 64 securityId, 65 false, // Sell 66 priceWei(basePrice * 1.02), // +2% 67 20 68 ); 69 await tx.wait(); 70 71 console.log(`Seeded orders for security ${securityId}`); 72 } 73 74 console.log("Sample orders seeded successfully."); 75 } 76 77 main().catch((error) => { 78 console.error(error); 79 process.exitCode = 1; 80 });