eatthepie
Version:
Command line app for interacting with Eat The Pie, the world lottery on World Chain.
139 lines (120 loc) ⢠4.35 kB
JavaScript
import inquirer from "inquirer";
import chalk from "chalk";
import { loadConfig } from "../utils/config.js";
import { createWalletClient } from "../utils/ethereum.js";
import { claimPrize } from "../services/gameService.js";
/**
* Error messages that require special handling
*/
const ERROR_MESSAGES = {
DRAW_NOT_COMPLETE: "Game draw not completed yet",
ALREADY_CLAIMED: "Prize already claimed",
NO_PRIZE: "No prize to claim",
};
/**
* Success messages
*/
const SUCCESS_MESSAGES = {
CLAIMED: "š Prize claimed successfully! š",
};
/**
* Handles the process of claiming a prize for a specific game number.
* This includes validating the game number and processing the claim transaction.
*/
async function claimPrizeHandler() {
try {
// Initialize wallet client and configuration
const config = await loadConfig();
const walletClient = createWalletClient(config);
console.log(chalk.cyan("\nš« Initiating prize claim process..."));
// Get game number from user
const gameNumber = await promptForGameNumber();
// Process the prize claim
await processPrizeClaim(walletClient, config.contractAddress, gameNumber);
} catch (error) {
handleClaimError(error);
}
}
/**
* Prompts the user to input a game number
* @returns {Promise<number>} The selected game number
*/
async function promptForGameNumber() {
const { gameNumber } = await inquirer.prompt([
{
type: "number",
name: "gameNumber",
message:
"š® Enter the game number for which you want to claim the prize:",
validate: (input) => input > 0 || "ā ļø Please enter a valid game number",
},
]);
return gameNumber;
}
/**
* Processes the prize claim transaction and displays results
* @param {WalletClient} walletClient - The wallet client instance
* @param {string} contractAddress - The lottery contract address
* @param {number} gameNumber - The game number to claim prize for
*/
async function processPrizeClaim(walletClient, contractAddress, gameNumber) {
console.log(chalk.yellow("\nš° Processing your prize claim..."));
const txHash = await claimPrize(walletClient, contractAddress, gameNumber);
displaySuccessMessages(txHash);
// Wait for confirmation
await waitForTransactionConfirmation(walletClient, txHash);
}
/**
* Waits for a transaction to be confirmed and displays the confirmation
* @param {WalletClient} walletClient - The wallet client instance
* @param {string} txHash - The transaction hash to wait for
*/
async function waitForTransactionConfirmation(walletClient, txHash) {
console.log(chalk.yellow("\nā³ Waiting for transaction to be confirmed..."));
const receipt = await walletClient.waitForTransactionReceipt({
hash: txHash,
confirmations: 1,
});
console.log(chalk.cyan("š¦ Block Number:"), receipt.blockNumber);
console.log(chalk.green("\nā
Transaction confirmed successfully!"));
}
/**
* Displays success messages after prize claim transaction
* @param {string} txHash - The transaction hash
*/
function displaySuccessMessages(txHash) {
console.log(chalk.yellow("\nš Transaction Hash:"), txHash);
console.log(chalk.green(SUCCESS_MESSAGES.CLAIMED));
console.log(chalk.cyan("š° Check your wallet for the claimed prize!"));
}
/**
* Handles errors that occur during the prize claiming process
* @param {Error} error - The error to handle
*/
function handleClaimError(error) {
const errorMessages = {
[ERROR_MESSAGES.DRAW_NOT_COMPLETE]: "ā° Game draw not completed yet.",
[ERROR_MESSAGES.ALREADY_CLAIMED]: "š¢ Prize already claimed for this game.",
[ERROR_MESSAGES.NO_PRIZE]: "šØ No prize to claim for this game.",
};
// Check if error message matches any of our known error types
for (const [errorType, message] of Object.entries(errorMessages)) {
if (error.message.includes(errorType)) {
console.log(chalk.yellow(`\n${message}`));
return;
}
}
// Handle unknown errors
console.error(chalk.red("\nā Error:"), error.shortMessage || error.message);
console.error(
chalk.red(
"\nā ļø Make sure your settings are correct.\nš§ Run 'config' to view them and 'setup' to reset them."
)
);
process.exit(1);
}
export default {
command: "claim-prize",
describe: "š Claim your prize",
handler: claimPrizeHandler,
};