Skip to content

Commit

Permalink
max liquidity
Browse files Browse the repository at this point in the history
  • Loading branch information
taureau75 committed Jun 16, 2023
1 parent 3e85b44 commit fac9c85
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
22 changes: 21 additions & 1 deletion frontend/components/bridge/Bridge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function Bridge() {
const [amount, setAmount] = useState<number | string>("")
const [destAmount, setDestAmount] = useState<number | string>("")
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<string>("")
const [sendingSolPayment, setSendingSolPayment] = useState<boolean>(false)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -224,6 +240,7 @@ function Bridge() {
}
fetchPrices()
fetchHistory()
fetchLiquidity()
}, [address, isConnected, isLoading, orgChainId, walletChain, orgTokenItem.name])

const handleTokenClick = (newTokenAddress: string) => {
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -578,6 +596,8 @@ function Bridge() {
}

setInterval(fetchHistory, 5000)
setTimeout(fetchLiquidity, 5000)
setTimeout(fetchLiquidity, 30000)
}

const getCurrentMarketPrices = () => {
Expand Down
6 changes: 3 additions & 3 deletions frontend/components/bridge/tokenSelector/TokenSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down Expand Up @@ -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
},
Expand Down Expand Up @@ -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
},
Expand Down

1 comment on commit fac9c85

@vercel
Copy link

@vercel vercel bot commented on fac9c85 Jun 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.