Leaderboard Smart Contract - TypeScript SDK
NPC Labs presents the TypeScript SDK for our On-Chain Leaderboard Smart Contract, a key contributor to B3 - the gaming ecosystem. This SDK enables game developers to seamlessly incorporate secure on-chain leaderboards for tracking and displaying user scores on the blockchain.
Features
- Create and manage multiple leaderboards with unique slugs
- Public and private leaderboards
- User eligibility management for private leaderboards
- User blacklisting
- Flexible score management (increment, decrement, update)
- Admin management system
- Real-time ranking and score tracking
- Time-based leaderboard lifecycle management
- Configurable score update behavior with
useLatestScore
flag
- Attempt limit management with
hasAttemptLimit
and initialAttempts
- Reverse sorting option with
isReverseSort
Installation
You can install our TypeScript SDK using the commands below:
yarn add @b3dotfun/leaderboards
npm install @b3dotfun/leaderboards
Getting Started
LeaderboardSDK Props
-
chainId
: (SupportedChainId
)
- This parameter specifies the chain ID for the network on which the SDK is operating (e.g.,
b3.id
, b3Sepolia.id
, base.id
).
- The SDK uses this ID to load the appropriate contract addresses and RPC URLs automatically.
- Import the specific chain ID from
viem/chains
(e.g., import { b3Sepolia } from "viem/chains"; const chainId = b3Sepolia.id;
).
-
walletKey
: string
(optional)
- This is an optional parameter that represents the wallet key used for transactions. If provided, it should be a hexadecimal string starting with
0x
.
- The
walletKey
is required for write operations that modify the state of the blockchain (e.g., deploying contracts, adding/removing admins, setting scores). For read operations (e.g., retrieving leaderboard data, user scores), the walletKey
is not required.
-
contractAddress
: string
(optional)
- This optional parameter specifies the address of the leaderboard contract. It should be a hexadecimal string starting with
0x
.
- The
contractAddress
is used to interact with the specific leaderboard contract. This parameter is only used when initializing the Leaderboard
class, not LeaderboardFactory
.
Score Update Behavior
The Leaderboard smart contract includes a useLatestScore
flag that determines how scores are updated:
Attempt Limit Management
The Leaderboard smart contract includes features for managing user attempts:
Reverse Sorting
The Leaderboard smart contract includes a isReverseSort
flag that determines how scores are sorted:
When creating a new leaderboard, you can specify these flags:
const leaderboardAddress = await factory.createLeaderboard(
ADMIN_ADDRESS,
slug,
BigInt(startTime),
BigInt(endTime),
title,
false,
true,
true,
3,
false
);
Examples
1. Creating and Managing Leaderboards
Here's a complete example of creating and managing leaderboards using the Factory contract:
import * as dotenv from "dotenv";
import type { Address } from "viem";
import { LeaderboardFactory } from "@b3dotfun/leaderboards";
import { b3Sepolia } from "viem/chains";
dotenv.config();
const CHAIN_ID = b3Sepolia.id;
const ADMIN_ADDRESS = "0x742d35Cc6634C0532925a3b844Bc454e4438f44e";
function initializeFactory(): LeaderboardFactory | null {
const factory = new 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;
}
async function createNewLeaderboard(
factory: LeaderboardFactory,
slug: string,
startTime: number,
endTime: number,
title: string,
isPrivate: boolean = false,
useLatestScore: boolean = false,
hasAttemptLimit: boolean = false,
initialAttempts: number = 0,
isReverseSort: boolean = false
): Promise<Address> {
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 ? "Yes (Latest Score Mode)" : "No (High Score Mode)"}`);
console.log(`- Has Attempt Limit: ${hasAttemptLimit ? "Yes" : "No"}`);
console.log(`- Initial Attempts: ${initialAttempts}`);
console.log(`- Reverse Sort: ${isReverseSort ? "Yes (Lower scores are better)" : "No (Higher scores are better)"}`);
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;
}
async function queryLeaderboards(factory: LeaderboardFactory, slug: string) {
const leaderboardsWithProps = await factory.getLeaderboardsWithProps(slug);
if (leaderboardsWithProps.length > 0) {
console.log(`\nAll leaderboards for "${slug}":`);
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");
});
}
const slugCount = await factory.getLeaderboardCountBySlug(slug);
console.log(`\nTotal number of leaderboards for "${slug}":`, slugCount);
}
async function main() {
try {
const factory = initializeFactory();
if (!factory) return;
const startTime = Math.floor(Date.now() / 1000);
const endTime = startTime + 24 * 3600;
const leaderboardAddress = await createNewLeaderboard(
factory,
"game-tournament-1",
startTime,
endTime,
"Game Tournament #1",
false,
false,
true,
3,
false
);
await queryLeaderboards(factory, "game-tournament-1");
} catch (error) {
console.error("Error:", error);
}
}
2. Interacting with Individual Leaderboards
Here's an example of interacting with a specific leaderboard:
import * as dotenv from "dotenv";
import type { Address } from "viem";
import { Leaderboard } from "@b3dotfun/leaderboards";
import { b3Sepolia } from "viem/chains";
dotenv.config();
const CHAIN_ID = b3Sepolia.id;
const LEADERBOARD_ADDRESS = "0x5BA4634aBB1D42897E2ba65f0cf9036B091Ea3B7";
const USER_ADDRESS = "0x1234567890123456789012345678901234567890";
function initializeLeaderboard(): Leaderboard | null {
const leaderboard = new Leaderboard(
CHAIN_ID,
process.env.LEADERBOARD_ADMIN_PRIVATE_KEY,
LEADERBOARD_ADDRESS
);
const isConnected = leaderboard.connect();
if (!isConnected) {
console.error("Failed to connect to the Leaderboard contract.");
return null;
}
console.log("Connected to the Leaderboard contract");
return leaderboard;
}
async function manageUserScores(leaderboard: Leaderboard, user: Address) {
const timestamp = Math.floor(Date.now() / 1000);
const isPrivate = await leaderboard.isPrivate();
if (isPrivate) {
const isEligible = await leaderboard.isUserEligible(user);
if (!isEligible) {
console.log(`User ${user} is not eligible for this private leaderboard`);
return;
}
}
const isBlacklisted = await leaderboard.isUserBlacklisted(user);
if (isBlacklisted) {
console.log(`User ${user} is blacklisted`);
return;
}
const props = await leaderboard.getLeaderboardProperties();
if (props?.hasAttemptLimit) {
const attempts = await leaderboard.getUserAttempts(user);
if (attempts <= 0) {
console.log(`User ${user} has no attempts remaining`);
return;
}
}
await leaderboard.updateScores([{ user, score: 1000, timestamp }]);
await leaderboard.incrementScores([{ user, score: 500, timestamp }]);
const userScore = await leaderboard.getUserScore(user);
if (userScore) {
console.log(`Current score for ${user}: ${userScore.score}`);
console.log(`Last updated at: ${new Date(userScore.lastUpdated * 1000).toISOString()}`);
} else {
console.log(`No score found for ${user}`);
}
}
async function manageEligibleUsers(leaderboard: Leaderboard, users: Address[]) {
const isPrivate = await leaderboard.isPrivate();
if (!isPrivate) {
console.log("This is not a private leaderboard");
return;
}
await leaderboard.addEligibleUsers(users);
const eligibleUsers = await leaderboard.getEligibleUsers();
console.log("Current eligible users:", eligibleUsers);
}
async function displayLeaderboardInfo(leaderboard: Leaderboard) {
const props = await leaderboard.getLeaderboardProperties();
const isActive = await leaderboard.isLeaderboardActive();
const userCount = await leaderboard.getLeaderboardUserCount();
if (props) {
console.log("\nLeaderboard Information:");
console.log("- Title:", props.title);
console.log("- Slug:", props.slug);
console.log("- Start time:", new Date(props.startTime * 1000).toISOString());
console.log("- End time:", new Date(props.endTime * 1000).toISOString());
console.log("- Status:", isActive ? "Active" : "Inactive");
console.log("- Concluded:", props.hasConcluded ? "Yes" : "No");
console.log("- Private:", props.isPrivate ? "Yes" : "No");
console.log("- Use Latest Score:", props.useLatestScore ? "Yes (Latest Score Mode)" : "No (High Score Mode)");
console.log("- Total Users:", userCount);
}
}
async function main() {
try {
const leaderboard = initializeLeaderboard();
if (!leaderboard) return;
await displayLeaderboardInfo(leaderboard);
await manageUserScores(leaderboard, USER_ADDRESS);
await manageEligibleUsers(leaderboard, [USER_ADDRESS]);
} catch (error) {
console.error("Error:", error);
}
}
Environment Setup
Create a .env
file in your project root with the following variables:
LEADERBOARD_ADMIN_PRIVATE_KEY="your_private_key_here"
# The CHAIN_ID is now passed directly to the constructor, not set via ENV
Important Notes
- Always ensure your private keys are kept secure and never committed to version control
- Use appropriate
chainId
values for the desired network (e.g., b3.id
for B3 Mainnet, b3Sepolia.id
for B3 Testnet, base.id
for Base Mainnet).
- For private leaderboards:
- Users must be added to the eligible users list before they can participate
- Only admins can manage user eligibility
- Blacklisted users cannot participate in any leaderboard activities
- When using attempt limits:
- Users cannot submit scores when they have no attempts remaining
- Admins can increase or decrease user attempts
- The examples above demonstrate basic usage. For production deployments, implement proper error handling and security measures
Features in Detail
Private Leaderboards
- Create private leaderboards with restricted access
- Manage eligible users through
addEligibleUsers
and removeEligibleUsers
- Check user eligibility with
isUserEligible
User Management
- Blacklist users with
blacklistUsers
- Unblacklist users with
unblacklistUser
- Add/remove admins with
addAdmin
and removeAdmin
- Manage user attempts with
getUserAttempts
, increaseUserAttempts
, and decreaseUserAttempts
Score Management
- Update scores with
updateScores
- Increment scores with
incrementScores
- Decrement scores with
decrementScores
- Get user scores and rankings
- View top scores and full leaderboard
Score Update Modes
- Configure leaderboards with different score update behaviors using the
useLatestScore
flag
- Latest Score Mode (
useLatestScore = true
): Always shows the most recent score
- Direct updates always use the latest value
- Increments and decrements are always applied
- Ideal for leaderboards tracking current performance
- High Score Mode (
useLatestScore = false
): Only keeps the highest score achieved
- Direct updates only occur if the new score is higher
- Increments are applied, but decrements are ignored
- Ideal for traditional high score leaderboards
Attempt Limit Management
- Enable attempt limits with
hasAttemptLimit
- Set initial attempts with
initialAttempts
- Check user attempts with
getUserAttempts
- Modify attempts with
increaseUserAttempts
and decreaseUserAttempts
Reverse Sorting
- Enable reverse sorting with
isReverseSort
- When enabled, lower scores are ranked higher
- Ideal for leaderboards where lower scores are better (e.g., time trials, golf scores)
Leaderboard Lifecycle
- Set start and end times
- Check leaderboard status (active/ended)
- Conclude leaderboards
- Update leaderboard parameters
Leaderboard Queries
- Get leaderboards by slug with
getLeaderboardsWithProps(slug)
- Get latest leaderboard for a slug with
getLatestLeaderboardWithProps(slug)
- Get all leaderboards across all slugs with
getAllLeaderboardsWithProps()
- Get leaderboard counts with
getLeaderboardCountBySlug(slug)
and getTotalLeaderboardCount()
For more detailed information about the smart contract functionality, please refer to our smart contract documentation.
Using JSON ABIs Directly
The SDK also exports the raw JSON ABIs for direct use with libraries like viem, ethers.js, or web3.js. This is useful if you want to interact with the contracts directly without using the SDK wrapper classes.
import { LeaderboardFactoryJson, LeaderboardJson } from '@b3dotfun/leaderboards';
import { createPublicClient, http } from 'viem';
import { mainnet } from 'viem/chains';
import { B3LeaderboardFactoryContractAddress, B3MainnetRpcUrl } from '@b3dotfun/leaderboards';
const client = createPublicClient({
chain: mainnet,
transport: http(B3MainnetRpcUrl),
});
const totalLeaderboards = await client.readContract({
address: B3LeaderboardFactoryContractAddress,
abi: LeaderboardFactoryJson,
functionName: 'getTotalLeaderboardCount',
});
console.log(`Total leaderboards: ${totalLeaderboards}`);
const leaderboards = await client.readContract({
address: B3LeaderboardFactoryContractAddress,
abi: LeaderboardFactoryJson,
functionName: 'getAllLeaderboards',
});
if (Array.isArray(leaderboards) && leaderboards.length > 0) {
const firstLeaderboardAddress = leaderboards[0];
const leaderboardProps = await client.readContract({
address: firstLeaderboardAddress,
abi: LeaderboardJson,
functionName: 'getLeaderboardProps',
});
console.log('Leaderboard properties:', leaderboardProps);
}
This approach gives you more flexibility when integrating with existing applications or when you need more control over the contract interactions.