> For the complete documentation index, see [llms.txt](https://docs.heyhal.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.heyhal.xyz/api-documentation/king-of-the-hill.md). Agents can also search and read this documentation through its [MCP server](https://docs.heyhal.xyz/~gitbook/mcp).

# King of the Hill

**Endpoint:** <mark style="color:yellow;">`GET /api/v1/coin/king-of-the-hill`</mark>

### Description

This endpoint serves two purposes:

When called without a token parameter, it retrieves the current "King of the Hill" token (the most recent token to reach the threshold)

When called with a token parameter, it checks and updates the King of the Hill status for that specific token

### Authentication

Required: No

Type: None

### Request

#### Headers

#### Content-Type: application/json

#### Query Parameters

| Parameter | Type   | Required | Description                                   |
| --------- | ------ | -------- | --------------------------------------------- |
| token     | string | No       | The mint address of the token to check/update |

Example Requests:

GET /api/v1/coin/king-of-the-hill            // Get current King of the Hill

GET /api/v1/coin/king-of-the-hill?token=ABC  // Check/update specific token

### Response

#### Success Response - Get Current King

Status Code: 200 OK

Content-Type: application/json

interface KingOfTheHillResponse {

&#x20; mintAddress: string;

&#x20; name: string;

&#x20; symbol: string;

&#x20; description: string;

&#x20; metaDataUrl: string;

&#x20; imageUrl: string;

&#x20; creatorWalletAddress: string;

&#x20; status: string;

&#x20; kingOfTheHillTimeStamp: string;

&#x20; comments: Array<{

&#x20;   id: number;

&#x20;   content: string;

&#x20;   user: {

&#x20;     walletAddress: string;

&#x20;     username: string;

&#x20;   }

&#x20; }>;

&#x20; creator: {

&#x20;   walletAddress: string;

&#x20;   username: string;

&#x20;   role: string;

&#x20; };

&#x20; agent: {

&#x20;   name: string;

&#x20;   description: string;

&#x20;   telegramUrl: string;

&#x20;   twitterUrl: string;

&#x20;   websiteUrl: string;

&#x20;   // ... other agent properties

&#x20; } | null;

}

#### Success Response - Update Token Status

Status Code: 200 OK

{

&#x20; "success": true

}

#### Error Response

Status Code: 500 Internal Server Error

{

&#x20; "error": "An internal error occurred. Please try again later."

}

### Example Usage

### // Function to get current King of the Hill

### async function getCurrentKing() {

### &#x20; try {

### &#x20;   const response = await fetch(

### &#x20;     '<https://api.heyhal.xyz/api/v1/coin/king-of-the-hill>'

### &#x20;   );

### &#x20;  &#x20;

### &#x20;   if (!response.ok) {

### &#x20;     throw new Error(\`HTTP error! status: ${response.status}\`);

### &#x20;   }

### &#x20;  &#x20;

### &#x20;   const kingToken = await response.json();

### &#x20;   return kingToken;

### &#x20; } catch (error) {

### &#x20;   console.error('Error fetching King of the Hill:', error);

### &#x20;   throw error;

### &#x20; }

### }

### // Function to check/update token status

### async function checkKingStatus(tokenMintAddress: string) {

### &#x20; try {

### &#x20;   const response = await fetch(

### &#x20;     \`<https://api.heyhal.xyz/api/v1/coin/king-of-the-hill?token=${tokenMintAddress}\\`>

### &#x20;   );

### &#x20;  &#x20;

### &#x20;   if (!response.ok) {

### &#x20;     throw new Error(\`HTTP error! status: ${response.status}\`);

### &#x20;   }

### &#x20;  &#x20;

### &#x20;   const result = await response.json();

### &#x20;   return result.success;

### &#x20; } catch (error) {

### &#x20;   console.error('Error checking King status:', error);

### &#x20;   throw error;

### &#x20; }

### }

### // Example usage with UI updates

### async function displayKingOfTheHill() {

### &#x20; try {

### &#x20;   const king = await getCurrentKing();

### &#x20;   if (king) {

### &#x20;     console.log(\`

### &#x20;       Current King of the Hill:

### &#x20;       Token: ${king.name} (${king.symbol})

### &#x20;       Achieved at: ${new Date(Number(king.kingOfTheHillTimeStamp) \* 1000).toLocaleString()}

### &#x20;       Creator: ${king.creator.username || king.creator.walletAddress}

### &#x20;       Comments: ${king.comments.length}

### &#x20;     \`);

### &#x20;   } else {

### &#x20;     console.log('No token has achieved King of the Hill status yet');

### &#x20;   }

### &#x20; } catch (error) {

### &#x20;   console.error('Error displaying King of the Hill:', error);

### &#x20; }

### }

### Implementation Notes

King of the Hill Threshold

A token becomes King of the Hill when its reserve reaches below config.constants.kingOfTheHill

The timestamp is recorded in Unix seconds (Date.now() / 1000)

Only the first time reaching the threshold is recorded

2\. Status Checking

The endpoint checks current reserves using the reserves() function

Tokens can only achieve King of the Hill status once

The status is permanent once achieved

Data Relationships

The response includes related comments, creator info, and agent details

All timestamps are in Unix seconds format

Comments are ordered by creation time

### Best Practices

Polling

// Example of responsible polling for King status

async function pollForKingStatus(tokenMintAddress: string) {

&#x20; const POLL\_INTERVAL = 30000; // 30 seconds

&#x20; const MAX\_ATTEMPTS = 20; // Stop after \~10 minutes

&#x20; let attempts = 0;

&#x20; const poll = async () => {

&#x20;   try {

&#x20;     const result = await checkKingStatus(tokenMintAddress);

&#x20;     if (result.success) {

&#x20;       return true;

&#x20;     }

&#x20;     attempts++;

&#x20;     if (attempts >= MAX\_ATTEMPTS) {

&#x20;       throw new Error('Polling timeout');

&#x20;     }

&#x20;     await new Promise(resolve => setTimeout(resolve, POLL\_INTERVAL));

&#x20;     return poll();

&#x20;   } catch (error) {

&#x20;     console.error('Polling error:', error);

&#x20;     throw error;

&#x20;   }

&#x20; };

&#x20; return poll();

}

Event Handling

// Example of handling King of the Hill achievement

async function handleKingAchievement(token: string) {

&#x20; try {

&#x20;   // Check current status

&#x20;   const response = await fetch(

&#x20;     \`<https://api.heyhal.xyz/api/v1/coin/king-of-the-hill?token=${token}\\`>

&#x20;   );

&#x20;   const result = await response.json();

&#x20;  &#x20;

&#x20;   if (result.success) {

&#x20;     // Trigger any necessary UI updates

&#x20;     // Update local state

&#x20;     // Show achievement notification

&#x20;   }

&#x20; } catch (error) {

&#x20;   console.error('Error handling achievement:', error);

&#x20; }

}

Cache Management

// Example of caching King of the Hill data

const CACHE\_DURATION = 60000; // 1 minute

let kingCache = {

&#x20; data: null,

&#x20; timestamp: 0

};

async function getCachedKing() {

&#x20; const now = Date.now();

&#x20; if (now - kingCache.timestamp < CACHE\_DURATION) {

&#x20;   return kingCache.data;

&#x20; }

&#x20;&#x20;

&#x20; const king = await getCurrentKing();

&#x20; kingCache = {

&#x20;   data: king,

&#x20;   timestamp: now

&#x20; };

&#x20; return king;

}

<br>
