Skip to content

Commit 2b437ca

Browse files
committed
registry: fetching value from etherjs
1 parent 523fecf commit 2b437ca

3 files changed

Lines changed: 158 additions & 20 deletions

File tree

src/modules/etherlink/explorer/EtherlinkDAO/EvmRegistryPage.tsx

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import React, { useContext, useMemo, useState, useEffect } from "react"
55

66
import { useIsProposalButtonDisabled } from "../../../../services/contracts/baseDAO/hooks/useCycleInfo"
77
import { InfoIcon } from "modules/explorer/components/styled/InfoIcon"
8-
import { MainButton } from "modules/common/MainButton"
8+
import { SmallButton } from "modules/common/SmallButton"
99
import { useEtherlinkDAOID } from "../router"
1010
import { EtherlinkContext } from "services/wagmi/context"
1111
import { useEvmProposalOps } from "services/contracts/etherlinkDAO/hooks/useEvmProposalOps"
1212
import { useHistory } from "react-router-dom"
1313
import { EvmRegistryTable } from "modules/etherlink/components/EvmRegistryTable"
1414
import { SearchInput } from "modules/explorer/pages/DAOList/components/Searchbar"
1515
import { styled } from "@material-ui/core"
16+
import { useRegistry } from "services/wagmi/etherlink/hooks/useRegistry"
1617

