Skip to content

Commit

Permalink
add chains app & Chain model (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlanglen authored Jul 15, 2024
1 parent e52f2d9 commit f6745dc
Show file tree
Hide file tree
Showing 14 changed files with 230 additions and 0 deletions.
28 changes: 28 additions & 0 deletions accounts/migrations/0004_account_chain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 5.0.4 on 2024-07-15 18:23

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("accounts", "0003_alter_account_options"),
("chains", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="account",
name="chain",
field=models.ForeignKey(
blank=True,
help_text="Blockchain this account is located on.",
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="accounts",
related_query_name="account",
to="chains.chain",
),
),
]
26 changes: 26 additions & 0 deletions accounts/migrations/0005_alter_account_chain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 5.0.4 on 2024-07-15 18:26

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("accounts", "0004_account_chain"),
("chains", "0002_add_near_chain"),
]

operations = [
migrations.AlterField(
model_name="account",
name="chain",
field=models.ForeignKey(
help_text="Blockchain this account is located on.",
on_delete=django.db.models.deletion.CASCADE,
related_name="accounts",
related_query_name="account",
to="chains.chain",
),
),
]
10 changes: 10 additions & 0 deletions accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.utils.translation import gettext_lazy as _

from base.logging import logger
from chains.models import Chain


class Account(models.Model):
Expand All @@ -17,6 +18,15 @@ class Account(models.Model):
validators=[],
help_text=_("On-chain account address."),
)
chain = models.ForeignKey(
Chain,
null=False,
blank=False,
on_delete=models.CASCADE,
related_name="accounts",
related_query_name="account",
help_text=_("Blockchain this account is located on."),
)
total_donations_in_usd = models.DecimalField(
_("total donations received in USD"),
max_digits=20,
Expand Down
2 changes: 2 additions & 0 deletions base/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
"django.contrib.staticfiles",
"rest_framework",
"drf_spectacular",
"django_extensions",
"corsheaders",
# "cachalot",
"celery",
Expand All @@ -104,6 +105,7 @@
"pots",
"tokens",
"nadabot",
"chains",
]

DEFAULT_PAGE_SIZE = 30
Expand Down
Empty file added chains/__init__.py
Empty file.
18 changes: 18 additions & 0 deletions chains/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.contrib import admin

from .models import Chain


@admin.register(Chain)
class ChainAdmin(admin.ModelAdmin):
list_display = (
"name",
"name_slug",
"rpc_url",
"explorer_url",
"evm_compat",
"evm_chain_id",
)
search_fields = ("name", "name_slug", "rpc_url", "explorer_url", "evm_chain_id")
list_filter = ("evm_compat",)
ordering = ("name",)
6 changes: 6 additions & 0 deletions chains/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class ChainsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "chains"
60 changes: 60 additions & 0 deletions chains/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Generated by Django 5.0.4 on 2024-07-15 18:23

import django_extensions.db.fields
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="Chain",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(db_index=True, max_length=32, unique=True)),
(
"name_slug",
django_extensions.db.fields.AutoSlugField(
blank=True,
editable=False,
max_length=32,
populate_from=("name",),
unique=True,
),
),
("rpc_url", models.URLField()),
("explorer_url", models.URLField()),
("evm_compat", models.BooleanField()),
(
"evm_chain_id",
models.IntegerField(blank=True, db_index=True, null=True),
),
],
options={
"ordering": ("name",),
},
),
migrations.AddConstraint(
model_name="chain",
constraint=models.CheckConstraint(
check=models.Q(
models.Q(("evm_chain_id__isnull", False), ("evm_compat", True)),
models.Q(("evm_chain_id__isnull", True), ("evm_compat", False)),
_connector="OR",
),
name="evm_chain_id_check",
),
),
]
25 changes: 25 additions & 0 deletions chains/migrations/0002_add_near_chain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 5.0.4 on 2024-07-15 18:24

from django.db import migrations


def create_near_chain(apps, schema_editor):
Chain = apps.get_model("chains", "Chain")
# Create the "near" chain
near_chain, created = Chain.objects.get_or_create(
name="NEAR", defaults={"evm_compat": False}
)

# Set "near" chain as the default for all existing accounts
Account = apps.get_model("accounts", "Account")
Account.objects.update(chain=near_chain)
print("Updated all accounts to use NEAR chain")


class Migration(migrations.Migration):

dependencies = [("chains", "0001_initial"), ("accounts", "0004_account_chain")]

operations = [
migrations.RunPython(create_near_chain),
]
Empty file added chains/migrations/__init__.py
Empty file.
48 changes: 48 additions & 0 deletions chains/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from django.db import models
from django_extensions.db.fields import AutoSlugField


class Chain(models.Model):
############################
# BLOCKCHAIN MODEL FIELDS
############################

# GENERAL
name = models.CharField(
max_length=32, unique=True, blank=False, null=False, db_index=True
)
name_slug = AutoSlugField(
populate_from=("name",),
max_length=32,
unique=True,
blank=False,
null=False,
db_index=True,
)

# URLS
rpc_url = models.URLField()
explorer_url = models.URLField()

# EVM
evm_compat = models.BooleanField(null=False, blank=False)
evm_chain_id = models.IntegerField(null=True, blank=True, db_index=True)

############################
# META
############################
class Meta:
# default record ordering
ordering = ("name",)

constraints = [
# if an evm then the evm_chain_id id must be set otherwise should be null
models.CheckConstraint(
name="evm_chain_id_check",
check=models.Q(evm_compat=True, evm_chain_id__isnull=False)
| models.Q(evm_compat=False, evm_chain_id__isnull=True),
),
]

def __str__(self):
return self.name
3 changes: 3 additions & 0 deletions chains/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions chains/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ sentry-sdk = { extras = ["django"], version = "^1.45.0" }
watchtower = "^3.1.0"
django-cors-headers = "^4.3.1"
drf-spectacular = "^0.27.2"
django-extensions = "^3.2.3"

[tool.poetry.group.dev.dependencies]
black = "^24.3.0"
Expand Down

0 comments on commit f6745dc

Please sign in to comment.