Skip to content

Commit

Permalink
Abi Encoder Events (#150)
Browse files Browse the repository at this point in the history
* Updated titles & tooltip issue

* Added events tab in abi encoder

* Updated 'View'Events' tab in abi encoder as per design

* Updated error component ui for the events tab

* Updated background colors for abi encoder read,write containers

* reverted the title capitalization changes

* Updated 'relative-time' to use 'Intl.RelativeTimeFormat'
  • Loading branch information
acharyarupak391 committed Apr 2, 2024
1 parent 7c039ed commit 5579adf
Show file tree
Hide file tree
Showing 23 changed files with 823 additions and 55 deletions.
4 changes: 2 additions & 2 deletions src/elements/buttons/LinkColorButton.scss
Expand Up @@ -14,7 +14,7 @@
color: colors.$primary-300;
}

&:not(:disabled):not(&[data-disabled]):not(&[data-destructive]) {
&:not(:disabled):not(&[data-disabled]):not(&[data-active="true"]):not(&[data-destructive]) {
&[data-state="hover"],
&:hover {
color: colors.$primary-700;
Expand Down Expand Up @@ -50,7 +50,7 @@
color: colors.$primary-600;
}

&:not(&:disabled):not(&[data-disabled]):not(&[data-destructive]) {
&:not(&:disabled):not(&[data-disabled]):not(&[data-active="true"]):not(&[data-destructive]) {
&[data-state="hover"],
&:hover {
color: colors.$primary-300;
Expand Down
4 changes: 2 additions & 2 deletions src/elements/buttons/LinkGrayButton.scss
Expand Up @@ -12,7 +12,7 @@
color: colors.$gray-300;
}

&:not(:disabled):not(&[data-disabled]):not(&[data-destructive]) {
&:not(:disabled):not(&[data-disabled]):not(&[data-active="true"]):not(&[data-destructive]) {
&[data-state="hover"],
&:hover {
color: colors.$gray-700;
Expand Down Expand Up @@ -44,7 +44,7 @@
color: colors.$gray-600;
}

&:not(&:disabled):not(&[data-disabled]):not(&[data-destructive]) {
&:not(&:disabled):not(&[data-disabled]):not(&[data-active="true"]):not(&[data-destructive]) {
&[data-state="hover"],
&:hover {
color: colors.$gray-300;
Expand Down
6 changes: 3 additions & 3 deletions src/elements/buttons/PrimaryButton.scss
Expand Up @@ -35,7 +35,7 @@
border-color: colors.$primary-200;
}

&:not(&:disabled):not(&[data-disabled]):not([data-destructive]) {
&:not(&:disabled):not(&[data-disabled]):not(&[data-active="true"]):not([data-destructive]) {
&[data-state="hover"],
&:hover {
color: colors.$white;
Expand Down Expand Up @@ -63,7 +63,7 @@
border: 1px solid colors.$error-200;
}

&:not(&:disabled):not(&[data-disabled]) {
&:not(&:disabled):not(&[data-disabled]):not(&[data-active="true"]) {
&[data-state="hover"],
&:hover {
background-color: colors.$error-700;
Expand All @@ -81,7 +81,7 @@
}

.dark .button.primary[data-destructive] {
&:not(&:disabled):not(&[data-disabled]) {
&:not(&:disabled):not(&[data-disabled]):not(&[data-active="true"]) {
&[data-state="focused"],
&:focus,
&:active,
Expand Down
8 changes: 4 additions & 4 deletions src/elements/buttons/SecondaryGrayButton.scss
Expand Up @@ -17,7 +17,7 @@
border: 1px solid colors.$gray-200;
}

&:not(:disabled):not(&[data-disabled]) {
&:not(:disabled):not(&[data-disabled]):not(&[data-active="true"]) {
&[data-state="hover"],
&:hover {
color: colors.$gray-800;
Expand All @@ -44,7 +44,7 @@
border: 1px solid colors.$error-200;
}

&:not(&:disabled):not(&[data-disabled]) {
&:not(&:disabled):not(&[data-disabled]):not(&[data-active="true"]) {
&[data-state="hover"],
&:hover {
color: colors.$error-800;
Expand All @@ -71,7 +71,7 @@
color: colors.$gray-700;
}

&:not(&:disabled):not(&[data-disabled]) {
&:not(&:disabled):not(&[data-disabled]):not(&[data-active="true"]) {
&[data-state="hover"],
&:hover {
color: colors.$gray-100;
Expand All @@ -98,7 +98,7 @@
color: colors.$error-900;
}

&:not(&:disabled):not(&[data-disabled]) {
&:not(&:disabled):not(&[data-disabled]):not(&[data-active="true"]) {
&[data-state="hover"],
&:hover {
color: colors.$error-300;
Expand Down
18 changes: 13 additions & 5 deletions src/react-code/Encoder/Func.jsx
Expand Up @@ -9,6 +9,7 @@ import { Icon } from '../components/Icon'
import { EncodeData } from './FunctionType/encode'
import { ReadContract } from './FunctionType/read'
import { WriteContract } from './FunctionType/write'
import { Events } from './FunctionType/events'
import {
encodeData

Expand All @@ -18,29 +19,35 @@ import { createJoiSchema, getDefaultEncodeData, getFunctionSignature } from './h
const TypeComponent = {
encode_data: EncodeData,
read_contract: ReadContract,
write_contract: WriteContract
write_contract: WriteContract,
view_events: Events
}

const Func = (props) => {
const [isOpen, setIsOpen] = useState(false)

const { type, func, interface: encodeInterface, count, call, isReady, abi } = props
const { inputs, name } = func
const { type, func, interface: encodeInterface, count, call, isReady, abi, address } = props
const { name } = func

const Component = TypeComponent[type]

const [encodedFn, setEncodedFn] = useState('')

useEffect(() => {
if (type !== 'write_contract') return
if (type !== 'write_contract') {
return
}

const encodeSignature = getFunctionSignature(func)
const encodeArgs = getDefaultEncodeData(func)
const _encodedFn = encodeData(encodeInterface, encodeSignature, encodeArgs, (err) => {
console.log(`Error in encoding ${name} method with signature: ${encodeSignature} and args:`, encodeArgs)
console.error(err)
})
if (_encodedFn) setEncodedFn(_encodedFn.slice(0, 10))

if (_encodedFn) {
setEncodedFn(_encodedFn.slice(0, 10))
}
}, [func, encodeInterface, type])

useEffect(() => {
Expand Down Expand Up @@ -74,6 +81,7 @@ const Func = (props) => {
encodeInterface={encodeInterface}
joiSchema={createJoiSchema(func)}
itemIndex={count}
address={address}
/>}
</div>
)
Expand Down
1 change: 1 addition & 0 deletions src/react-code/Encoder/Func.scss
Expand Up @@ -97,6 +97,7 @@
.array.item {
position: relative;
padding-left: 16px;
overflow: visible !important;

button.close, button.plus {
position: absolute;
Expand Down
10 changes: 6 additions & 4 deletions src/react-code/Encoder/FunctionType/decode.jsx
@@ -1,10 +1,9 @@
import { Fragment, useCallback, useState } from 'react'
import { useCallback, useState } from 'react'
import { decodeData, parseEncoded } from '../helpers/solidity/methods'
import { TextArea } from '../../components/TextArea'
import { Button } from '../../components/Button/Button'

import './decode.scss'
import { getTypeInfo } from '../helpers/web3-tools/abi-encoder'

const DecodeData = ({ encodeInterface }) => {
const [encodedData, setEncodedData] = useState('')
Expand All @@ -26,7 +25,10 @@ const DecodeData = ({ encodeInterface }) => {
const _decodedData = decodeData(encodeInterface, parsed, (errorMessage) => {
setError(errorMessage)
})
if (_decodedData) setDecodedData(_decodedData)

if (_decodedData) {
setDecodedData(_decodedData)
}
}, [encodedData])

const ArgList = ({ args, firstParent = true }) => {
Expand Down Expand Up @@ -111,7 +113,7 @@ const DecodeData = ({ encodeInterface }) => {
id={'decode-data-input'}
rows={5}
value={encodedData}
onChange={(e) => setEncodedData(e.target.value)}
onChange={e => setEncodedData(e.target.value)}
error={error}
/>

Expand Down
2 changes: 1 addition & 1 deletion src/react-code/Encoder/FunctionType/encode.scss
Expand Up @@ -21,7 +21,7 @@
}

.dark & {
background-color: colors.$gray-900;
background-color: colors.$black-800;
}

textarea:disabled {
Expand Down
120 changes: 120 additions & 0 deletions src/react-code/Encoder/FunctionType/events.jsx
@@ -0,0 +1,120 @@
import { useCallback, useEffect, useState } from 'react'
import './events.scss'
import { useGetLogs } from '../hooks/useGetLogs'
import { InputWithLabel } from '../../components/InputWithLabel/InputWithLabel'
import { useDebouncedValue } from '../hooks/useDebouncedValue'
import { LogsTable } from '../components/LogsTable'
import { Icon } from '../../components/Icon'

const Events = ({ address, func, encodeInterface }) => {
const [fromBlock, setFromBlock] = useState('')
const [toBlock, setToBlock] = useState('')

const fromBlockDebounced = useDebouncedValue(fromBlock, 500)
const toBlockDebounced = useDebouncedValue(toBlock, 500)

const [logs, setLogs] = useState([])

const { isReady, getLogs, loading, error, resetError, noLogs } = useGetLogs({
address,
func,
iface: encodeInterface
})

const handleSubmit = useCallback(async (from, to) => {
try {
const logs = await getLogs(from, to)
setLogs(logs)
} catch (err) {
console.error(err)
}
}, [getLogs])

useEffect(() => {
if (!isReady || loading || !fromBlockDebounced || !toBlockDebounced) {
return
}

handleSubmit(fromBlockDebounced, toBlockDebounced)
}, [fromBlockDebounced, toBlockDebounced, isReady])

const handleInputChange = (field, event) => {
resetError()

if (field === 'fromBlock') {
setFromBlock(event.target.value)
} else {
setToBlock(event.target.value)
}
}

return (
<div className="events container">
<form onSubmit={handleSubmit}>
<div className='row'>
<InputWithLabel
label={'From block'}
placeholder={'1'}
type={'number'}
id={'events-input-from-block'}
onChange={e => handleInputChange('fromBlock', e)}
value={fromBlock}
/>

<InputWithLabel
label={'To block'}
placeholder={'1200'}
type={'number'}
id={'events-input-to-block'}
onChange={e => handleInputChange('toBlock', e)}
value={toBlock}
/>
</div>
</form>

{
loading && (
<div className='info'>
<p>Loading...</p>
</div>
)
}

{
noLogs && !loading && (
<div className='info'>
<p>No logs found</p>
</div>
)
}

{
(!loading && error) && (
<div className='error'>
<Icon variant='alert-circle' size={'lg'} />

<div>
<p className='title'>Error getting logs</p>
<p className='text'>{error}</p>

<button onClick={() => handleSubmit(fromBlockDebounced, toBlockDebounced)}>
Try Again
<Icon variant='refresh-ccw-02' size={'lg'} />
</button>
</div>
</div>
)
}

{
Boolean(!loading && logs.length) && (
<div className='logs'>
<LogsTable logs={logs} />
</div>
)
}
</div>
)
}

export { Events }

0 comments on commit 5579adf

Please sign in to comment.