From c547918e29da80b091aa91a785042cf80fda34b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=B8=98=E0=B8=93=E0=B8=A7=E0=B8=B1=E0=B8=92=E0=B8=99?= =?UTF-8?q?=E0=B9=8C=20=E0=B8=9B=E0=B8=A3=E0=B8=B0=E0=B8=A7=E0=B8=B1?= =?UTF-8?q?=E0=B8=99=E0=B8=95=E0=B8=B2?= Date: Mon, 4 May 2026 23:14:36 +0700 Subject: [PATCH 1/2] refactor(api): centralize shared network config for API handlers --- functions/api/_shared.mjs | 24 ++++++++++++++++++++++++ functions/api/health.js | 13 ++++++++----- functions/api/network.js | 13 +++++-------- functions/api/web3/status.js | 12 +++++------- 4 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 functions/api/_shared.mjs diff --git a/functions/api/_shared.mjs b/functions/api/_shared.mjs new file mode 100644 index 0000000..2905c13 --- /dev/null +++ b/functions/api/_shared.mjs @@ -0,0 +1,24 @@ +export const DEFAULT_CHAIN_ID = 13390; +export const DEFAULT_RPC_URL = 'https://rpc.meechain.live'; + +export const DEFAULT_CONTRACTS = { + token: '0x5FbDB2315678afecb367f032d93F642f64180aa3', + nft: '0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512', + portal: '0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0', +}; + +export function resolveChainId(env) { + return Number.parseInt(env.CHAIN_ID || String(DEFAULT_CHAIN_ID), 10); +} + +export function resolveRpcUrl(env) { + return env.DRPC_RPC_URL || env.VITE_RPC_URL || DEFAULT_RPC_URL; +} + +export function resolveContracts(env) { + return { + token: env.VITE_TOKEN_CONTRACT_ADDRESS || DEFAULT_CONTRACTS.token, + nft: env.VITE_NFT_CONTRACT_ADDRESS || DEFAULT_CONTRACTS.nft, + portal: env.VITE_STAKING_CONTRACT_ADDRESS || DEFAULT_CONTRACTS.portal, + }; +} diff --git a/functions/api/health.js b/functions/api/health.js index 6bafc87..0a173ce 100644 --- a/functions/api/health.js +++ b/functions/api/health.js @@ -2,20 +2,23 @@ // ║ Cloudflare Pages Function: /api/health ║ // ╚══════════════════════════════════════════════════════╝ +import { resolveChainId, resolveContracts, resolveRpcUrl } from './_shared.mjs'; + export async function onRequestGet(ctx) { const { env } = ctx; + const contracts = resolveContracts(env); + const data = { status: 'ok', model: 'gpt-5-mini', bot: 'MeeBot AI', web3: false, - chainId: 13390, - rpc: env.DRPC_RPC_URL || 'https://rpc.meechain.live', + chainId: resolveChainId(env), + rpc: resolveRpcUrl(env), contracts: { - token: env.VITE_TOKEN_CONTRACT_ADDRESS || '0x5FbDB2315678afecb367f032d93F642f64180aa3', - nft: env.VITE_NFT_CONTRACT_ADDRESS || '0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512', - staking: env.VITE_STAKING_CONTRACT_ADDRESS || '0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0', + ...contracts, + staking: contracts.portal, }, domain: 'meebot.io', version: '2.0.0', diff --git a/functions/api/network.js b/functions/api/network.js index 8c328b4..ef81333 100644 --- a/functions/api/network.js +++ b/functions/api/network.js @@ -2,24 +2,21 @@ // ║ Cloudflare Pages Function: /api/network ║ // ╚══════════════════════════════════════════════════════╝ +import { resolveChainId, resolveContracts, resolveRpcUrl } from './_shared.mjs'; + export async function onRequestGet(ctx) { const { env } = ctx; - const chainId = parseInt(env.CHAIN_ID || '13390', 10); + const chainId = resolveChainId(env); const data = { chainId: `0x${chainId.toString(16)}`, // 0x344e chainName: 'MeeChain Ritual Chain', rpcUrls: [ - env.DRPC_RPC_URL || 'https://rpc.meechain.live', - env.VITE_RPC_URL || 'https://rpc.meechain.live', + resolveRpcUrl(env), ], nativeCurrency: { name: 'MEE Token', symbol: 'MEE', decimals: 18 }, blockExplorerUrls:['http://explorer.meechain.run.place'], - contracts: { - token: env.VITE_TOKEN_CONTRACT_ADDRESS || '0x5FbDB2315678afecb367f032d93F642f64180aa3', - nft: env.VITE_NFT_CONTRACT_ADDRESS || '0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512', - portal: env.VITE_STAKING_CONTRACT_ADDRESS || '0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0', - }, + contracts: resolveContracts(env), }; return new Response(JSON.stringify(data), { diff --git a/functions/api/web3/status.js b/functions/api/web3/status.js index 2603001..7eecc9f 100644 --- a/functions/api/web3/status.js +++ b/functions/api/web3/status.js @@ -2,16 +2,14 @@ // ║ Cloudflare Pages Function: /api/web3/status ║ // ╚══════════════════════════════════════════════════════╝ +import { resolveChainId, resolveContracts, resolveRpcUrl } from '../_shared.mjs'; + export async function onRequestGet(ctx) { const { env } = ctx; - const rpcUrl = env.DRPC_RPC_URL || 'https://rpc.meechain.live'; - const chainId = parseInt(env.CHAIN_ID || '13390', 10); - const contracts = { - token: env.VITE_TOKEN_CONTRACT_ADDRESS || '0x5FbDB2315678afecb367f032d93F642f64180aa3', - nft: env.VITE_NFT_CONTRACT_ADDRESS || '0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512', - portal: env.VITE_STAKING_CONTRACT_ADDRESS || '0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0', - }; + const rpcUrl = resolveRpcUrl(env); + const chainId = resolveChainId(env); + const contracts = resolveContracts(env); // Try to get block number via eth_blockNumber JSON-RPC let connected = false; From 170ae1e98812eea7f58f52f6e9110f9a107a61d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=B8=98=E0=B8=93=E0=B8=A7=E0=B8=B1=E0=B8=92=E0=B8=99?= =?UTF-8?q?=E0=B9=8C=20=E0=B8=9B=E0=B8=A3=E0=B8=B0=E0=B8=A7=E0=B8=B1?= =?UTF-8?q?=E0=B8=99=E0=B8=95=E0=B8=B2?= Date: Thu, 18 Jun 2026 19:46:36 +0700 Subject: [PATCH 2/2] Update functions/api/health.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com> Signed-off-by: ธณวัฒน์ ประวันตา --- functions/api/health.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/functions/api/health.js b/functions/api/health.js index 916ce95..9c9a108 100644 --- a/functions/api/health.js +++ b/functions/api/health.js @@ -19,7 +19,8 @@ export async function onRequestGet(ctx) { chainId: resolveChainId(env), rpc: resolveRpcUrl(env), contracts: { - ...contracts, + token: contracts.token, + nft: contracts.nft, staking: contracts.portal, chainId: config.chainId, rpc: config.rpcPrimary,