Skip to content

Commit

Permalink
Merge pull request #148 from CudoVentures/CUDOS-2460-update-env-and-c…
Browse files Browse the repository at this point in the history
…lean-up

update env usage and code cleanup
  • Loading branch information
mlukanova authored May 15, 2023
2 parents 5e33286 + 2c4f57c commit 22205cc
Show file tree
Hide file tree
Showing 52 changed files with 255 additions and 602 deletions.
84 changes: 14 additions & 70 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,72 +1,16 @@
# LOCAL
VITE_APP_LOCAL_CAPTCHA_SITE_KEY=''
VITE_APP_LOCAL_FAUCET_URL=''
VITE_APP_LOCAL_FAUCET_ADDRESS=''
VITE_APP_LOCAL_BRIDGE_URL=''
VITE_APP_LOCAL_GRAPHQL_URL=''
VITE_APP_LOCAL_GRAPHQL_WS=''
VITE_APP_LOCAL_RPC=''
VITE_APP_LOCAL_API=''
VITE_APP_LOCAL_EXPLORER_URL=''
VITE_APP_LOCAL_STAKING_URL=''
VITE_APP_LOCAL_CHAIN_NAME=''
VITE_APP_LOCAL_CHAIN_ID=''

# DEV
VITE_APP_DEV_CAPTCHA_SITE_KEY=''
VITE_APP_DEV_FAUCET_URL=''
VITE_APP_DEV_FAUCET_ADDRESS=''
VITE_APP_DEV_BRIDGE_URL=''
VITE_APP_DEV_GRAPHQL_URL=''
VITE_APP_DEV_GRAPHQL_WS=''
VITE_APP_DEV_RPC=''
VITE_APP_DEV_API=''
VITE_APP_DEV_EXPLORER_URL=''
VITE_APP_DEV_STAKING_URL=''
VITE_APP_DEV_CHAIN_NAME=''
VITE_APP_DEV_CHAIN_ID=''

# PRIVATE
VITE_APP_PRIVATE_CAPTCHA_SITE_KEY=''
VITE_APP_PRIVATE_FAUCET_URL=''
VITE_APP_PRIVATE_FAUCET_ADDRESS=''
VITE_APP_PRIVATE_BRIDGE_URL=''
VITE_APP_PRIVATE_GRAPHQL_URL=''
VITE_APP_PRIVATE_GRAPHQL_WS=''
VITE_APP_PRIVATE_RPC=''
VITE_APP_PRIVATE_API=''
VITE_APP_PRIVATE_EXPLORER_URL=''
VITE_APP_PRIVATE_STAKING_URL=''
VITE_APP_PRIVATE_CHAIN_NAME=''
VITE_APP_PRIVATE_CHAIN_ID=''

# PUBLIC
VITE_APP_PUBLIC_CAPTCHA_SITE_KEY=''
VITE_APP_PUBLIC_FAUCET_URL=''
VITE_APP_PUBLIC_FAUCET_ADDRESS=''
VITE_APP_PUBLIC_BRIDGE_URL=''
VITE_APP_PUBLIC_GRAPHQL_UR=''
VITE_APP_PUBLIC_GRAPHQL_WS=''
VITE_APP_PUBLIC_RPC=''
VITE_APP_PUBLIC_API=''
VITE_APP_PUBLIC_EXPLORER_URL=''
VITE_APP_PUBLIC_STAKING_URL=''
VITE_APP_PUBLIC_CHAIN_NAME=''
VITE_APP_PUBLIC_CHAIN_ID=''

# MAINNET
VITE_APP_MAINNET_BRIDGE_URL=''
VITE_APP_MAINNET_GRAPHQL_URL=''
VITE_APP_MAINNET_GRAPHQL_WS=''
VITE_APP_MAINNET_RPC=''
VITE_APP_MAINNET_API=''
VITE_APP_MAINNET_EXPLORER_URL=''
VITE_APP_MAINNET_STAKING_URL=''
VITE_APP_MAINNET_CHAIN_NAME=''
VITE_APP_MAINNET_CHAIN_ID=''

# GENERAL SETTINGS
# APP SETTINGS
VITE_APP_DEPLOYMENT_VERSION='' #v0.0.1 format
VITE_NODE_ENV='' #Production or empty
VITE_APP_DEFAULT_NETWORK='' # LOCAL | PRIVATE | PUBLIC | MAINNET
VITE_APP_NODE_ENV='' #Production or empty
VITE_APP_GAS_PRICE=5000000000000
VITE_APP_CAPTCHA_SITE_KEY=''
VITE_APP_FAUCET_URL=''
VITE_APP_FAUCET_ADDRESS=''
VITE_APP_BRIDGE_URL=''
VITE_APP_GRAPHQL_URL=''
VITE_APP_GRAPHQL_WS=''
VITE_APP_RPC=''
VITE_APP_API=''
VITE_APP_STAKING_URL=''
VITE_APP_EXPLORER_URL=''
VITE_APP_CHAIN_NAME=''
VITE_APP_CHAIN_ID=''
8 changes: 8 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Edge",
"request": "launch",
"type": "pwa-msedge",
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder}",
"userDataDir": false
},
{
"type": "pwa-chrome",
"request": "launch",
Expand Down
60 changes: 18 additions & 42 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { useCallback, useEffect, useState } from 'react'
import { useCallback, useEffect } from 'react'
import { ThemeProvider } from '@mui/material/styles'
import { useDispatch, useSelector } from 'react-redux'
import { Box, CssBaseline } from '@mui/material'
import { ApolloClient, ApolloProvider, NormalizedCacheObject } from '@apollo/client'
import { ApolloProvider } from '@apollo/client'
import { Routes, Route, useLocation, Navigate } from 'react-router-dom'
import { updateUser } from 'store/profile'
import { updateUserTransactions } from 'store/userTransactions'
import NotificationPopup from 'components/NotificationPopup'
import CosmosNetworkConfig from 'ledgers/CosmosNetworkConfig'
import { switchLedgerType } from 'ledgers/utils'
import { connectUser } from './utils/projectUtils'
import { useApollo } from './graphql/client'
Expand All @@ -25,7 +24,7 @@ import theme from './theme'
import { RootState } from './store'

import '@fontsource/poppins'
import { ApolloLinks, defaultApolloLinks } from 'graphql/helpers'
import { defaultApolloLinks } from 'graphql/helpers'
import { CHAIN_DETAILS } from 'utils/constants'
import NetworkChangingLoading from 'components/NetworkChangeLoading'
import { networkLoadingStyles } from 'components/NetworkChangeLoading/styles'
Expand All @@ -35,22 +34,20 @@ const App = () => {
const location = useLocation()
const themeColor = useSelector((state: RootState) => state.settings.theme)
const newApolloClient = useApollo(null)
const [currentApolloClient, setCurrentApolloClient] = useState<ApolloClient<NormalizedCacheObject>>(
newApolloClient(defaultApolloLinks)
)
const isMainnet = CHAIN_DETAILS.CHAIN_ID === 'cudos-1'

const {
lastLoggedAddress,
chosenNetwork: currentNetwork,
connectedLedger,
loadingState
} = useSelector((state: RootState) => state.profile)

const dispatch = useDispatch()

const connectAccount = useCallback(async (chosenNetwork: string, walletName: SUPPORTED_WALLET) => {
const connectAccount = useCallback(async (walletName: SUPPORTED_WALLET) => {
try {

const { address } = await switchLedgerType(chosenNetwork!, walletName)
const { address } = await switchLedgerType(walletName)
if (address !== lastLoggedAddress || lastLoggedAddress === '') {
dispatch(
updateUserTransactions({
Expand All @@ -62,7 +59,7 @@ const App = () => {
)
}

const connectedUser = await connectUser(chosenNetwork, walletName)
const connectedUser = await connectUser(walletName)
dispatch(updateUser(connectedUser))

} catch (e) {
Expand All @@ -82,46 +79,27 @@ const App = () => {
})
)

await connectAccount(currentNetwork, SUPPORTED_WALLET.Keplr)
await connectAccount(SUPPORTED_WALLET.Keplr)
})

if (isExtensionEnabled(SUPPORTED_WALLET.Cosmostation)) {
window.cosmostation.cosmos.on('accountChanged', async () => {
await connectAccount(currentNetwork, SUPPORTED_WALLET.Cosmostation)
await connectAccount(SUPPORTED_WALLET.Cosmostation)
})
}

return () => {
window.removeEventListener('keplr_keystorechange', async () => {
await connectAccount(currentNetwork, SUPPORTED_WALLET.Keplr)
await connectAccount(SUPPORTED_WALLET.Keplr)
})
window.removeEventListener('accountChanged', async () => {
await connectAccount(currentNetwork, SUPPORTED_WALLET.Cosmostation)
await connectAccount(SUPPORTED_WALLET.Cosmostation)
})
}
}, [])


useEffect(() => {
dispatch(updateUser({ loadingState: true })
)
const newApolloLinks: ApolloLinks = {
uri: CHAIN_DETAILS.GRAPHQL_URL[currentNetwork! as keyof typeof CHAIN_DETAILS.GRAPHQL_URL],
url: CHAIN_DETAILS.GRAPHQL_WS[currentNetwork! as keyof typeof CHAIN_DETAILS.GRAPHQL_WS]
}

setCurrentApolloClient(newApolloClient(newApolloLinks))

if (connectedLedger) {
connectAccount(currentNetwork, connectedLedger)
}

setTimeout(() => { dispatch(updateUser({ loadingState: false })) }, 4000)

}, [currentNetwork])

return (
<ApolloProvider client={currentApolloClient!}>
<ApolloProvider client={newApolloClient(defaultApolloLinks)}>
<ThemeProvider theme={theme[themeColor!]}>
<CssBaseline />
{location.pathname !== '/' ? null : (
Expand Down Expand Up @@ -150,13 +128,11 @@ const App = () => {
<Route path=":proposalId" element={<ProposalDetails />} />
</Route>
</Route>
{
CHAIN_DETAILS.CHAIN_ID[currentNetwork! as keyof typeof CHAIN_DETAILS.CHAIN_ID]
=== CHAIN_DETAILS.CHAIN_ID.MAINNET ? null : (
<Route path="faucet">
<Route index element={<Faucet />} />
</Route>
)}
{isMainnet ? null : (
<Route path="faucet">
<Route index element={<Faucet />} />
</Route>
)}
<Route path="*" element={<Navigate to="/dashboard" />} />
</Routes>

Expand Down
3 changes: 1 addition & 2 deletions src/api/getAccountDelegations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import { CHAIN_DETAILS } from 'utils/constants'
import { AccountDelegationsDocument } from '../graphql/account_actions'

export const fetchDelegations = async (
chosenNetwork: string,
address: string,
signal?: AbortSignal
) => {
const delegationsArray: { address: string; amount: string }[] = []

try {
const { data } = await axios.post(
CHAIN_DETAILS.GRAPHQL_URL[chosenNetwork! as keyof typeof CHAIN_DETAILS.GRAPHQL_URL].toString(),
CHAIN_DETAILS.GRAPHQL_URL,
{
variables: { address },
query: AccountDelegationsDocument
Expand Down
3 changes: 1 addition & 2 deletions src/api/getAccountRedelegations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { CHAIN_DETAILS } from 'utils/constants'
import { AccountRedelegationsDocument } from '../graphql/account_actions'

export const fetchRedelegations = async (
chosenNetwork: string,
address: string,
signal?: AbortSignal
) => {
Expand All @@ -16,7 +15,7 @@ export const fetchRedelegations = async (

try {
const { data } = await axios.post(
CHAIN_DETAILS.GRAPHQL_URL[chosenNetwork! as keyof typeof CHAIN_DETAILS.GRAPHQL_URL].toString(),
CHAIN_DETAILS.GRAPHQL_URL,
{
variables: { address },
query: AccountRedelegationsDocument
Expand Down
3 changes: 1 addition & 2 deletions src/api/getAccountUndelegations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { CHAIN_DETAILS } from 'utils/constants'
import { AccountUndelegationsDocument } from '../graphql/account_actions'

export const fetchUndedelegations = async (
chosenNetwork: string,
address: string,
signal?: AbortSignal
) => {
Expand All @@ -15,7 +14,7 @@ export const fetchUndedelegations = async (

try {
const { data } = await axios.post(
CHAIN_DETAILS.GRAPHQL_URL[chosenNetwork! as keyof typeof CHAIN_DETAILS.GRAPHQL_URL].toString(),
CHAIN_DETAILS.GRAPHQL_URL,
{
variables: { address },
query: AccountUndelegationsDocument
Expand Down
4 changes: 2 additions & 2 deletions src/api/getRewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { formatBigNum } from '../utils/projectUtils'
import { AccountDelegationRewardsDocument } from '../graphql/account_actions'
import { CHAIN_DETAILS } from 'utils/constants'

export const fetchRewards = async (chosenNetwork: string, address: string, signal?: AbortSignal) => {
export const fetchRewards = async (address: string, signal?: AbortSignal) => {
const defaultReturnValue = new BigNumber(0)
const rewardArray: Array<BigNumber> = []
const validatorArray: { address: string; amount: string }[] = []
try {
const { data } = await axios.post(
CHAIN_DETAILS.GRAPHQL_URL[chosenNetwork! as keyof typeof CHAIN_DETAILS.GRAPHQL_URL].toString(),
CHAIN_DETAILS.GRAPHQL_URL,
{
variables: { address },
query: AccountDelegationRewardsDocument
Expand Down
3 changes: 1 addition & 2 deletions src/api/getUnbondingBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { formatBigNum } from 'utils/projectUtils'
import { AccountUnbondingBalanceDocument } from '../graphql/account_actions'

export const getUnbondingBalance = async (
chosenNetwork: string,
address: string,
signal?: AbortSignal
) => {
Expand All @@ -14,7 +13,7 @@ export const getUnbondingBalance = async (

try {
const { data } = await axios.post(
CHAIN_DETAILS.GRAPHQL_URL[chosenNetwork! as keyof typeof CHAIN_DETAILS.GRAPHQL_URL]?.toString(),
CHAIN_DETAILS.GRAPHQL_URL,
{
variables: { address },
query: AccountUnbondingBalanceDocument
Expand Down
17 changes: 5 additions & 12 deletions src/components/Dialog/components/DelegationModal/Delegation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ const Delegation: React.FC<DelegationProps> = ({ modalProps, handleModal }) => {
const [delegationAmount, setDelegationAmount] = useState<string>('')
const { validator, amount, fee } = modalProps

const { address, connectedLedger, chosenNetwork } = useSelector(
const { address, connectedLedger } = useSelector(
({ profile }: RootState) => profile
)
const dispatch = useDispatch()

useEffect(() => {
let isMounted = true
const loadBalance = async () => {
const client = await signingClient(chosenNetwork, connectedLedger!)
const client = await signingClient(connectedLedger!)

const walletBalance = await client.getBalance(
address,
Expand Down Expand Up @@ -104,7 +104,7 @@ const Delegation: React.FC<DelegationProps> = ({ modalProps, handleModal }) => {
value: msg
}

const client = await signingClient(chosenNetwork, connectedLedger!)
const client = await signingClient(connectedLedger!)

const gasUsed = await client.simulate(address, [msgAny], 'memo')

Expand Down Expand Up @@ -162,7 +162,6 @@ const Delegation: React.FC<DelegationProps> = ({ modalProps, handleModal }) => {

try {
const delegationResult = await delegate(
chosenNetwork,
address,
validator?.address || '',
amount || '',
Expand All @@ -176,13 +175,11 @@ const Delegation: React.FC<DelegationProps> = ({ modalProps, handleModal }) => {
txHash: delegationResult.transactionHash
})

const walletBalance = await getWalletBalance(chosenNetwork!, address)
const walletBalance = await getWalletBalance(address)
const { delegationsArray } = await fetchDelegations(
chosenNetwork!,
address
)
const stakedAmountBalance = await getStakedBalance(
chosenNetwork!,
address
)

Expand Down Expand Up @@ -256,11 +253,7 @@ const Delegation: React.FC<DelegationProps> = ({ modalProps, handleModal }) => {
fontWeight={700}
color="primary.main"
>
{
CHAIN_DETAILS.CHAIN_NAME[
chosenNetwork as keyof typeof CHAIN_DETAILS.CHAIN_NAME
]
}
{CHAIN_DETAILS.CHAIN_NAME}
</Typography>
</Box>
</Box>
Expand Down
Loading

0 comments on commit 22205cc

Please sign in to comment.