Skip to content

Commit

Permalink
fix(partnerConfiguration): fix creating CMAccount with withdraw role …
Browse files Browse the repository at this point in the history
…granted
  • Loading branch information
aeddaqqa committed Sep 17, 2024
1 parent 485125c commit 6bb2c33
Show file tree
Hide file tree
Showing 7 changed files with 331 additions and 119 deletions.
2 changes: 0 additions & 2 deletions src/helpers/partnerConfigurationContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ export function reducer(state = initialState, action) {
try {
const { services } = action.payload
if (!services || services.length < 1) return state
console.log({ services1: services[1][0] })
console.log({ services2: services[1][1][2] })
let parsedServices = services[0].map((service, index) => {
let capabilities = services[1][index][2].map(elem => elem)
return {
Expand Down
61 changes: 57 additions & 4 deletions src/helpers/useSmartContract.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export const SmartContractProvider: React.FC<SmartContractProviderProps> = ({ ch
return { success: false, error: 'Account is not initialized' }
}
try {
console.log({ state, cmAccountAddress })
for (const service of state.services) {
const tx = await accountWritableContract.addService(
service.name,
Expand All @@ -68,11 +67,11 @@ export const SmartContractProvider: React.FC<SmartContractProviderProps> = ({ ch
await offChainPaymentTx.wait()
}
if (state.isCam) {
const tx = await accountWriteContract.addSupportedToken(ethers.ZeroAddress)
const tx = await accountWritableContract.addSupportedToken(ethers.ZeroAddress)
await tx.wait()
}
const WITHDRAWER_ROLE = await readFromContract('account', 'WITHDRAWER_ROLE')
const tx = await accountWriteContract.grantRole(WITHDRAWER_ROLE, wallet.address)
const WITHDRAWER_ROLE = await accountReadOnlyContract.WITHDRAWER_ROLE()
const tx = await accountWritableContract.grantRole(WITHDRAWER_ROLE, wallet.address)
await tx.wait()
setContractCMAccountAddress(cmAccountAddress)
setAccountReadContract(accountReadOnlyContract)
Expand Down Expand Up @@ -128,6 +127,7 @@ export const SmartContractProvider: React.FC<SmartContractProviderProps> = ({ ch
console.error(`Reason: ${decodedError?.name} (${decodedError?.args})`)
}
}, [accountWriteContract])

const initializeEthers = async () => {
const selectedNetwork = store.getters['Network/selectedNetwork']
const ethersProvider = new ethers.JsonRpcProvider(
Expand Down Expand Up @@ -164,6 +164,58 @@ export const SmartContractProvider: React.FC<SmartContractProviderProps> = ({ ch
if (contractCMAccountAddress) initializeCMAccountContract()
}, [provider, contractCMAccountAddress])

const getCMAccountMappings = useCallback(async () => {
try {
const mappings = new Map()
const CMACCOUNT_ROLE = await readFromContract('manager', 'CMACCOUNT_ROLE')
const roleMemberCount = await readFromContract(
'manager',
'getRoleMemberCount',
CMACCOUNT_ROLE,
)

const promises = []
for (let i = 0; i < roleMemberCount; i++) {
promises.push(
managerReadContract.getRoleMember(CMACCOUNT_ROLE, i).then(async role => {
const creator = await readFromContract(
'manager',
'getCMAccountCreator',
role,
)
return { role, creator }
}),
)
}

const results = await Promise.all(promises)
results.forEach(({ role, creator }) => {
mappings.set(role.toLowerCase(), creator.toLowerCase())
})

const findAddress = query => {
query = query.toLowerCase()
if (mappings.has(query)) {
return { role: query, creator: mappings.get(query) }
}
for (const [role, creator] of mappings) {
if (creator === query) {
return { role, creator: query }
}
}
return null
}

return {
findAddress,
getAllMappings: () => Object.fromEntries(mappings),
}
} catch (error) {
console.error('Error in getCMAccountMappings:', error)
throw error
}
}, [managerReadContract])

const readFromContract = async (
contractType: 'manager' | 'account',
method: string,
Expand Down Expand Up @@ -204,6 +256,7 @@ export const SmartContractProvider: React.FC<SmartContractProviderProps> = ({ ch
}

const value = {
getCMAccountMappings,
upgradeCMAccount,
contractCMAccountAddress,
setContractCMAccountAddress,
Expand Down
2 changes: 1 addition & 1 deletion src/layout/RoutesSuite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default function RoutesSuite() {
<Route path=":partnerID" element={<Partner />} />
<Route path="messenger-configuration">
<Route index element={<Partner />} />
<Route path="messenger" element={<Overreview />} />
<Route path="mymessenger" element={<Overreview />} />
<Route path="mydetails" element={<Partner />} />
<Route path="distribution" element={<ConfigurDistrubitor />} />
<Route path="supplier" element={<ConfigurSupplier />} />
Expand Down
16 changes: 16 additions & 0 deletions src/utils/display-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ export interface Fund {
value?: number
}

export const transformServiceNames = services => {
return services
.map(service => {
const parts = service.name.split('.')
const versionIndex = parts.findIndex(part => part.startsWith('v'))
if (versionIndex !== -1) {
const relevantParts = parts.slice(versionIndex + 1)
if (relevantParts[relevantParts.length - 1] === 'Service') {
relevantParts.pop()
}
return relevantParts.join('')
}
return service
})
.join(', ')
}
// export function getStartDate(endDate: DateTime, timeframe: string): DateTime {
// switch (timeframe) {
// case Timeframe.DAYS_7:
Expand Down
13 changes: 5 additions & 8 deletions src/views/partners/MyMessenger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { usePartnerConfig } from '../../helpers/usePartnerConfig'
import { useSmartContract } from '../../helpers/useSmartContract'
import useWalletBalance from '../../helpers/useWalletBalance'
import { useFetchPartnerDataQuery } from '../../redux/services/partners'
import { transformServiceNames } from '../../utils/display-utils'
import { Configuration } from './Configuration'

const AmountInput = ({ amount, onAmountChange, onMaxAmountClick, maxAmount }) => {
Expand Down Expand Up @@ -518,10 +519,8 @@ const MyMessenger = () => {
<Typography sx={{ flex: '0 0 20%' }} variant="body2">
Wanted Services
</Typography>
<Typography variant="overline">
{state.stepsConfig[2].services
.map(service => service.name)
.join(', ')}
<Typography variant="caption">
{transformServiceNames(state.stepsConfig[2].services)}
</Typography>
</Box>
)}
Expand All @@ -530,10 +529,8 @@ const MyMessenger = () => {
<Typography sx={{ flex: '0 0 20%' }} variant="body2">
Offered Services
</Typography>
<Typography variant="overline">
{state.stepsConfig[1].services
.map(service => service.name)
.join(', ')}
<Typography variant="caption">
{transformServiceNames(state.stepsConfig[1].services)}
</Typography>
</Box>
)}
Expand Down
Loading

0 comments on commit 6bb2c33

Please sign in to comment.