Skip to content

Commit

Permalink
Add kima bridge (#381)
Browse files Browse the repository at this point in the history
* adding kima-transaction-widget to `Bridge` page

* fix webpack config, apply plugins on external loader

* update kima-widet integration

* update npm access token

* update npm access token

* update npm access token

* add backend url

* upgrade widget version

* * remove unused code
* update backend url

* success, error handlers as callback

* * remove npm access token
* refactor Kima component's props

* small fix

* add sendData

* Update src/pages/gd/Bridge/index.tsx

---------

Co-authored-by: Lewis B <[email protected]>
Co-authored-by: sirpy <[email protected]>
  • Loading branch information
3 people authored Mar 15, 2023
1 parent d350332 commit 097c5ee
Show file tree
Hide file tree
Showing 12 changed files with 2,366 additions and 410 deletions.
4 changes: 2 additions & 2 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
nodeLinker: node-modules

plugins:
- path: .yarn/plugins/@yarnpkg/plugin-postinstall.cjs
spec: 'https://raw.githubusercontent.com/gravitywelluk/yarn-plugin-postinstall/master/bundles/%40yarnpkg/plugin-postinstall.js'
- path: .yarn/plugins/@yarnpkg/plugin-postinstall.cjs
spec: "https://raw.githubusercontent.com/gravitywelluk/yarn-plugin-postinstall/master/bundles/%40yarnpkg/plugin-postinstall.js"

yarnPath: .yarn/releases/yarn-3.2.3.cjs
6 changes: 6 additions & 0 deletions config-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ const {
override,
addBabelPresets,
addWebpackAlias,
addWebpackModuleRule,
} = require('customize-cra')
const path = require('path')

module.exports = override(
addWebpackModuleRule({
test: /\.mjs$/,
include: /node_modules/,
type: 'javascript/auto',
}),
addPostcssPlugins([require('tailwindcss'), require('postcss-preset-env')({ stage: 1 })]),
...addExternalBabelPlugins(
'babel-plugin-react-native-web',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@
"@gooddollar/web3sdk": "^0.1.20",
"@gooddollar/web3sdk-v2": "^0.1.85",
"@headlessui/react": "1.5.0",
"@kimafinance/kima-transaction-widget": "^1.1.20-beta.1",
"@types/react-text-mask": "^5.4.8",
"@types/text-mask-addons": "^3.8.1",
"@uniswap/sdk-core": "^3.0.0",
Expand Down
5 changes: 3 additions & 2 deletions src/components/NetworkModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default function NetworkModal(): JSX.Element | null {

const networkLabel: string | null = error ? null : (NETWORK_LABEL as any)[chainId]
const network = getNetworkEnv()
// eslint-disable-next-line react-hooks/exhaustive-deps
const prodNetworks = process.env.REACT_APP_CELO_PHASE_1
? [AdditionalChainId.CELO, ChainId.MAINNET, AdditionalChainId.FUSE]
: [ChainId.MAINNET, AdditionalChainId.FUSE]
Expand All @@ -79,7 +80,7 @@ export default function NetworkModal(): JSX.Element | null {
default:
return prodNetworks
}
}, [error, network])
}, [network, prodNetworks])

const closeNetworkModal = useCallback(() => {
setToAddNetwork(undefined)
Expand All @@ -102,7 +103,7 @@ export default function NetworkModal(): JSX.Element | null {
network: ChainId[chain],
})
},
[switchNetwork]
[switchNetwork, sendData, toggleNetworkModal]
)

return (
Expand Down
9 changes: 7 additions & 2 deletions src/components/SideBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export default function SideBar({ mobile, closeSidebar }: { mobile?: boolean; cl
show: true,
},
],
[i18n, process.env]
[i18n]
)

