UNPKG

@b3dotfun/leaderboards

Version:

SDK to interact with leaderboards smart contract

149 lines 7.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const dotenv = tslib_1.__importStar(require("dotenv")); const chains_1 = require("viem/chains"); const leaderboardFactory_1 = require("../leaderboardFactory"); dotenv.config(); const CHAIN_ID = chains_1.base.id; // Use "testnet" for testnet, "mainnet" for mainnet const ADMIN_ADDRESS = "0x66f2D0B4B85856360741735fB2531844de7f1d5e"; /** * Initialize and connect to the LeaderboardFactory contract * @returns Connected LeaderboardFactory instance */ function initializeFactory() { const factory = new leaderboardFactory_1.LeaderboardFactory(CHAIN_ID, process.env.LEADERBOARD_ADMIN_PRIVATE_KEY); const isConnected = factory.connect(); if (!isConnected) { console.error("Failed to connect to the Factory contract."); return null; } console.log("Connected to the Factory contract"); return factory; } /** * Create a new leaderboard with specified parameters * @param factory Connected LeaderboardFactory instance * @param slug Unique identifier for the leaderboard * @param startTime Start time of the leaderboard in seconds * @param endTime End time of the leaderboard in seconds * @param title Title of the leaderboard * @param isPrivate Whether the leaderboard is private (optional) * @returns The address of the created leaderboard contract */ async function createNewLeaderboard(factory, slug, startTime, endTime, title = `${slug}-${new Date().toISOString()}`, // Default title if not specified isPrivate = false, useLatestScore = false, hasAttemptLimit = false, initialAttempts = 0, isReverseSort = false) { console.log(`Creating leaderboard "${slug}" with following parameters:`); console.log(`- Title: ${title}`); console.log(`- Start time: ${new Date(startTime * 1000).toISOString()}`); console.log(`- End time: ${new Date(endTime * 1000).toISOString()}`); console.log(`- Private: ${isPrivate}`); console.log(`- Use Latest Score: ${useLatestScore}`); console.log(`- Has Attempt Limit: ${hasAttemptLimit}`); console.log(`- Initial Attempts: ${initialAttempts}`); console.log(`- Reverse Sort: ${isReverseSort}`); const leaderboardAddress = await factory.createLeaderboard(ADMIN_ADDRESS, slug, BigInt(startTime), BigInt(endTime), title, isPrivate, useLatestScore, hasAttemptLimit, initialAttempts, isReverseSort); console.log("Created new leaderboard at address:", leaderboardAddress); return leaderboardAddress; } /** * Query and display all leaderboards for a specific slug * @param factory Connected LeaderboardFactory instance * @param slug Unique identifier to query */ async function queryLeaderboards(factory, slug) { // Get all leaderboards with their properties const leaderboardsWithProps = await factory.getLeaderboardsWithProps(slug); if (leaderboardsWithProps.length > 0) { console.log(`\nAll leaderboards for "${slug}":`); console.log("================================="); leaderboardsWithProps.forEach((leaderboard, index) => { console.log(`\nLeaderboard #${index + 1}:`); console.log("- Address:", leaderboard.address); console.log("- Title:", leaderboard.properties.title); console.log("- Start time:", new Date(leaderboard.properties.startTime * 1000).toISOString()); console.log("- End time:", new Date(leaderboard.properties.endTime * 1000).toISOString()); console.log("- Concluded:", leaderboard.properties.hasConcluded ? "Yes" : "No"); console.log("- Private:", leaderboard.properties.isPrivate ? "Yes" : "No"); console.log("- Use Latest Score:", leaderboard.properties.useLatestScore ? "Yes" : "No"); console.log("- Has Attempt Limit:", leaderboard.properties.hasAttemptLimit ? "Yes" : "No"); console.log("- Initial Attempts:", leaderboard.properties.initialAttempts); console.log("- Reverse Sort:", leaderboard.properties.isReverseSort ? "Yes" : "No"); }); // Get count for this slug const slugCount = await factory.getLeaderboardCountBySlug(slug); console.log(`\nTotal number of leaderboards for "${slug}":`, slugCount); } else { console.log(`\nNo leaderboards found for "${slug}"`); } } /** * Get and display global leaderboard statistics * @param factory Connected LeaderboardFactory instance */ async function getGlobalStats(factory) { const totalCount = await factory.getTotalLeaderboardCount(); console.log("Total leaderboards across all slugs:", totalCount); } /** * Get and display all leaderboards across all slugs * @param factory Connected LeaderboardFactory instance */ async function getAllLeaderboards(factory) { // Get all leaderboards with their properties const allLeaderboardsWithProps = await factory.getAllLeaderboardsWithProps(); if (allLeaderboardsWithProps.length > 0) { console.log("\nAll Leaderboards Across All Slugs:"); console.log("=================================="); allLeaderboardsWithProps.forEach((leaderboard, index) => { console.log(`\nLeaderboard #${index + 1}:`); console.log("- Address:", leaderboard.address); console.log("- Slug:", leaderboard.properties.slug); console.log("- Title:", leaderboard.properties.title); console.log("- Start time:", new Date(leaderboard.properties.startTime * 1000).toISOString()); console.log("- End time:", new Date(leaderboard.properties.endTime * 1000).toISOString()); console.log("- Concluded:", leaderboard.properties.hasConcluded ? "Yes" : "No"); console.log("- Private:", leaderboard.properties.isPrivate ? "Yes" : "No"); console.log("- Use Latest Score:", leaderboard.properties.useLatestScore ? "Yes" : "No"); console.log("- Has Attempt Limit:", leaderboard.properties.hasAttemptLimit ? "Yes" : "No"); console.log("- Initial Attempts:", leaderboard.properties.initialAttempts); console.log("- Reverse Sort:", leaderboard.properties.isReverseSort ? "Yes" : "No"); }); console.log(`\nTotal number of leaderboards: ${allLeaderboardsWithProps.length}`); } else { console.log("\nNo leaderboards found"); } } async function main() { try { // Initialize the factory const factory = initializeFactory(); if (!factory) return; // Calculate timestamps for a 24-hour leaderboard starting now const startTime = Math.floor(Date.now() / 1000); // Current time in seconds const endTime = startTime + 24 * 3600; // 24 hours from now // Create a new leaderboard const leaderboardAddress = await createNewLeaderboard(factory, "game-tournament-1", startTime, endTime, "Game Tournament #1", // title false, // isPrivate false, // useLatestScore true, // hasAttemptLimit 3, // initialAttempts false // isReverseSort ); // Query leaderboard information await queryLeaderboards(factory, "game-tournament-1"); // Get all leaderboards across all slugs await getAllLeaderboards(factory); // Get global statistics await getGlobalStats(factory); } catch (error) { console.error("Error:", error); } } // Execute the example main().catch(console.error); //# sourceMappingURL=leaderboardFactory.js.map