Skip to content

Commit

Permalink
Add admin view (#62)
Browse files Browse the repository at this point in the history
* Add admin view

* Refactor function name

* Refactor etherscan link

* Add get_tx_hash abstract method

* Update safe_locking_service/locking_events/admin.py

Co-authored-by: Uxío <Uxio0@users.noreply.github.com>

---------

Co-authored-by: Uxío <Uxio0@users.noreply.github.com>
  • Loading branch information
moisses89 and Uxio0 authored Apr 9, 2024
1 parent d8dad9a commit bb8f924
Showing 1 changed file with 79 additions and 2 deletions.
81 changes: 79 additions & 2 deletions safe_locking_service/locking_events/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,80 @@
# from django.contrib import admin
from abc import abstractmethod
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:
@abstractmethod
def get_tx_hash(self, obj):
pass

def tx_hash_etherscan_link(
self, obj: Union[EthereumTx, LockEvent, UnlockEvent, WithdrawnEvent]
) -> SafeString:
"""
Return the etherscan link for every transaction hash
:param obj:
:return:
"""
ethereum_client = EthereumClientProvider()
etherscan = EtherscanClient(EthereumNetwork(ethereum_client.get_chain_id()))

return format_html(
'<a href="{}/tx/{}">{}</a>',
etherscan.base_url,
self.get_tx_hash(obj),
self.get_tx_hash(obj),
)

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"]

def get_tx_hash(self, obj: EthereumTx):
return obj.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"]

def get_tx_hash(self, obj: Union[LockEvent, UnlockEvent, WithdrawnEvent]):
return obj.ethereum_tx_id

0 comments on commit bb8f924

Please sign in to comment.