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' ...@@ -16,7 +16,7 @@ import { type WalletData } from './src/types/WalletData.js'
// IMPORT HANDLERS // IMPORT HANDLERS
import { handleAdminChoice, handleAdherentChoice, handleConnectedChoice } from './src/handlers/roleHandlers.js' import { handleAdminChoice, handleAdherentChoice, handleConnectedChoice } from './src/handlers/roleHandlers.js'
import { type Role } from './src/types/Role.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 tezos = new TezosToolkit('https://ghostnet.tezos.marigold.dev')
const program = new Command() const program = new Command()
...@@ -54,29 +54,28 @@ program.command('main') ...@@ -54,29 +54,28 @@ program.command('main')
] ]
switch (role) { switch (role) {
case 'ADMIN': 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 inquirer.prompt(questions).then(async (answers: { choice: string }) => {
await handleAdminChoice(answers.choice, tezos) await handleAdminChoice(answers.choice, tezos)
}) })
break break
case 'ADHERENT': case 'ADHERENT':
// VOTER POUR UNE PROPOSITION // TODO: CLOTURER ET RESOUDRE LES PROPOSITIONS DONT IL EST LE CREATEUR
// 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']
questions[0].choices = ['Faire une proposition', 'Voter pour une proposition', 'Voir les propositions', 'Créer un token']
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 inquirer.prompt(questions).then(async (answers: { choice: string }) => {
await handleAdherentChoice(answers.choice, tezos) await handleAdherentChoice(answers.choice, tezos)
}) })
break break
case 'CONNECTED': 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 inquirer.prompt(questions).then(async (answers: { choice: string }) => {
await handleConnectedChoice(answers.choice, tezos) 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' ...@@ -12,6 +12,7 @@ import { resolveProposal } from '../features/proposal/resolveProposal.js'
import { showProposals } from '../features/proposal/showProposals.js' import { showProposals } from '../features/proposal/showProposals.js'
import { closeProposal } from '../features/proposal/closeProposal.js' import { closeProposal } from '../features/proposal/closeProposal.js'
import { voteProposal } from '../features/proposal/voteProposal.js' import { voteProposal } from '../features/proposal/voteProposal.js'
import { showTokenBalance } from '../features/token/showTokenBalance.js'
/** /**
* Handles administrator actions based on the specified choice. * Handles administrator actions based on the specified choice.
...@@ -36,6 +37,9 @@ async function handleAdminChoice (choice: string, tezos: TezosToolkit): Promise< ...@@ -36,6 +37,9 @@ async function handleAdminChoice (choice: string, tezos: TezosToolkit): Promise<
case 'Voir les propositions': case 'Voir les propositions':
await showProposals(tezos) await showProposals(tezos)
break break
case 'Voir ma balance de tokens':
await showTokenBalance(tezos)
break
case 'Voir mon portefeuille': case 'Voir mon portefeuille':
await showBalance(tezos) await showBalance(tezos)
break break
...@@ -71,6 +75,9 @@ async function handleAdherentChoice (choice: string, tezos: TezosToolkit): Promi ...@@ -71,6 +75,9 @@ async function handleAdherentChoice (choice: string, tezos: TezosToolkit): Promi
case 'Brûler un token': case 'Brûler un token':
await burnToken(tezos) await burnToken(tezos)
break break
case 'Voir ma balance de tokens':
await showTokenBalance(tezos)
break
default: default:
console.log(chalk.bgRedBright('\nChoix invalide\n')) console.log(chalk.bgRedBright('\nChoix invalide\n'))
break break
...@@ -106,6 +113,9 @@ async function handleConnectedChoice (choice: string, tezos: TezosToolkit): Prom ...@@ -106,6 +113,9 @@ async function handleConnectedChoice (choice: string, tezos: TezosToolkit): Prom
case 'Brûler un token': case 'Brûler un token':
await burnToken(tezos) await burnToken(tezos)
break break
case 'Voir ma balance de tokens':
await showTokenBalance(tezos)
break
default: default:
console.log(chalk.bgRedBright('\nChoix invalide\n')) console.log(chalk.bgRedBright('\nChoix invalide\n'))
break break
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import { type TezosToolkit } from '@taquito/taquito' import { type TezosToolkit } from '@taquito/taquito'
// IMPORT SERVICE // IMPORT SERVICE
import { burnToken, createFAToken } from '../../services/token.service.js' import { burnToken, createFAToken, getTokenBalance } from '../../services/token.service.js'
import inquirer from 'inquirer' import inquirer from 'inquirer'
/** /**
...@@ -57,4 +57,17 @@ async function handleBurnToken (tezos: TezosToolkit): Promise<void> { ...@@ -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> { ...@@ -16,18 +16,15 @@ async function burnToken (tezos: TezosToolkit): Promise<void> {
await op.confirmation() await op.confirmation()
} }
// NEED UPDATE ENTRYPOINT !! async function getTokenBalance (tezos: TezosToolkit): Promise<number> {
async function hasToken (tezos: TezosToolkit): Promise<boolean> { const contract = await tezos.contract.at(address)
// const contract = await tezos.contract.at(address)
// const executionContextParams = {
// viewCaller: contract.address
// }
// const op: Operation = await contract.methodsObject.hasToken().executeView(executionContextParams)
// 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 ...@@ -12,6 +12,7 @@ import { showAssociations } from '../../src/features/association/showAssociation
import { resolveProposal } from '../../src/features/proposal/resolveProposal' import { resolveProposal } from '../../src/features/proposal/resolveProposal'
import { showProposals } from '../../src/features/proposal/showProposals' import { showProposals } from '../../src/features/proposal/showProposals'
import { closeProposal } from '../../src/features/proposal/closeProposal' import { closeProposal } from '../../src/features/proposal/closeProposal'
import { showTokenBalance } from '../../src/features/token/showTokenBalance'
vi.mock('../../src/features/association/createAssociation', () => ({ vi.mock('../../src/features/association/createAssociation', () => ({
createAssociation: vi.fn() createAssociation: vi.fn()
...@@ -57,6 +58,10 @@ vi.mock('../../src/features/proposal/showProposals', () => ({ ...@@ -57,6 +58,10 @@ vi.mock('../../src/features/proposal/showProposals', () => ({
showProposals: vi.fn() showProposals: vi.fn()
})) }))
vi.mock('../../src/features/token/showTokenBalance', () => ({
showTokenBalance: vi.fn()
}))
const mockedTezosToolkit = {} as unknown as TezosToolkit const mockedTezosToolkit = {} as unknown as TezosToolkit
describe('roleHandlers', () => { describe('roleHandlers', () => {
...@@ -105,6 +110,14 @@ 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"', () => { describe('when choice is "Voir mon portefeuille"', () => {
it('should call showBalance', async () => { it('should call showBalance', async () => {
await handleAdminChoice('Voir mon portefeuille', mockedTezosToolkit) await handleAdminChoice('Voir mon portefeuille', mockedTezosToolkit)
...@@ -151,7 +164,7 @@ describe('roleHandlers', () => { ...@@ -151,7 +164,7 @@ describe('roleHandlers', () => {
describe('when choice is "Brûler un token"', () => { describe('when choice is "Brûler un token"', () => {
it('should call burnToken', async () => { it('should call burnToken', async () => {
await handleAdminChoice('Brûler un token', mockedTezosToolkit) await handleAdherentChoice('Brûler un token', mockedTezosToolkit)
expect(burnToken).toBeCalled() expect(burnToken).toBeCalled()
}) })
...@@ -159,12 +172,20 @@ describe('roleHandlers', () => { ...@@ -159,12 +172,20 @@ describe('roleHandlers', () => {
describe('when choice is "Voir les propositions"', () => { describe('when choice is "Voir les propositions"', () => {
it('should call showProposals', async () => { it('should call showProposals', async () => {
await handleAdminChoice('Voir les propositions', mockedTezosToolkit) await handleAdherentChoice('Voir les propositions', mockedTezosToolkit)
expect(showProposals).toBeCalled() 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', () => { describe('when choice is invalid', () => {
it('should log "Choix invalide"', async () => { it('should log "Choix invalide"', async () => {
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
...@@ -218,12 +239,20 @@ describe('roleHandlers', () => { ...@@ -218,12 +239,20 @@ describe('roleHandlers', () => {
describe('when choice is "Brûler un token"', () => { describe('when choice is "Brûler un token"', () => {
it('should call burnToken', async () => { it('should call burnToken', async () => {
await handleAdminChoice('Brûler un token', mockedTezosToolkit) await handleConnectedChoice('Brûler un token', mockedTezosToolkit)
expect(burnToken).toBeCalled() 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', () => { describe('when choice is invalid', () => {
it('should log "Choix invalide"', async () => { it('should log "Choix invalide"', async () => {
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) 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