-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
1 changed file
with
79 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |