Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-orbs committed Oct 16, 2024
1 parent ee8e960 commit 4d101a1
Show file tree
Hide file tree
Showing 24 changed files with 503 additions and 204 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@ethersproject/hash": "^5.7.0",
"@orbs-network/liquidity-hub-sdk": "^1.0.40",
"@orbs-network/swap-ui": "^0.0.14",
"@orbs-network/twap-sdk": "^2.0.37",
"@orbs-network/twap-sdk": "^2.0.38",
"@paraswap/sdk": "^6.10.0",
"@radix-ui/react-accordion": "^1.2.1",
"@radix-ui/react-avatar": "^1.1.0",
Expand Down
3 changes: 1 addition & 2 deletions src/components/tokens/token-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
format,
cn,
fromBigNumber,
fromBigNumberToStr,
ErrorCodes,
} from '@/lib'
import { Skeleton } from '../ui/skeleton'
Expand All @@ -30,7 +29,7 @@ export type TokenCardProps = {
label: string
amount: string
amountUsd?: string
balance: string
balance: any
selectedToken: Token
tokens: TokensWithBalances
onSelectToken: (token: Token) => void
Expand Down
10 changes: 5 additions & 5 deletions src/lib/useHandleInputError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import {
} from "./useTokensWithBalances";

/* Handles amount input errors */
type UseHandleInputError = {
inToken: Token | null;
inputAmount: string;
};

export function useInputError({
inputAmount,
inToken,
}: UseHandleInputError) {
}: {
inToken: Token | null;
inputAmount: string;
}) {
const tokensWithBalances = useTokensWithBalances();
const tokenBalance = useTokenBalance(inToken?.address);
return useMemo(() => {
Expand Down
4 changes: 1 addition & 3 deletions src/lib/useTokensWithBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import { networks } from '@/lib/networks'

export function useTokensWithBalances() {
const account = useAccount()
const { data: tokens, isLoading: tokensLoading } = useTokensList({
chainId: networks.poly.id,
})
const { data: tokens, isLoading: tokensLoading } = useTokensList()
const {
query: { data: balances, isLoading: balancesLoading, refetch },
queryKey,
Expand Down
5 changes: 2 additions & 3 deletions src/lib/wrapToken.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { toast } from "sonner"
import { Address } from "viem"
import { simulateContract, writeContract } from 'wagmi/actions'
import { IWETHabi } from "./abis"
import { networks } from "./networks"
Expand All @@ -13,16 +14,14 @@ export const wrapToken = async (account: string, inAmount: string) => {
functionName: 'deposit',
account: account as Address,
address: networks.poly.wToken.address as Address,
value: inAmount
value: BigInt(inAmount.replace('.', ''))
})

// Perform the deposit contract function
const txHash = await writeContract(wagmiConfig, simulatedData.request)
await waitForConfirmations(txHash, 1, 20)
return txHash
} catch (error) {
console.log({ error });

const errorMessage = getErrorMessage(
error,
'An error occurred while wrapping your token'
Expand Down
2 changes: 1 addition & 1 deletion src/trade/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { network, networks, toExactAmount, toRawAmount } from "@/lib";
import { network, toExactAmount, toRawAmount } from "@/lib";
import { useMemo } from "react";
import { useChainId } from "wagmi";

Expand Down
197 changes: 197 additions & 0 deletions src/trade/liquidity-hub/liquidity-hub-confirmation-dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
import { Button } from '@/components/ui/button'
import {
Dialog,
DialogContent,
DialogDescription,
DialogTitle,
} from '@/components/ui/dialog'
import { LiquidityProvider, SwapSteps, Token } from '@/types'
import { Card } from '@/components/ui/card'
import { SwapFlow, SwapStep, SwapStatus } from '@orbs-network/swap-ui'
import { useMemo } from 'react'
import { DataDetails } from '@/components/ui/data-details'
import {
format,
fromBigNumber,
getLiquidityProviderName,
getSteps,
resolveNativeTokenAddress,
toBigNumber,
useGetRequiresApproval,
} from '@/lib'
import { useAccount } from 'wagmi'
import { Address } from 'viem'

export type SwapConfirmationDialogProps = {
inToken: Token
outToken: Token
isOpen: boolean
onClose: () => void
confirmSwap: () => void
swapStatus?: SwapStatus
currentStep?: SwapSteps
signature?: string
gasAmountOut?: string
liquidityProvider: LiquidityProvider
inAmount?: number
inAmountUsd?: string
outAmount?: number
outAmountUsd?: string
allowancePermitAddress: string
}

// Construct steps for swap to display in UI
const useSteps = (
liquidityProvider: LiquidityProvider,
requiresApproval: boolean,
inToken?: Token,
signature?: string
) => {
return useMemo((): SwapStep[] => {
if (!inToken) return []

const steps = getSteps({
noWrap: liquidityProvider === 'paraswap',
inTokenAddress: inToken.address,
requiresApproval,
})

return steps.map((step) => {
if (step === SwapSteps.Wrap) {
return {
id: SwapSteps.Wrap,
title: `Wrap ${inToken.symbol}`,
description: `Wrap ${inToken.symbol}`,
image: inToken?.logoUrl,
}
}
if (step === SwapSteps.Approve) {
return {
id: SwapSteps.Approve,
title: `Approve ${inToken.symbol}`,
description: `Approve ${inToken.symbol}`,
image: inToken?.logoUrl,
}
}
return {
id: SwapSteps.Swap,
title: `Swap ${inToken.symbol}`,
description: `Swap ${inToken.symbol}`,
image: inToken?.logoUrl,
timeout: signature ? 60_000 : 40_000,
}
})
}, [inToken, liquidityProvider, requiresApproval, signature])
}

export function SwapConfirmationDialog({
inToken,
outToken,
isOpen,
onClose,
confirmSwap,
swapStatus,
currentStep,
signature,
gasAmountOut,
liquidityProvider,
inAmount,
inAmountUsd,
outAmount,
outAmountUsd,
allowancePermitAddress,
}: SwapConfirmationDialogProps) {
const { address } = useAccount()

const gasPrice = useMemo(() => {
if (!outAmountUsd || !gasAmountOut) return 0
const gas = fromBigNumber(gasAmountOut, outToken.decimals)
const usd = Number(outAmountUsd) / Number(outAmount)
return Number(gas) * usd
}, [outAmountUsd, gasAmountOut, outToken.decimals, outAmount])

const { requiresApproval, approvalLoading } = useGetRequiresApproval(
allowancePermitAddress as Address,
resolveNativeTokenAddress(inToken?.address),
toBigNumber(inAmount || 0, inToken?.decimals)
)

const steps = useSteps(
liquidityProvider,
requiresApproval,
inToken,
signature
)

return (
<Dialog open={isOpen} onOpenChange={onClose}>
<DialogContent>
<DialogTitle>Swap</DialogTitle>
<DialogDescription></DialogDescription>
<div className="flex flex-col gap-4">
<div className="p-4">
<SwapFlow
inAmount={format.crypto(inAmount || 0)}
outAmount={format.crypto(outAmount || 0)}
mainContent={
<SwapFlow.Main
fromTitle="Sell"
toTitle="Buy"
steps={steps}
inUsd={format.dollar(Number(inAmountUsd || '0'))}
outUsd={format.dollar(Number(outAmountUsd || '0'))}
currentStep={currentStep as number}
/>
}
swapStatus={swapStatus}
successContent={<SwapFlow.Success explorerUrl="/" />}
failedContent={<SwapFlow.Failed />}
inToken={{
symbol: inToken.symbol,
logo: inToken.logoUrl,
}}
outToken={{
symbol: outToken.symbol,
logo: outToken.logoUrl,
}}
/>
</div>

{!swapStatus && address && (
<>
<Card className="bg-slate-900">
<div className="p-4 flex flex-col gap-2">
<DataDetails
data={{
Network: 'Polygon',
'Network fee': format.dollar(gasPrice),
'Routing source':
getLiquidityProviderName(liquidityProvider),
}}
/>
</div>
</Card>
<Card className="bg-slate-900">
<div className="p-4">
<DataDetails
data={{
Recipient: format.address(address),
}}
/>
</div>
</Card>

<Button
size="lg"
onClick={() => confirmSwap()}
disabled={approvalLoading}
>
Swap {inToken?.symbol} for {outToken?.symbol}
</Button>
</>
)}
</div>
</DialogContent>
</Dialog>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import { useCallback, useMemo, useState } from 'react'
import { SwapStatus } from '@orbs-network/swap-ui'
import { useAccount } from 'wagmi'
import { SwapDetails } from '../../components/swap-details'
import { SwapConfirmationDialog } from './swap-confirmation-dialog'
import { useLiquidityHubQuote } from './liquidity-hub/useLiquidityHubQuote'
import { SwapConfirmationDialog } from './liquidity-hub-confirmation-dialog'
import { useLiquidityHubQuote } from './useLiquidityHubQuote'
import { Button } from '@/components/ui/button'
import { useLiquidityHubSwapCallback } from './liquidity-hub/useLiquidityHubSwapCallback'
import { useLiquidityHubSwapCallback } from './useLiquidityHubSwapCallback'
import { permit2Address, Quote } from '@orbs-network/liquidity-hub-sdk'
import {
useDefaultTokens,
useHandleInputError,
ErrorCodes,
fromBigNumber,
toBigNumber,
Expand All @@ -37,14 +36,14 @@ import {
import { Switch } from '@/components/ui/switch'
import { Label } from '@/components/ui/label'
import { Input } from '@/components/ui/input'
import { useInputError } from '../../lib/useHandleInputError'

export function Swap() {
const { tokensWithBalances, refetch: refetchBalances } =
useTokensWithBalances()
const [inToken, setInToken] = useState<Token | null>(null)
const [outToken, setOutToken] = useState<Token | null>(null)
const [inputAmount, setInputAmount] = useState<string>('')
const [inputError, setInputError] = useState<string | null>(null)
const [acceptedQuote, setAcceptedQuote] = useState<Quote | undefined>()
const [liquidityHubDisabled, setLiquidityHubDisabled] = useState(false)
const [currentStep, setCurrentStep] = useState<SwapSteps | undefined>(
Expand All @@ -71,11 +70,9 @@ export function Swap() {
})

// Handle Amount Input Error
useHandleInputError({
const inputError = useInputError({
inputAmount,
inToken,
tokensWithBalances,
setInputError,
})

// Handle Token Switch
Expand All @@ -88,7 +85,6 @@ export function Swap() {
const resetSwap = useCallback(() => {
setAcceptedQuote(undefined)
setInputAmount('')
setInputError(null)
setCurrentStep(undefined)
setSignature(undefined)
setSwapStatus(undefined)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { useMutation } from '@tanstack/react-query'
import { signTypedData, simulateContract, writeContract } from 'wagmi/actions'
import { signTypedData } from 'wagmi/actions'
import { _TypedDataEncoder } from '@ethersproject/hash'
import { permit2Address, Quote } from '@orbs-network/liquidity-hub-sdk'
import { SwapStatus } from '@orbs-network/swap-ui'
import { useLiquidityHubSDK } from './useLiquidityHubSDK'
import { SwapSteps } from '@/types'
import { Address } from 'viem'
import {
wagmiConfig,
IWETHabi,
networks,
waitForConfirmations,
promiseWithTimeout,
getSteps,
Expand Down
File renamed without changes.
10 changes: 4 additions & 6 deletions src/trade/trade.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
import { Swap } from './liquidity-hub/liquidity-hub-swap'
import { Twap } from './twap/twap'

export function Trade() {
return (
<div className="max-w-lg w-full px-4">
<h1 className="text-5xl font-bold mb-8">Trade</h1>

<Tabs defaultValue="twap" className="w-full">
<Tabs defaultValue="swap" className="w-full">
<TabsList>
<TabsTrigger value="swap">Swap</TabsTrigger>
<TabsTrigger value="twap">TWAP</TabsTrigger>
<TabsTrigger value="limit">Limit</TabsTrigger>
</TabsList>
<TabsContent value="swap">
{/* <Swap /> */}
<Swap />
</TabsContent>
<TabsContent value="twap">
<Twap />
</TabsContent>
<TabsContent value="limit">
<div className="w-full rounded-lg bg-gray-100 dark:bg-gray-800 p-4 mt-4 flex flex-col items-center justify-center">
<img src="/dex-playground/dlimit-logo.svg" className="w-32 h-32" />
<div>Coming soon</div>
</div>
<Twap isLimitPanel={true} />
</TabsContent>
</Tabs>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/trade/twap/components/limit-price-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export function LimitPriceInput() {
if (isMarketOrder) return null;

return (
<Card className="bg-slate-50 dark:bg-slate-900 p-4 flex flex-col gap-4 mb-4">
<Card className="bg-slate-50 dark:bg-slate-900 p-4 flex flex-col gap-4">
<div className="flex justify-between items-center">
<div className="flex flex-row gap-1 items-center">
<h2 className="text-gray-500 dark:text-gray-400"> when 1 </h2>
Expand Down
Loading

0 comments on commit 4d101a1

Please sign in to comment.