Skip to content

Commit

Permalink
Add block explorer uri templates (#176)
Browse files Browse the repository at this point in the history
Add Block Explorer URI templates for address and tx_hash
  • Loading branch information
fmrsabino authored Jul 26, 2021
1 parent 24ffd9f commit b792f3b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/chains/migrations/0020_block_explorer_templates.py
Original file line number Diff line number Diff line change
@@ -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),
]
6 changes: 5 additions & 1 deletion src/chains/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions src/chains/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions src/chains/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
12 changes: 12 additions & 0 deletions src/chains/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b792f3b

Please sign in to comment.