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

Contract page events | Issue #38 #64

Merged
merged 13 commits into from
Aug 5, 2024
Merged

Conversation

armandocodecr
Copy link
Contributor

@armandocodecr armandocodecr commented Aug 4, 2024

Resolves #38

Implement Contract Events List on Contract Details Page

Summary

This pull request addresses the user story of displaying a list of events involving a contract on its details page. The events are presented in a table format, similar to the Etherscan contract details events page.

Changes

1. address-events.tsx

The AddressEvents component was updated to fetch and display contract events. The component now retrieves the contract's bytecode and logs, decodes the logs using the ABI, and formats the data to display in a table. The table shows the transaction hash, block number, timestamp, method, and decoded logs.

2. utils.ts

New utility functions were added to utils.ts to support the decoding and formatting of logs and timestamps:

  • decodeData: Decodes log data from hexadecimal to human-readable formats (hex, number, address).
  • formatEventLog: Formats logs with decoded arguments, including function name, topics, and decoded data.
  • formatTimestamp: Formats timestamps to include distance from the current time, UTC, and timezone-specific formats.

3. New Component DecodedLogDisplay

A new component DecodedLogDisplay was created to handle the display of decoded log details. This component provides an interface for selecting the display format of the log data (hex, number, address) and dynamically updates the displayed information based on user selection.

4. New Component select.tsx

To facilitate the selection functionality in DecodedLogDisplay, a new Select component was created using the shadcn/ui library. This component allows users to choose the format in which they want to view the decoded log data.

5. package.json

The package.json file was updated to include new dependencies:

  • Added @radix-ui/react-select for the Select component.
  • Added ethers for handling hexadecimal and address conversions. The ethers.js library was necessary for more robust handling of Ethereum data formats, ensuring accurate decoding and formatting of log data.

6. New Interface File events.interface.ts

A new file events.interface.ts was created to define TypeScript interfaces for the decoded arguments and formatted logs. These interfaces ensure consistent typing across the application for decoded event data.

7. getLogs Function Integration

The getLogs function from viem was integrated to fetch logs emitted from the specified contract. This function is essential for retrieving event logs to display on the contract details page.

Validation

  • The implementation should resemble the Etherscan contract details events page.
  • No search button was added.
  • Pagination was not handled at this stage.

Resources

Additional Information

I have created a video on YouTube demonstrating the functionality of my code. Please refer to it for a visual representation of the implemented features.

Demo issue

Copy link

vercel bot commented Aug 4, 2024

@armandocodecr is attempting to deploy a commit to the Walnut Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Collaborator

@saimeunt saimeunt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@armandocodecr Nice PR, this is definitely heading in the right direction, please make the requested changes and keep up the good work.

package.json Outdated
@@ -30,6 +31,7 @@
"clsx": "^2.1.1",
"date-fns": "^3.6.0",
"date-fns-tz": "^3.1.3",
"ethers": "^6.13.2",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not introduce redundant dependencies, this project already use viem and you must stick to it (see https://viem.sh/docs/ethers-migration).

return (
<Card>
<CardHeader>Latest 25 Contract Events</CardHeader>
<CardContent>CONTRACT EVENTS</CardContent>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move your table inside the card content and keep the header.
Also please move your table into its own component in a separate file.

import { formatEventLog, formatTimestamp } from "@/lib/utils";

import { Table, TableHeader, TableRow, TableHead, TableBody, TableCell } from "@/components/ui/table";
import { DecodedLogDisplay } from "@/components/ui/decodedLog-display"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

components/ui is only for shadcn/ui components, move your DecodedLogDisplay component under pages/address.

l2PublicClient.getLogs({ address, fromBlock, toBlock: 'latest' }),
]);

const signatureLookup = new whatsabi.loaders.OpenChainSignatureLookup();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use loadFunctions from lib/signatures.ts instead of raw whatsabi.

...formattedLog,
transactionHash: log.transactionHash,
blockNumber: log.blockNumber,
timestamp: {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure you should use formatTimestamp here, I think it's better to just get the timestamp from the block and call formatTimestamp in the render function.

href={`/block/${item.blockNumber}`}
className="text-sm font-medium leading-none"
>
{item.blockNumber.toString().slice(1)}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why slice(1) ?

</div>
</TableCell>
<TableCell className="align-top">
{item.method}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should display the transaction signature here with TxMethodBadge.

src/lib/utils.ts Outdated
@@ -1,4 +1,7 @@
import { type ClassValue, clsx } from "clsx";
import { getBytes, hexlify, getAddress as ethersGetAddress, isHexString } from "ethers";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will need to replace these by their viem counterpart.

src/lib/utils.ts Outdated
};
}

const eventSignatures = await signatureLookup.loadEvents(log.topics[0] as Address);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise, we have loadEvents in lib/signatures.ts now.

@armandocodecr
Copy link
Contributor Author

Hi @saimeunt !

I have uploaded a few commits that resolve the changes you suggested. I look forward to your feedback!

@saimeunt saimeunt merged commit 1a502eb into walnuthq:main Aug 5, 2024
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Contract page events
2 participants