const internalLinks = useMemo(
Expand All @@ -183,6 +183,11 @@ export default function SideBar({ mobile, closeSidebar }: { mobile?: boolean; cl
text: 'Portfolio',
show: true,
},
{
route: '/bridge',
text: 'Bridge',
show: true,
},
{
route: '/microbridge',
text: 'Micro Bridge',
Expand All @@ -194,7 +199,7 @@ export default function SideBar({ mobile, closeSidebar }: { mobile?: boolean; cl
show: true,
},
],
[i18n, process.env]
[]
)

return (
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useSendAnalyticsData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface IAnalyticsData {
tokens?: [string | undefined, string | undefined]
network?: string
token?: string
error?: string
}

function useSendAnalyticsData(): (data: IAnalyticsData) => void {
Expand Down
4 changes: 2 additions & 2 deletions src/pages/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function App(): JSX.Element {

useEffect(() => {
sendData({ event: 'goto_page', action: `goto_${pathname}` })
}, [pathname])
}, [pathname, sendData])

useEffect(() => {
const parsed = parse(search, { parseArrays: false, ignoreQueryPrefix: true })
Expand All @@ -87,7 +87,7 @@ function App(): JSX.Element {
if (bodyRef.current) {
bodyRef.current.scrollTo(0, 0)
}
}, [pathname])
}, [/* used */ pathname])

useEffect(() => {
if (!search) return
Expand Down
54 changes: 54 additions & 0 deletions src/pages/gd/Bridge/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import React, { memo, useCallback, useMemo } from 'react'
import useActiveWeb3React from 'hooks/useActiveWeb3React'
import { useApplicationTheme } from 'state/application/hooks'
import {
KimaTransactionWidget,
KimaProvider,
FontSizeOptions,
ColorModeOptions,
ModeOptions,
DAppOptions,
} from '@kimafinance/kima-transaction-widget'
import '@kimafinance/kima-transaction-widget/dist/index.css'
import useSendAnalyticsData from 'hooks/useSendAnalyticsData'

const Bridge = memo(() => {
const { library } = useActiveWeb3React()
const [theme] = useApplicationTheme()
const sendData = useSendAnalyticsData()

const successHandler = useCallback(() => {
sendData({ event: 'kima_bridge', action: 'bridge_success' })
}, [])

const errorHandler = useCallback((e) => {
console.log('Kima bridge error:', e?.message, e)
sendData({ event: 'kima_bridge', action: 'bridge_failure', error: e?.message })
}, [])

const options = useMemo(
() => ({
theme: {
colorMode: theme === 'light' ? ColorModeOptions.light : ColorModeOptions.dark,
fontSize: FontSizeOptions.medium,
fontFamily: 'Roboto',
backgroundColorDark: 'rgb(21, 26, 48)',
},
mode: ModeOptions.bridge,
dAppOption: DAppOptions.G$,
kimaBackendUrl: 'https://gooddollar-beta.kima.finance',
kimaNodeProviderQuery: 'https://api_testnet.kima.finance',
provider: library,
compliantOption: false,
}),
[theme, library]
)

return (
<KimaProvider>
<KimaTransactionWidget {...options} successHandler={successHandler} errorHandler={errorHandler} />
</KimaProvider>
)
})

export default Bridge
2 changes: 1 addition & 1 deletion src/pages/gd/Claim/ClaimBalance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const ClaimBalance = ({ refresh }: { refresh: QueryParams['refresh'] }) =
toggleNetworkModal()
}
})
}, [switchNetwork, claimedAlt])
}, [switchNetwork, altChain, toggleNetworkModal])

return (
<View
Expand Down
15 changes: 8 additions & 7 deletions src/pages/gd/Swap/SwapCore/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const SwapCore = memo(() => {
token: network === 'FUSE' ? FUSE : ETHER,
value: '',
})
}, [chainId]) // on first render chainId is undefined
}, [/*used */ chainId, network]) // on first render chainId is undefined

