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

fix(partnerConfig): Use specified C-Chain/P-Chain address for each network #251

Merged
merged 2 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/@types/partners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface PartnerDataType {
}

export interface AttributesType {
cChainAddresses?: { Network: string; cAddress: string; id: number }[]
cChainAddress?: string
contactEmail?: string
companyName?: string
Expand All @@ -12,7 +13,7 @@ export interface AttributesType {
contactFirstname?: string
contactLastname?: string
contactPhone?: string
pChainAddresses?: string[]
pChainAddresses?: { Network: string; pAddress: string; id: number }[]
pChainAddress?: string
companyShortDescription?: string
companyLongDescription?: string
Expand Down
9 changes: 7 additions & 2 deletions src/components/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,14 @@ const Input = ({ ...rest }) => {
/>
{error && (
<Typography variant="caption" color="error">
{parseFloat(state.balance) < 100 || !state.balance || state.balance === '0'
{parseFloat(state.balance) < 100 ||
!state.balance ||
state.balance === '0' ||
parseFloat(maxBalance) < 100
? 'Balance must be at least 100'
: `Balance cannot exceed ${parseFloat(maxBalance) - 0.5}`}
: parseFloat(state.balance) < 100.5
? `Insufficient funds: Your wallet balance is too low to create a Camino Messenger account.`
: `Prefund Amount cannot exceed ${parseFloat(maxBalance) - 0.5}`}
</Typography>
)}
</>
Expand Down
17 changes: 13 additions & 4 deletions src/components/Partners/PartnerCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { useEffectOnce } from '../../hooks/useEffectOnce'
import useWallet from '../../hooks/useWallet'
import { selectValidators } from '../../redux/slices/app-config'
import { getActiveNetwork } from '../../redux/slices/network'
import PartnerBusinessFields from './PartnerBusinessFields'
import PartnerFlag from './PartnerFlag'
import PartnerLogo from './PartnerLogo'
Expand All @@ -23,10 +24,13 @@
const [isValidator, setIsValidator] = useState(false)
const validators = useAppSelector(selectValidators)
const { getAddress } = useWallet()
const activeNetwork = useAppSelector(getActiveNetwork)
const chackValidatorStatus = async (address: string) => {
if (!pChainAddress) setIsValidator(false)
let nodeID = await getRegisteredNode(getAddress(address))
setIsValidator(!!validators.find(v => v.nodeID === nodeID))
try {
if (!pChainAddress) setIsValidator(false)
let nodeID = await getRegisteredNode(getAddress(address))
setIsValidator(!!validators.find(v => v.nodeID === nodeID))
} catch (e) {}
}
const {
attributes: {
Expand All @@ -41,8 +45,13 @@
},
} = partner
useEffect(() => {
if (pChainAddress) chackValidatorStatus(pChainAddress)
if (pChainAddresses) {
let partnerAddresses = pChainAddresses.find(
elem => elem.Network.toLowerCase() === activeNetwork.name.toLowerCase(),
)
if (partnerAddresses) chackValidatorStatus(partnerAddresses.pAddress)
}
}, [partner, validators])

Check warning on line 54 in src/components/Partners/PartnerCard.tsx

View workflow job for this annotation

GitHub Actions / yarn-build

React Hook useEffect has missing dependencies: 'activeNetwork.name', 'chackValidatorStatus', and 'pChainAddresses'. Either include them or remove the dependency array

Check warning on line 54 in src/components/Partners/PartnerCard.tsx

View workflow job for this annotation

GitHub Actions / yarn-build

React Hook useEffect has missing dependencies: 'activeNetwork.name', 'chackValidatorStatus', and 'pChainAddresses'. Either include them or remove the dependency array
return (
<Box
onClick={onClick}
Expand Down
35 changes: 33 additions & 2 deletions src/components/Partners/PartnersFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ActionType, StatePartnersType, partnersActions } from '../../helpers/partnersReducer'

import { Box, Checkbox, FormControlLabel, Typography } from '@mui/material'
import { Box, Button, Checkbox, FormControlLabel, Typography } from '@mui/material'
import React from 'react'
import { REGISTER_PARTNER_URL } from '../../constants/route-paths'
import BusinessFieldFilter from './BusinessFieldFilter'
import SearchInput from './SearchInput'

Expand All @@ -14,7 +15,15 @@ const PartnersFilter: React.FC<PartnersFilterProps> = ({ state, dispatchPartners
const searchByName = param =>
dispatchPartnersActions({ type: partnersActions.UPDATE_COMPANY_NAME, payload: param })
return (
<Box sx={{ display: 'flex', gap: '1rem', alignItems: 'center', flexWrap: 'wrap' }}>
<Box
sx={{
display: 'flex',
gap: '1rem',
alignItems: 'center',
flexWrap: 'wrap',
justifyContent: 'flex-start',
}}
>
<SearchInput searchByName={searchByName} />
<BusinessFieldFilter state={state} dispatchPartnersActions={dispatchPartnersActions} />
<Box sx={{ display: 'flex', alignItems: 'center', gap: '12px' }}>
Expand Down Expand Up @@ -67,6 +76,28 @@ const PartnersFilter: React.FC<PartnersFilterProps> = ({ state, dispatchPartners
/>
</Box>
</Box>
<Box
sx={{
flex: '1',
display: 'flex',
justifyContent: 'flex-end',
alignItems: 'flex-start',
}}
>
<Button
variant="contained"
component="a"
href={REGISTER_PARTNER_URL}
target="_blank"
rel="noopener noreferrer"
sx={{
whiteSpace: 'nowrap',
minWidth: 'max-content',
}}
>
Register as Partner
</Button>
</Box>
</Box>
)
}
Expand Down
4 changes: 3 additions & 1 deletion src/constants/route-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ export const XCHAIN = BASE_PATH + '/x-chain'

export const PCHAIN = BASE_PATH + '/p-chain'

export const TERMS_OF_USE_URL = 'https://camino.network/terms/'
export const TERMS_OF_USE_URL = 'https://camino.network/terms/'

export const REGISTER_PARTNER_URL = 'https://9gp400vrnwo.typeform.com/supporter'
28 changes: 20 additions & 8 deletions src/layout/PartnersLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Box, Button, Link, Toolbar, Typography } from '@mui/material'

import { Paper } from '@mui/material'
import React, { useEffect } from 'react'
import React, { useEffect, useMemo } from 'react'
import { Navigate, Outlet, useNavigate, useParams } from 'react-router'
import store from 'wallet/store'
import { PartnerConfigurationProvider } from '../helpers/partnerConfigurationContext'
Expand Down Expand Up @@ -64,18 +64,24 @@ const ClaimProfile = () => {

const PartnersLayout = () => {
const path = window.location.pathname
const { data, isLoading } = useIsPartnerQuery({
const { data, isLoading, refetch } = useIsPartnerQuery({
cChainAddress: store?.state?.activeWallet?.ethAddress
? '0x' + store?.state?.activeWallet?.ethAddress
: '',
})
let { partnerID } = useParams()
const { data: partner } = useFetchPartnerDataQuery({
const { data: partner, refetch: refetchPartenrData } = useFetchPartnerDataQuery({
companyName: partnerID,
})
const walletName = useAppSelector(getWalletName)
const navigate = useNavigate()

const activeNetwork = useAppSelector(getActiveNetwork)
useEffect(() => {
if (activeNetwork) {
refetch()
refetchPartenrData()
}
}, [activeNetwork])
useEffect(() => {
if (
walletName &&
Expand All @@ -86,7 +92,13 @@ const PartnersLayout = () => {
}
}, [walletName])

const activeNetwork = useAppSelector(getActiveNetwork)
const partnerCChainAddress = useMemo(() => {
let cAddress = data?.attributes?.cChainAddresses.find(
elem => elem.Network === activeNetwork.name.toLowerCase(),
)
if (cAddress) return cAddress
return ''
}, [data])
if (isLoading) return <></>
if (
path.includes('partners/messenger-configuration') &&
Expand Down Expand Up @@ -129,7 +141,7 @@ const PartnersLayout = () => {
</Toolbar>
{((path.includes('partners/messenger-configuration') &&
!!data &&
data?.attributes?.cChainAddress) ||
partnerCChainAddress) ||
(partner &&
partner?.contractAddress &&
partnerID === partner.attributes.companyName)) && (
Expand Down Expand Up @@ -158,7 +170,7 @@ const PartnersLayout = () => {
mt:
(path.includes('partners/messenger-configuration') &&
!!data &&
data?.attributes?.cChainAddress) ||
partnerCChainAddress) ||
(path !== '/partners' && partner?.contractAddress)
? '9rem'
: '5rem',
Expand All @@ -173,7 +185,7 @@ const PartnersLayout = () => {
{!path.includes('partners/messenger-configuration') ||
(path.includes('partners/messenger-configuration') &&
!!data &&
data?.attributes?.cChainAddress) ? (
partnerCChainAddress) ? (
<Outlet />
) : (
<ClaimProfile />
Expand Down
50 changes: 33 additions & 17 deletions src/redux/services/partners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export const partnersApi = createApi({

let query = '?populate=*'

const selectedNetwork = store.getters['Network/selectedNetwork']
if (!isNaN(page) && !onMessenger) {
query += `&sort[0]=companyName:asc&pagination[page]=${page}&pagination[pageSize]=12`
}
Expand All @@ -176,11 +177,10 @@ export const partnersApi = createApi({
query += `&filters[companyName][$contains]=${companyName}`
}
if (validators) {
query += `&filters[pChainAddress][$ne]=null`
query += `&filters[$and][0][pChainAddresses][pAddress][$notNull]=true&filters[$and][1][pChainAddresses][Network][$eq]=${selectedNetwork.name.toLowerCase()}`
}
// Only add the cChainAddress filter if onMessenger is explicitly true
if (onMessenger === true) {
query += `&sort[0]=companyName:asc&_limit=-1&filters[cChainAddress][$ne]=null`
query += `&sort[0]=companyName:asc&_limit=-1&filters[$and][0][cChainAddresses][cAddress][$notNull]=true&filters[$and][1][cChainAddresses][Network][$eq]=${selectedNetwork.name.toLowerCase()}`
}

return {
Expand All @@ -205,11 +205,14 @@ export const partnersApi = createApi({

const partnersWithServices = await Promise.all(
response.data.map(async partner => {
if (partner.attributes.cChainAddress) {
let partnerCChainAddress = partner?.attributes?.cChainAddresses?.find(
elem => elem.Network === selectedNetwork.name.toLowerCase(),
)
if (partnerCChainAddress?.cAddress) {
const contractAddress = Array.from(contractMappings.entries()).find(
([_, partnerAddress]) =>
partnerAddress.toLowerCase() ===
partner.attributes.cChainAddress.toLowerCase(),
partnerCChainAddress?.cAddress.toLowerCase(),
)?.[0]

if (contractAddress) {
Expand Down Expand Up @@ -250,7 +253,7 @@ export const partnersApi = createApi({
contractAddress,
bots,
supportedCurrencies,
isOnMessenger: Boolean(partner.attributes.cChainAddress),
isOnMessenger: Boolean(partnerCChainAddress.cAddress),
}
}
}
Expand Down Expand Up @@ -295,9 +298,10 @@ export const partnersApi = createApi({
query: ({ companyName, cChainAddress }) => {
if (cChainAddress) {
const baseUrl = getBaseUrl()
const selectedNetwork = store.getters['Network/selectedNetwork']
let query =
'?populate=*&sort[0]=companyName:asc&pagination[page]=1&pagination[pageSize]=12'
query += `&filters[cChainAddress][$eq]=${cChainAddress}`
query += `&filters[$and][0][cChainAddresses][cAddress][$containsi]=${cChainAddress}&filters[$and][1][cChainAddresses][Network][$eq]=${selectedNetwork.name.toLowerCase()}`
return `${baseUrl}${query}`
}
if (companyName) {
Expand All @@ -310,17 +314,20 @@ export const partnersApi = createApi({
},
async transformResponse(response: PartnersResponseType, _meta, arg) {
const partnerData = response.data[0]
if (partnerData && partnerData.attributes.cChainAddress) {
const selectedNetwork = store.getters['Network/selectedNetwork']
let partnerCChainAddress = partnerData?.attributes?.cChainAddresses.find(
elem => elem.Network === selectedNetwork.name.toLowerCase(),
)

if (partnerData && partnerCChainAddress.cAddress) {
const selectedNetwork = store.getters['Network/selectedNetwork']
const providerUrl = `${selectedNetwork.protocol}://${selectedNetwork.ip}:${selectedNetwork.port}/ext/bc/C/rpc`
const provider = new ethers.JsonRpcProvider(providerUrl)
const contractMappings = await getContractMappings()
const contractAddress = Array.from(contractMappings.entries()).find(
([_, partnerAddress]) =>
partnerAddress.toLowerCase() ===
(
arg.cChainAddress || partnerData.attributes.cChainAddress
).toLowerCase(),
(arg.cChainAddress || partnerCChainAddress.cAddress).toLowerCase(),
)?.[0]
if (contractAddress) {
const { supportedServices, wantedServices, bots, supportedCurrencies } =
Expand Down Expand Up @@ -385,6 +392,7 @@ export const partnersApi = createApi({
}) => {
const baseUrl = getBaseUrl()
let query = '?populate=*'
const selectedNetwork = store.getters['Network/selectedNetwork']

if (!isNaN(page)) {
query += `&sort[0]=companyName:asc&_limit=-1`
Expand All @@ -403,10 +411,10 @@ export const partnersApi = createApi({
query += `&filters[companyName][$contains]=${companyName}`
}
if (validators) {
query += `&filters[pChainAddress][$ne]=null`
query += `&filters[$and][0][pChainAddresses][pAddress][$notNull]=true&filters[$and][1][pChainAddresses][Network][$eq]=${selectedNetwork.name.toLowerCase()}`
}
// Only add the cChainAddress filter if onMessenger is explicitly true
query += `&filters[cChainAddress][$ne]=null`
query += `&filters[$and][0][cChainAddresses][cAddress][$notNull]=true&filters[$and][1][cChainAddresses][Network][$eq]=${selectedNetwork.name.toLowerCase()}`

return {
url: `${baseUrl}${query}`,
Expand All @@ -433,11 +441,14 @@ export const partnersApi = createApi({

const partnersWithServices = await Promise.all(
response.data.map(async partner => {
if (partner.attributes.cChainAddress) {
let partnerCChainAddress = partner?.attributes?.cChainAddresses.find(
elem => elem.Network === selectedNetwork.name.toLowerCase(),
)
if (partnerCChainAddress.cAddress) {
const contractAddress = Array.from(contractMappings.entries()).find(
([_, partnerAddress]) =>
partnerAddress.toLowerCase() ===
partner.attributes.cChainAddress.toLowerCase(),
partnerCChainAddress.cAddress.toLowerCase(),
)?.[0]

if (contractAddress) {
Expand Down Expand Up @@ -514,14 +525,19 @@ export const partnersApi = createApi({
isPartner: build.query<PartnerDataType, { cChainAddress: string }>({
query: ({ cChainAddress }) => {
const baseUrl = getBaseUrl()
const selectedNetwork = store.getters['Network/selectedNetwork']
let query =
'?populate=*&sort[0]=companyName:asc&pagination[page]=1&pagination[pageSize]=12'
query += `&filters[cChainAddress][$eq]=${cChainAddress}`
query += `&filters[$and][0][cChainAddresses][cAddress][$containsi]=${cChainAddress}&filters[$and][1][cChainAddresses][Network][$eq]=${selectedNetwork.name.toLowerCase()}`
return `${baseUrl}${query}`
},
async transformResponse(response: PartnersResponseType, _meta, { cChainAddress }) {
const partnerData = response.data[0]
if (partnerData && partnerData.attributes.cChainAddress) {
const selectedNetwork = store.getters['Network/selectedNetwork']
let partnerCChainAddress = partnerData?.attributes?.cChainAddresses.find(
elem => elem.Network === selectedNetwork.name.toLowerCase(),
)
if (partnerData && partnerCChainAddress.cAddress) {
const selectedNetwork = store.getters['Network/selectedNetwork']
const providerUrl = `${selectedNetwork.protocol}://${selectedNetwork.ip}:${selectedNetwork.port}/ext/bc/C/rpc`
const provider = new ethers.JsonRpcProvider(providerUrl)
Expand Down
10 changes: 8 additions & 2 deletions src/views/partners/ConfigurDistrubitor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import {
} from '../../helpers/partnerConfigurationContext'
import { usePartnerConfig } from '../../helpers/usePartnerConfig'
import { useSmartContract } from '../../helpers/useSmartContract'
import { useAppDispatch } from '../../hooks/reduxHooks'
import { useAppDispatch, useAppSelector } from '../../hooks/reduxHooks'
import { useFetchPartnerDataQuery } from '../../redux/services/partners'
import { updateNotificationStatus } from '../../redux/slices/app-config'
import { getActiveNetwork } from '../../redux/slices/network'
import { Configuration } from './Configuration'

function ServiceChangesPreview({ added, removed }) {
Expand Down Expand Up @@ -148,9 +149,14 @@ export const BasicWantedServices = () => {
const { partnerID } = useParams()
const { state, dispatch } = usePartnerConfigurationContext()
const [distrubitorState, dispatchDistrubitorState] = useReducer(reducer, { ...state, step: 2 })
const { data: partner } = useFetchPartnerDataQuery({
const { data: partner, refetch } = useFetchPartnerDataQuery({
companyName: partnerID,
})
const activeNetwork = useAppSelector(getActiveNetwork)
useEffect(() => {
if (activeNetwork) refetch()
}, [activeNetwork])

const navigate = useNavigate()
useEffect(() => {
if (partner)
Expand Down
Loading
Loading