Skip to content
Snippets Groups Projects
Select Git revision
  • 10b940badd5507990d63c77c18a172e578d05bdc
  • main default protected
2 results

handlers.ts

Blame
  • handlers.ts 6.50 KiB
    // IMPORT EXTERNAL LIBS
    import chalk from 'chalk'
    import { type TezosToolkit } from '@taquito/taquito'
    
    // IMPORT UTILS
    import { askQuestion } from '../utils/askQuestion.js'
    
    // IMPORT TYPES
    import { type Association } from '../types/Association.js'
    import { type Proposal } from '../types/Proposal.js'
    
    // IMPORT SERVICES
    import { getBalance } from '../services/balance.service.js'
    import { createAssociation, getAssociationDetails, getAssociations, joinAssociation } from '../services/association.service.js'
    import { createProposal } from '../services/proposal.service.js'
    import { createFAToken } from '../services/contract.service.js'
    import { burnToken } from '../services/token.service.js'
    
    /**
     * 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 handleCreateAssociation (tezos: TezosToolkit): Promise<void> {
      const name = await askQuestion(chalk.yellow('Nom: '))
      const description = await askQuestion(chalk.yellow('Description: '))
      const association: Association = {
        name,
        description
      }
    
      console.log("Création de l'association en cours...")
      try {
        const op = await createAssociation(association, tezos)
        console.log(op.hash)
        console.log(chalk.green('\nVotre association a été crée !!\n'))
      } catch {
        console.log(chalk.bgRedBright("\nErreur lors de la création de l'association\n"))
      }
    }
    
    /**
     * 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 {
        const op = await createFAToken(nbTokenFungible, tezos)
        console.log(op.hash)
        console.log(chalk.green('\nVotre token a été créé !!\n'))
      } catch {
        console.log(chalk.bgRedBright('\nErreur lors de la création du token\n'))
      }
    }
    
    /**
     * 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`)
      } catch {
        console.log(chalk.bgRedBright('\nErreur lors de la récupération de votre portefeuille\n'))
      }
    }
    
    /**
     * 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 = {
        title,
        description
      }
    
      try {
        console.log('Création de la proposition en cours...')
        const op = await createProposal(proposal, tezos)
        console.log(op.hash)
        console.log(chalk.green('\nVous avez soumis une proposition !!\n'))
      } catch {
        console.log(chalk.bgRedBright('\nErreur lors de la création de la proposition\n'))
      }
    }
    
    /**
     * 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 {
        console.log("Inscription à l'association en cours...")
        const op = await joinAssociation(name, tezos)
        console.log(op.hash)
        console.log(chalk.green('\nVous avez soumis une proposition !!\n'))
      } catch {
        // ERREUR -> PAS ASSEZ DE TOKEN
        // ERREUR -> ASSOCIATION NON EXISTANTE
        console.log(chalk.bgRedBright("\nErreur lors de l'inscription à l'association\n"))
      }
      console.log(chalk.green("\nVous avez rejoint l'association !!\n"))
    }
    
    /**
     * Handles the process of listing associations.
     * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations.
     * @returns {Promise<void>} A promise resolved once the joining process is complete.
     */
    async function handleGetAssociations (tezos: TezosToolkit): Promise<void> {
      try {
        console.log('Récupération des associations en cours...')
        const op = await getAssociations(tezos)
        console.log(op.hash)
        // LISTER LES ASSOCIATIONS
      } catch {
        console.log(chalk.bgRedBright('\nErreur lors de la récupération des associations\n'))
      }
    }
    
    /**
     * Handles the process of getting association details.
     * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations.
     * @returns {Promise<void>} A promise resolved once the joining process is complete.
     */
    async function handleGetAssociationDetails (tezos: TezosToolkit): Promise<void> {
      const name = await askQuestion(chalk.yellow("Nom de l'association: "))
    
      try {
        console.log("Récupération des détails de l'association en cours...")
        const op = await getAssociationDetails(name, tezos)
        console.log(op.hash)
        // RETURN ASSOCIATIONS
      } catch {
        console.log(chalk.bgRedBright("\nErreur lors de la récupération des détails de l'association\n"))
      }
    }
    
    /**
     * 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 handleBurnToken (tezos: TezosToolkit): Promise<void> {
      const nbTokenToBurn = await askQuestion(chalk.yellow('Nombre de token à bruler: '))
    
      try {
        console.log('Brulure des tokens en cours...')
        const op = await burnToken(Number(nbTokenToBurn), tezos)
        console.log(op.hash)
        console.log(chalk.green('\nVous avez bruler vos tokens !!\n'))
      } catch {
        console.log(chalk.bgRedBright('\nErreur lors de la brulure de vos tokens\n'))
      }
    }
    
    export { handleCreateAssociation, handleCreateToken, handleGetBalance, handleCreateProposal, handleJoinAssociation, handleGetAssociations, handleGetAssociationDetails, handleBurnToken }