Skip to content

Commit

Permalink
Merge pull request #244 from chain4travel/aeddaqqa/partner-config-fixes
Browse files Browse the repository at this point in the history
fix(partnerConfig): adjust baseUrl depending on the selected network
  • Loading branch information
aeddaqqa authored Sep 25, 2024
2 parents 9485948 + 0944a33 commit 11f0cca
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 18 deletions.
4 changes: 4 additions & 0 deletions src/constants/apps-consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@ export const BUSINESS_FIELDS = [
'Metaverse',
'Climate Technology',
]

export const CONTRACTCMACCOUNTMANAGERADDRESSCOLUMBUS = '0xE5B2f76C778D082b07BDd7D51FFe83E3E055B47F'

export const CONTRACTCMACCOUNTMANAGERADDRESSCAMINO = '0xf9FE1eaAB73a2902136FE7A83E0703338D3b9F1e'
26 changes: 21 additions & 5 deletions src/helpers/useSmartContract.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import React, {
useState,
} from 'react'
import store from 'wallet/store'
import {
CONTRACTCMACCOUNTMANAGERADDRESSCAMINO,
CONTRACTCMACCOUNTMANAGERADDRESSCOLUMBUS,
} from '../constants/apps-consts'
import { useAppSelector } from '../hooks/reduxHooks'
import { getActiveNetwork } from '../redux/slices/network'
import CMAccount from './CMAccountManagerModule#CMAccount.json'
Expand All @@ -33,7 +37,7 @@ export const SmartContractProvider: React.FC<SmartContractProviderProps> = ({ ch
const [account, setAccount] = useState<string | null>(null)
const [contractCMAccountAddress, setContractCMAccountAddress] = useState<string | null>('')
const auth = useAppSelector(state => state.appConfig.isAuth)
const contractCMAccountManagerAddress = '0xE5B2f76C778D082b07BDd7D51FFe83E3E055B47F'
const activeNetwork = useAppSelector(getActiveNetwork)

const CMAccountCreated = async cmAccountAddress => {
const accountWritableContract = new ethers.Contract(cmAccountAddress, CMAccount, wallet)
Expand Down Expand Up @@ -108,10 +112,19 @@ export const SmartContractProvider: React.FC<SmartContractProviderProps> = ({ ch
`${selectedNetwork.protocol}://${selectedNetwork.ip}:${selectedNetwork.port}/ext/bc/C/rpc`,
)
try {
if (
activeNetwork.name.toLowerCase() !== 'columbus' &&
activeNetwork.name.toLowerCase() !== 'camino'
)
return
let contractAddress =
activeNetwork.name.toLowerCase() === 'columbus'
? CONTRACTCMACCOUNTMANAGERADDRESSCOLUMBUS
: CONTRACTCMACCOUNTMANAGERADDRESSCAMINO
if (auth) {
const wallet = new ethers.Wallet(store.state.activeWallet?.ethKey, ethersProvider)
const managerWritableContract = new ethers.Contract(
contractCMAccountManagerAddress,
contractAddress,
CMAccountManager.abi,
wallet,
)
Expand All @@ -120,7 +133,7 @@ export const SmartContractProvider: React.FC<SmartContractProviderProps> = ({ ch
setAccount(wallet.address)
}
const managerReadOnlyContract = new ethers.Contract(
contractCMAccountManagerAddress,
contractAddress,
CMAccountManager.abi,
ethersProvider,
)
Expand All @@ -130,10 +143,13 @@ export const SmartContractProvider: React.FC<SmartContractProviderProps> = ({ ch
console.error('User denied account access:', error)
}
}
const activeNetwork = useAppSelector(getActiveNetwork)

useEffect(() => {
if (activeNetwork.name.toLowerCase() === 'columbus') initializeEthers()
if (
activeNetwork.name.toLowerCase() === 'columbus' ||
activeNetwork.name.toLowerCase() === 'camino'
)
initializeEthers()
}, [activeNetwork, auth])

useEffect(() => {
Expand Down
3 changes: 2 additions & 1 deletion src/layout/PartnersLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ const PartnersLayout = () => {
if (
path.includes('partners/messenger-configuration') &&
!store.state.isAuth &&
activeNetwork.name.toLowerCase() !== 'columbus'
(activeNetwork.name.toLowerCase() !== 'columbus' ||
activeNetwork.name.toLowerCase() !== 'camino')
) {
return <Navigate to="/login" replace />
}
Expand Down
61 changes: 50 additions & 11 deletions src/redux/services/partners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
import { ethers } from 'ethers'
import store from 'wallet/store'
import { PartnerDataType, PartnersResponseType } from '../../@types/partners'
import {
CONTRACTCMACCOUNTMANAGERADDRESSCAMINO,
CONTRACTCMACCOUNTMANAGERADDRESSCOLUMBUS,
} from '../../constants/apps-consts'
import CMAccount from '../../helpers/CMAccountManagerModule#CMAccount.json'
import CMAccountManager from '../../helpers/ManagerProxyModule#CMAccountManager.json'
import { StatePartnersType } from '../../helpers/partnersReducer'

const baseUrl = 'https://dev.strapi.camino.network/api/partners'
// const baseUrl = 'https://api.strapi.camino.network/partners'
const BASE_URLS = {
dev: 'https://dev.strapi.camino.network/api/partners',
prod: 'https://api.strapi.camino.network/partners',
}

function createPartnerContract(address: string, provider: ethers.Provider) {
return new ethers.Contract(address, CMAccount, provider)
Expand Down Expand Up @@ -63,8 +69,12 @@ async function getContractMappings(): Promise<Map<string, string>> {
const selectedNetwork = store.getters['Network/selectedNetwork']
const providerUrl = `${selectedNetwork.protocol}://${selectedNetwork.ip}:${selectedNetwork.port}/ext/bc/C/rpc`
const provider = new ethers.JsonRpcProvider(providerUrl)
let contractAddress =
selectedNetwork.name.toLowerCase() === 'columbus'
? CONTRACTCMACCOUNTMANAGERADDRESSCOLUMBUS
: CONTRACTCMACCOUNTMANAGERADDRESSCAMINO
const managerReadOnlyContract = new ethers.Contract(
'0xE5B2f76C778D082b07BDd7D51FFe83E3E055B47F',
contractAddress,
CMAccountManager.abi,
provider,
)
Expand Down Expand Up @@ -129,11 +139,28 @@ function getServiceName(fullName: unknown): string {
return parts[parts.length - 1] || ''
}

const getBaseUrl = () => {
const currentPath = typeof window !== 'undefined' ? window.location.hostname : ''
if (
currentPath === 'localhost' ||
currentPath.includes('dev') ||
currentPath.includes('stage')
) {
return BASE_URLS.dev
} else {
return BASE_URLS.prod
}
}

export const partnersApi = createApi({
baseQuery: fetchBaseQuery({ baseUrl: baseUrl }),
baseQuery: fetchBaseQuery({
baseUrl: '',
}),
endpoints: build => ({
listPartners: build.query<any, StatePartnersType>({
query: ({ page, companyName, businessField, validators, onMessenger }) => {
const baseUrl = getBaseUrl()

let query = '?populate=*'

if (!isNaN(page)) {
Expand Down Expand Up @@ -161,14 +188,18 @@ export const partnersApi = createApi({
}

return {
url: query,
url: `${baseUrl}${query}`,
method: 'GET',
params: { onMessenger: onMessenger === true ? 'true' : 'false' },
}
},
async transformResponse(response: PartnersResponseType, meta, arg) {
const selectedNetwork = store.getters['Network/selectedNetwork']
if (selectedNetwork.name.toLowerCase() !== 'columbus') return response
if (
selectedNetwork.name.toLowerCase() !== 'columbus' &&
selectedNetwork.name.toLowerCase() !== 'camino'
)
return response

const providerUrl = `${selectedNetwork.protocol}://${selectedNetwork.ip}:${selectedNetwork.port}/ext/bc/C/rpc`
const provider = new ethers.JsonRpcProvider(providerUrl)
Expand Down Expand Up @@ -265,16 +296,18 @@ export const partnersApi = createApi({
>({
query: ({ companyName, cChainAddress }) => {
if (cChainAddress) {
const baseUrl = getBaseUrl()
let query =
'?populate=*&sort[0]=companyName:asc&pagination[page]=1&pagination[pageSize]=12'
query += `&filters[cChainAddress][$eq]=${cChainAddress}`
return query
return `${baseUrl}${query}`
}
if (companyName) {
const baseUrl = getBaseUrl()
let query =
'?populate=*&sort[0]=companyName:asc&pagination[page]=1&pagination[pageSize]=12'
query += `&filters[companyName][$contains]=${companyName}`
return query
return `${baseUrl}${query}`
}
},
async transformResponse(response: PartnersResponseType, _meta, arg) {
Expand Down Expand Up @@ -352,6 +385,7 @@ export const partnersApi = createApi({
supportedResult,
wantedResult,
}) => {
const baseUrl = getBaseUrl()
let query = '?populate=*'

if (!isNaN(page)) {
Expand All @@ -377,7 +411,7 @@ export const partnersApi = createApi({
query += `&filters[cChainAddress][$ne]=null`

return {
url: query,
url: `${baseUrl}${query}`,
method: 'GET',
params: {
onMessenger: onMessenger === true ? 'true' : 'false',
Expand All @@ -388,7 +422,11 @@ export const partnersApi = createApi({
},
async transformResponse(response: PartnersResponseType, meta, arg) {
const selectedNetwork = store.getters['Network/selectedNetwork']
if (selectedNetwork.name.toLowerCase() !== 'columbus') return response
if (
selectedNetwork.name.toLowerCase() !== 'columbus' &&
selectedNetwork.name.toLowerCase() !== 'camino'
)
return response

const providerUrl = `${selectedNetwork.protocol}://${selectedNetwork.ip}:${selectedNetwork.port}/ext/bc/C/rpc`
const provider = new ethers.JsonRpcProvider(providerUrl)
Expand Down Expand Up @@ -477,10 +515,11 @@ export const partnersApi = createApi({
}),
isPartner: build.query<PartnerDataType, { cChainAddress: string }>({
query: ({ cChainAddress }) => {
const baseUrl = getBaseUrl()
let query =
'?populate=*&sort[0]=companyName:asc&pagination[page]=1&pagination[pageSize]=12'
query += `&filters[cChainAddress][$eq]=${cChainAddress}`
return query
return `${baseUrl}${query}`
},
async transformResponse(response: PartnersResponseType, _meta, { cChainAddress }) {
const partnerData = response.data[0]
Expand Down
3 changes: 2 additions & 1 deletion src/views/settings/Links.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ export default function Links({ type = 'else', partner }: { type?: string; partn
/>,
auth &&
store.state.activeWallet?.type !== 'multisig' &&
activeNetwork.name.toLowerCase() === 'columbus' && (
(activeNetwork.name.toLowerCase() === 'columbus' ||
activeNetwork.name.toLowerCase() === 'camino') && (
<Tab
onClick={() => navigate('/partners/messenger-configuration')}
className="tab"
Expand Down

0 comments on commit 11f0cca

Please sign in to comment.