Skip to content

Commit

Permalink
Add beaconChainExplorerUriTemplate property (#1241)
Browse files Browse the repository at this point in the history
Adds a new `beaconChainExplorerUriTemplate` property, similar to `blockExplorerUriTemplate`:

- Add nullable `beacon_chain_explorer_uri_public_key_template` property.
- Add relevant serializer and map property
- Add mock value for tests.
- Generate migration and add relevant test.
  • Loading branch information
iamacook authored Oct 2, 2024
1 parent d70363f commit 790fdc9
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.8 on 2024-09-30 07:56

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("chains", "0043_chain_create_call_address_and_more"),
]

operations = [
migrations.AddField(
model_name="chain",
name="beacon_chain_explorer_uri_public_key_template",
field=models.URLField(blank=True, null=True),
),
]
55 changes: 55 additions & 0 deletions src/chains/migrations/tests/test_migration_044.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from django.db.migrations.state import StateApps

from chains.migrations.tests.utils import TestMigrations


class Migration0044TestCase(TestMigrations):
migrate_from = "0043_chain_create_call_address_and_more"
migrate_to = "0044_chain_beacon_chain_explorer_uri_public_key_template"

def setUpBeforeMigration(self, apps: StateApps) -> None:
Chain = apps.get_model("chains", "Chain")
Chain.objects.create(
id=1,
name="Mainnet",
short_name="eth",
description="",
l2=False,
rpc_authentication="API_KEY_PATH",
rpc_uri="https://mainnet.infura.io/v3/",
safe_apps_rpc_authentication="API_KEY_PATH",
safe_apps_rpc_uri="https://mainnet.infura.io/v3/",
block_explorer_uri_address_template="https://etherscan.io/address/{{address}}",
block_explorer_uri_tx_hash_template="https://etherscan.io/tx/{{txHash}}",
currency_name="Ether",
currency_symbol="ETH",
currency_decimals=18,
currency_logo_uri="https://gnosis-safe-token-logos.s3.amazonaws.com/ethereum-eth-logo.png",
transaction_service_uri="http://mainnet-safe-transaction-web.safe.svc.cluster.local",
vpc_transaction_service_uri="",
theme_text_color="#001428",
theme_background_color="#E8E7E6",
ens_registry_address="0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e",
recommended_master_copy_version="1.3.0",
prices_provider_native_coin="ethereum",
prices_provider_chain_name="ethereum",
hidden=False,
balances_provider_chain_name="ethereum",
balances_provider_enabled=True,
safe_singleton_address=None,
safe_proxy_factory_address=None,
multi_send_address=None,
multi_send_call_only_address=None,
fallback_handler_address=None,
sign_message_lib_address=None,
create_call_address=None,
simulate_tx_accessor_address=None,
safe_web_authn_signer_factory_address=None,
)

def test_new_fields(self) -> None:
Chain = self.apps_registry.get_model("chains", "Chain")

chain = Chain.objects.get(id=1)

self.assertEqual(chain.beacon_chain_explorer_uri_public_key_template, None)
3 changes: 3 additions & 0 deletions src/chains/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ class RpcAuthentication(models.TextChoices):
block_explorer_uri_address_template = models.URLField()
block_explorer_uri_tx_hash_template = models.URLField()
block_explorer_uri_api_template = models.URLField()
beacon_chain_explorer_uri_public_key_template = models.URLField(
blank=True, null=True
)
currency_name = models.CharField(max_length=255)
currency_symbol = models.CharField(max_length=255)
currency_decimals = models.IntegerField(default=18)
Expand Down
13 changes: 13 additions & 0 deletions src/chains/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ class BlockExplorerUriTemplateSerializer(serializers.Serializer[Chain]):
api = serializers.URLField(source="block_explorer_uri_api_template")


class BeaconChainExplorerUriTemplateSerializer(serializers.Serializer[Chain]):
public_key = serializers.URLField(
source="beacon_chain_explorer_uri_public_key_template"
)


class FeatureSerializer(serializers.ModelSerializer[Feature]):
class Meta:
fields = ["key"]
Expand Down Expand Up @@ -177,6 +183,7 @@ class ChainSerializer(serializers.ModelSerializer[Chain]):
safe_apps_rpc_uri = serializers.SerializerMethodField()
public_rpc_uri = serializers.SerializerMethodField()
block_explorer_uri_template = serializers.SerializerMethodField()
beacon_chain_explorer_uri_template = serializers.SerializerMethodField()
native_currency = serializers.SerializerMethodField()
prices_provider = serializers.SerializerMethodField()
contract_addresses = serializers.SerializerMethodField()
Expand Down Expand Up @@ -205,6 +212,7 @@ class Meta:
"safe_apps_rpc_uri",
"public_rpc_uri",
"block_explorer_uri_template",
"beacon_chain_explorer_uri_template",
"native_currency",
"prices_provider",
"balances_provider",
Expand Down Expand Up @@ -250,6 +258,11 @@ def get_public_rpc_uri(obj: Chain) -> ReturnDict[Any, Any]:
def get_block_explorer_uri_template(obj: Chain) -> ReturnDict[Any, Any]:
return BlockExplorerUriTemplateSerializer(obj).data

@staticmethod
@swagger_serializer_method(serializer_or_field=BeaconChainExplorerUriTemplateSerializer) # type: ignore[misc]
def get_beacon_chain_explorer_uri_template(obj: Chain) -> ReturnDict[Any, Any]:
return BeaconChainExplorerUriTemplateSerializer(obj).data

@swagger_serializer_method(serializer_or_field=GasPriceSerializer) # type: ignore[misc]
def get_gas_price(self, instance: Chain) -> ReturnDict[Any, Any]:
ranked_gas_prices = instance.gasprice_set.all().order_by("rank")
Expand Down
1 change: 1 addition & 0 deletions src/chains/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Meta:
block_explorer_uri_address_template = factory.Faker("url")
block_explorer_uri_tx_hash_template = factory.Faker("url")
block_explorer_uri_api_template = factory.Faker("url")
beacon_chain_explorer_uri_public_key_template = factory.Faker("url")
currency_name = factory.Faker("cryptocurrency_name")
currency_symbol = factory.Faker("cryptocurrency_code")
currency_decimals = factory.Faker("pyint")
Expand Down
6 changes: 6 additions & 0 deletions src/chains/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ def test_json_payload_format(self) -> None:
"txHash": chain.block_explorer_uri_tx_hash_template,
"api": chain.block_explorer_uri_api_template,
},
"beaconChainExplorerUriTemplate": {
"publicKey": chain.beacon_chain_explorer_uri_public_key_template,
},
"nativeCurrency": {
"name": chain.currency_name,
"symbol": chain.currency_symbol,
Expand Down Expand Up @@ -198,6 +201,9 @@ def test_json_payload_format(self) -> None:
"txHash": chain.block_explorer_uri_tx_hash_template,
"api": chain.block_explorer_uri_api_template,
},
"beaconChainExplorerUriTemplate": {
"publicKey": chain.beacon_chain_explorer_uri_public_key_template,
},
"nativeCurrency": {
"name": chain.currency_name,
"symbol": chain.currency_symbol,
Expand Down

0 comments on commit 790fdc9

Please sign in to comment.