-
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.
- Loading branch information
Showing
36 changed files
with
2,984 additions
and
132 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -7,4 +7,4 @@ dist | |
.turbo | ||
|
||
|
||
.next | ||
.next |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export * from './Sidebar'; | ||
export * from "./Sidebar" |
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,17 @@ | ||
'use client'; | ||
import { use3Wagmi, AddressFn } from '@/lib'; | ||
import { useBalance } from 'wagmi'; | ||
|
||
export function Balance(props: { tokenAddress?: `0x${string}` }) { | ||
const { address } = use3Wagmi(); | ||
|
||
const { isLoading, error, data } = useBalance({ | ||
address: address as `0x${string}`, | ||
token: props.tokenAddress, | ||
}); | ||
|
||
if (isLoading) return <p>...</p>; | ||
if (error) return <p>...</p>; | ||
|
||
return <>{AddressFn.shortValue(data.value!)}</>; | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './TokenSelector'; | ||
export * from './Balance'; |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
'use client'; | ||
'use client' | ||
import React from 'react'; | ||
|
||
export function ConfirmTransaction() { | ||
|
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,152 @@ | ||
'use client'; | ||
import { AppButton } from '@/comps'; | ||
import { Celo, ChainId, fromWei, fromWeiRounded, TokenAddresses, TokenFn, TokenId, Tokens } from '@/lib'; | ||
import React, { useEffect, useMemo } from 'react'; | ||
import { IoSwapVertical } from 'react-icons/io5'; | ||
import { ChangeSection } from './ValueSection'; | ||
import { useAccount } from 'wagmi'; | ||
import { useSwap } from './useSwap'; | ||
import { useSwapQuote } from './swap/useSwapQuote'; | ||
import { toast } from 'sonner'; | ||
|
||
export default function Swap() { | ||
const { selectedToken, update, exchangeValue, balances, ...store } = useSwap(); | ||
const { address, chainId, isConnected } = useAccount(); | ||
// const [fieldValue, setFieldValue] = useState(); | ||
const tokensForChain = useMemo(() => { | ||
return chainId ? TokenFn.getTokenOptionsByChainId(chainId) : TokenFn.getTokenOptionsByChainId(Celo.chainId); | ||
}, [chainId]); | ||
|
||
const swappableTokenOptions = useMemo(() => { | ||
return TokenFn.getSwappableTokenOptions(selectedToken.fromTokens.id, chainId ? chainId : Celo.chainId); | ||
}, [chainId, exchangeValue]); | ||
|
||
const { isLoading, quote, rate } = useSwapQuote( | ||
exchangeValue.fromToken, | ||
store.direction, | ||
selectedToken.fromTokens.id, | ||
selectedToken.toTokens.id | ||
); | ||
// useEffect(() => { | ||
// if (store.address === null || store.chainId === null) { | ||
// if (address && chainId) { | ||
// update({ | ||
// address, | ||
// chainId, | ||
// }); | ||
// } | ||
// } | ||
// }, []); | ||
|
||
useEffect(() => { | ||
// setFieldValue('quote', quote); | ||
update({ | ||
quote: store.quote, | ||
}); | ||
}, [store.quote]); | ||
|
||
useEffect(() => { | ||
if ( | ||
chainId && | ||
isConnected && | ||
!TokenFn.isSwappable(selectedToken.fromTokens.id, selectedToken.toTokens.id, chainId) | ||
) { | ||
update({ | ||
selectedToken: { | ||
...selectedToken, | ||
toTokens: TokenFn.getTokenById(swappableTokenOptions.length < 1 ? TokenId.cUSD : swappableTokenOptions[0])!, | ||
}, | ||
}); | ||
// setFieldValue('toTokenId', swappableTokenOptions.length < 1 ? TokenId.cUSD : swappableTokenOptions[0]); | ||
} | ||
}, [chainId, exchangeValue.fromToken, swappableTokenOptions, isConnected]); | ||
|
||
const roundedBalance = fromWeiRounded( | ||
balances[selectedToken.fromTokens.id], | ||
Tokens[selectedToken.fromTokens.id].decimals | ||
); | ||
const isRoundedBalanceGreaterThanZero = Boolean(Number.parseInt(roundedBalance) > 0); | ||
const onClickUseMax = () => { | ||
update({ | ||
amount: fromWei(balances[selectedToken.fromTokens.id], Tokens[selectedToken.fromTokens.id].decimals), | ||
}); | ||
|
||
if (selectedToken.fromTokens.id === TokenId.CELO) { | ||
toast.warning('Consider keeping some CELO for transaction fees'); | ||
} | ||
}; | ||
|
||
const onSubmit = () => { | ||
console.log('selected from', exchangeValue.fromToken.toString()); | ||
console.log('selected to', exchangeValue.toToken.toString()); | ||
update({ | ||
showConfirm: true, | ||
}); | ||
}; | ||
return ( | ||
<div> | ||
<div className="w-full relative bg-background"> | ||
<ChangeSection | ||
title={'You send'} | ||
balance={`4000 ${Tokens.CELO}`} | ||
// balance={`4000 ${selectedToken.fromTokens.symbol ?? Tokens.CELO}`} | ||
token={selectedToken.fromTokens} | ||
onTokenClick={() => { | ||
update({ | ||
showTokens: true, | ||
lastClicked: 'SEND', | ||
}); | ||
}} | ||
value={exchangeValue.fromToken.toString()} | ||
onChange={function (val: string): void { | ||
update({ | ||
exchangeValue: { | ||
toToken: parseInt(val) * 1.56, | ||
fromToken: parseInt(val), | ||
}, | ||
}); | ||
}} | ||
tokenAddress={TokenAddresses[chainId! as ChainId][selectedToken.fromTokens.id] as `0x${string}`} | ||
/> | ||
|
||
<div | ||
className="my-1 flex items-center justify-center absolute bottom-[40%] left-[45%] " | ||
onClick={() => { | ||
update({ | ||
selectedToken: { | ||
toTokens: selectedToken.fromTokens, | ||
fromTokens: selectedToken.toTokens, | ||
}, | ||
}); | ||
}} | ||
> | ||
<div className="bg-card p-2 rounded-lg border-4 border-background"> | ||
<IoSwapVertical size={24} className="text-primary" /> | ||
</div> | ||
</div> | ||
|
||
<ChangeSection | ||
title={'You receive'} | ||
balance={`4000 ${selectedToken.toTokens.symbol}`} | ||
token={selectedToken.toTokens} | ||
isReadOnly | ||
onTokenClick={() => { | ||
update({ | ||
showTokens: true, | ||
lastClicked: 'RECEIVE', | ||
}); | ||
}} | ||
value={exchangeValue.toToken.toString()} | ||
onChange={function (val: string): void {}} | ||
tokenAddress={TokenAddresses[chainId! as ChainId][selectedToken.toTokens.id] as `0x${string}`} | ||
/> | ||
</div> | ||
<div className='flex flex-col items-center justify-center w-full'> | ||
{!isLoading && rate ? `${rate} ${selectedToken.fromTokens.id} ~ 1 ${selectedToken.toTokens.id}` : '...'} | ||
<AppButton className="w-[75%]" onClick={onSubmit}> | ||
Continue | ||
</AppButton> | ||
</div> | ||
</div> | ||
); | ||
} |
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,91 @@ | ||
'use client'; | ||
import React, { useEffect, useState } from 'react'; | ||
// import { useApproveTransaction } from './swap/useApproveTransaction'; | ||
// import { useSwapTransaction } from './swap/useSwapTransaction'; | ||
// import { logger } from '@/utils'; | ||
// import { useAccount } from 'wagmi'; | ||
// import { useSwap } from './useSwap'; | ||
import { AppButton, TextH, TextP } from '@/comps'; | ||
|
||
export default function SwapConfirm() { | ||
// const { selectedToken, update, exchangeValue, balances, ...store } = useSwap(); | ||
// const { address, chainId, isConnected } = useAccount(); | ||
// const { sendApproveTx, isApproveTxSuccess, isApproveTxLoading } = useApproveTransaction( | ||
// chainId!, | ||
// selectedToken.fromTokens.id, | ||
// exchangeValue.fromToken.toString(), | ||
// address | ||
// ); | ||
|
||
const [isApproveConfirmed, setApproveConfirmed] = useState(false); | ||
|
||
// const { sendSwapTx, isSwapTxLoading, isSwapTxSuccess } = useSwapTransaction( | ||
// chainId, | ||
// fromTokenId, | ||
// toTokenId, | ||
// amountWei, | ||
// thresholdAmountWei, | ||
// direction, | ||
// address, | ||
// isApproveConfirmed | ||
// ); | ||
|
||
// const onSubmit = async () => { | ||
// if (!rate || !amountWei || !address || !isConnected) return; | ||
|
||
// if (!sendApproveTx || isApproveTxSuccess || isApproveTxLoading) { | ||
// logger.debug('Approve already started or finished, ignoring submit'); | ||
// return; | ||
// } | ||
|
||
// setIsModalOpen(true); | ||
|
||
// try { | ||
// logger.info('Sending approve tx'); | ||
// const approveResult = await sendApproveTx(); | ||
// const approveReceipt = await approveResult.wait(1); | ||
// toastToYourSuccess('Approve complete, starting swap', approveReceipt.transactionHash, chainId); | ||
// setApproveConfirmed(true); | ||
// logger.info(`Tx receipt received for approve: ${approveReceipt.transactionHash}`); | ||
// } catch (error) { | ||
// logger.error('Failed to approve token', error); | ||
// setIsModalOpen(false); | ||
// } | ||
// }; | ||
|
||
// // TODO find a way to have this trigger from the onSubmit | ||
// useEffect(() => { | ||
// if (isSwapTxLoading || isSwapTxSuccess || !isApproveTxSuccess || !sendSwapTx) return; | ||
// logger.info('Sending swap tx'); | ||
|
||
// sendSwapTx() | ||
// .then((swapResult) => swapResult.wait(1)) | ||
// .then((swapReceipt) => { | ||
// logger.info(`Tx receipt received for swap: ${swapReceipt.transactionHash}`); | ||
// toastToYourSuccess('Swap Complete!', swapReceipt.transactionHash, chainId); | ||
// // dispatch(setFormValues(null)); | ||
// }) | ||
// .catch((e) => { | ||
// logger.error('Swap failure:', e); | ||
// }) | ||
// .finally(() => setIsModalOpen(false)); | ||
// }, [isApproveTxSuccess, isSwapTxLoading, isSwapTxSuccess, sendSwapTx, chainId, dispatch]); | ||
|
||
// // const onClickBack = () => { | ||
// // dispatch(setFormValues(null)); | ||
// // }; | ||
|
||
// const onClickRefresh = () => { | ||
// // Note, rates automatically re-fetch regularly | ||
// refetch().catch((e) => logger.error('Failed to refetch quote:', e)); | ||
// }; | ||
|
||
return ( | ||
<div className="px-5 py-2 gap-y-2 w-full flex flex-col items-center space-y-3"> | ||
ConfirmUi | ||
<TextH>You are about to swap 0.03 CELO for 45 cUSD</TextH> | ||
<TextP>Rate 0.08/celo</TextP> | ||
<AppButton>Confirm Transaction</AppButton> | ||
</div> | ||
); | ||
} |
Oops, something went wrong.