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

Ri/event handlers updates #20

Merged
merged 3 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
35 changes: 30 additions & 5 deletions components/NewSubgraph/NewSubgraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,21 @@ const ButtonsContainer = styled.div`
}
`

export type ExtendedContract = Contract & { errorMessage?: string }

const ADDRESS_REGEX = /^0x[0-9a-f]{40}$/i
const MAPPING_FILENAME = 'mapping.ts'
export type ExtendedContract = Contract & { errorMessage?: string }

export const NewSubgraph = () => {
const CHAIN_ID = '1'

const [subgraphId, setSubgraphId] = useEditorState<string | null>('subgraph-file')
const { subgraph, saveContracts } = useLocalSubgraph(subgraphId)
const { subgraph, saveContracts, saveMapping } = useLocalSubgraph(subgraphId)
const [contractAddress, setContractAddress] = useState('')
const [started, setStarted] = useState(false)
const [selectedContracts, setSelectedContracts] = useState<ExtendedContract[]>(
subgraph?.contracts || []
)

const [mappingFunctionNames, setMappingFunctionNames] = useState<string[]>([])
const router = useRouter()
const [fnExtractionLoading, setFnExtractionLoading] = useState(false)
Expand Down Expand Up @@ -144,6 +145,12 @@ export const NewSubgraph = () => {
}
}, [subgraph?.mappings[DEFAULT_MAPPING]])

useEffect(() => {
if (subgraph?.contracts && !selectedContracts.length) {
setSelectedContracts(subgraph.contracts)
}
}, [subgraph])

useEffect(() => {
setStarted(true)
}, [])
Expand All @@ -168,10 +175,28 @@ export const NewSubgraph = () => {

const save = () => {
let id = subgraphId
const newFnsToInsert: string[] = []
const contractsToSave = selectedContracts.map(sc => ({
...sc,
events: sc.events.map((sce, i) => {
if (sce.handler === 'newFunction') {
const newFnName = `handle${sce.signature.split('(')[0]}${i}`
newFnsToInsert.push(`\nexport function ${newFnName}():void {}\n`)
return { ...sce, handler: newFnName }
}

return sce
}),
}))

if (subgraph) {
saveContracts(selectedContracts)
saveMapping(
MAPPING_FILENAME,
subgraph.mappings[MAPPING_FILENAME].concat(newFnsToInsert.join('m'))
)
saveContracts(contractsToSave)
} else {
id = newSubgraph({ contracts: selectedContracts })
id = newSubgraph({ contracts: contractsToSave })
}
setSubgraphId(id)
router.push('/editor/subgraph')
Expand Down
23 changes: 16 additions & 7 deletions components/NewSubgraph/SelectedContract.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function parseEventsFromAbi(abi: any[]) {
.map(
e =>
`${e.name}(${e.inputs
.map((ei: any) => `${ei.indexed ? 'indexed ' : ''}${ei.type}`)
.map((ei: any) => `${ei.indexed ? 'indexed ' : ''}${ei.type} ${ei.name}`)
.join(', ')})`
)
}
Expand All @@ -135,11 +135,16 @@ export const SelectedContract = (props: SelectedContractProps) => {
label: efa,
value: efa,
}))
const mappingFunctionsSelectOptions = mappingFunctionNames.map(mfn => ({
label: mfn,
value: mfn,
}))
const [eventHandlers, setEventHandlers] = useState<Event[]>([{ signature: '', handler: '' }])
const mappingFunctionsSelectOptions = [
{ label: 'newFunction()', value: 'newFunction' },
robertistok marked this conversation as resolved.
Show resolved Hide resolved
...mappingFunctionNames.map(mfn => ({
label: mfn,
value: mfn,
})),
]
const [eventHandlers, setEventHandlers] = useState<Event[]>([
{ signature: '', handler: 'newFunction' },
])

const fetchMetadata = async () => {
const metadataReq = await fetch(`/api/etherscan/${addresses[CHAIN_ID]}/metadata`)
Expand All @@ -165,6 +170,8 @@ export const SelectedContract = (props: SelectedContractProps) => {
}
}, [addresses, abi, source])

console.log(eventHandlers)
robertistok marked this conversation as resolved.
Show resolved Hide resolved

useEffect(() => {
updateContract(addresses[CHAIN_ID], {
events: eventHandlers.filter(eh => eh.handler !== '' && eh.signature !== ''),
Expand Down Expand Up @@ -284,7 +291,9 @@ export const SelectedContract = (props: SelectedContractProps) => {
fullWidth={false}
variant="outline"
className="new-event-handler-btn"
onClick={() => setEventHandlers(prev => [...prev, { signature: '', handler: '' }])}
onClick={() =>
setEventHandlers(prev => [...prev, { signature: '', handler: 'newFunction' }])
}
{...(!contractHasEvents && { disabled: true, title: 'Contract has no events defined' })}>
Add new event handler
</Button>
Expand Down