services.mjs 1.43 KB
Newer Older
John Doe's avatar
John Doe committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
import inquirer from 'inquirer';
import { deposit, transact, balance } from '../script/zk.js'
import { ethers } from 'ethers';

let trees;
let data;

export const serviceController = {
    error: "",
    balance: balance,
    deposit: async () => {

        let amountPrompt = await inquirer.prompt({
            type: "input",
            name: "amount",
            message: "Enter the amount of XFT to deposit: "
        })

        if (isNaN(amountPrompt.amount)) {
            let errorMsg = "No amount specified";
            serviceController.error = errorMsg;
            console.log(errorMsg);
            return;
        }

        const arg = { amount: ethers.parseEther(amountPrompt.amount) };
        [trees, data] = await deposit(arg);
    },
    withdraw: async () => {

        let amountPrompt = await inquirer.prompt({
            type: "input",
            name: "amount",
            message: "Enter the amount of XFT to withdraw: "
        })

        if (isNaN(amountPrompt.amount)) {
            let errorMsg = "No amount specified";
            serviceController.error = errorMsg;
            console.log(errorMsg);
            return;
        }
        const args = { amount: ethers.parseEther(amountPrompt.amount), trees: trees, data: data };

        await transact(args);
    }
}

export const commandController = {
    'd': serviceController.deposit,
    'w': serviceController.withdraw,
    'r': () => true,
    'exit': process.exit
}