Skip to content

Commit 39e0827

Browse files
authored
Merge pull request #28 from ewdlop/ewdlop-patch-22
Update interact.js
2 parents 3cd93f7 + 06f5de6 commit 39e0827

File tree

1 file changed

+54
-9
lines changed

1 file changed

+54
-9
lines changed

App/scripts/interact.js

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,68 @@
11
const Web3 = require('web3');
2-
//const MyNFT = require('../build/contracts/MyNFT.json');
3-
require('dotenv').config(); // To use environment variables
2+
require('dotenv').config();
3+
const log4js = require('log4js');
44

5-
const web3 = new Web3('http://localhost:8545'); // Connect to Ganache
5+
// Configure log4js
6+
log4js.configure({
7+
appenders: {
8+
console: { type: 'console' }, // Logs to the console
9+
file: { type: 'file', filename: 'nft.log' }, // Logs to a file
10+
// Add more appenders as needed (e.g., for different log levels)
11+
},
12+
categories: {
13+
default: { appenders: ['console', 'file'], level: 'info' }, // Default category
14+
// You can create different categories for different parts of your app
15+
// nft: { appenders: ['file'], level: 'debug' } For more detailed NFT logs
16+
},
17+
});
618

7-
//const contractAddress = MyNFT.networks['5777'].address; // Replace with your network ID if different
8-
//const contract = new web3.eth.Contract(MyNFT.abi, contractAddress);
19+
const logger = log4js.getLogger(); // Get the default logger
20+
// const logger = log4js.getLogger('nft'); // To use the 'nft' category
921

10-
const account = process.env.ACCOUNT_ADDRESS; // Use environment variable for account
1122

12-
console.log(account)
23+
const web3 = new Web3('http://localhost:8545');
24+
25+
const account = process.env.ACCOUNT_ADDRESS;
26+
27+
logger.info(`Account address: ${account}`); // Log the account address
28+
29+
// Example usage of different log levels:
30+
// logger.debug('This is a debug message.');
31+
// logger.info('This is an info message.');
32+
// logger.warn('This is a warning message.');
33+
// logger.error('This is an error message.');
34+
35+
36+
// const contractAddress = MyNFT.networks['5777'].address;
37+
// const contract = new web3.eth.Contract(MyNFT.abi, contractAddress);
1338

1439
// async function mintNFT() {
1540
// try {
1641
// const tx = await contract.methods.mintNFT(account).send({ from: account });
17-
// console.log('Transaction:', tx);
42+
// logger.info('Transaction:', tx); // Log the transaction details
1843
// } catch (error) {
19-
// console.error('Error minting NFT:', error);
44+
// logger.error('Error minting NFT:', error); // Log the error
2045
// }
2146
// }
2247

2348
// mintNFT();
49+
50+
51+
// Example of logging Web3 connection status (you might put this in a function):
52+
async function checkWeb3Connection() {
53+
try {
54+
const isConnected = await web3.eth.net.isListening();
55+
if (isConnected) {
56+
logger.info('Successfully connected to Ganache.');
57+
const networkId = await web3.eth.net.getId();
58+
logger.info(`Network ID: ${networkId}`);
59+
60+
} else {
61+
logger.error('Failed to connect to Ganache.');
62+
}
63+
} catch (error) {
64+
logger.error('Error checking Web3 connection:', error);
65+
}
66+
}
67+
68+
checkWeb3Connection();

0 commit comments

Comments
 (0)