Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

434: Finalize uniswap widget #438

Merged
merged 8 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 42 additions & 33 deletions src/pages/gd/Swap/SwapCelo/UniSwap.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import React, { useCallback } from 'react'

import useActiveWeb3React from 'hooks/useActiveWeb3React'
import { useApplicationTheme } from 'state/application/hooks'
import { darkTheme, lightTheme, OnTxFail, OnTxSubmit, OnTxSuccess, SwapWidget, TokenInfo } from '@uniswap/widgets'
import { Currency, CurrencyAmount } from '@uniswap/sdk-core'
import { useConnectWallet } from '@web3-onboard/react'
import { AsyncStorage, getDevice, G$ContractAddresses, useGetEnvChainId, useWeb3Context } from '@gooddollar/web3sdk-v2'
import {
AsyncStorage,
getDevice,
G$ContractAddresses,
useGetEnvChainId,
useWeb3Context,
SupportedChains,
} from '@gooddollar/web3sdk-v2'
import { useDispatch } from 'react-redux'
import { addTransaction } from 'state/transactions/actions'
import { ChainId } from '@sushiswap/sdk'
import { isMobile } from 'react-device-detect'

import useActiveWeb3React from 'hooks/useActiveWeb3React'
import { useApplicationTheme } from 'state/application/hooks'
import useSendAnalytics from 'hooks/useSendAnalyticsData'

const jsonRpcUrlMap = {
122: ['https://rpc.fuse.io', 'https://fuse-rpc.gateway.pokt.network'],
Expand Down Expand Up @@ -46,12 +56,15 @@ const celoTokenList: TokenInfo[] = [
export const UniSwap = (): JSX.Element => {
const [theme] = useApplicationTheme()
const uniTheme = theme === 'dark' ? darkTheme : lightTheme
const { account } = useActiveWeb3React()
const { web3Provider } = useWeb3Context()
const { account, chainId } = useActiveWeb3React()
const network = SupportedChains[chainId]
const [, connect] = useConnectWallet()
const globalDispatch = useDispatch()
const sendData = useSendAnalytics()
const { connectedEnv } = useGetEnvChainId(42220)
const gdTokenAddress = G$ContractAddresses('GoodDollar', connectedEnv) as string

const customTheme = {
...uniTheme,
primary: '#404040',
Expand Down Expand Up @@ -97,7 +110,7 @@ export const UniSwap = (): JSX.Element => {
}, [connect])

const handleError = useCallback(async (e) => {
console.log('handleError -->', { e })
sendData({ event: 'swap', action: 'swap_failed', error: e })
L03TJ3 marked this conversation as resolved.
Show resolved Hide resolved
}, [])

const handleTxFailed: OnTxFail = useCallback(async (error: string, data: any) => {
Expand All @@ -113,6 +126,8 @@ export const UniSwap = (): JSX.Element => {
const { tokenAddress } = info
const symbol = tokenSymbols[tokenAddress]
const summary = symbol ? `Approved spending of ${symbol}` : 'Approved spending'
const type = symbol ? 'sell' : 'buy'
sendData({ event: 'swap', action: 'swap_approve', type, network })
sirpy marked this conversation as resolved.
Show resolved Hide resolved
globalDispatch(
addTransaction({
chainId: 42220 as ChainId,
Expand Down Expand Up @@ -145,6 +160,17 @@ export const UniSwap = (): JSX.Element => {
const swappedAmount = inputAmount.toSignificant(6)
const receivedAmount = outputAmount.toSignificant(6)
const summary = `Swapped ${swappedAmount} ${input.symbol} to ${receivedAmount} ${output.symbol}`
const type = input.symbol === 'G$' ? 'sell' : 'buy'

sendData({
event: 'swap',
action: 'swap_confirm',
amount: type === 'buy' ? receivedAmount : swappedAmount,
tokens: [input.symbol, output.symbol],
type,
network,
})

globalDispatch(
addTransaction({
chainId: 42220 as ChainId,
Expand All @@ -158,40 +184,22 @@ export const UniSwap = (): JSX.Element => {
}
}
},
[account]
[account, network]
)

const handleTxSuccess: OnTxSuccess = useCallback(async (txHash: string, data: any) => {
console.log('handleTxSuccess -->', { txHash, data })
//todo: potentially showing share modal (as we do on other chains when buying G$)
// <ShareTransaction
// title={i18n._(t`Swap Completed`)}
// text={i18n._(
// t`You just used your crypto for good to help fund crypto UBI for all with GoodDollar!`
// )}
// shareProps={{
// title: i18n._(t`Share with friends`),
// copyText: 'I just bought GoodDollars at https://goodswap.xyz to make the world better',
// show: true,
// linkedin: {
// url: 'https://gooddollar.org',
// },
// twitter: {
// url: 'https://gooddollar.org',
// hashtags: ['InvestForGood'],
// },
// facebook: {
// url: 'https://gooddollar.org',
// hashtag: '#InvestForGood',
// },
// }}
// />
}, [])
const handleTxSuccess: OnTxSuccess = useCallback(
async (txHash: string, data: any) => {
const { inputAmount } = data.info.trade.swaps[0]
const type = inputAmount.currency.symbol === 'G$' ? 'sell' : 'buy'
sendData({ event: 'swap', action: 'swap_success', type, network })
},
[network]
)

return (
<div>
<SwapWidget
width="550px"
width={isMobile ? 'auto' : '550px'}
tokenList={celoTokenList}
defaultInputTokenAddress={gdTokenAddress}
permit2={true}
Expand All @@ -203,6 +211,7 @@ export const UniSwap = (): JSX.Element => {
onTxFail={handleTxFailed}
onTxSubmit={handleTxSubmit}
onTxSuccess={handleTxSuccess}
dialogOptions={{ pageCentered: !!isMobile }}
/>
</div>
)
Expand Down
2 changes: 1 addition & 1 deletion src/state/transactions/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function useAllTransactions(): { [txHash: string]: TransactionDetails } {

const state = useSelector<AppState, AppState['transactions']>((state) => state.transactions)
const filterTransactions = {}
console.log('useAllTransactions -->', { state })

if (chainId && state[chainId]) {
Object.values(state[chainId])?.forEach((tx) => {
if (tx.from === account) {
Expand Down