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

Commit

Permalink
types
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Jul 5, 2024
1 parent f5aca0c commit b2e4180
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 94 deletions.
41 changes: 18 additions & 23 deletions hooks/swap/types.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
export interface TokenSelected {
balance: string
coin_type: string
contract: string
zrc20: string
chain_id: number
symbol: string
decimals: number
chain_name: string
ticker: string
}

export interface DestinationTokenSelected {
chain_name: string
chain_id: string
coin_type: string
contract: string
zrc20: string
}

export interface CrossChainFee {
amount: string
decimals: number
Expand All @@ -28,6 +8,8 @@ export interface CrossChainFee {
export interface Balance {
id: string
contract: string
balance: number
chain_name: string
}

export interface Inbound {
Expand All @@ -36,10 +18,23 @@ export interface Inbound {
}

export interface Token {
balance: string
coin_type: string
contract: string
zrc20: string
chain_id: number
symbol: string
decimals: number
chain_name: string
coin_type: string
ticker: string
zrc20?: string
contract?: string
}

export interface Error {
message: string
enabled: boolean
priority: number
}

export interface Errors {
[key: string]: Error
}
17 changes: 9 additions & 8 deletions hooks/swap/useAmountValidation.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useEffect, useState } from "react"

import type { CrossChainFee, TokenSelected } from "./types"
import type { CrossChainFee, Token } from "./types"

const useAmountValidation = (
sourceTokenSelected: TokenSelected | null,
sourceTokenSelected: Token | null,
sourceAmount: string,
crossChainFee: CrossChainFee | null,
sendType: string
sendType: string | null
) => {
const [isAmountGTFee, setIsAmountGTFee] = useState(false)
const [isAmountLTBalance, setIsAmountLTBalance] = useState(false)
Expand All @@ -15,14 +15,15 @@ const useAmountValidation = (
const am = parseFloat(sourceAmount || "0")
const ltBalance =
am >= 0 && am <= parseFloat(sourceTokenSelected?.balance || "0")
if (["crossChainZeta"].includes(sendType)) {

const type = sendType || ""

if (["crossChainZeta"].includes(type)) {
const gtFee = am > parseFloat(crossChainFee?.amount.toString() || "0")
setIsAmountGTFee(gtFee)
setIsAmountLTBalance(ltBalance)
} else if (["crossChainSwap", "crossChainSwapBTC"].includes(sendType)) {
const gtFee =
parseFloat(sourceAmount || "0") >
parseFloat(crossChainFee?.amount.toString() || "0")
} else if (["crossChainSwap", "crossChainSwapBTC"].includes(type)) {
const gtFee = am > parseFloat(crossChainFee?.amount.toString() || "0")
setIsAmountGTFee(gtFee)
setIsAmountLTBalance(ltBalance)
} else {
Expand Down
18 changes: 7 additions & 11 deletions hooks/swap/useCrossChainFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ import { utils } from "ethers"
import { roundNumber } from "@/lib/utils"
import { useZetaChainClient } from "@/hooks/useZetaChainClient"

import type {
CrossChainFee,
DestinationTokenSelected,
TokenSelected,
} from "./types"
import type { CrossChainFee, Token } from "./types"

const useCrossChainFee = (
sourceTokenSelected: TokenSelected | null,
destinationTokenSelected: DestinationTokenSelected | null,
sendType: string
sourceTokenSelected: Token | null,
destinationTokenSelected: Token | null,
sendType: string | null
) => {
const { fees } = useFeesContext()
const { client } = useZetaChainClient()
Expand Down Expand Up @@ -44,8 +40,8 @@ const useCrossChainFee = (
}, [sourceTokenSelected, destinationTokenSelected, sendType])

const getCrossChainFee = async (
s: TokenSelected | null,
d: DestinationTokenSelected | null
s: Token | null,
d: Token | null
): Promise<CrossChainFee | null> => {
if (!sendType || !s || !d) return null

Expand All @@ -54,7 +50,7 @@ const useCrossChainFee = (
const dest = d.chain_name
const toZetaChain = dest === "zeta_testnet"
const fee = fees["messaging"].find(
(f: { chainID: string }) => f.chainID === d.chain_id
(f: { chainID: number }) => f.chainID === d.chain_id
)
if (!fee) return null
const amount = toZetaChain ? "0" : fee.totalFee
Expand Down
4 changes: 2 additions & 2 deletions hooks/swap/useDestinationAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { useEffect, useState } from "react"
import { bech32 } from "bech32"
import { ethers, utils } from "ethers"

import type { DestinationTokenSelected } from "./types"
import type { Token } from "./types"

const useDestinationAddress = (
address: `0x${string}` | undefined,
destinationTokenSelected: DestinationTokenSelected | null,
destinationTokenSelected: Token | null,
bitcoinAddress: string | null
) => {
const [addressSelected, setAddressSelected] = useState<string>("")
Expand Down
21 changes: 8 additions & 13 deletions hooks/swap/useDestinationAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@ import debounce from "lodash/debounce"
import { roundNumber } from "@/lib/utils"
import { useZetaChainClient } from "@/hooks/useZetaChainClient"

import type {
Balance,
CrossChainFee,
DestinationTokenSelected,
TokenSelected,
} from "./types"
import type { Balance, CrossChainFee, Token } from "./types"

const useDestinationAmount = (
sourceTokenSelected: TokenSelected | null,
destinationTokenSelected: DestinationTokenSelected | null,
sourceTokenSelected: Token | null,
destinationTokenSelected: Token | null,
sourceAmount: string,
crossChainFee: CrossChainFee | null,
sendType: string
sendType: string | null
) => {
const { client } = useZetaChainClient()
const [destinationAmount, setDestinationAmount] = useState<string>("")
Expand All @@ -29,8 +24,8 @@ const useDestinationAmount = (
useEffect(() => {
setDestinationAmount("")
const fetchQuoteCrossChain = async (
s: TokenSelected,
d: DestinationTokenSelected,
s: Token,
d: Token,
sourceAmount: string,
withdraw: boolean
) => {
Expand Down Expand Up @@ -102,8 +97,8 @@ const useDestinationAmount = (
])

const getQuoteCrossChain = async (
s: TokenSelected,
d: DestinationTokenSelected,
s: Token,
d: Token,
sourceAmount: string,
withdraw: boolean
) => {
Expand Down
51 changes: 44 additions & 7 deletions hooks/swap/useSendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { useAccount } from "wagmi"
import { useEthersSigner } from "@/hooks/useEthersSigner"

import SwapToAnyToken from "./SwapToAnyToken.json"
import type { DestinationTokenSelected, Inbound, TokenSelected } from "./types"
import type { Inbound, Token } from "./types"

const useSendTransaction = (
sendType: string,
sourceTokenSelected: TokenSelected,
destinationTokenSelected: DestinationTokenSelected,
sendType: string | null,
sourceTokenSelected: Token | null,
destinationTokenSelected: Token | null,
sourceAmount: string,
addressSelected: string,
setSourceAmount: (amount: string) => void,
Expand All @@ -30,12 +30,17 @@ const useSendTransaction = (
const [isSending, setIsSending] = useState(false)

const handleSend = async () => {
setIsSending(true)

if (!sendType) {
throw new Error("Send type is not defined.")
}
if (!address) {
setIsSending(false)
throw new Error("Address undefined.")
}
if (!sourceTokenSelected || !destinationTokenSelected) {
throw new Error("Token not selected.")
}

setIsSending(true)

try {
await m[sendType]()
Expand Down Expand Up @@ -85,6 +90,10 @@ const useSendTransaction = (
console.error("Bitcoin address undefined.")
return
}
if (!destinationTokenSelected) {
console.error("Destination token not selected.")
return
}
const a = parseFloat(sourceAmount) * 1e8
const bitcoinTSSAddress = "tb1qy9pqmk2pd9sv63g27jt8r657wy0d9ueeh0nqur"
const contract = omnichainSwapContractAddress.replace(/^0x/, "")
Expand Down Expand Up @@ -144,6 +153,9 @@ const useSendTransaction = (
}

m.crossChainZeta = async () => {
if (!sourceTokenSelected || !destinationTokenSelected) {
return
}
const from = sourceTokenSelected.chain_name
const to = destinationTokenSelected.chain_name
const tx = await client.sendZeta({
Expand All @@ -160,6 +172,9 @@ const useSendTransaction = (
}

m.withdrawBTC = async () => {
if (!sourceTokenSelected || !destinationTokenSelected) {
return
}
const from = sourceTokenSelected.chain_name
const to = destinationTokenSelected.chain_name
const btc = bitcoinAddress
Expand Down Expand Up @@ -199,6 +214,9 @@ const useSendTransaction = (
}

m.transferERC20EVM = async () => {
if (!sourceTokenSelected) {
return
}
const contract = new ethers.Contract(
sourceTokenSelected.contract as string,
ERC20_ABI.abi,
Expand All @@ -216,6 +234,9 @@ const useSendTransaction = (
}

m.withdrawZRC20 = async () => {
if (!sourceTokenSelected || !destinationTokenSelected) {
return
}
const destination = destinationTokenSelected.chain_name
const zrc20 = getAddress("zrc20", destination as ParamChainName)
if (!zrc20) {
Expand All @@ -238,6 +259,9 @@ const useSendTransaction = (
}

m.depositNative = async () => {
if (!sourceTokenSelected || !destinationTokenSelected) {
return
}
const from = sourceTokenSelected.chain_name
const to = destinationTokenSelected.chain_name
const token = sourceTokenSelected.symbol
Expand All @@ -254,6 +278,9 @@ const useSendTransaction = (
}

m.fromZetaChainSwapAndWithdraw = async () => {
if (!sourceTokenSelected || !destinationTokenSelected) {
return
}
const swapContract = new ethers.Contract(
omnichainSwapContractAddress,
SwapToAnyToken.abi,
Expand Down Expand Up @@ -291,6 +318,9 @@ const useSendTransaction = (
}

m.fromZetaChainSwap = async () => {
if (!sourceTokenSelected || !destinationTokenSelected) {
return
}
const swapContract = new ethers.Contract(
omnichainSwapContractAddress,
SwapToAnyToken.abi,
Expand Down Expand Up @@ -323,6 +353,9 @@ const useSendTransaction = (
}

m.depositERC20 = async () => {
if (!sourceTokenSelected || !destinationTokenSelected) {
return
}
const custodyAddress = getAddress(
"erc20Custody",
sourceTokenSelected.chain_name as ParamChainName
Expand Down Expand Up @@ -385,6 +418,10 @@ const useSendTransaction = (
console.error("Bitcoin address undefined.")
return
}
if (!destinationTokenSelected) {
console.error("Destination token not selected.")
return
}
const a = parseFloat(sourceAmount) * 1e8
const bitcoinTSSAddress = "tb1qy9pqmk2pd9sv63g27jt8r657wy0d9ueeh0nqur"
const contract = omnichainSwapContractAddress.replace(/^0x/, "")
Expand Down
Loading

0 comments on commit b2e4180

Please sign in to comment.