diff --git a/index.ts b/index.ts index f7402dd015da5ed4febc6bfbe23925dc0517fd31..8bd2e3049cd8be3bb0c37717d0ab9a3d5dad6302 100644 --- a/index.ts +++ b/index.ts @@ -16,28 +16,26 @@ import { handleAdminChoice, handleAdherentChoice, handleConnectedChoice } from ' const tezos = new TezosToolkit('https://ghostnet.ecadinfra.com'); const program = new Command(); -program - .option('-f, --file <filePath>', 'Chemin du fichier JSON du portefeuille') - .parse(process.argv); - -if (!program.args) { - console.error('Erreur: Veuillez spécifier le chemin du fichier JSON du portefeuille avec l\'option -f'); - process.exit(1); -} - -// LOAD WALLET FROM JSON -const walletData = JSON.parse(fs.readFileSync(program.args[0], 'utf8')); - -// REGISTER THE PROVIDER -const signer = new InMemorySigner(walletData.privateKey); -tezos.setProvider({ signer }); +program.name('my-asso') + .description('My Asso CLI') + .version('1.0.0'); + +program.command('main') + .argument('<filePath>', 'Chemin du fichier JSON du portefeuille') + .action(async (file) => { + // LOAD WALLET FROM JSON + const walletData = JSON.parse(fs.readFileSync(file, 'utf8')); + + // REGISTER THE PROVIDER + const signer = new InMemorySigner(walletData.privateKey); + tezos.setProvider({ signer }); -program - .action(async () => { + // DISPLAY HEADER await getAsciiArtText() console.log(`\n`); console.log(chalk.bgBlue(`Vous êtes connecté en tant que ${getRole(tezos)}\n`)) + // START MAIN APPLICATION while (true) { let choice: string switch(getRole(tezos)) { @@ -56,5 +54,5 @@ program } } }); - - program.parse(process.argv); + +program.parse() \ No newline at end of file diff --git a/src/handlers/handlers.ts b/src/handlers/handlers.ts index c0a5a3fc87cda5680bcc7c1d194bbd764b4ba5b9..5d881fa15c1a193ecc14009ae06015cbe6cd6b55 100644 --- a/src/handlers/handlers.ts +++ b/src/handlers/handlers.ts @@ -15,7 +15,12 @@ import { createAssociation, joinAssociation } from '../services/association.serv import { createProposal } from '../services/proposal.service.js'; import { createFA21Contract } from '../services/contract.service.js'; -async function handleCreateAsssociation(tezos: TezosToolkit) { +/** + * Handles the process of creating an association. + * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. + * @returns {Promise<void>} A promise resolved once the association creation process is complete. + */ +async function handleCreateAsssociation(tezos: TezosToolkit): Promise<void> { const name = await askQuestion(chalk.yellow('Nom: ')); const description = await askQuestion(chalk.yellow('Description: ')); const association: Association = { @@ -32,8 +37,13 @@ async function handleCreateAsssociation(tezos: TezosToolkit) { console.log("Erreur lors de la création de l'association"); } } - -async function handleCreateToken(tezos: TezosToolkit) { + +/** + * Handles the process of creating a fungible token. + * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. + * @returns {Promise<void>} A promise resolved once the token creation process is complete. + */ +async function handleCreateToken(tezos: TezosToolkit): Promise<void> { const nbTokenFungible: string = await askQuestion(chalk.yellow('Nombre de token fongible: ')); try { @@ -45,7 +55,12 @@ async function handleCreateToken(tezos: TezosToolkit) { } } -async function handleGetBalance(tezos: TezosToolkit) { +/** + * Handles the process of retrieving the balance of the wallet. + * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. + * @returns {Promise<void>} A promise resolved once the balance retrieval process is complete. + */ +async function handleGetBalance(tezos: TezosToolkit): Promise<void> { try { const balance = await getBalance(tezos); console.log(`\nSolde du portefeuille: ${balance} ꜩ\n`); @@ -54,7 +69,12 @@ async function handleGetBalance(tezos: TezosToolkit) { } } -async function handleCreateProposal(tezos: TezosToolkit) { +/** + * Handles the process of creating a proposal. + * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. + * @returns {Promise<void>} A promise resolved once the proposal creation process is complete. + */ +async function handleCreateProposal(tezos: TezosToolkit): Promise<void> { const title = await askQuestion(chalk.yellow('Titre: ')); const description = await askQuestion(chalk.yellow('Description: ')); const proposal: Proposal = { @@ -72,7 +92,12 @@ async function handleCreateProposal(tezos: TezosToolkit) { } } -async function handleJoinAssociation(tezos: TezosToolkit) { +/** + * Handles the process of joining an association. + * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. + * @returns {Promise<void>} A promise resolved once the joining process is complete. + */ +async function handleJoinAssociation(tezos: TezosToolkit): Promise<void> { const name = await askQuestion(chalk.yellow("Nom de l'association: ")); try { @@ -86,7 +111,13 @@ async function handleJoinAssociation(tezos: TezosToolkit) { console.log(chalk.green("\nVous avez rejoint l'association !!\n")); } -async function handleAdminChoice(choice: string, tezos: TezosToolkit) { +/** + * Handles administrator actions based on the specified choice. + * @param {string} choice - The administrator's choice. + * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. + * @returns {Promise<void>} A promise resolved once the processing is complete. + */ +async function handleAdminChoice(choice: string, tezos: TezosToolkit): Promise<void> { switch(choice) { case "0": break; @@ -100,12 +131,18 @@ async function handleAdminChoice(choice: string, tezos: TezosToolkit) { await handleGetBalance(tezos); break; default: - console.log('Choix invalide'); + console.log(chalk.bgRedBright('\nChoix invalide\n')); break; } } -async function handleAdherentChoice(choice: string, tezos: TezosToolkit) { +/** + * Handles adherent actions based on the specified choice. + * @param {string} choice - The adherent's choice. + * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. + * @returns {Promise<void>} A promise resolved once the processing is complete. + */ +async function handleAdherentChoice(choice: string, tezos: TezosToolkit): Promise<void> { switch(choice) { case "0": break; @@ -116,12 +153,18 @@ async function handleAdherentChoice(choice: string, tezos: TezosToolkit) { await handleGetBalance(tezos); break; default: - console.log('Choix invalide'); + console.log(chalk.bgRedBright('\nChoix invalide\n')); break; } } -async function handleConnectedChoice(choice: string, tezos: TezosToolkit) { +/** + * Handles connected actions based on the specified choice. + * @param {string} choice - The connected's choice. + * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. + * @returns {Promise<void>} A promise resolved once the processing is complete. + */ +async function handleConnectedChoice(choice: string, tezos: TezosToolkit): Promise<void> { switch(choice) { case "0": break; @@ -132,7 +175,7 @@ async function handleConnectedChoice(choice: string, tezos: TezosToolkit) { await handleGetBalance(tezos); break; default: - console.log('Choix invalide'); + console.log(chalk.bgRedBright('\nChoix invalide\n')); break; } }