Spaces:
Sleeping
Sleeping
| // deploy.js - Script to deploy the MorphGuard Verification smart contract | |
| const Web3 = require('web3'); | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| // Connect to local Ethereum node | |
| const web3 = new Web3('http://localhost:8545'); | |
| async function deploy() { | |
| try { | |
| console.log('Connected to Ethereum node:', await web3.eth.getNodeInfo()); | |
| // Get accounts | |
| const accounts = await web3.eth.getAccounts(); | |
| const deployer = accounts[0]; // Use the first account for deployment | |
| console.log('Deploying from account:', deployer); | |
| console.log('Account balance:', web3.utils.fromWei(await web3.eth.getBalance(deployer), 'ether'), 'ETH'); | |
| // Read contract source and compile | |
| const contractPath = path.join(__dirname, 'SmartContract.sol'); | |
| const contractSource = fs.readFileSync(contractPath, 'utf8'); | |
| // For simplicity, in a real scenario you would use solc for compilation | |
| // Here we'll assume you've already compiled the contract using Remix or Truffle | |
| // and have the ABI and bytecode | |
| // Read compiled contract data | |
| const compiledContract = { | |
| abi: JSON.parse(fs.readFileSync(path.join(__dirname, 'abi.json'), 'utf8')), | |
| bytecode: fs.readFileSync(path.join(__dirname, 'bytecode.bin'), 'utf8') | |
| }; | |
| // Create contract instance | |
| const Contract = new web3.eth.Contract(compiledContract.abi); | |
| // Deploy contract | |
| console.log('Deploying MorphGuardVerification contract...'); | |
| const deployTx = Contract.deploy({ | |
| data: compiledContract.bytecode | |
| }); | |
| // Send the transaction | |
| const gas = await deployTx.estimateGas(); | |
| const deployedContract = await deployTx.send({ | |
| from: deployer, | |
| gas | |
| }); | |
| console.log('Contract deployed at address:', deployedContract.options.address); | |
| // Save the contract address to a file | |
| fs.writeFileSync( | |
| path.join(__dirname, 'contract-address.txt'), | |
| deployedContract.options.address | |
| ); | |
| // Create environment variable settings file | |
| const envContent = ` | |
| # MorphGuard Ethereum settings | |
| ETH_ENDPOINT=http://localhost:8545 | |
| ETH_CONTRACT_ADDRESS=${deployedContract.options.address} | |
| ETH_WALLET_ADDRESS=${deployer} | |
| # Warning: Store your private key securely in production! | |
| # ETH_PRIVATE_KEY=your_private_key_here | |
| `.trim(); | |
| fs.writeFileSync(path.join(__dirname, '..', '.env.ethereum'), envContent); | |
| console.log('Deployment completed successfully!'); | |
| console.log('Environment variables saved to .env.ethereum'); | |
| } catch (error) { | |
| console.error('Deployment failed:', error); | |
| } | |
| } | |
| deploy(); |