1718
const ShellHeroContainer = styled(ContentContainer)({
1819
background: "inherit !important",
@@ -26,13 +27,19 @@ export const EvmRegistryPage: React.FC = () => {
2627
const theme = useTheme()
2728
const isMobileSmall = useMediaQuery(theme.breakpoints.down("xs"))
2829
const daoId = useEtherlinkDAOID()
29-
const { daoSelected } = useContext(EtherlinkContext)
30+
const { daoSelected, provider, network } = useContext(EtherlinkContext)
3031
const shouldDisable = useIsProposalButtonDisabled(daoId)
3132
const { setMetadataFieldValue, setCurrentStep, setDaoRegistry } = useEvmProposalOps()
3233
const history = useHistory()
3334
const [searchText, setSearchText] = useState("")
3435
const [debouncedSearchText, setDebouncedSearchText] = useState("")
3536

37+
const {
38+
data: rpcRegistryEntries,
39+
isLoading: isRpcLoading,
40+
error: rpcError
41+
} = useRegistry(provider, daoSelected?.registryAddress, network || "")
42+
3643
useEffect(() => {
3744
const timer = setTimeout(() => {
3845
setDebouncedSearchText(searchText)
@@ -41,16 +48,59 @@ export const EvmRegistryPage: React.FC = () => {
4148
return () => clearTimeout(timer)
4249
}, [searchText])
4350

44-
const registryList = useMemo(() => {
51+
const firestoreRegistryList = useMemo(() => {
4552
if (!daoSelected?.registry) {
4653
return []
4754
}
4855
return Object.entries(daoSelected?.registry).map(([key, value]) => ({
4956
key,
5057
value: value as string,
51-
onClick: () => {}
58+
onClick: () => {
59+
setMetadataFieldValue("type", "edit_registry")
60+
setDaoRegistry("key", key)
61+
setDaoRegistry("value", value as string)
62+
setCurrentStep(1)
63+
history.push(`${window.location.pathname.slice(0, -8)}proposals`)
64+
}
5265
}))
53-
}, [daoSelected?.registry])
66+
}, [daoSelected?.registry, setMetadataFieldValue, setDaoRegistry, setCurrentStep, history])
67+
68+
const registryList = useMemo(() => {
69+
const rpcEntries = rpcRegistryEntries || []
70+
const firestoreEntries = firestoreRegistryList || []
71+
72+
console.log("[Registry Source] RPC entries:", rpcEntries)
73+
console.log("[Registry Source] Firestore entries:", firestoreEntries)
74+
console.log("[Registry Source] RPC loading:", isRpcLoading)
75+
console.log("[Registry Source] RPC error:", rpcError)
76+
77+
if (rpcEntries.length > 0 && !isRpcLoading && !rpcError) {
78+
console.log("[Registry Source] Using: RPC")
79+
return rpcEntries.map(entry => ({
80+
key: entry.key,
81+
value: entry.value,
82+
onClick: () => {
83+
setMetadataFieldValue("type", "edit_registry")
84+
setDaoRegistry("key", entry.key)
85+
setDaoRegistry("value", entry.value)
86+
setCurrentStep(1)
87+
history.push(`${window.location.pathname.slice(0, -8)}proposals`)
88+
}
89+
}))
90+
} else {
91+
console.log("[Registry Source] Using: Firestore")
92+
return firestoreEntries
93+
}
94+
}, [
95+
rpcRegistryEntries,
96+
firestoreRegistryList,
97+
isRpcLoading,
98+
rpcError,
99+
setMetadataFieldValue,
100+
setDaoRegistry,
101+
setCurrentStep,
102+
history
103+
])
54104

55105
const filteredRegistryList = useMemo(() => {
56106
if (!debouncedSearchText) {
@@ -82,7 +132,7 @@ export const EvmRegistryPage: React.FC = () => {
82132
direction={isMobileSmall ? "column" : "row"}
83133
xs={isMobileSmall ? undefined : true}
84134
>
85-
<MainButton
135+
<SmallButton
86136
variant="contained"
87137
color="secondary"
88138
onClick={() => {
@@ -93,7 +143,7 @@ export const EvmRegistryPage: React.FC = () => {
93143
disabled={shouldDisable}
94144
>
95145
New Item
96-
</MainButton>
146+
</SmallButton>
97147
{shouldDisable && (
98148
<Tooltip placement="bottom" title="Not on proposal creation period">
99149
<InfoIcon color="secondary" />
@@ -105,19 +155,7 @@ export const EvmRegistryPage: React.FC = () => {
105155
</Grid>
106156
</ShellHeroContainer>
107157
<Grid item>
108-
<EvmRegistryTable
109-
data={filteredRegistryList.map(rItem => ({
110-
key: rItem.key,
111-
value: rItem.value,
112-
onClick: () => {
113-
setMetadataFieldValue("type", "edit_registry")
114-
setDaoRegistry("key", rItem.key)
115-
setDaoRegistry("value", rItem.value)
116-
setCurrentStep(1)
117-
history.push(`${window.location.pathname.slice(0, -8)}proposals`)
118-
}
119-
}))}
120-
/>
158+
<EvmRegistryTable data={filteredRegistryList} />
121159
</Grid>
122160
</Grid>
123161
</>

src/modules/etherlink/utils.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,46 @@ export async function getTokenHolders(network: string, tokenAddress: string) {
279279
const data = await response.json()
280280
return data
281281
}
282+
283+
export const registryContractABI = [
284+
{
285+
inputs: [],
286+
name: "getAllKeys",
287+
outputs: [{ internalType: "string[]", name: "", type: "string[]" }],
288+
stateMutability: "view",
289+
type: "function"
290+
},
291+
{
292+
inputs: [],
293+
name: "getAllValues",
294+
outputs: [{ internalType: "string[]", name: "", type: "string[]" }],
295+
stateMutability: "view",
296+
type: "function"
297+
},
298+
{
299+
inputs: [{ internalType: "string", name: "key", type: "string" }],
300+
name: "getRegistryValue",
301+
outputs: [{ internalType: "string", name: "", type: "string" }],
302+
stateMutability: "view",
303+
type: "function"
304+
},
305+
{
306+
inputs: [
307+
{ internalType: "string", name: "key", type: "string" },
308+
{ internalType: "string", name: "value", type: "string" }
309+
],
310+
name: "editRegistry",
311+
outputs: [],
312+
stateMutability: "nonpayable",
313+
type: "function"
314+
},
315+
{
316+
anonymous: false,
317+
inputs: [
318+
{ indexed: false, internalType: "string", name: "key", type: "string" },
319+
{ indexed: false, internalType: "string", name: "value", type: "string" }
320+
],
321+
name: "RegistryUpdated",
322+
type: "event"
323+
}
324+
]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { useQuery } from "react-query"
2+
import { ethers } from "ethers"
3+
import { registryContractABI } from "modules/etherlink/utils"
4+
5+
export interface RegistryEntry {
6+
key: string
7+
value: string
8+
}
9+
10+
export const useRegistry = (provider: any, registryAddress: string | undefined, network: string) => {
11+
return useQuery<RegistryEntry[], Error>(
12+
["registry", network, registryAddress],
13+
async () => {
14+
if (!provider || !registryAddress || !network) {
15+
throw new Error("Missing required parameters")
16+
}
17+
18+
if (!ethers.isAddress(registryAddress)) {
19+
throw new Error(`Invalid registry address: ${registryAddress}`)
20+
}
21+
22+
console.log("[Registry RPC] Fetching from:", registryAddress, "on network:", network)
23+
24+
const registryContract = new ethers.Contract(registryAddress, registryContractABI, provider)
25+
26+
try {
27+
const [keys, values] = await Promise.all([registryContract.getAllKeys(), registryContract.getAllValues()])
28+
29+
console.log("[Registry RPC] Raw keys:", keys)
30+
console.log("[Registry RPC] Raw values:", values)
31+
32+
const keyArray = Array.isArray(keys) ? keys : []
33+
const valueArray = Array.isArray(values) ? values : []
34+
35+
const entries: RegistryEntry[] = keyArray.map((key: string, index: number) => ({
36+
key,
37+
value: valueArray[index] || ""
38+
}))
39+
40+
console.log("[Registry RPC] Mapped entries:", entries)
41+
console.log("[Registry Source] Using: RPC, data:", entries)
42+
43+
return entries
44+
} catch (error) {
45+
console.error("[Registry RPC] Error fetching registry:", error)
46+
throw error
47+
}
48+
},
49+
{
50+
enabled: !!provider && !!registryAddress && !!network && ethers.isAddress(registryAddress || ""),
51+
staleTime: 5 * 60 * 1000,
52+
cacheTime: 10 * 60 * 1000,
53+
retry: 2,
54+
retryDelay: 1000
55+
}
56+
)
57+
}

0 commit comments

Comments
 (0)