forked from coinbase/onchainkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Swap component refactor (coinbase#522)
- Loading branch information
1 parent
6f07c50
commit 7906f46
Showing
13 changed files
with
378 additions
and
201 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{/* import { Swap, SwapAmountInput, SwapButton } from '../../../../src/swap'; */} | ||
import App from '../App'; | ||
|
||
# `<Swap />` | ||
|
||
### Alert! Component is actively in development. Stay tuned for upcoming releases. | ||
|
||
The `Swap` component is a comprehensive interface for users to execute token swaps. It includes two instances of the `SwapAmountInput` component, enabling users to specify the amount of tokens to sell and buy. Additionally, the component features a "Swap" button for initiating the transaction. | ||
|
||
## Usage | ||
|
||
```tsx [code] | ||
<Swap account={account} onError={onError}> | ||
<SwapAmountInput label="Sell" token={fromToken} type="from" /> | ||
<SwapAmountInput label="Buy" token={toToken} type="to" /> | ||
<SwapButton onError={onError} onSubmit={onSubmit} /> | ||
</Swap> | ||
``` | ||
|
||
## Props | ||
|
||
[`SwapReact`](/swap/types#SwapReact) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { useCallback, useMemo, useState } from 'react'; | ||
import { cn } from '../../lib/utils'; | ||
import { SwapContext } from '../context'; | ||
import { getSwapQuote } from '../core/getSwapQuote'; | ||
import { isSwapError } from '../utils'; | ||
import type { SwapError, SwapReact } from '../types'; | ||
import type { Token } from '../../token'; | ||
|
||
export function Swap({ account, children, onError }: SwapReact) { | ||
const [fromAmount, setFromAmount] = useState(''); | ||
const [fromToken, setFromToken] = useState<Token>(); | ||
const [toAmount, setToAmount] = useState(''); | ||
const [toToken, setToToken] = useState<Token>(); | ||
|
||
const handleToAmountChange = useCallback( | ||
async (amount: string) => { | ||
setToAmount(amount); | ||
const hasRequiredFields = fromToken && toToken && amount; | ||
if (!hasRequiredFields) { | ||
return; | ||
} | ||
try { | ||
const response = await getSwapQuote({ | ||
from: fromToken, | ||
to: toToken, | ||
amount, | ||
amountReference: 'to', | ||
}); | ||
if (isSwapError(response)) { | ||
onError?.(response); | ||
return; | ||
} | ||
setFromAmount(response?.fromAmount); | ||
} catch (error) { | ||
onError?.(error as SwapError); | ||
} | ||
}, | ||
[fromToken, toToken, setFromAmount, setToAmount], | ||
); | ||
|
||
const handleFromAmountChange = useCallback( | ||
async (amount: string) => { | ||
setFromAmount(amount); | ||
const hasRequiredFields = fromToken && toToken && amount; | ||
if (!hasRequiredFields) { | ||
return; | ||
} | ||
try { | ||
const response = await getSwapQuote({ | ||
from: fromToken, | ||
to: toToken, | ||
amount, | ||
amountReference: 'from', | ||
}); | ||
if (isSwapError(response)) { | ||
onError?.(response); | ||
return; | ||
} | ||
setToAmount(response?.toAmount); | ||
} catch (error) { | ||
onError?.(error as SwapError); | ||
} | ||
}, | ||
[fromToken, toToken, setFromAmount, setToAmount], | ||
); | ||
|
||
const value = useMemo(() => { | ||
return { | ||
account, | ||
fromAmount, | ||
fromToken, | ||
setFromAmount: handleFromAmountChange, | ||
setFromToken, | ||
setToAmount: handleToAmountChange, | ||
setToToken, | ||
toAmount, | ||
toToken, | ||
}; | ||
}, [ | ||
account, | ||
fromAmount, | ||
fromToken, | ||
handleFromAmountChange, | ||
handleToAmountChange, | ||
setFromToken, | ||
setToAmount, | ||
setToToken, | ||
toAmount, | ||
toToken, | ||
]); | ||
|
||
return ( | ||
<SwapContext.Provider value={value}> | ||
<div className="flex w-[400px] flex-col rounded-xl bg-white"> | ||
<label | ||
className={cn( | ||
'box-border w-full border-b border-solid p-4 text-base', | ||
'font-semibold leading-6 text-[#030712]', | ||
)} | ||
> | ||
Swap | ||
</label> | ||
{children} | ||
</div> | ||
</SwapContext.Provider> | ||
); | ||
} |
Oops, something went wrong.