From 46683db3d3969bcbc1946d4528b72bae720e3a53 Mon Sep 17 00:00:00 2001 From: wfnuser Date: Tue, 12 Nov 2024 22:31:10 +0700 Subject: [PATCH] feat: support config chain by env file --- .env | 5 ++++ .env.example | 5 ++++ src/constants/data.ts | 61 ++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/.env b/.env index 870631f..4156252 100644 --- a/.env +++ b/.env @@ -16,3 +16,8 @@ VITE_BASE_URL=/api VITE_API_TARGET= # namespace VITE_API_NAMESPACE= + +# current chain +VITE_CURRENT_CHAIN= +# payment chain +VITE_PAYMENT_CHAIN= diff --git a/.env.example b/.env.example index bc25f9c..e60c0e2 100644 --- a/.env.example +++ b/.env.example @@ -16,3 +16,8 @@ VITE_API_NAMESPACE= # brand VITE_BRAND_NAME= VITE_COMPANY_NAME= + +# current chain +VITE_CURRENT_CHAIN= +# payment chain +VITE_PAYMENT_CHAIN= diff --git a/src/constants/data.ts b/src/constants/data.ts index 0a3a7cf..4f7fe50 100644 --- a/src/constants/data.ts +++ b/src/constants/data.ts @@ -1,7 +1,7 @@ import { getAppearances } from '@/lib/appearances' import { NavItem, UserPermission } from '@/types' import { SdkCtorOptions, SDK } from 'youbet-sdk' -import { polygon } from 'viem/chains' +import { polygon, moonbaseAlpha, Chain } from 'viem/chains' export const getNavItems = (userPermission?: UserPermission): NavItem[] => { const appearances = getAppearances() @@ -121,6 +121,15 @@ export const getNavItems = (userPermission?: UserPermission): NavItem[] => { export const DEFAULT_PAGINATION_LIMIT = 4 // TODO: unify the options with viem chains +const moonbaseAlphaOptions: SdkCtorOptions = { + networkOptions: { + rpcUrl: moonbaseAlpha.rpcUrls.default.http[0], + chainId: moonbaseAlpha.id, + contractAddress: '0x977BCA065Cb342c568A33EA0A12e9cB27645BD1d', + }, + chainName: 'Moonbase Alpha', +} + const openCampusTestOptions: SdkCtorOptions = { networkOptions: { rpcUrl: 'https://open-campus-codex-sepolia.drpc.org', @@ -146,10 +155,54 @@ const eduChain = { }, } +// define supported chains +const SUPPORTED_CHAINS: Record = { + educhain: eduChain, + moonbase: moonbaseAlpha, + polygon: polygon, +} + +// TODO: move this chain options to a separated chain config file +// define chain options +const CHAIN_OPTIONS: Record = { + educhain: openCampusTestOptions, + moonbase: moonbaseAlphaOptions, +} + +const getCurrentChain = (): Chain => { + const chainName = import.meta.env.VITE_CURRENT_CHAIN || 'educhain' + const chain = SUPPORTED_CHAINS[chainName] + if (!chain) { + console.warn(`Chain ${chainName} not found, falling back to eduChain`) + return eduChain + } + return chain +} + +const getPaymentChain = (): Chain => { + const chainName = import.meta.env.VITE_PAYMENT_CHAIN || 'polygon' + const chain = SUPPORTED_CHAINS[chainName] + if (!chain) { + console.warn(`Payment chain ${chainName} not found, falling back to polygon`) + return polygon + } + return chain +} + +const getChainOptions = (): SdkCtorOptions => { + const chainName = import.meta.env.VITE_CURRENT_CHAIN || 'educhain' + const options = CHAIN_OPTIONS[chainName] + if (!options) { + console.warn(`Chain options for ${chainName} not found, falling back to openCampusTestOptions`) + return openCampusTestOptions + } + return options +} + // TODO: should support multiple chains and configured by the user -export const currentChain = eduChain +export const currentChain = getCurrentChain() // TODO: for openbuild payment - currently only polygon is supported -export const paymentChain = polygon -export const currentChainOptions = openCampusTestOptions +export const paymentChain = getPaymentChain() +export const currentChainOptions = getChainOptions() // TODO: move to other file export const sdk = new SDK(currentChainOptions)