Skip to content

Commit

Permalink
continue refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
abcrane123 committed Jun 12, 2024
1 parent 5cd8a44 commit fbafe15
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 40 deletions.
23 changes: 2 additions & 21 deletions site/docs/pages/swap/swap.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The `Swap` component is a comprehensive interface for users to execute token swa
<Swap>
<SwapAmountInputV2 type="from" label="Sell" token={fromToken} />
<SwapAmountInputV2 type="to" label="Buy" token={toToken} />
<SwapButton onSubmit={onSubmit} fromToken={fromToken} toToken={toToken} />
<SwapButton onSubmit={onSubmit} />
</Swap>
```

Expand Down Expand Up @@ -51,26 +51,7 @@ The `Swap` component is a comprehensive interface for users to execute token swa
chainId: 8453,
}}
/>
<SwapButton
onSubmit={() => {}}
fromToken={{
name: 'Ethereum',
address: '',
symbol: 'ETH',
decimals: 18,
image: 'https://wallet-api-production.s3.amazonaws.com/uploads/tokens/eth_288.png',
chainId: 8453,
}}
toToken={{
name: 'Dai',
address: '0x50c5725949a6f0c72e6c4a641f24049a917db0cb',
symbol: 'DAI',
decimals: 18,
image:
'https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/d0/d7/d0d7784975771dbbac9a22c8c0c12928cc6f658cbcf2bbbf7c909f0fa2426dec-NmU4ZWViMDItOTQyYy00Yjk5LTkzODUtNGJlZmJiMTUxOTgy',
chainId: 8453,
}}
/>
<SwapButton />
</Swap>
</App>

Expand Down
26 changes: 21 additions & 5 deletions src/swap/components/Swap.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
import { useMemo, useState } from 'react';
import type { SwapReact } from '../types';
import { useCallback, useMemo, useState } from 'react';
import type { SwapParams, SwapReact } from '../types';
import { SwapContext } from '../context';

export function Swap({ account, children }: SwapReact) {
export function Swap({ account, children, fromToken, toToken }: SwapReact) {
const [fromAmount, setFromAmount] = useState('');
const [toAmount, setToAmount] = useState('');

const submitSwapTx = (params?: SwapParams) => {
// TODO: implement hook
};

const handleSubmit = useCallback(() => {
if (account && fromToken && toToken && fromAmount) {
submitSwapTx({
amount: fromAmount,
fromAddress: account.address,
from: fromToken,
to: toToken,
});
}
}, []);

const value = useMemo(() => {
return {
account,
fromAmount,
onSubmit: handleSubmit,
setFromAmount,
toAmount,
setToAmount,
account,
toAmount,
};
}, [account, fromAmount, setFromAmount, setToAmount, toAmount]);

Expand Down
4 changes: 2 additions & 2 deletions src/swap/components/SwapAmountInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export function SwapAmountInput({
const handleAmountChange = useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
if (isValidAmount(event.target.value)) {
setAmount(event.target.value);
setAmount?.(event.target.value);
}
},
[setAmount],
);

const handleMaxButtonClick = useCallback(() => {
if (tokenBalance && isValidAmount(tokenBalance)) {
setAmount(tokenBalance);
setAmount?.(tokenBalance);
}
}, [tokenBalance, setAmount]);

Expand Down
12 changes: 6 additions & 6 deletions src/swap/components/SwapButton.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { useCallback, useContext } from 'react';
import { SwapContext } from '../context';
import type { SwapButtonReact } from '../types';
import { SwapButtonReact } from '../types';

export function SwapButton({ fromToken, onSubmit, toToken }: SwapButtonReact) {
const { account, fromAmount } = useContext(SwapContext);
export function SwapButton({ onSubmit }: SwapButtonReact) {
const { onSubmit: onSubmitSwap } = useContext(SwapContext);

const handleSubmit = useCallback(() => {
if (account && fromToken && toToken && fromAmount) {
onSubmit({ fromAddress: account.address, from: fromToken, to: toToken, amount: fromAmount });
}
onSubmitSwap();
onSubmit?.();
}, []);

return (
<div className="w-full p-4">
<button
Expand Down
13 changes: 7 additions & 6 deletions src/swap/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,25 +148,26 @@ export type TransactionParams = {
export type SwapReact = {
account?: Account;
children: ReactNode;
fromToken?: Token;
toToken?: Token;
};

export type SwapContextType = {
account?: Account;
fromAmount: string;
onSubmit: () => void;
setFromAmount: (a: string) => void;
toAmount: string;
setToAmount: (a: string) => void;
account?: Account;
toAmount: string;
};

export type SwapParams = {
amount: string;
fromAddress: `0x${string}`;
from: Token;
to: Token;
amount: string;
};

export type SwapButtonReact = {
onSubmit: (params?: SwapParams) => void;
fromToken: Token;
toToken: Token;
onSubmit?: (params?: SwapParams) => void;
};

0 comments on commit fbafe15

Please sign in to comment.