Skip to content

Commit 9596e06

Browse files
refactor: improve code quality
1 parent 62c8572 commit 9596e06

File tree

4 files changed

+52
-21
lines changed

4 files changed

+52
-21
lines changed

src/config/config.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,24 @@ export const LOCAL_STORAGE_PREFIX = 'Web3Messaging';
88
export const CONTACT_URL =
99
'https://airtable.com/appDiKrXe5wJgGpdP/pagm2GF2eNdX2ysw3/form';
1010

11+
// Chain ID constants
12+
export const BELLECOUR_CHAIN_ID = 134;
13+
export const ARBITRUM_CHAIN_ID = 42161;
14+
15+
// Workerpool configuration
16+
export const WORKERPOOL_ADDRESS_OR_ENS = 'prod-v8-learn.main.pools.iexec.eth';
17+
export const WORKERPOOL_MAX_PRICE = 0.1 * 1e9; // 0.1 RLC in wei
18+
1119
/**
1220
* See smart-contract transactions:
1321
* https://blockscout-bellecour.iex.ec/address/0x781482C39CcE25546583EaC4957Fb7Bf04C277D2
1422
*
1523
* See all idapps in the whitelist:
1624
* https://explorer.iex.ec/bellecour/address/0x0c6c77a11068db9fadfba25182e02863361f58da
1725
*/
18-
export const WORKERPOOL_ADDRESS_OR_ENS = 'prod-v8-learn.main.pools.iexec.eth';
19-
2026
export const SUPPORTED_CHAINS = [
2127
{
22-
id: 134,
28+
id: BELLECOUR_CHAIN_ID,
2329
name: 'Bellecour',
2430
slug: 'bellecour',
2531
color: '#F4942566',
@@ -37,7 +43,7 @@ export const SUPPORTED_CHAINS = [
3743
},
3844
},
3945
{
40-
id: 42161,
46+
id: ARBITRUM_CHAIN_ID,
4147
name: 'Arbitrum',
4248
slug: 'arbitrum-mainnet',
4349
color: '#F4942566',

src/hooks/useWatchAccount.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ export function useWatchAccount() {
1919
setConnector(connector);
2020
setIsConnected(isConnected);
2121
setAddress(address);
22-
if (accountChain?.id && chainId !== accountChain?.id) {
23-
setTimeout(() => {
24-
setChainId(accountChain?.id);
25-
}, 10);
22+
if (accountChain?.id && chainId !== accountChain.id) {
23+
setChainId(accountChain.id);
2624
}
2725

2826
// Update dataProtector client
@@ -31,5 +29,5 @@ export function useWatchAccount() {
3129
return;
3230
}
3331
cleanIExecSDKs();
34-
}, [connector, status, address, accountChain]);
32+
}, [connector, status, address, accountChain, chainId, setConnector, setIsConnected, setAddress, setChainId]);
3533
}

src/utils/chain.utils.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ export function getSupportedChains() {
44
return SUPPORTED_CHAINS;
55
}
66

7-
export function getSubgraphUrl(chainId: number) {
8-
const subgraphUrl = getSupportedChains().find(
9-
(chain) => chain.id === chainId
10-
)?.subgraphUrl;
11-
if (!subgraphUrl) {
7+
export function getSubgraphUrl(chainId: number): string {
8+
const chain = getSupportedChains().find((chain) => chain.id === chainId);
9+
if (!chain?.subgraphUrl) {
1210
throw new Error(`Subgraph URL not found for chain ID: ${chainId}`);
1311
}
14-
return subgraphUrl;
12+
return chain.subgraphUrl;
1513
}
1614

17-
export function getWhitelistAddresses(chainId: number | undefined) {
15+
export function getWhitelistAddresses(chainId: number | undefined): { web3mail: string; web3telegram: string } {
1816
if (chainId === undefined) {
1917
throw new Error('Chain ID is required to get whitelist addresses');
2018
}
19+
2120
const chain = getSupportedChains().find((chain) => chain.id === chainId);
2221
if (!chain?.whitelist) {
2322
throw new Error(`Whitelist not found for chain ID: ${chainId}`);
2423
}
24+
2525
return chain.whitelist;
2626
}
2727

@@ -33,7 +33,22 @@ export function getChainFromId(id: number | undefined) {
3333
return getSupportedChains().find((c) => c.id === id);
3434
}
3535

36-
export function getBlockExplorerUrl(chainId: number) {
36+
export function getBlockExplorerUrl(chainId: number): string {
37+
const chain = getChainFromId(chainId);
38+
if (!chain?.blockExplorerUrl) {
39+
console.warn(`Block explorer URL not found for chain ID: ${chainId}, using default`);
40+
return 'https://blockscout.com/';
41+
}
42+
return chain.blockExplorerUrl;
43+
}
44+
45+
// Helper function to check if a chain is supported
46+
export function isChainSupported(chainId: number): boolean {
47+
return getSupportedChains().some(chain => chain.id === chainId);
48+
}
49+
50+
// Helper function to get chain name
51+
export function getChainName(chainId: number): string {
3752
const chain = getChainFromId(chainId);
38-
return chain?.blockExplorerUrl ?? 'https://blockscout.com/';
53+
return chain?.name || `Unknown Chain (${chainId})`;
3954
}

src/views/contact/sendMessage.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { WORKERPOOL_ADDRESS_OR_ENS } from '@/config/config';
1+
import {
2+
WORKERPOOL_ADDRESS_OR_ENS,
3+
BELLECOUR_CHAIN_ID,
4+
WORKERPOOL_MAX_PRICE
5+
} from '@/config/config';
26
import { Address } from '@/types';
37
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
48
import { useState } from 'react';
@@ -73,6 +77,12 @@ export default function SendMessage() {
7377
protectedData.data?.schema &&
7478
getDataType(protectedData.data?.schema) === 'mail';
7579

80+
// Helper function to create workerpool configuration
81+
const getWorkerpoolConfig = () => ({
82+
workerpoolAddressOrEns: chainId === BELLECOUR_CHAIN_ID ? WORKERPOOL_ADDRESS_OR_ENS : undefined,
83+
workerpoolMaxPrice: WORKERPOOL_MAX_PRICE,
84+
});
85+
7686
const handleChange = (e: { target: { name: string; value: string } }) => {
7787
const { name, value } = e.target;
7888
setFormData((prevData) => ({
@@ -116,7 +126,8 @@ export default function SendMessage() {
116126
senderName: formData.senderName,
117127
contentType: formData.contentType,
118128
emailContent: formData.messageContent,
119-
workerpoolAddressOrEns: WORKERPOOL_ADDRESS_OR_ENS,
129+
workerpoolAddressOrEns: getWorkerpoolConfig().workerpoolAddressOrEns,
130+
workerpoolMaxPrice: getWorkerpoolConfig().workerpoolMaxPrice,
120131
});
121132
return sendMail;
122133
} else {
@@ -126,7 +137,8 @@ export default function SendMessage() {
126137
protectedData: protectedDataAddress!,
127138
senderName: formData.senderName,
128139
telegramContent: formData.messageContent,
129-
workerpoolAddressOrEns: WORKERPOOL_ADDRESS_OR_ENS,
140+
workerpoolAddressOrEns: getWorkerpoolConfig().workerpoolAddressOrEns,
141+
workerpoolMaxPrice: getWorkerpoolConfig().workerpoolMaxPrice,
130142
});
131143
return sendTelegram;
132144
}

0 commit comments

Comments
 (0)