Skip to content

Commit

Permalink
Stellar network support
Browse files Browse the repository at this point in the history
  • Loading branch information
mliu committed Jan 9, 2025
1 parent 7e621ae commit bc38084
Showing 1 changed file with 53 additions and 3 deletions.
56 changes: 53 additions & 3 deletions packages/huma-web-shared/src/hooks/useChainInfo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { Web3Provider } from '@ethersproject/providers'
import { CHAIN_TYPE, SolanaChainEnum } from '@huma-finance/shared'
import {
CHAIN_TYPE,
SolanaChainEnum,
StellarChainEnum,
} from '@huma-finance/shared'
import { useWallet } from '@solana/wallet-adapter-react'
import { useWeb3React } from '@web3-react/core'
import { useEffect, useState } from 'react'
import { useContext, useEffect, useState } from 'react'
import { StellarConnectionContext } from '../stellar'

export const useChainInfo = (
isDev: boolean,
Expand All @@ -18,6 +23,7 @@ export const useChainInfo = (
provider: evmProvider,
} = useWeb3React()
const { publicKey: solanaPublicKey } = useWallet()
const { address: stellarAccount } = useContext(StellarConnectionContext)

useEffect(() => {
if (chainType === CHAIN_TYPE.EVM) {
Expand All @@ -33,7 +39,24 @@ export const useChainInfo = (
isDev ? SolanaChainEnum.SolanaDevnet : SolanaChainEnum.SolanaMainnet,
)
}
}, [chainType, evmAccount, evmChainId, evmProvider, isDev, solanaPublicKey])

if (chainType === CHAIN_TYPE.STELLAR) {
setAccount(stellarAccount?.toString())
setChainId(
isDev
? StellarChainEnum.StellarTestnet
: StellarChainEnum.StellarMainnet,
)
}
}, [
chainType,
evmAccount,
evmChainId,
evmProvider,
isDev,
solanaPublicKey,
stellarAccount,
])

return {
account,
Expand All @@ -51,12 +74,18 @@ export const useChainsInfo = (isDev: boolean) => {
isDev,
CHAIN_TYPE.SOLANA,
)
const { account: stellarAccount, chainId: stellarChainId } = useChainInfo(
isDev,
CHAIN_TYPE.STELLAR,
)

return {
evmAccount,
evmChainId,
solanaAccount,
solanaChainId,
stellarAccount,
stellarChainId,
}
}

Expand All @@ -66,13 +95,34 @@ export const useActiveChainInfo = (
) => {
const evmChainInfo = useChainInfo(isDev, CHAIN_TYPE.EVM)
const solanaChainInfo = useChainInfo(isDev, CHAIN_TYPE.SOLANA)
const stellarChainInfo = useChainInfo(isDev, CHAIN_TYPE.STELLAR)

switch (activeNetwork) {
case CHAIN_TYPE.EVM:
return evmChainInfo
case CHAIN_TYPE.SOLANA:
return solanaChainInfo
case CHAIN_TYPE.STELLAR:
return stellarChainInfo
default:
return null
}
}

export const useActiveAccountChain = (isDev: boolean): CHAIN_TYPE | null => {
const evmChainInfo = useChainInfo(isDev, CHAIN_TYPE.EVM)
const solanaChainInfo = useChainInfo(isDev, CHAIN_TYPE.SOLANA)
const stellarChainInfo = useChainInfo(isDev, CHAIN_TYPE.STELLAR)

if (evmChainInfo.account) {
return CHAIN_TYPE.EVM
}
if (solanaChainInfo.account) {
return CHAIN_TYPE.SOLANA
}
if (stellarChainInfo.account) {
return CHAIN_TYPE.STELLAR
}

return null
}

0 comments on commit bc38084

Please sign in to comment.