const handleSetPair = useCallback(
(value: Partial<SwapVariant>) =>
Expand All @@ -65,7 +65,7 @@ const SwapCore = memo(() => {
})),
[]
)
const handleSetPairValue = useCallback((value: string) => handleSetPair({ value }), [])
const handleSetPairValue = useCallback((value: string) => handleSetPair({ value }), [handleSetPair])
const handleBuyingValue = useCallback((value) => setBuying(!value), [])

const tokenList = useTokens()
Expand Down Expand Up @@ -126,7 +126,7 @@ const SwapCore = memo(() => {

buying && field === 'external' ? setCalcExternal(false) : setCalcInternal(false)
}, 400))
}, [account, chainId, lastEdited, buying, web3, slippageTolerance.value])
}, [account, chainId, lastEdited, buying, web3, slippageTolerance.value, swapPair, swapValue, handleSetPairValue])
const [approving, setApproving] = useState(false)
const [showConfirm, setShowConfirm] = useState(false)
const [approved, setApproved] = useState(false)
Expand All @@ -145,13 +145,14 @@ const SwapCore = memo(() => {
}
}

// eslint-disable-next-line react-hooks-addons/no-unused-deps
useEffect(() => setApproved(false), [swapValue, swapPair, buying])

const balanceNotEnough = useMemo(
() =>
BigInt(meta?.inputAmount.multiply(meta?.inputAmount.decimalScale).toFixed(0) ?? '0') >
BigInt(buying ? pairBalance?.raw.toString() ?? '0' : swapBalance?.raw.toString() ?? '0'),
[meta?.inputAmount, pairBalance]
[meta?.inputAmount, pairBalance, buying, swapBalance?.raw]
)

const route = useMemo(() => {
Expand All @@ -173,7 +174,7 @@ const SwapCore = memo(() => {
} else {
return route.startsWith('cDAI') ? `${G$?.symbol} > ${route}` : `${G$?.symbol} > cDAI > ${route}`
}
}, [meta?.route, buying, chainId])
}, [meta?.route, buying, chainId, G$?.symbol])

const isFuse = SupportedChainId[Number(chainId)] === 'FUSE'

Expand Down Expand Up @@ -290,14 +291,14 @@ const SwapCore = memo(() => {
network,
})
setShowConfirm(true)
}, [sendData, setShowConfirm, buying])
}, [sendData, setShowConfirm, buying, network])

const onSwapConfirmed = useCallback(async () => {
handleSetPairValue('')
setSwapValue('')
setMeta(undefined)
sendData({ event: 'swap', action: 'swap_success', type: buying ? 'buy' : 'sell', network })
}, [handleSetPairValue, setSwapValue, setMeta])
}, [handleSetPairValue, setSwapValue, setMeta, buying, network, sendData])

return (
<SwapContext.Provider
Expand Down
2 changes: 2 additions & 0 deletions src/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { RedirectHashRoutes, RedirectPathToSwapOnly } from 'pages/routes/redirec
import Portfolio from 'pages/gd/Portfolio'
import MicroBridge from 'pages/gd/MicroBridge'
import Claim from 'pages/gd/Claim'
import Bridge from 'pages/gd/Bridge'
import useActiveWeb3React from 'hooks/useActiveWeb3React'

function Routes(): JSX.Element {
Expand All @@ -18,6 +19,7 @@ function Routes(): JSX.Element {
<Route exact strict path="/swap" component={Swap} key={chainId} />
<Route exact strict path="/stakes" component={Stakes} />
<Route exact strict path="/portfolio" component={Portfolio} />
<Route exact strict path="/bridge" component={Bridge} />
{process.env.REACT_APP_CELO_PHASE_2 && <Route exact strict path="/claim" component={Claim} />}
{process.env.REACT_APP_CELO_PHASE_3 && <Route exact strict path="/microbridge" component={MicroBridge} />}
<Route exact strict path="/" component={RedirectHashRoutes} />
Expand Down
Loading

0 comments on commit 097c5ee

Please sign in to comment.