Skip to content
Snippets Groups Projects
Commit 7bf5d60a authored by Nawfel Senoussi's avatar Nawfel Senoussi
Browse files

add showTokenBalance and hasToken

parent 31e75dcc
No related branches found
No related tags found
No related merge requests found
......@@ -16,7 +16,7 @@ import { type WalletData } from './src/types/WalletData.js'
// IMPORT HANDLERS
import { handleAdminChoice, handleAdherentChoice, handleConnectedChoice } from './src/handlers/roleHandlers.js'
import { type Role } from './src/types/Role.js'
import { handleHasToken } from './src/utils/hasToken.js'
import { hasToken } from './src/features/token/hasToken.js'
const tezos = new TezosToolkit('https://ghostnet.tezos.marigold.dev')
const program = new Command()
......@@ -54,29 +54,28 @@ program.command('main')
]
switch (role) {
case 'ADMIN':
questions[0].choices = ['Résoudre une proposition', 'Clôturer une proposition', 'Voir les propositions', 'Créer un token']
questions[0].choices = ['Résoudre une proposition', 'Clôturer une proposition', 'Voir les propositions', 'Créer un token', 'Voir ma balance de tokens']
if (await handleHasToken(tezos)) { questions[0].choices.push('Brûler un token') }
if (await hasToken(tezos)) { questions[0].choices.push('Brûler un token') }
await inquirer.prompt(questions).then(async (answers: { choice: string }) => {
await handleAdminChoice(answers.choice, tezos)
})
break
case 'ADHERENT':
// VOTER POUR UNE PROPOSITION
// CLOTURER ET RESOUDRE LES PROPOSITIONS DONT IL EST LE CREATEUR
questions[0].choices = ['Faire une proposition', 'Voter pour une proposition', 'Voir les propositions', 'Créer un token']
// TODO: CLOTURER ET RESOUDRE LES PROPOSITIONS DONT IL EST LE CREATEUR
questions[0].choices = ['Faire une proposition', 'Voter pour une proposition', 'Voir les propositions', 'Créer un token', 'Voir ma balance de tokens']
if (await handleHasToken(tezos)) { questions[0].choices.push('Brûler un token') }
if (await hasToken(tezos)) { questions[0].choices.push('Brûler un token') }
await inquirer.prompt(questions).then(async (answers: { choice: string }) => {
await handleAdherentChoice(answers.choice, tezos)
})
break
case 'CONNECTED':
questions[0].choices = ['Rejoindre une association', 'Voir les associations', "Voir les détails d'une association", 'Créer un token']
questions[0].choices = ['Rejoindre une association', 'Voir les associations', "Voir les détails d'une association", 'Créer un token', 'Voir ma balance de tokens']
if (await handleHasToken(tezos)) { questions[0].choices.push('Brûler un token', 'Créer une association') }
if (await hasToken(tezos)) { questions[0].choices.push('Brûler un token', 'Créer une association') }
await inquirer.prompt(questions).then(async (answers: { choice: string }) => {
await handleConnectedChoice(answers.choice, tezos)
......
import { type TezosToolkit } from '@taquito/taquito'
import chalk from 'chalk'
import { handleGetTokenBalance } from '../../handlers/token/tokenHandlers.js'
/**
* Return if there's a token.
* @param {TezosToolkit} tezos - The instance of the Tezos toolkit.
* @returns {Promise<boolean>} A promise resolved once the token balance is displayed.
*/
async function hasToken (tezos: TezosToolkit): Promise<boolean> {
let hasToken = false
await handleGetTokenBalance(tezos).then((response) => {
hasToken = response > 0
}).catch((error) => {
console.log(chalk.bgRed(`\n${error.message}`))
return false
})
return hasToken
}
export { hasToken }
import { type TezosToolkit } from '@taquito/taquito'
import chalk from 'chalk'
import { handleGetTokenBalance } from '../../handlers/token/tokenHandlers.js'
/**
* Displays token balance.
* @param {TezosToolkit} tezos - The instance of the Tezos toolkit.
* @returns {Promise<void>} A promise resolved once the token balance is displayed.
*/
async function showTokenBalance (tezos: TezosToolkit): Promise<void> {
await handleGetTokenBalance(tezos).then((response) => {
console.log(`${chalk.cyan('Nombre de token:')} ${chalk.yellow(response)}`)
}).catch((error) => {
console.log(chalk.bgRed(`\n${error.message}`))
})
}
export { showTokenBalance }
......@@ -12,6 +12,7 @@ import { resolveProposal } from '../features/proposal/resolveProposal.js'
import { showProposals } from '../features/proposal/showProposals.js'
import { closeProposal } from '../features/proposal/closeProposal.js'
import { voteProposal } from '../features/proposal/voteProposal.js'
import { showTokenBalance } from '../features/token/showTokenBalance.js'
/**
* Handles administrator actions based on the specified choice.
......@@ -36,6 +37,9 @@ async function handleAdminChoice (choice: string, tezos: TezosToolkit): Promise<
case 'Voir les propositions':
await showProposals(tezos)
break
case 'Voir ma balance de tokens':
await showTokenBalance(tezos)
break
case 'Voir mon portefeuille':
await showBalance(tezos)
break
......@@ -71,6 +75,9 @@ async function handleAdherentChoice (choice: string, tezos: TezosToolkit): Promi
case 'Brûler un token':
await burnToken(tezos)
break
case 'Voir ma balance de tokens':
await showTokenBalance(tezos)
break
default:
console.log(chalk.bgRedBright('\nChoix invalide\n'))
break
......@@ -106,6 +113,9 @@ async function handleConnectedChoice (choice: string, tezos: TezosToolkit): Prom
case 'Brûler un token':
await burnToken(tezos)
break
case 'Voir ma balance de tokens':
await showTokenBalance(tezos)
break
default:
console.log(chalk.bgRedBright('\nChoix invalide\n'))
break
......
......@@ -2,7 +2,7 @@
import { type TezosToolkit } from '@taquito/taquito'
// IMPORT SERVICE
import { burnToken, createFAToken } from '../../services/token.service.js'
import { burnToken, createFAToken, getTokenBalance } from '../../services/token.service.js'
import inquirer from 'inquirer'
/**
......@@ -57,4 +57,17 @@ async function handleBurnToken (tezos: TezosToolkit): Promise<void> {
}
}
export { handleCreateToken, handleBurnToken }
/**
* Handles the process of get token balance.
* @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations.
* @returns {Promise<void>} A promise resolved once the retrieve process is complete.
*/
async function handleGetTokenBalance (tezos: TezosToolkit): Promise<number> {
try {
return await getTokenBalance(tezos)
} catch (error) {
throw new Error(`${error}`)
}
}
export { handleCreateToken, handleBurnToken, handleGetTokenBalance }
......@@ -16,18 +16,15 @@ async function burnToken (tezos: TezosToolkit): Promise<void> {
await op.confirmation()
}
// NEED UPDATE ENTRYPOINT !!
async function hasToken (tezos: TezosToolkit): Promise<boolean> {
// const contract = await tezos.contract.at(address)
// const executionContextParams = {
// viewCaller: contract.address
// }
// const op: Operation = await contract.methodsObject.hasToken().executeView(executionContextParams)
async function getTokenBalance (tezos: TezosToolkit): Promise<number> {
const contract = await tezos.contract.at(address)
// await op.confirmation()
const executionContextParams = {
viewCaller: contract.address
}
const balance = await contract.contractViews.get_balance(await tezos.signer.publicKeyHash()).executeView(executionContextParams)
return true
return balance.token.toNumber()
}
export { createFAToken, burnToken, hasToken }
export { createFAToken, burnToken, getTokenBalance }
import { type TezosToolkit } from '@taquito/taquito'
import { hasToken } from '../services/token.service.js'
async function handleHasToken (tezos: TezosToolkit): Promise<boolean> {
return await hasToken(tezos)
}
export { handleHasToken }
......@@ -12,6 +12,7 @@ import { showAssociations } from '../../src/features/association/showAssociation
import { resolveProposal } from '../../src/features/proposal/resolveProposal'
import { showProposals } from '../../src/features/proposal/showProposals'
import { closeProposal } from '../../src/features/proposal/closeProposal'
import { showTokenBalance } from '../../src/features/token/showTokenBalance'
vi.mock('../../src/features/association/createAssociation', () => ({
createAssociation: vi.fn()
......@@ -57,6 +58,10 @@ vi.mock('../../src/features/proposal/showProposals', () => ({
showProposals: vi.fn()
}))
vi.mock('../../src/features/token/showTokenBalance', () => ({
showTokenBalance: vi.fn()
}))
const mockedTezosToolkit = {} as unknown as TezosToolkit
describe('roleHandlers', () => {
......@@ -105,6 +110,14 @@ describe('roleHandlers', () => {
})
})
describe('when choice is "Voir ma balance de tokens"', () => {
it('should call showTokenBalance', async () => {
await handleAdminChoice('Voir ma balance de tokens', mockedTezosToolkit)
expect(showTokenBalance).toBeCalled()
})
})
describe('when choice is "Voir mon portefeuille"', () => {
it('should call showBalance', async () => {
await handleAdminChoice('Voir mon portefeuille', mockedTezosToolkit)
......@@ -151,7 +164,7 @@ describe('roleHandlers', () => {
describe('when choice is "Brûler un token"', () => {
it('should call burnToken', async () => {
await handleAdminChoice('Brûler un token', mockedTezosToolkit)
await handleAdherentChoice('Brûler un token', mockedTezosToolkit)
expect(burnToken).toBeCalled()
})
......@@ -159,12 +172,20 @@ describe('roleHandlers', () => {
describe('when choice is "Voir les propositions"', () => {
it('should call showProposals', async () => {
await handleAdminChoice('Voir les propositions', mockedTezosToolkit)
await handleAdherentChoice('Voir les propositions', mockedTezosToolkit)
expect(showProposals).toBeCalled()
})
})
describe('when choice is "Voir ma balance de tokens"', () => {
it('should call showTokenBalance', async () => {
await handleAdherentChoice('Voir ma balance de tokens', mockedTezosToolkit)
expect(showTokenBalance).toBeCalled()
})
})
describe('when choice is invalid', () => {
it('should log "Choix invalide"', async () => {
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
......@@ -218,12 +239,20 @@ describe('roleHandlers', () => {
describe('when choice is "Brûler un token"', () => {
it('should call burnToken', async () => {
await handleAdminChoice('Brûler un token', mockedTezosToolkit)
await handleConnectedChoice('Brûler un token', mockedTezosToolkit)
expect(burnToken).toBeCalled()
})
})
describe('when choice is "Voir ma balance de tokens"', () => {
it('should call showTokenBalance', async () => {
await handleConnectedChoice('Voir ma balance de tokens', mockedTezosToolkit)
expect(showTokenBalance).toBeCalled()
})
})
describe('when choice is invalid', () => {
it('should log "Choix invalide"', async () => {
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment