Skip to content

Commit

Permalink
change account to id (#51)
Browse files Browse the repository at this point in the history
* change account to id

* add donate contract
  • Loading branch information
Prometheo committed Jul 25, 2024
1 parent fee6cc0 commit a64918d
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 47 deletions.
2 changes: 1 addition & 1 deletion accounts/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def get(self, request: Request, *args, **kwargs):
applicant=account, status=PotApplicationStatus.APPROVED
)
pot_ids = applications.values_list("pot_id", flat=True)
pots = Pot.objects.filter(id__in=pot_ids)
pots = Pot.objects.filter(account__in=pot_ids)
if request.query_params.get("status") == "live":
pots = pots.filter(
matching_round_start__lte=now, matching_round_end__gte=now
Expand Down
4 changes: 2 additions & 2 deletions donations/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DonationAdmin(admin.ModelAdmin):
"message",
"donor__id",
) # Correct field name from 'donor__address' to 'donor__id' if 'id' is used in the model
list_filter = ("donated_at", "donor", "pot", "pot__id")
list_filter = ("donated_at", "donor", "pot", "pot__account")
date_hierarchy = "donated_at"
ordering = ("-donated_at",)

Expand All @@ -43,7 +43,7 @@ def recipient_address(self, obj):
return obj.recipient.id if obj.recipient else None

def token_address(self, obj):
return obj.token.id
return obj.token.account

