Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Jul 5, 2024
1 parent 009cb74 commit c17b433
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 94 deletions.
2 changes: 1 addition & 1 deletion components/SwapLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ interface SwapLayoutProps {
destinationBalances: any[]
setDestinationToken: (token: any) => void
computeSendType: (sourceToken: any, destinationToken: any) => string | null
addressSelected: string
addressSelected: any
customAddressOpen: boolean
setCustomAddressOpen: (open: boolean) => void
canChangeAddress: boolean
Expand Down
108 changes: 16 additions & 92 deletions components/swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useAccount, useNetwork, useSwitchNetwork } from "wagmi"
import { formatAddress } from "@/lib/utils"
import useAmountValidation from "@/hooks/swap/useAmountValidation"
import useCrossChainFee from "@/hooks/swap/useCrossChainFee"
import useDestinationAddress from "@/hooks/swap/useDestinationAddress"
import useDestinationAmount from "@/hooks/swap/useDestinationAmount"
import useSendTransaction from "@/hooks/swap/useSendTransaction"
import useSendType, { computeSendType } from "@/hooks/swap/useSendType"
Expand All @@ -31,13 +32,6 @@ const Swap = () => {
const [sourceTokenOpen, setSourceTokenOpen] = useState(false)
const [destinationTokenOpen, setDestinationTokenOpen] = useState(false)
const [isRightChain, setIsRightChain] = useState(true)
const [addressSelected, setAddressSelected] = useState<any>(null)
const [isAddressSelectedValid, setIsAddressSelectedValid] = useState(false)
const [canChangeAddress, setCanChangeAddress] = useState(false)
const [customAddress, setCustomAddress] = useState("")
const [customAddressSelected, setCustomAddressSelected] = useState("")
const [customAddressOpen, setCustomAddressOpen] = useState(false)
const [isCustomAddressValid, setIsCustomAddressValid] = useState(false)
const [isFeeOpen, setIsFeeOpen] = useState(false)
const [sendButtonText, setSendButtonText] = useState("Send tokens")
const {
Expand Down Expand Up @@ -81,7 +75,22 @@ const Swap = () => {
destinationAmountIsLoading
)

const { address } = useAccount()

const {
addressSelected,
isAddressSelectedValid,
customAddressOpen,
setCustomAddressOpen,
canChangeAddress,
customAddress,
setCustomAddress,
isCustomAddressValid,
saveCustomAddress,
} = useDestinationAddress(address, destinationTokenSelected, bitcoinAddress)

const { handleSend, isSending } = useSendTransaction(
sendType,
sourceTokenSelected,
destinationTokenSelected,
sourceAmount,
Expand All @@ -95,20 +104,6 @@ const Swap = () => {
client
)

const { address } = useAccount()

// Set whether address can be changed
useEffect(() => {
setCanChangeAddress(
[
"transferNativeEVM",
"transferERC20EVM",
"crossChainSwap",
"transferBTC",
].includes(sendType as any)
)
}, [sourceAmount, sendType, destinationTokenSelected])

useEffect(() => {
if (sourceTokenSelected && destinationTokenSelected) {
updateError("sendTypeUnsupported", { enabled: !sendType })
Expand Down Expand Up @@ -136,53 +131,6 @@ const Swap = () => {
sourceAmount,
])

// Set destination address
useEffect(() => {
if (!isAddressSelectedValid && destinationTokenSelected) {
if (destinationTokenSelected.chain_name === "btc_testnet") {
setAddressSelected(bitcoinAddress)
} else {
setAddressSelected(address)
}
}
}, [destinationTokenSelected, isAddressSelectedValid])

useEffect(() => {
setAddressSelected(customAddressSelected || address)
}, [customAddressSelected, address])

const saveCustomAddress = () => {
if (isCustomAddressValid) {
setCustomAddressSelected(customAddress)
setCustomAddress(customAddress)
setCustomAddressOpen(false)
}
}

// Set whether the custom destination address is valid
useEffect(() => {
let isValidBech32 = false
try {
if (bech32.decode(customAddress)) {
const bech32address = utils.solidityPack(
["bytes"],
[utils.toUtf8Bytes(customAddress)]
)
if (bech32address) {
isValidBech32 = true
}
}
} catch (e) {}
const isValidEVMAddress = ethers.utils.isAddress(customAddress)
if (!destinationTokenSelected) {
setIsCustomAddressValid(true)
} else if (destinationTokenSelected?.chain_name === "btc_testnet") {
setIsCustomAddressValid(isValidBech32)
} else {
setIsCustomAddressValid(isValidEVMAddress)
}
}, [customAddress, destinationTokenSelected])

const sendDisabled =
!sendType ||
!isAmountGTFee ||
Expand All @@ -203,30 +151,6 @@ const Swap = () => {
}
}, [destinationAmountIsLoading, sendDisabled, priorityErrors])

// Set whether the selected destination address is valid
useEffect(() => {
let isValidBech32 = false
try {
if (bech32.decode(addressSelected)) {
const bech32address = utils.solidityPack(
["bytes"],
[utils.toUtf8Bytes(addressSelected)]
)
if (bech32address) {
isValidBech32 = true
}
}
} catch (e) {}
const isValidEVMAddress = ethers.utils.isAddress(addressSelected)
if (!destinationTokenSelected) {
setIsAddressSelectedValid(true)
} else if (destinationTokenSelected?.chain_name === "btc_testnet") {
setIsAddressSelectedValid(isValidBech32)
} else {
setIsAddressSelectedValid(isValidEVMAddress)
}
}, [addressSelected, destinationTokenSelected])

// Set whether the chain currently selected is the right one
useEffect(() => {
if (sourceTokenSelected?.chain_name === "btc_testnet") {
Expand Down
105 changes: 105 additions & 0 deletions hooks/swap/useDestinationAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { useEffect, useState } from "react"
import { bech32 } from "bech32"
import { ethers, utils } from "ethers"

const useDestinationAddress = (
address: any,
destinationTokenSelected: any,
bitcoinAddress: string | null
) => {
const [addressSelected, setAddressSelected] = useState<string | null>(null)
const [isAddressSelectedValid, setIsAddressSelectedValid] = useState(false)
const [customAddress, setCustomAddress] = useState("")
const [customAddressSelected, setCustomAddressSelected] = useState<
string | null
>("")
const [customAddressOpen, setCustomAddressOpen] = useState(false)
const [isCustomAddressValid, setIsCustomAddressValid] = useState(false)

useEffect(() => {
if (!isAddressSelectedValid && destinationTokenSelected) {
if (destinationTokenSelected.chain_name === "btc_testnet") {
setAddressSelected(bitcoinAddress)
} else {
setAddressSelected(address)
}
}
}, [
destinationTokenSelected,
isAddressSelectedValid,
bitcoinAddress,
address,
])

useEffect(() => {
setAddressSelected(customAddressSelected || address)
}, [customAddressSelected, address])

useEffect(() => {
let isValidBech32 = false
try {
if (customAddress && bech32.decode(customAddress)) {
const bech32address = utils.solidityPack(
["bytes"],
[utils.toUtf8Bytes(customAddress)]
)
if (bech32address) {
isValidBech32 = true
}
}
} catch (e) {}
const isValidEVMAddress = ethers.utils.isAddress(customAddress)
if (!destinationTokenSelected) {
setIsCustomAddressValid(true)
} else if (destinationTokenSelected?.chain_name === "btc_testnet") {
setIsCustomAddressValid(isValidBech32)
} else {
setIsCustomAddressValid(isValidEVMAddress)
}
}, [customAddress, destinationTokenSelected])

const saveCustomAddress = () => {
if (isCustomAddressValid) {
setCustomAddressSelected(customAddress)
setCustomAddress(customAddress)
setCustomAddressOpen(false)
}
}

useEffect(() => {
let isValidBech32 = false
try {
if (addressSelected && bech32.decode(addressSelected)) {
const bech32address = utils.solidityPack(
["bytes"],
[utils.toUtf8Bytes(addressSelected)]
)
if (bech32address) {
isValidBech32 = true
}
}
} catch (e) {}
const isValidEVMAddress = ethers.utils.isAddress(addressSelected || "")
if (!destinationTokenSelected) {
setIsAddressSelectedValid(true)
} else if (destinationTokenSelected?.chain_name === "btc_testnet") {
setIsAddressSelectedValid(isValidBech32)
} else {
setIsAddressSelectedValid(isValidEVMAddress)
}
}, [addressSelected, destinationTokenSelected])

return {
addressSelected,
isAddressSelectedValid,
customAddressOpen,
setCustomAddressOpen,
canChangeAddress: true,
customAddress,
setCustomAddress,
isCustomAddressValid,
saveCustomAddress,
}
}

export default useDestinationAddress
3 changes: 2 additions & 1 deletion hooks/swap/useSendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useEthersSigner } from "@/hooks/useEthersSigner"
import SwapToAnyToken from "./SwapToAnyToken.json"

const useSendTransaction = (
sendType: any,
sourceTokenSelected: any,
destinationTokenSelected: any,
sourceAmount: any,
Expand All @@ -30,7 +31,7 @@ const useSendTransaction = (
const signer = useEthersSigner()
const [isSending, setIsSending] = useState(false)

const handleSend = async (sendType: any) => {
const handleSend = async () => {
setIsSending(true)

if (!address) {
Expand Down

0 comments on commit c17b433

Please sign in to comment.