diff --git a/src/chains/migrations/0020_block_explorer_templates.py b/src/chains/migrations/0020_block_explorer_templates.py new file mode 100644 index 00000000..ba6c7685 --- /dev/null +++ b/src/chains/migrations/0020_block_explorer_templates.py @@ -0,0 +1,36 @@ +# Generated by Django 3.2.5 on 2021-07-26 08:25 +from django.db import migrations, models +from django.db.models import F + + +def copy_block_explorer_uri(apps, schema_editor): + chain_model = apps.get_model("chains", "chain") + db_alias = schema_editor.connection.alias + chain_model.objects.using(db_alias).all().update( + block_explorer_uri_address_template=F("block_explorer_uri"), + block_explorer_uri_tx_hash_template=F("block_explorer_uri"), + ) + + +class Migration(migrations.Migration): + dependencies = [ + ("chains", "0019_add_safe_apps_rpc"), + ] + + operations = [ + migrations.AddField( + model_name="chain", + name="block_explorer_uri_address_template", + field=models.URLField(default=""), + preserve_default=False, + ), + migrations.AddField( + model_name="chain", + name="block_explorer_uri_tx_hash_template", + field=models.URLField(default=""), + preserve_default=False, + ), + # Reverse means deleting the fields but this is accomplished via the reverse of AddField + # so an empty lambda function is provided + migrations.RunPython(copy_block_explorer_uri, lambda apps, schema_editor: None), + ] diff --git a/src/chains/models.py b/src/chains/models.py index d9b883a4..aae5ab6f 100644 --- a/src/chains/models.py +++ b/src/chains/models.py @@ -37,7 +37,11 @@ class RpcAuthentication(models.TextChoices): default=RpcAuthentication.NO_AUTHENTICATION, ) safe_apps_rpc_uri = models.URLField(default="") - block_explorer_uri = models.URLField() + block_explorer_uri = ( + models.URLField() + ) # TODO: Remove in a later migration (once the clients no longer need it) + block_explorer_uri_address_template = models.URLField() + block_explorer_uri_tx_hash_template = models.URLField() currency_name = models.CharField(max_length=255) currency_symbol = models.CharField(max_length=255) currency_decimals = models.IntegerField(default=18) diff --git a/src/chains/serializers.py b/src/chains/serializers.py index ee0dd95a..1c09952f 100644 --- a/src/chains/serializers.py +++ b/src/chains/serializers.py @@ -63,11 +63,17 @@ def get_value(self, obj): return obj.safe_apps_rpc_uri +class BlockExplorerUriTemplateSerializer(serializers.Serializer): + address = serializers.URLField(source="block_explorer_uri_address_template") + tx_hash = serializers.URLField(source="block_explorer_uri_tx_hash_template") + + class ChainSerializer(serializers.ModelSerializer): chain_id = serializers.CharField(source="id") chain_name = serializers.CharField(source="name") rpc_uri = serializers.SerializerMethodField() safe_apps_rpc_uri = serializers.SerializerMethodField() + block_explorer_uri_template = serializers.SerializerMethodField() native_currency = serializers.SerializerMethodField() transaction_service = serializers.URLField( source="transaction_service_uri", default=None @@ -83,6 +89,7 @@ class Meta: "chain_name", "rpc_uri", "safe_apps_rpc_uri", + "block_explorer_uri_template", "block_explorer_uri", "native_currency", "transaction_service", @@ -112,6 +119,11 @@ def get_safe_apps_rpc_uri(obj): def get_rpc_uri(obj): return RpcUriSerializer(obj).data + @staticmethod + @swagger_serializer_method(serializer_or_field=BlockExplorerUriTemplateSerializer) + def get_block_explorer_uri_template(obj): + return BlockExplorerUriTemplateSerializer(obj).data + @staticmethod def get_gas_price(obj): if obj.gas_price_oracle_uri and obj.gas_price_fixed_wei is None: diff --git a/src/chains/tests/factories.py b/src/chains/tests/factories.py index b851ae6c..1d827e62 100644 --- a/src/chains/tests/factories.py +++ b/src/chains/tests/factories.py @@ -23,6 +23,8 @@ class Meta: ) safe_apps_rpc_uri = factory.Faker("url") block_explorer_uri = factory.Faker("url") + block_explorer_uri_address_template = factory.Faker("url") + block_explorer_uri_tx_hash_template = factory.Faker("url") currency_name = factory.Faker("cryptocurrency_name") currency_symbol = factory.Faker("cryptocurrency_code") currency_decimals = factory.Faker("pyint") diff --git a/src/chains/tests/test_views.py b/src/chains/tests/test_views.py index 55fe08ad..f0d5f5b7 100644 --- a/src/chains/tests/test_views.py +++ b/src/chains/tests/test_views.py @@ -36,6 +36,10 @@ def test_json_payload_format(self): "value": chain.safe_apps_rpc_uri, }, "blockExplorerUri": chain.block_explorer_uri, + "blockExplorerUriTemplate": { + "address": chain.block_explorer_uri_address_template, + "txHash": chain.block_explorer_uri_tx_hash_template, + }, "nativeCurrency": { "name": chain.currency_name, "symbol": chain.currency_symbol, @@ -134,6 +138,10 @@ def test_json_payload_format(self): "value": chain.safe_apps_rpc_uri, }, "blockExplorerUri": chain.block_explorer_uri, + "blockExplorerUriTemplate": { + "address": chain.block_explorer_uri_address_template, + "txHash": chain.block_explorer_uri_tx_hash_template, + }, "nativeCurrency": { "name": chain.currency_name, "symbol": chain.currency_symbol, @@ -181,6 +189,10 @@ def test_match(self): "value": chain.safe_apps_rpc_uri, }, "block_explorer_uri": chain.block_explorer_uri, + "block_explorer_uri_template": { + "address": chain.block_explorer_uri_address_template, + "tx_hash": chain.block_explorer_uri_tx_hash_template, + }, "native_currency": { "name": chain.currency_name, "symbol": chain.currency_symbol,