From fac9c858a05857734681c5827b15699b0edc1802 Mon Sep 17 00:00:00 2001 From: taureau75 Date: Fri, 16 Jun 2023 19:15:22 -0400 Subject: [PATCH] max liquidity --- frontend/components/bridge/Bridge.tsx | 22 ++++++++++++++++++- .../bridge/tokenSelector/TokenSelector.tsx | 6 ++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/frontend/components/bridge/Bridge.tsx b/frontend/components/bridge/Bridge.tsx index 70e654b..ad43854 100644 --- a/frontend/components/bridge/Bridge.tsx +++ b/frontend/components/bridge/Bridge.tsx @@ -81,6 +81,7 @@ function Bridge() { const [amount, setAmount] = useState("") const [destAmount, setDestAmount] = useState("") const [prices, setPrices] = useState<{ [priceKey: string]: number }>({ "btc_usd": 0, "eth_usd": 0, "sol_usd": 0 }) + const [liquidity, setLiquidity] = useState<{ [liquidityKey: string]: number }>({ "btc": 0, "sol": 0, "eth_btc": 0, "eth_sol": 0 }) const [withdrawAddress, setWithdrawAddress] = useState("") const [depositAddress, setDepositAddress] = useState("") const [sendingSolPayment, setSendingSolPayment] = useState(false) @@ -189,6 +190,21 @@ function Bridge() { } } + const fetchLiquidity = async () => { + try { + const availableLiquidity = await fetch("https://api.zap.zigzag.exchange/available_liquidity").then(r => r.json()) + setLiquidity(availableLiquidity) + } catch (err: any) { + console.log(err?.message || err) + } + } + + const getCurrentDestLiquidity = () => { + if (destTokenItem.name == "ETH" && orgTokenItem.name == "SOL") return liquidity.eth_sol + if (destTokenItem.name == "ETH" && orgTokenItem.name == "BTC") return liquidity.eth_btc + return liquidity[destTokenItem.name.toLowerCase()] + } + const fetchHistory = async () => { try { let historyAddress = address @@ -224,6 +240,7 @@ function Bridge() { } fetchPrices() fetchHistory() + fetchLiquidity() }, [address, isConnected, isLoading, orgChainId, walletChain, orgTokenItem.name]) const handleTokenClick = (newTokenAddress: string) => { @@ -397,7 +414,8 @@ function Bridge() { const chains = ([orgChainId, destChainId]).sort() if (chains[0] === 2 && chains[1] === 3) return "Unsupported Chain Swap" if (!amount) return "Enter Amount" - if (amount > orgTokenItem.maxSize) return `Max size is ${orgTokenItem.maxSize} ${orgTokenItem.name}` + const maxSize = getCurrentDestLiquidity() - destTokenItem.liquidityBuffer + if (destAmount > maxSize) return `Max size is ${maxSize.toPrecision(6)} ${destTokenItem.name}` if (amount < orgTokenItem.minSize) return `Min size is ${orgTokenItem.minSize} ${orgTokenItem.name}` if (!address && orgTokenItem.name === "ETH") return "Connect Wallet" if (!address && orgTokenItem.name === "SOL") return "Connect Wallet" @@ -578,6 +596,8 @@ function Bridge() { } setInterval(fetchHistory, 5000) + setTimeout(fetchLiquidity, 5000) + setTimeout(fetchLiquidity, 30000) } const getCurrentMarketPrices = () => { diff --git a/frontend/components/bridge/tokenSelector/TokenSelector.tsx b/frontend/components/bridge/tokenSelector/TokenSelector.tsx index 309766f..be00d7f 100644 --- a/frontend/components/bridge/tokenSelector/TokenSelector.tsx +++ b/frontend/components/bridge/tokenSelector/TokenSelector.tsx @@ -21,7 +21,7 @@ export const evmTokenItems = [ icon: "eth.svg", networkIcon: "eth.svg", priceKey: "eth_usd", - maxSize: 10, + liquidityBuffer: 0.1, minSize: 0.004, networkFee: 0.0015 }, @@ -84,7 +84,7 @@ export const solTokenItems = [ icon: "sol.svg", networkIcon: "sol.svg", priceKey: "sol_usd", - maxSize: 500, + liquidityBuffer: 0.1, minSize: 0.3, networkFee: 0.001 }, @@ -130,7 +130,7 @@ export const btcTokenItems = [ icon: "btc.svg", networkIcon: "btc.svg", priceKey: "btc_usd", - maxSize: 1, + liquidityBuffer: 0.001, minSize: 0.0003, networkFee: 0.0001 },