comprend tu ces fichier server.js const express = require('express'); const ganache =…
- User
comprend tu ces fichier server.js const express = require('express'); const ganache = require('ganache'); const solc = require('solc'); const { ethers } = require('ethers');
const app = express(); const apiPort = 3000; const ganachePort = 8545;
app.use(express.json()); app.use(express.static('public'));
// Lance Ganache en mode serveur HTTP const ganacheServer = ganache.server({ logging: { quiet: true }, wallet: { totalAccounts: 10, defaultBalance: 100 } }); ganacheServer.listen(ganachePort, () => { console.log(
Ganache HTTP server listening on port ${ganachePort}); });// Connecte ethers à ce serveur Ganache HTTP const providerEthers = new ethers.providers.JsonRpcProvider(
http://localhost:${ganachePort}); const signer = providerEthers.getSigner();let transactionsHistory = []; let deployedContracts = [];
// Mise à jour de l’historique transactions (20 derniers blocs) async function updateTransactionHistory() { try { const latestBlockNum = await providerEthers.getBlockNumber(); const txs = []; for (let i = latestBlockNum; i > latestBlockNum - 20 && i >= 0; i--) { const block = await providerEthers.getBlockWithTransactions(i); if (block && block.transactions) { txs.push(...block.transactions); } } transactionsHistory = txs; } catch (err) { console.error('Erreur updateTransactionHistory:', err.message); } }
// Update initiale updateTransactionHistory();
// API : comptes + soldes app.get('/api/accounts', async (req, res) => { try { const accounts = await providerEthers.listAccounts(); const balances = {}; for (const addr of accounts) { const bal = await providerEthers.getBalance(addr); balances[addr] = Number(ethers.utils.formatEther(bal)); } res.json({ accounts, balances }); } catch (err) { res.status(500).json({ error: err.message }); } });
// API : envoyer transaction app.post('/api/sendTransaction', async (req, res) => { try { const { from, to, value } = req.body; if (!from || !to || !value) { return res.status(400).json({ error: 'from, to et value requis' }); } const txResponse = await signer.sendTransaction({ from, to, value: ethers.utils.parseEther(value.toString()), gasLimit: 21000 }); await txResponse.wait(); await updateTransactionHistory(); res.json({ txHash: txResponse.hash }); } catch (err) { res.status(500).json({ error: err.message }); } });
// API : blocs avec pagination app.get('/api/blocks', async (req, res) => { try { const page = parseInt(req.query.page) || 1; const limit = parseInt(req.query.limit) || 5; const latestBlockNum = await providerEthers.getBlockNumber();
const blocks = []; const startBlock = latestBlockNum - (page - 1) * limit; for (let i = startBlock; i > startBlock - limit && i >= 0; i--) { const block = await providerEthers.getBlockWithTransactions(i); if (block) blocks.push(block); } res.json({ blocks, page, limit });} catch (err) { res.status(500).json({ error: err.message }); } });
- Assistant
- User
comment creer un core blockchaine dans ce projet pour creer des noeud sur plusieur ordinateur
- Assistant
- User
je souhainte pas installer quoi que se sois sur mon ordinateur ni sur ce leui de mes utlilisateur alors aide mois trouver solution
- Assistant
- User
ok je veux que tu me donne un exemple de code pour gérer plusieurs instances Ganache dans un même backend et exposer l’accès au client
- Assistant
- User
si jais bien compris je mon ganache en ligne avec et chaque utilistaur aura une interface sur son odinateur ce cre un multi noeud
- Assistant
- User
c quoi une intece plusieurs instances Ganache
- Assistant
- User
detaiile moi plus la solution 3 precedente
- Assistant
- User
integrer infura dans le projet
- Assistant
- User
et pour les noeud des utilisateur
- Assistant
- User
et les frais de gas sur infura
- Assistant
- User
c possible surr le projet denvoyer une trransaction sur ganach ganache
- Assistant