Skip to content

Commit

Permalink
add memechan live coins list into settings
Browse files Browse the repository at this point in the history
  • Loading branch information
avernikoz committed May 11, 2024
1 parent 34abf6b commit 1dcb3a4
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/chains/memecoin-list/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const MEMECHAN_LIVE_COIN_CALLBACK_QUERY_DATA_PREFIX = 'memechan-live-coins-';
26 changes: 26 additions & 0 deletions src/chains/memecoin-list/showMemechanList.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import goHome from '../../inline-keyboards/goHome';
import { BotContext } from '../../types';
import { getMemechanLiveCoinsList, getSuiScanCoinLink } from '../utils';

export async function showMemechanLiveCoinsList(ctx: BotContext) {
const coinWhitelist = await getMemechanLiveCoinsList();

if (coinWhitelist === null) {
await ctx.reply('Failed to fetch memechan live coins list. Please, try again later or contact support.', {
reply_markup: goHome,
});

return;
}

let whitelistString = '🪙 <b>Memechan Live Coins</b> 🪙\n\n';
coinWhitelist.forEach(
(coinData) => (whitelistString += `<a href="${getSuiScanCoinLink(coinData.type)}">${coinData.symbol}</a>\n`),
);

await ctx.reply(whitelistString, {
reply_markup: goHome,
parse_mode: 'HTML',
link_preview_options: { is_disabled: true },
});
}
11 changes: 11 additions & 0 deletions src/chains/memecoin-list/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { MEMECHAN_LIVE_COIN_CALLBACK_QUERY_DATA_PREFIX } from './config';

export function getMemechanCoinCallbackQueryData(coinIndex: number): string {
return `${MEMECHAN_LIVE_COIN_CALLBACK_QUERY_DATA_PREFIX}${coinIndex}`;
}

export function getMemechanCoinIndexFromCallbackQueryData(callbackQueryData: string): number {
const prefixLength = MEMECHAN_LIVE_COIN_CALLBACK_QUERY_DATA_PREFIX.length;

return +callbackQueryData.substring(prefixLength);
}
19 changes: 19 additions & 0 deletions src/chains/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getPriceApi } from './priceapi.utils';
import { COIN_WHITELIST_URL, SELL_DELAY_AFTER_BUY_FOR_CLAIMERS_IN_MS } from './sui.config';
import { CoinForPool, CoinWhitelistItem } from './types';
import closeConversation from '../inline-keyboards/closeConversation';
import memechanlivecoinslist from '../storage/memechangg-live-coint';

/**
* Checks if the given string is a valid suiscan link.
Expand Down Expand Up @@ -292,6 +293,24 @@ export async function getCoinWhitelist(): Promise<CoinWhitelistItem[] | null> {
}
}

export async function getMemechanLiveCoinsList(): Promise<CoinWhitelistItem[] | null> {
try {
const whitelistJson = memechanlivecoinslist;

if (!isCoinWhitelistItemArray(whitelistJson)) {
console.warn('[getMemechanLiveCoinsList] Fetched coins list is not valid. Parsed JSON:', whitelistJson);

return null;
}

return whitelistJson;
} catch (error) {
console.error('[getCoinWhitelist] Error occured:', error);

return null;
}
}

export function isCoinWhitelistItemArray(data: unknown): data is CoinWhitelistItem[] {
return (
Array.isArray(data) &&
Expand Down
5 changes: 5 additions & 0 deletions src/menu/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import goHome from '../inline-keyboards/goHome';
import { BotContext } from '../types';
// eslint-disable-next-line max-len
import { showPriceDifferenceThresholdPage } from '../chains/settings/price-difference-threshold/show-price-difference-page';
import { showMemechanLiveCoinsList } from '../chains/memecoin-list/showMemechanList';

const settingsMenu = new Menu<BotContext>('settings')
.text('Slippage', async (ctx) => {
Expand All @@ -32,6 +33,10 @@ const settingsMenu = new Menu<BotContext>('settings')
});
}
})
.row()
.text('Memechan Live Coins', async (ctx) => {
await showMemechanLiveCoinsList(ctx);
})
.text('User Agreement', async (ctx) => {
await ctx.reply(userAgreement, { parse_mode: 'HTML', reply_markup: goHome });
})
Expand Down
9 changes: 9 additions & 0 deletions src/middleware/callbackQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { assets, home } from '../chains/sui.functions';
import { retryAndGoHomeButtonsData } from '../inline-keyboards/retryConversationButtonsFactory';
import { BotContext } from '../types';
import { CallbackQueryData } from '../types/callback-queries-data';
import { showMemechanLiveCoinsList } from '../chains/memecoin-list/showMemechanList';

export function useCallbackQueries(bot: Bot<BotContext>) {
bot.callbackQuery('close-conversation', async (ctx) => {
Expand Down Expand Up @@ -42,6 +43,7 @@ export function useCallbackQueries(bot: Bot<BotContext>) {
useCoinWhitelistCallbackQueries(bot);
useSwapConfirmationCallbackQueries(bot);
usePriceDifferenceThresholdCallbackQueries(bot);
useMemechanLiveCoinsCallbackQueries(bot);

Object.keys(retryAndGoHomeButtonsData).forEach((conversationId) => {
bot.callbackQuery(`retry-${conversationId}`, async (ctx) => {
Expand Down Expand Up @@ -131,6 +133,13 @@ function useCoinWhitelistCallbackQueries(bot: Bot<BotContext>) {
});
}

function useMemechanLiveCoinsCallbackQueries(bot: Bot<BotContext>) {
bot.callbackQuery(CallbackQueryData.MemechanLiveCoinsList, async (ctx) => {
await ctx.answerCallbackQuery();
await showMemechanLiveCoinsList(ctx);
});
}

function useSwapConfirmationCallbackQueries(bot: Bot<BotContext>) {
bot.callbackQuery(CallbackQueryData.EnableSwapConfirmation, async (ctx) => {
ctx.session.settings.swapWithConfirmation = true;
Expand Down
8 changes: 8 additions & 0 deletions src/storage/memechangg-live-coint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// eslint-disable-next-line max-len
// Source: https://7mgmqkuj18.execute-api.us-east-1.amazonaws.com/prod/live/coins?sortBy=lastReply&direction=desc&status=LIVE
export default [
{
symbol: 'RINWIF',
type: '0x4c023b94ba2e42e5ce1400191d0228216359f4de894150b813b1f514d2668426::rinwif::RINWIF',
},
];
1 change: 1 addition & 0 deletions src/types/callback-queries-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export enum CallbackQueryData {
Home = 'go-home',
DisableSwapConfirmation = 'disable-swap-confirmation',
EnableSwapConfirmation = 'enable-swap-confirmation',
MemechanLiveCoinsList = 'memechan-live-coins-list',
}

0 comments on commit 1dcb3a4

Please sign in to comment.