Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/update bridge sdk #265

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface ActionButtonProps {
isDisabled: boolean
L2L3message?: { destination: string; data: string }
setErrorMessage: (arg0: string) => void
bridger?: Bridger | null
bridger: Bridger
symbol?: string
decimals?: number
balance?: string
Expand Down Expand Up @@ -253,7 +253,7 @@ const ActionButton: React.FC<ActionButtonProps> = ({
return []
}

return [selectedBridgeToken, nativeToken].filter((token): token is Token => token !== undefined)
return [selectedBridgeToken, nativeToken].filter(Boolean) as Token[]
})()

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ const BridgeView = ({
direction: DepositDirection
setDirection: (arg0: DepositDirection) => void
}) => {
const [bridger, setBridger] = useState<Bridger | null>(null)

const [value, setValue] = useState('0')
const [message, setMessage] = useState<{ destination: string; data: string }>({ destination: '', data: '' })
const [isMessageExpanded, setIsMessageExpanded] = useState(false)
Expand Down Expand Up @@ -81,9 +79,13 @@ const BridgeView = ({
const handleTokenChange = async (token: Token) => {
setSelectedBridgeToken(token)
}

const { getEstimatedFee, useAllowances } = useBridger()

const originChainId = direction === 'DEPOSIT' ? selectedLowNetwork.chainId : selectedHighNetwork.chainId
const destinationChainId = direction === 'DEPOSIT' ? selectedHighNetwork.chainId : selectedLowNetwork.chainId
const [bridger, setBridger] = useState<Bridger>(getBridger(originChainId, destinationChainId, selectedBridgeToken.tokenAddressMap))


const estimatedFee = getEstimatedFee({
bridger,
value,
Expand Down Expand Up @@ -302,7 +304,7 @@ const BridgeView = ({
isDisabled={!!inputErrorMessages.value || !!inputErrorMessages.destination || !!inputErrorMessages.data}
setErrorMessage={setNetworkErrorMessage}
L2L3message={isMessageExpanded ? message : { data: '', destination: '' }}
bridger={bridger ?? null}
bridger={bridger}
symbol={tokenInformation?.symbol ?? ''}
decimals={tokenInformation?.decimalPlaces ?? 18}
balance={tokenInformation?.tokenBalance}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface MultiTokenApprovalProps {
setShowApproval: (showApproval: boolean) => void
balance: string | undefined
nativeBalance: string | undefined
bridger: Bridger | null
bridger: Bridger
decimals: number | undefined
tokens: Token[]
startingTokenIndex: number
Expand Down
19 changes: 5 additions & 14 deletions webapps/world-builder-dashboard/src/hooks/useBridgeTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useMutation, useQueryClient } from 'react-query'
import { useQuery } from 'react-query'
import { useNavigate } from 'react-router-dom'
import { getNetworks } from '../../constants'
import { ethers } from 'ethers'
import { BridgeTransfer, BridgeTransferStatus, getBridgeTransfer } from 'game7-bridge-sdk'
import { useBlockchainContext } from '@/contexts/BlockchainContext'
import { useBridgeNotificationsContext } from '@/contexts/BridgeNotificationsContext'
Expand All @@ -13,7 +12,7 @@ interface UseTransferDataProps {
}

export const useBridgeTransfer = () => {
const { connectedAccount, selectedNetworkType, switchChain } = useBlockchainContext()
const { connectedAccount, selectedNetworkType, getProvider } = useBlockchainContext()
// Retry function with exponential backoff for handling 429 errors
const retryWithExponentialBackoff = async (fn: () => Promise<any>, retries = 20, delay = 1000, jitterFactor = 0.5) => {
let attempt = 0
Expand Down Expand Up @@ -155,19 +154,11 @@ export const useBridgeTransfer = () => {
}


let targetChain = getNetworks(selectedNetworkType)?.find(network => { network.chainId === destinationChainId })

let provider
if (window.ethereum) {
provider = new ethers.providers.Web3Provider(window.ethereum)
const currentChain = await provider.getNetwork()
if (targetChain && currentChain.chainId !== targetChain.chainId) {
await switchChain(targetChain)
provider = new ethers.providers.Web3Provider(window.ethereum)
}
} else {
throw new Error('Wallet is not installed!')
const targetChain = getNetworks(selectedNetworkType)?.find((network) => network.chainId === destinationChainId);
if (!targetChain) {
throw new Error('Target chain is undefined');
}
const provider = await getProvider(targetChain);
const signer = provider.getSigner()

// Bridge Transfer execute
Expand Down
4 changes: 2 additions & 2 deletions webapps/world-builder-dashboard/src/hooks/useBridger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const useBridger = () => {
selectedHighNetwork,
tokenInformation
}: {
bridger: Bridger | null
bridger: Bridger
value: string
direction: 'DEPOSIT' | 'WITHDRAW'
selectedLowNetwork: NetworkInterface
Expand Down Expand Up @@ -111,7 +111,7 @@ export const useBridger = () => {
selectedHighNetwork,
connectedAccount
}: {
bridger: Bridger | null
bridger: Bridger
direction: DepositDirection
selectedLowNetwork: NetworkInterface
selectedHighNetwork: NetworkInterface
Expand Down
Loading