def referrer_address(self, obj):
return obj.referrer.id if obj.referrer else None
Expand Down
2 changes: 1 addition & 1 deletion donations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def fetch_usd_prices(self):
price_usd = token.fetch_usd_prices_common(self.donated_at)
if not price_usd:
logger.info(
f"No USD price found for token {token.name} ({token.id.id}) at {self.donated_at}"
f"No USD price found for token {token.name} ({token.account.id}) at {self.donated_at}"
)
return
total_amount = token.format_price(self.total_amount)
Expand Down
10 changes: 5 additions & 5 deletions indexer_app/management/commands/populatedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def handle(self, *args, **options):
if "decimals" in ft_metadata:
token_defaults["decimals"] = ft_metadata["decimals"]
ft_token, _ = Token.objects.update_or_create(
id=ft_acct, defaults=token_defaults
account=ft_acct, defaults=token_defaults
)
if donation.get("referrer_id"):
referrer, _ = Account.objects.get_or_create(
Expand Down Expand Up @@ -260,7 +260,7 @@ def handle(self, *args, **options):
if config.get("chef"):
chef, _ = Account.objects.get_or_create(id=config["chef"])
pot_defaults = {
"pot_factory_id": POTFACTORY_ID,
"pot_factory_account": POTFACTORY_ID,
"owner_id": config["owner"],
"chef_id": config["chef"],
"name": config["pot_name"],
Expand Down Expand Up @@ -311,7 +311,7 @@ def handle(self, *args, **options):
"protocol_config_provider": config["protocol_config_provider"],
}
pot, created = Pot.objects.update_or_create(
id=pot_acct, defaults=pot_defaults
account=pot_acct, defaults=pot_defaults
)
print("pot created? ", created)
print("adding admins")
Expand Down Expand Up @@ -427,7 +427,7 @@ def handle(self, *args, **options):

# pot donations always NEAR
ft_acct, _ = Account.objects.get_or_create(id="near")
ft_token, _ = Token.objects.get_or_create(id=ft_acct)
ft_token, _ = Token.objects.get_or_create(account=ft_acct)
donation_defaults = {
"donor": donor,
"total_amount": donation["total_amount"],
Expand Down Expand Up @@ -483,7 +483,7 @@ def handle(self, *args, **options):
recipient, _ = Account.objects.get_or_create(
id=payout["project_id"]
)
near_token, _ = Token.objects.get_or_create(id=near_acct)
near_token, _ = Token.objects.get_or_create(account=near_acct)
payout_defaults = {
"pot": pot,
"recipient": recipient,
Expand Down
10 changes: 5 additions & 5 deletions indexer_app/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def update_pot_statistics():
matching_pool_donations = Donation.objects.filter(pot=pot, matching_pool=True)
public_donations = Donation.objects.filter(pot=pot, matching_pool=False)
try:
print(f"Processing pot: {pot.id}")
print(f"Processing pot: {pot.account}")

# total matching pool
pot.total_matching_pool = sum(
Expand All @@ -219,17 +219,17 @@ def update_pot_statistics():
jobs_logger.info(f"Total matching pool USD: {pot.total_matching_pool_usd}")

# matching pool balance (get from contract)
url = f"{settings.FASTNEAR_RPC_URL}/account/{pot.id.id}/view/get_config"
url = f"{settings.FASTNEAR_RPC_URL}/account/{pot.account.id}/view/get_config"
response = requests.get(url)
if response.status_code != 200:
jobs_logger.error(
f"Failed to get matching pool balance for pot {pot.id}: {response.text}"
f"Failed to get matching pool balance for pot {pot.account}: {response.text}"
)
else:
data = response.json()
pot.matching_pool_balance = data["matching_pool_balance"]
jobs_logger.info(
f"Matching pool balance for pot {pot.id}: {pot.matching_pool_balance}"
f"Matching pool balance for pot {pot.account}: {pot.matching_pool_balance}"
)

# matching pool donations count
Expand Down Expand Up @@ -273,7 +273,7 @@ def update_pot_statistics():
]
)
except Exception as e:
jobs_logger.error(f"Failed to update statistics for pot {pot.id}: {e}")
jobs_logger.error(f"Failed to update statistics for pot {pot.account}: {e}")


@shared_task
Expand Down
24 changes: 13 additions & 11 deletions indexer_app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async def handle_new_nadabot_registry(
registry, _ = await Account.objects.aget_or_create(id=receiverId)
owner, _ = await Account.objects.aget_or_create(id=data["owner"])
nadabot_registry, created = await NadabotRegistry.objects.aupdate_or_create(
id=registry,
account=registry,
owner=owner,
created_at=created_at,
updated_at=created_at,
Expand Down Expand Up @@ -140,7 +140,7 @@ async def handle_new_pot(
# Create Pot object
logger.info(f"creating pot with owner {owner_id}....")
pot_defaults = {
"pot_factory_id": predecessorId,
"pot_factory_account": predecessorId,
"deployer": signer,
"deployed_at": created_at,
"source_metadata": data["source_metadata"],
Expand Down Expand Up @@ -186,7 +186,7 @@ async def handle_new_pot(
"protocol_config_provider": data["protocol_config_provider"],
}
pot, created = await Pot.objects.aupdate_or_create(
id=receiver, defaults=pot_defaults
account=receiver, defaults=pot_defaults
)

# Add admins to the Pot
Expand Down Expand Up @@ -264,7 +264,7 @@ async def handle_pot_config_update(
}

pot, created = await Pot.objects.aupdate_or_create(
id=receiver_id, defaults=pot_config
account=receiver_id, defaults=pot_config
)

if data.get("admins"):
Expand Down Expand Up @@ -304,7 +304,7 @@ async def handle_new_pot_factory(data: dict, receiver_id: str, created_at: datet
}
# Create Factory object
factory, factory_created = await PotFactory.objects.aupdate_or_create(
id=receiver, defaults=defaults
account=receiver, defaults=defaults
)

# Add admins to the PotFactory
Expand Down Expand Up @@ -663,10 +663,10 @@ async def handle_set_payouts(data: dict, receiver_id: str, receipt: Receipt):

logger.info(f"set payout data: {data}, {receiver_id}")
payouts = data.get("payouts", [])
pot = await Pot.objects.aget(id=receiver_id)
pot = await Pot.objects.aget(account=receiver_id)
near_acct, _ = await Account.objects.aget_or_create(id="near")
near_token, _ = await Token.objects.aget_or_create(
id=near_acct
account=near_acct
) # Pots only support native NEAR

insertion_data = []
Expand Down Expand Up @@ -793,7 +793,7 @@ async def handle_list_admin_removal(data, receiver_id, signer_id, receiptId):
async def handle_add_nadabot_admin(data, receiverId):
logger.info(f"adding admin...: {data}, {receiverId}")
try:
obj = await NadabotRegistry.objects.aget(id=receiverId)
obj = await NadabotRegistry.objects.aget(account=receiverId)

for acct in data["account_ids"]:
user, _ = await Account.objects.aget_or_create(id=acct)
Expand Down Expand Up @@ -852,6 +852,8 @@ async def handle_new_donation(
)

try:
# insert donate contract which is the receiver id(because of activity relationship mainly)
donate_contract, _ = await Account.objects.aget_or_create(id=receiver_id)
# Upsert donor account
donor, _ = await Account.objects.aget_or_create(id=donation_data["donor_id"])
recipient = None
Expand Down Expand Up @@ -901,7 +903,7 @@ async def handle_new_donation(
if "decimals" in ft_metadata:
token_defaults["decimals"] = ft_metadata["decimals"]
token, _ = await Token.objects.aupdate_or_create(
id=token_acct, defaults=token_defaults
account=token_acct, defaults=token_defaults
)

except Exception as e:
Expand Down Expand Up @@ -933,7 +935,7 @@ async def handle_new_donation(
}

if donation_type == "pot":
default_data["pot"] = await Pot.objects.aget(id=receiver_id)
default_data["pot"] = await Pot.objects.aget(account=receiver_id)

logger.info(f"default donation data: {default_data}")

Expand Down Expand Up @@ -1043,7 +1045,7 @@ async def handle_update_default_human_threshold(data: dict, receiverId: str):

try:

reg = await NadabotRegistry.objects.filter(id=receiverId).aupdate(
reg = await NadabotRegistry.objects.filter(account=receiverId).aupdate(
**{"default_human_threshold": data["default_human_threshold"]}
)
logger.info("updated threshold..")
Expand Down
18 changes: 18 additions & 0 deletions nadabot/migrations/0002_rename_id_nadabotregistry_account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.6 on 2024-07-19 22:52

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("nadabot", "0001_initial"),
]

operations = [
migrations.RenameField(
model_name="nadabotregistry",
old_name="id",
new_name="account",
),
]
2 changes: 1 addition & 1 deletion nadabot/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class RuleType(models.TextChoices):


class NadabotRegistry(models.Model):
id = models.OneToOneField(
account = models.OneToOneField(
Account,
related_name="registry_id",
on_delete=models.CASCADE,
Expand Down
14 changes: 7 additions & 7 deletions pots/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def __init__(self, *args, **kwargs):
@admin.register(PotFactory)
class PotFactoryAdmin(admin.ModelAdmin):
form = PotFactoryForm
list_display = ("id", "owner", "deployed_at")
search_fields = ("id", "owner__id")
list_display = ("account", "owner", "deployed_at")
search_fields = ("account", "owner__id")

def get_form(self, request, obj=None, **kwargs):
form = super(PotFactoryAdmin, self).get_form(request, obj, **kwargs)
Expand Down Expand Up @@ -79,8 +79,8 @@ def __init__(self, *args, **kwargs):
@admin.register(Pot)
class PotAdmin(admin.ModelAdmin):
form = PotForm
list_display = ("id", "pot_factory", "deployer", "deployed_at", "name")
search_fields = ("id", "name", "deployer__id")
list_display = ("account", "pot_factory", "deployer", "deployed_at", "name")
search_fields = ("account", "name", "deployer__id")
list_filter = ("deployed_at",)

def get_form(self, request, obj=None, **kwargs):
Expand Down Expand Up @@ -118,7 +118,7 @@ def __init__(self, *args, **kwargs):
class PotApplicationAdmin(admin.ModelAdmin):
form = PotApplicationAdminForm
list_display = ("id", "pot", "applicant", "status", "submitted_at")
search_fields = ("pot__id", "applicant__id")
search_fields = ("pot__account", "applicant__id")
list_filter = ("status", "submitted_at")
autocomplete_fields = ["applicant"]

Expand Down Expand Up @@ -188,7 +188,7 @@ def application_applicant_id(self, obj):
@admin.register(PotPayout)
class PotPayoutAdmin(admin.ModelAdmin):
list_display = ("id", "pot", "recipient", "amount", "amount_paid_usd", "paid_at")
search_fields = ("pot__id", "recipient__id")
search_fields = ("pot__account", "recipient__id")
list_filter = ("paid_at",)

def has_add_permission(self, request):
Expand All @@ -204,7 +204,7 @@ def has_delete_permission(self, request, obj=None):
@admin.register(PotPayoutChallenge)
class PotPayoutChallengeAdmin(admin.ModelAdmin):
list_display = ("id", "challenger", "pot", "message", "created_at")
search_fields = ("challenger__id", "pot__id")
search_fields = ("challenger__id", "pot__account")
list_filter = ("created_at",)

def has_add_permission(self, request):
Expand Down
10 changes: 5 additions & 5 deletions pots/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class PotDetailAPI(APIView):
def get(self, request: Request, *args, **kwargs):
pot_id = kwargs.get("pot_id")
try:
pot = Pot.objects.get(id=pot_id)
pot = Pot.objects.get(account=pot_id)
except Pot.DoesNotExist:
return Response({"message": f"Pot with ID {pot_id} not found."}, status=404)
serializer = PotSerializer(pot)
Expand Down Expand Up @@ -129,7 +129,7 @@ class PotApplicationsAPI(APIView, PageNumberPagination):
def get(self, request: Request, *args, **kwargs):
pot_id = kwargs.get("pot_id")
try:
pot = Pot.objects.get(id=pot_id)
pot = Pot.objects.get(account=pot_id)
except Pot.DoesNotExist:
return Response({"message": f"Pot with ID {pot_id} not found."}, status=404)

Expand Down Expand Up @@ -166,7 +166,7 @@ class PotDonationsAPI(APIView, PageNumberPagination):
def get(self, request: Request, *args, **kwargs):
pot_id = kwargs.get("pot_id")
try:
pot = Pot.objects.get(id=pot_id)
pot = Pot.objects.get(account=pot_id)
except Pot.DoesNotExist:
return Response({"message": f"Pot with ID {pot_id} not found."}, status=404)

Expand Down Expand Up @@ -203,7 +203,7 @@ class PotSponsorsAPI(APIView, PageNumberPagination):
def get(self, request: Request, *args, **kwargs):
pot_id = kwargs.get("pot_id")
try:
pot = Pot.objects.get(id=pot_id)
pot = Pot.objects.get(account=pot_id)
except Pot.DoesNotExist:
return Response({"message": f"Pot with ID {pot_id} not found."}, status=404)

Expand Down Expand Up @@ -245,7 +245,7 @@ class PotPayoutsAPI(APIView, PageNumberPagination):
def get(self, request: Request, *args, **kwargs):
pot_id = kwargs.get("pot_id")
try:
pot = Pot.objects.get(id=pot_id)
pot = Pot.objects.get(account=pot_id)
except Pot.DoesNotExist:
return Response({"message": f"Pot with ID {pot_id} not found."}, status=404)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 5.0.6 on 2024-07-19 22:52

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("pots", "0010_alter_potpayout_paid_at"),
]

operations = [
migrations.RenameField(
model_name="pot",
old_name="id",
new_name="account",
),
migrations.RenameField(
model_name="potfactory",
old_name="id",
new_name="account",
),
]
6 changes: 3 additions & 3 deletions pots/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


class PotFactory(models.Model):
id = models.OneToOneField(
account = models.OneToOneField(
Account,
primary_key=True,
related_name="pot_factory",
Expand Down Expand Up @@ -71,7 +71,7 @@ class Meta:


class Pot(models.Model):
id = models.OneToOneField(
account = models.OneToOneField(
Account,
primary_key=True,
related_name="pot",
Expand Down Expand Up @@ -480,7 +480,7 @@ def fetch_usd_prices(self):
return
self.amount_paid_usd = token.format_price(self.amount) * price_usd
self.save()
logger.info(f"Saved USD prices for pot payout for pot id: {self.pot.id}")
logger.info(f"Saved USD prices for pot payout for pot id: {self.pot.account}")
except Exception as e:
logger.error(f"Failed to calculate and save USD prices: {e}")

Expand Down
Loading

0 comments on commit a64918d

Please sign in to comment.