|
| 1 | +import { ENVIRONMENT, ENVIRONMENT_VARIABLES } from '@/lib/constants'; |
| 2 | +import { type LifecycleStatus, FundSwap } from '@coinbase/onchainkit/swap'; |
| 3 | +import type { SwapError } from '@coinbase/onchainkit/swap'; |
| 4 | +import type { Token } from '@coinbase/onchainkit/token'; |
| 5 | +import { useCallback, useContext } from 'react'; |
| 6 | +import type { TransactionReceipt } from 'viem'; |
| 7 | +import { base } from 'viem/chains'; |
| 8 | +import { AppContext } from '../AppProvider'; |
| 9 | + |
| 10 | +const FALLBACK_DEFAULT_MAX_SLIPPAGE = 3; |
| 11 | + |
| 12 | +function FundSwapComponent() { |
| 13 | + const { chainId, isSponsored, defaultMaxSlippage } = useContext(AppContext); |
| 14 | + |
| 15 | + const degenToken: Token = { |
| 16 | + name: 'DEGEN', |
| 17 | + address: '0x4ed4e862860bed51a9570b96d89af5e1b0efefed', |
| 18 | + symbol: 'DEGEN', |
| 19 | + decimals: 18, |
| 20 | + image: |
| 21 | + 'https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/3b/bf/3bbf118b5e6dc2f9e7fc607a6e7526647b4ba8f0bea87125f971446d57b296d2-MDNmNjY0MmEtNGFiZi00N2I0LWIwMTItMDUyMzg2ZDZhMWNm', |
| 22 | + chainId: base.id, |
| 23 | + }; |
| 24 | + |
| 25 | + const ethToken: Token = { |
| 26 | + name: 'ETH', |
| 27 | + address: '', |
| 28 | + symbol: 'ETH', |
| 29 | + decimals: 18, |
| 30 | + image: |
| 31 | + 'https://wallet-api-production.s3.amazonaws.com/uploads/tokens/eth_288.png', |
| 32 | + chainId: base.id, |
| 33 | + }; |
| 34 | + |
| 35 | + const usdcToken: Token = { |
| 36 | + name: 'USDC', |
| 37 | + address: '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913', |
| 38 | + symbol: 'USDC', |
| 39 | + decimals: 6, |
| 40 | + image: |
| 41 | + 'https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/44/2b/442b80bd16af0c0d9b22e03a16753823fe826e5bfd457292b55fa0ba8c1ba213-ZWUzYjJmZGUtMDYxNy00NDcyLTg0NjQtMWI4OGEwYjBiODE2', |
| 42 | + chainId: base.id, |
| 43 | + }; |
| 44 | + |
| 45 | + const wethToken: Token = { |
| 46 | + name: 'Wrapped Ether', |
| 47 | + address: '0x4200000000000000000000000000000000000006', |
| 48 | + symbol: 'WETH', |
| 49 | + decimals: 6, |
| 50 | + image: |
| 51 | + 'https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/47/bc/47bc3593c2dec7c846b66b7ba5f6fa6bd69ec34f8ebb931f2a43072e5aaac7a8-YmUwNmRjZDUtMjczYy00NDFiLWJhZDUtMzgwNjFmYWM0Njkx', |
| 52 | + chainId: base.id, |
| 53 | + }; |
| 54 | + |
| 55 | + const swappableTokens = [degenToken, ethToken, usdcToken, wethToken]; |
| 56 | + |
| 57 | + const handleOnStatus = useCallback((lifecycleStatus: LifecycleStatus) => { |
| 58 | + console.log('Status:', lifecycleStatus); |
| 59 | + }, []); |
| 60 | + |
| 61 | + const handleOnSuccess = useCallback( |
| 62 | + (transactionReceipt: TransactionReceipt) => { |
| 63 | + console.log('Success:', transactionReceipt); |
| 64 | + }, |
| 65 | + [], |
| 66 | + ); |
| 67 | + |
| 68 | + const handleOnError = useCallback((swapError: SwapError) => { |
| 69 | + console.log('Error:', swapError); |
| 70 | + }, []); |
| 71 | + |
| 72 | + return ( |
| 73 | + <div className="relative flex h-full w-full flex-col items-center"> |
| 74 | + {chainId !== base.id ? ( |
| 75 | + <div className="absolute top-0 left-0 z-10 flex h-full w-full flex-col justify-center rounded-xl bg-[#000000] bg-opacity-50 text-center"> |
| 76 | + <div className="mx-auto w-2/3 rounded-md bg-muted p-6 text-sm"> |
| 77 | + Swap Demo is only available on Base. |
| 78 | + <br /> |
| 79 | + You're connected to a different network. Switch to Base to continue |
| 80 | + using the app. |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + ) : ( |
| 84 | + <></> |
| 85 | + )} |
| 86 | + |
| 87 | + {ENVIRONMENT_VARIABLES[ENVIRONMENT.ENVIRONMENT] === 'production' && |
| 88 | + chainId === base.id ? ( |
| 89 | + <div className="mb-5 italic"> |
| 90 | + Note: Swap is disabled on production. To test, run the app locally. |
| 91 | + </div> |
| 92 | + ) : null} |
| 93 | + |
| 94 | + <FundSwap |
| 95 | + className="w-full" |
| 96 | + onStatus={handleOnStatus} |
| 97 | + onSuccess={handleOnSuccess} |
| 98 | + onError={handleOnError} |
| 99 | + config={{ |
| 100 | + maxSlippage: defaultMaxSlippage || FALLBACK_DEFAULT_MAX_SLIPPAGE, |
| 101 | + }} |
| 102 | + isSponsored={isSponsored} |
| 103 | + toToken={degenToken} |
| 104 | + /> |
| 105 | + </div> |
| 106 | + ); |
| 107 | +} |
| 108 | + |
| 109 | +export default function FundSwapDemo() { |
| 110 | + return ( |
| 111 | + <div className="mx-auto"> |
| 112 | + <FundSwapComponent /> |
| 113 | + </div> |
| 114 | + ); |
| 115 | +} |
0 commit comments