Skip to content

Commit

Permalink
feat: support config chain by env file
Browse files Browse the repository at this point in the history
  • Loading branch information
wfnuser committed Nov 12, 2024
1 parent 90215ce commit 46683db
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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=
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ VITE_API_NAMESPACE=
# brand
VITE_BRAND_NAME=
VITE_COMPANY_NAME=

# current chain
VITE_CURRENT_CHAIN=
# payment chain
VITE_PAYMENT_CHAIN=
61 changes: 57 additions & 4 deletions src/constants/data.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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',
Expand All @@ -146,10 +155,54 @@ const eduChain = {
},
}

// define supported chains
const SUPPORTED_CHAINS: Record<string, Chain> = {
educhain: eduChain,
moonbase: moonbaseAlpha,
polygon: polygon,
}

// TODO: move this chain options to a separated chain config file
// define chain options
const CHAIN_OPTIONS: Record<string, SdkCtorOptions> = {
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)

0 comments on commit 46683db

Please sign in to comment.