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

split logic

parent 98228c4d
Branches main
No related tags found
No related merge requests found
import { Command } from 'commander'; import { Command } from 'commander';
import chalk from 'chalk'; import chalk from 'chalk';
import { createInterface } from 'readline';
import fs from "fs"; import fs from "fs";
import { TezosToolkit } from '@taquito/taquito'; import { TezosToolkit } from '@taquito/taquito';
import { InMemorySigner } from '@taquito/signer'; import { InMemorySigner } from '@taquito/signer';
import figlet from 'figlet'; import { askQuestion } from './src/utils/askQuestion.js';
import { getBalance } from './src/services/getBalance.js';
// START SERVICE import { getAsciiArtText } from './src/utils/getAsciiArtText.js';
function createToken(address: string, tezos) {
tezos.wallet.at(address)
.then((contract) => contract.methodsObject.discoverPlanet({
density: 0,
planet_type: "terrestrial",
coordinate: [0,0,0],
name: "test"
}).send())
.then((op) => {
console.log(`Hash: ${op.opHash}`);
return op.confirmation();
})
.then((result) => {
console.log(result);
if (result.completed) {
console.log(`Transaction correctly processed!
Block: ${result.block.header.level}
Chain ID: ${result.block.chain_id}`);
} else {
console.log('An error has occurred');
}
})
.catch((err) => console.log(err));
}
// END SERVICE
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();
async function askQuestion(question: string): Promise<string> {
const readline = createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise(resolve => {
readline.question(question, (answer: string) => {
resolve(answer.trim());
readline.close();
});
});
}
program program
.option('-f, --file <filePath>', 'Chemin du fichier JSON du portefeuille') .option('-f, --file <filePath>', 'Chemin du fichier JSON du portefeuille')
.parse(process.argv); .parse(process.argv);
...@@ -60,43 +19,16 @@ if (!program.args) { ...@@ -60,43 +19,16 @@ if (!program.args) {
process.exit(1); process.exit(1);
} }
// Charger le fichier JSON du portefeuille // LOAD WALLET FROM JSON
const walletData = JSON.parse(fs.readFileSync(program.args[0], 'utf8')); const walletData = JSON.parse(fs.readFileSync(program.args[0], 'utf8'));
// REGISTER THE PROVIDER
const signer = new InMemorySigner(walletData.privateKey); const signer = new InMemorySigner(walletData.privateKey);
tezos.setProvider({ signer }); tezos.setProvider({ signer });
async function getBalance(privateKey) {
try {
// Obtenez le solde du portefeuille associé à la clé privée
const balance = await tezos.tz.getBalance(await tezos.signer.publicKeyHash());
console.log(`\nSolde du portefeuille: ${balance.toNumber() / 1000000} ꜩ\n`);
} catch (error) {
console.error('Erreur lors de la récupération du solde:', error);
}
}
program program
.action(async () => { .action(async () => {
await figlet.text( await getAsciiArtText()
"My Asso",
{
font: "Big Money-nw",
horizontalLayout: "default",
verticalLayout: "default",
width: 80,
whitespaceBreak: true,
},
function (err, data) {
if (err) {
console.log("Something went wrong...");
console.dir(err);
return;
}
console.log(data);
}
);
console.log(`\n`); console.log(`\n`);
// Définir un moyen de connaître le rôle // Définir un moyen de connaître le rôle
...@@ -128,7 +60,8 @@ program ...@@ -128,7 +60,8 @@ program
// Faire le call pour créer un token // Faire le call pour créer un token
console.log(chalk.green("\nVotre token a été créé !!\n")); console.log(chalk.green("\nVotre token a été créé !!\n"));
} else if (choice === '3') { } else if (choice === '3') {
await getBalance(walletData.privateKey); const balance = await getBalance(tezos);
console.log(`\nSolde du portefeuille: ${balance} ꜩ\n`);
} else { } else {
console.log('Choix invalide'); console.log('Choix invalide');
} }
...@@ -146,7 +79,8 @@ program ...@@ -146,7 +79,8 @@ program
console.log(chalk.green("\nVous avez soumis une proposition !!\n")); console.log(chalk.green("\nVous avez soumis une proposition !!\n"));
// Faire l'appel a post proposal association // Faire l'appel a post proposal association
} else if (choice === '2') { } else if (choice === '2') {
await getBalance(walletData.privateKey); const balance = await getBalance(tezos);
console.log(`\nSolde du portefeuille: ${balance} ꜩ\n`);
} else { } else {
console.log('Choix invalide'); console.log('Choix invalide');
} }
...@@ -166,7 +100,8 @@ program ...@@ -166,7 +100,8 @@ program
console.log(chalk.green("\nVous avez rejoint l'association !!\n")); console.log(chalk.green("\nVous avez rejoint l'association !!\n"));
// Faire l'appel a join association // Faire l'appel a join association
} else if (choice === '2') { } else if (choice === '2') {
await getBalance(walletData.privateKey); const balance = await getBalance(tezos);
console.log(`\nSolde du portefeuille: ${balance} ꜩ\n`);
} else { } else {
console.log('Choix invalide'); console.log('Choix invalide');
} }
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
}, },
"devDependencies": { "devDependencies": {
"@types/commander": "^2.12.2", "@types/commander": "^2.12.2",
"@types/figlet": "^1.5.8",
"@types/node": "^20.11.19" "@types/node": "^20.11.19"
} }
}, },
...@@ -415,6 +416,12 @@ ...@@ -415,6 +416,12 @@
"commander": "*" "commander": "*"
} }
}, },
"node_modules/@types/figlet": {
"version": "1.5.8",
"resolved": "https://registry.npmjs.org/@types/figlet/-/figlet-1.5.8.tgz",
"integrity": "sha512-G22AUvy4Tl95XLE7jmUM8s8mKcoz+Hr+Xm9W90gJsppJq9f9tHvOGkrpn4gRX0q/cLtBdNkWtWCKDg2UDZoZvQ==",
"dev": true
},
"node_modules/@types/node": { "node_modules/@types/node": {
"version": "20.11.19", "version": "20.11.19",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.19.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.19.tgz",
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
}, },
"devDependencies": { "devDependencies": {
"@types/commander": "^2.12.2", "@types/commander": "^2.12.2",
"@types/figlet": "^1.5.8",
"@types/node": "^20.11.19" "@types/node": "^20.11.19"
} }
} }
function createToken(address: string, tezos) { function createToken(address: string, tezos: any) {
const planetRequest = {
name: "Earth",
coordinate: [0, 0, 0],
planet_type: ["Terrestrial"],
density: 1n
};
tezos.wallet.at(address) tezos.wallet.at(address)
.then((contract) => contract.methodsObject.areYouThere(true).send()) .then(async (contract) => {
await contract.methods.discoverPlanet(planetRequest, contract.storage()).send();
})
.then((op) => { .then((op) => {
console.log(`Hash: ${op.opHash}`); console.log(`Hash: ${op.opHash}`);
return op.confirmation(); return op.confirmation();
......
import { TezosToolkit } from "@taquito/taquito";
async function getBalance(tezos: TezosToolkit) {
try {
const balance = await tezos.tz.getBalance(await tezos.signer.publicKeyHash());
return balance.toNumber() / 1000000
} catch (error) {
console.error('Erreur lors de la récupération du solde:', error);
}
}
export { getBalance }
\ No newline at end of file
import { createInterface } from 'readline';
async function askQuestion(question: string): Promise<string> {
const readline = createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise(resolve => {
readline.question(question, (answer: string) => {
resolve(answer.trim());
readline.close();
});
});
}
export { askQuestion }
\ No newline at end of file
import figlet from 'figlet';
async function getAsciiArtText() {
await figlet.text(
"My Asso",
{
font: "Big Money-nw",
horizontalLayout: "default",
verticalLayout: "default",
width: 80,
whitespaceBreak: true,
},
function (err, data) {
if (err) {
console.log("Something went wrong...");
console.dir(err);
return;
}
console.log(data);
}
);
}
export { getAsciiArtText }
\ No newline at end of file
{ {
"compilerOptions": { "compilerOptions": {
"target": "es6", "target": "ES2022",
"module": "esnext", "module": "esnext",
"esModuleInterop": true, "esModuleInterop": true,
"moduleResolution": "node", "moduleResolution": "node",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment