Skip to content

Commit

Permalink
remove chainbridge
Browse files Browse the repository at this point in the history
  • Loading branch information
kingsleydon committed Feb 23, 2024
1 parent cfb0d81 commit adc29f6
Show file tree
Hide file tree
Showing 24 changed files with 55 additions and 266 deletions.
10 changes: 3 additions & 7 deletions apps/app/components/BasePool/DetailPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ import usePolkadotApi from '@/hooks/usePolkadotApi'
import useSelectedVaultState from '@/hooks/useSelectedVaultState'
import useSignAndSend from '@/hooks/useSignAndSend'
import {aprToApy} from '@/lib/apr'

import {
type BasePoolCommonFragment,
useDelegationByIdQuery,
} from '@/lib/subsquidQuery'
import {colors} from '@/lib/theme'
import {subsquidClientAtom} from '@/store/common'
import Settings from '@mui/icons-material/Settings'
import {TabContext, TabList, TabPanel} from '@mui/lab'

import {subsquidClientAtom} from '@/store/common'
import {
Box,
Button,
Expand All @@ -35,8 +33,6 @@ import {
} from '@mui/material'
import {toCurrency, toPercentage} from '@phala/lib'
import {polkadotAccountAtom} from '@phala/store'
import {addDays} from 'date-fns'
import Decimal from 'decimal.js'
import {useAtom} from 'jotai'
import {type FC, useCallback, useMemo, useState} from 'react'
import DelegationChart from '../Delegation/Chart'
Expand Down Expand Up @@ -278,15 +274,15 @@ const DetailPage: FC<{basePool: BasePoolCommonFragment}> = ({basePool}) => {
<TabList
indicatorColor={color}
textColor={color}
onChange={(e, newValue) => {
onChange={(_, newValue) => {
setChartTab(newValue as BasePoolChartKind)
}}
>
{charts.map(([label, value]) => (
<Tab label={label} value={value} key={value} />
))}
</TabList>
{charts.map(([label, value]) => (
{charts.map(([, value]) => (
<TabPanel
value={value}
sx={{px: 0, pb: 0, flex: 1}}
Expand Down
6 changes: 3 additions & 3 deletions apps/app/components/DelegationNftCover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const getNftCover = (
const DelegationNftCover: FC<{
variant: CoverVariant
delegation: {basePool: {kind: BasePoolKind}; value: string}
nft: {mintTime?: string | null}
nft?: {mintTime?: string | null} | null
}> = ({variant, delegation, nft}) => {
const [loop, setLoop] = useState(false)
const ref = useRef<HTMLVideoElement>(null)
Expand All @@ -61,9 +61,9 @@ const DelegationNftCover: FC<{
variant,
delegation.basePool.kind,
delegation.value,
nft.mintTime,
nft?.mintTime,
),
[variant, delegation.basePool.kind, delegation.value, nft.mintTime],
[variant, delegation.basePool.kind, delegation.value, nft?.mintTime],
)
return (
<video
Expand Down
7 changes: 1 addition & 6 deletions apps/app/components/WrapDecimal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ const WrapDecimal: FC<{children: ReactNode}> = ({children}) => {
return (
<>
{integer}.
<Box
component="span"
sx={(theme) => ({
filter: 'brightness(0.5)',
})}
>
<Box component="span" sx={{filter: 'brightness(0.5)'}}>
{decimal}
</Box>
</>
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion apps/app/pages/api/circulation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {NextApiRequest, NextApiResponse} from 'next'

const handler = async (
req: NextApiRequest,
_req: NextApiRequest,
res: NextApiResponse<string>,
): Promise<void> => {
const circulation = await fetch(
Expand Down
4 changes: 2 additions & 2 deletions apps/app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
],
"strictNullChecks": true
},
"include": ["next-env.d.ts", ".next/types/**/*.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules", "polkadot-types/**/*.ts"]
}
4 changes: 0 additions & 4 deletions apps/subbridge/assets/chainbridge_abi.json

This file was deleted.

13 changes: 1 addition & 12 deletions apps/subbridge/components/BridgeBody/Action/EvmAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
bridgeInfoAtom,
decimalsAtom,
fromChainAtom,
toChainAtom,
} from '@/store/bridge'
import {evmAccountAtom, isNetworkWrongAtom} from '@/store/ethers'
import {LoadingButton} from '@mui/lab'
Expand All @@ -23,7 +22,6 @@ const EvmAction: FC<{onConfirm: () => void}> = ({onConfirm}) => {
const [approveLoading, setApproveLoading] = useState(false)
const {enqueueSnackbar} = useSnackbar()
const [fromChain] = useAtom(fromChainAtom)
const [toChain] = useAtom(toChainAtom)
const [amount] = useAtom(amountAtom)
const ethersAssetContract = useCurrentEthersAssetContract()
const [evmAccount] = useAtom(evmAccountAtom)
Expand All @@ -32,20 +30,11 @@ const EvmAction: FC<{onConfirm: () => void}> = ({onConfirm}) => {
const {kind: bridgeKind} = useAtomValue(bridgeInfoAtom)
const switchNetwork = useSwitchNetwork()
const decimals = useAtomValue(decimalsAtom)
const needApproval =
bridgeKind === 'evmChainBridge' || bridgeKind === 'evmSygma'
const needApproval = bridgeKind === 'evmSygma'
let spender: string | undefined
if (bridgeKind === 'evmSygma') {
spender = (fromChain as EvmChain).sygmaHandler
} else if (
fromChain.kind === 'evm' &&
fromChain.chainBridgeContract != null
) {
spender =
fromChain.chainBridgeContract.spender[toChain.id] ??
fromChain.chainBridgeContract.spender.default
}

const {data: allowance} = useSWR(
needApproval &&
ethersAssetContract != null &&
Expand Down
1 change: 0 additions & 1 deletion apps/subbridge/config/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export interface Asset {
reservedAddress?: {
[chainId in ChainId]?: string
}
chainBridgeResourceId?: `0x${string}`
destChainTransactionFee: Partial<Record<ChainId, Decimal>>
existentialDeposit: Partial<Record<ChainId, Decimal>>
sygmaResourceId?: string
Expand Down
1 change: 0 additions & 1 deletion apps/subbridge/config/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {type AssetId} from './asset'
import {type ChainId} from './chain'

export type BridgeKind =
| 'evmChainBridge'
| 'evmXTokens'
| 'polkadotXTokens'
| 'phalaXTransfer'
Expand Down
17 changes: 0 additions & 17 deletions apps/subbridge/config/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@ export interface EvmChain extends BaseChain {
evmChainId: number
currencySymbol: string
paraId?: number // for compatible chains
chainBridgeContract?: {
address: `0x${string}`
spender: {
[toChainId in ChainId | 'default']?: `0x${string}`
}
}
xTokensContractAddress?: `0x${string}`
generalIndex?: number
sygmaHandler?: string
Expand Down Expand Up @@ -155,13 +149,6 @@ export const CHAINS: Readonly<
kind: 'evm',
evmChainId: 1,
currencySymbol: 'ETH',
chainBridgeContract: {
address: '0x8F92e7353b180937895E0C5937d616E8ea1A2Bb9',
spender: {
phala: '0xcd38b15a419491c7c1238b0659f65c755792e257',
khala: '0xEEc0fb4913119567cDfC0c5fc2Bf8f9F9B226c2d',
},
},
generalIndex: 1,
explorerURL: 'https://etherscan.io/',
sygmaChainId: 1,
Expand Down Expand Up @@ -215,10 +202,6 @@ export const CHAINS: Readonly<
currencySymbol: 'MOVR',
paraId: 2023,
nativeAsset: 'movr',
chainBridgeContract: {
address: '0xCe6652551A989C13B41f70cac504341A5F711c8d',
spender: {default: '0xf88337a0db6e24Dff0fCD7F92ab0655B97A68d38'},
},
xTokensContractAddress: '0x0000000000000000000000000000000000000804',
generalIndex: 2,
explorerURL: 'https://moonriver.moonscan.io/',
Expand Down
37 changes: 6 additions & 31 deletions apps/subbridge/hooks/useEstimatedGasFee.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
ethersGasPriceFetcher,
evmChainBridgeEstimatedGasFetcher,
evmXTokensEstimatedGasFetcher,
} from '@/lib/ethersFetcher'
import {getEvmSygmaTransfer} from '@/lib/evmSygma'
Expand All @@ -19,54 +18,33 @@ import {evmAccountAtom} from '@/store/ethers'
import Decimal from 'decimal.js'
import {useAtomValue} from 'jotai'
import useSWR from 'swr'
import {
useEthersChainBridgeContract,
useEthersXTokensContract,
} from './useEthersContract'
import {useEthersXTokensContract} from './useEthersContract'
import {useEthersWeb3Provider} from './useEthersProvider'
import {useCurrentPolkadotApi, usePolkadotApi} from './usePolkadotApi'
import {useCurrentPolkadotApi} from './usePolkadotApi'

export const useEstimatedGasFee = (): Decimal | undefined => {
const ethersChainBridgeContract = useEthersChainBridgeContract()
const ethersXTokensContract = useEthersXTokensContract()
const fromChain = useAtomValue(fromChainAtom)
const toChain = useAtomValue(toChainAtom)
const asset = useAtomValue(assetAtom)
const ethersWeb3Provider = useEthersWeb3Provider()
const evmAccount = useAtomValue(evmAccountAtom)
const decimals = useAtomValue(decimalsAtom)
const khalaApi = usePolkadotApi(toChain.id === 'phala' ? 'phala' : 'khala')
const polkadotApi = useCurrentPolkadotApi()
const bridge = useAtomValue(bridgeInfoAtom)
const resourceId =
typeof asset.chainBridgeResourceId === 'string'
? asset.chainBridgeResourceId
: asset.chainBridgeResourceId?.[toChain.id]

const {data: ethersGasPrice} = useSWR(
ethersWeb3Provider,
ethersGasPriceFetcher,
)
const {data: evmChainBridgeEstimatedGas} = useSWR(
bridge.kind === 'evmChainBridge' &&
ethersChainBridgeContract != null &&
khalaApi != null &&
resourceId != null && [
ethersChainBridgeContract,
khalaApi,
resourceId,
toChain.id,
],
evmChainBridgeEstimatedGasFetcher,
)

const xc20Address = asset.xc20Address?.[fromChain.id]
const {data: evmXTokensEstimatedGas} = useSWR(
bridge.kind === 'evmXTokens' &&
ethersXTokensContract != null &&
toChain.paraId != null &&
asset.xc20Address?.[fromChain.id] != null && [
xc20Address != null && [
ethersXTokensContract,
asset.xc20Address[fromChain.id],
xc20Address,
toChain.paraId,
decimals,
],
Expand Down Expand Up @@ -127,10 +105,7 @@ export const useEstimatedGasFee = (): Decimal | undefined => {

return (
(ethersGasPrice != null
? (evmChainBridgeEstimatedGas != null
? ethersGasPrice.times(evmChainBridgeEstimatedGas)
: undefined) ??
(evmXTokensEstimatedGas != null
? (evmXTokensEstimatedGas != null
? ethersGasPrice.times(evmXTokensEstimatedGas)
: undefined) ??
(evmSygmaEstimatedGas != null
Expand Down
27 changes: 4 additions & 23 deletions apps/subbridge/hooks/useEthersContract.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import chainBridgeAbi from '@/assets/chainbridge_abi.json'
import moonriverXTokensAbi from '@/assets/moonriver_xtokens_abi.json'
import tokenStandardAbi from '@/assets/token_standard_abi.json'
import {ASSETS, type AssetId} from '@/config/asset'
Expand All @@ -9,9 +8,8 @@ import {useAtomValue} from 'jotai'
import useSWRImmutable from 'swr/immutable'
import {useEthersWeb3Provider} from './useEthersProvider'

type AbiKind = 'chainBridge' | 'moonriverXTokens' | 'tokenStandard'
type AbiKind = 'moonriverXTokens' | 'tokenStandard'
const abi: Record<AbiKind, ethers.ContractInterface> = {
chainBridge: chainBridgeAbi,
moonriverXTokens: moonriverXTokensAbi,
tokenStandard: tokenStandardAbi,
}
Expand Down Expand Up @@ -42,7 +40,7 @@ export function useEthersAssetContract(

const {data} = useSWRImmutable(
provider != null && address != null
? [provider, address, 'tokenStandard']
? [provider, address, 'tokenStandard' as AbiKind]
: null,
fetcher,
)
Expand All @@ -62,7 +60,7 @@ export const useCurrentEthersAssetContract = ():
const provider = useEthersWeb3Provider()
const {data} = useSWRImmutable(
provider != null && address != null
? [provider, address, 'tokenStandard']
? [provider, address, 'tokenStandard' as AbiKind]
: null,
fetcher,
)
Expand All @@ -78,24 +76,7 @@ export const useEthersXTokensContract = (): ethers.Contract | undefined => {

const {data} = useSWRImmutable(
provider != null && address != null
? [provider, address, 'moonriverXTokens']
: null,
fetcher,
)

return data
}

export const useEthersChainBridgeContract = (): ethers.Contract | undefined => {
const fromChain = useAtomValue(fromChainAtom)
const provider = useEthersWeb3Provider()
const address =
fromChain.kind === 'evm'
? fromChain.chainBridgeContract?.address
: undefined
const {data} = useSWRImmutable(
provider != null && address != null
? [provider, address, 'chainBridge']
? [provider, address, 'moonriverXTokens' as AbiKind]
: null,
fetcher,
)
Expand Down
3 changes: 2 additions & 1 deletion apps/subbridge/hooks/useEthersProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import {ethers} from 'ethers'
import {useAtomValue} from 'jotai'
import useSWRImmutable from 'swr/immutable'

const ethersWeb3ProviderFetcher = async ([ethereumProvider]: [
const ethersWeb3ProviderFetcher = async ([ethereumProvider, _]: [
ethers.providers.ExternalProvider,
number,
]): Promise<ethers.providers.Web3Provider> => {
const provider = new ethers.providers.Web3Provider(ethereumProvider)
return provider
Expand Down
Loading

0 comments on commit adc29f6

Please sign in to comment.