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

Add admin view #62

Merged
merged 5 commits into from
Apr 9, 2024
Merged
Changes from 3 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
74 changes: 72 additions & 2 deletions safe_locking_service/locking_events/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,73 @@
# from django.contrib import admin
from typing import Union

# Register your models here.
from django.contrib import admin
from django.utils.html import format_html
from django.utils.safestring import SafeString

from gnosis.eth.clients.etherscan_client import EtherscanClient
from gnosis.eth.django.admin import AdvancedAdminSearchMixin
from gnosis.eth.ethereum_client import EthereumClientProvider
from gnosis.eth.ethereum_network import EthereumNetwork

from safe_locking_service.locking_events.models import (
EthereumTx,
LockEvent,
UnlockEvent,
WithdrawnEvent,
)


class TxHashEtherscanMixin:
def tx_hash_etherscan_link(
self, obj: Union[LockEvent, UnlockEvent, WithdrawnEvent]
) -> SafeString:
"""
Return the etherscan link by transaction hash

moisses89 marked this conversation as resolved.
Show resolved Hide resolved
:param obj:
:return:
"""
ethereum_client = EthereumClientProvider()
etherscan = EtherscanClient(EthereumNetwork(ethereum_client.get_chain_id()))

if isinstance(obj, EthereumTx):
tx_hash = obj.tx_hash
Copy link
Member

Choose a reason for hiding this comment

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

Instead of doing this maybe define an abstract method get_tx_hash so you can define that function in every object inheriting this. Also you could add this

else:
tx_hash = obj.ethereum_tx_id
return format_html(
'<a href="{}/tx/{}">{}</a>',
etherscan.base_url,
tx_hash,
tx_hash,
)

tx_hash_etherscan_link.short_description = "Tx hash etherscan link"
tx_hash_etherscan_link.allow_tags = True


@admin.register(EthereumTx)
class EthereumTxAdmin(AdvancedAdminSearchMixin, admin.ModelAdmin, TxHashEtherscanMixin):
list_display = (
"block_timestamp",
"tx_hash_etherscan_link",
"block_number",
"block_hash",
"confirmed",
)
ordering = ["-block_timestamp"]
list_filter = ("confirmed",)
search_fields = ["==tx_hash"]


@admin.register(LockEvent, UnlockEvent, WithdrawnEvent)
class CommonEventAdmin(
AdvancedAdminSearchMixin, admin.ModelAdmin, TxHashEtherscanMixin
):
list_display = (
"timestamp",
"tx_hash_etherscan_link",
"holder",
"amount",
)
ordering = ["-timestamp"]
search_fields = ["==ethereum_tx_id", "==holder"]
Loading