Skip to content

Commit

Permalink
Merge pull request #20 from PotLock/refactor-batch-dpnation
Browse files Browse the repository at this point in the history
refactor batch function, edit model
  • Loading branch information
lachlanglen authored May 3, 2024
2 parents 8aeb330 + d03c11a commit a615839
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 276 deletions.
17 changes: 17 additions & 0 deletions activities/migrations/0002_alter_activity_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.0.4 on 2024-05-03 10:29

from django.db import migrations


class Migration(migrations.Migration):

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

operations = [
migrations.AlterModelOptions(
name="activity",
options={"verbose_name_plural": "Activities"},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 5.0.4 on 2024-05-03 10:29

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("accounts", "0001_initial"),
("donations", "0004_alter_donation_on_chain_id"),
("pots", "0001_initial"),
]

operations = [
migrations.RemoveConstraint(
model_name="donation",
name="unique_pot_on_chain_id",
),
migrations.AddConstraint(
model_name="donation",
constraint=models.UniqueConstraint(
condition=models.Q(("pot__isnull", True)),
fields=("on_chain_id",),
name="unique_on_chain_id_when_pot_is_null",
),
),
migrations.AddConstraint(
model_name="donation",
constraint=models.UniqueConstraint(
condition=models.Q(("pot__isnull", False)),
fields=("on_chain_id", "pot"),
name="unique_on_chain_id_with_pot",
),
),
]
11 changes: 9 additions & 2 deletions donations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ class Donation(models.Model):
class Meta:
constraints = [
models.UniqueConstraint(
fields=["on_chain_id", "pot"], name="unique_pot_on_chain_id"
)
fields=["on_chain_id"],
condition=models.Q(pot__isnull=True),
name="unique_on_chain_id_when_pot_is_null",
),
models.UniqueConstraint(
fields=["on_chain_id", "pot"],
condition=models.Q(pot__isnull=False),
name="unique_on_chain_id_with_pot",
),
]
38 changes: 23 additions & 15 deletions indexer_app/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
from django.core.cache import cache
from near_lake_framework import near_primitives

from base.utils import convert_ns_to_utc
from pots.utils import match_pot_factory_version_pattern

from .logging import logger
from .utils import (
handle_batch_donations,
handle_default_list_status_change,
handle_list_admin_removal,
handle_list_registration_update,
Expand All @@ -22,8 +26,6 @@
handle_set_payouts,
handle_transfer_payout,
)
from base.utils import convert_ns_to_utc
from pots.utils import match_pot_factory_version_pattern


async def handle_streamer_message(streamer_message: near_primitives.StreamerMessage):
Expand Down Expand Up @@ -62,7 +64,7 @@ async def handle_streamer_message(streamer_message: near_primitives.StreamerMess
try:
parsed_log = json.loads(log[len("EVENT_JSON:") :])
except json.JSONDecodeError:
logging.warning(
logger.warning(
f"Receipt ID: `{receipt_execution_outcome.receipt.receipt_id}`\nError during parsing logs from JSON string to dict"
)
continue
Expand Down Expand Up @@ -158,16 +160,24 @@ async def handle_streamer_message(streamer_message: near_primitives.StreamerMess

case "donate": # TODO: donate that produces result
logger.info(f"switching bazooka to knifee works!! donate his blood: {args_dict}, {receipt}, {action}, {log_data}")
await handle_new_donations(
args_dict,
receiver_id,
signer_id,
"direct",
receipt,
status_obj,
log_data,
created_at,
)
if len(log_data) > 1:
await handle_batch_donations(
receiver_id,
signer_id,
"direct",
receipt,
log_data
)
else:
await handle_new_donations(
args_dict,
receiver_id,
signer_id,
"direct",
receipt,
status_obj,
log_data,
)
break

case "handle_protocol_fee_callback":
Expand All @@ -180,7 +190,6 @@ async def handle_streamer_message(streamer_message: near_primitives.StreamerMess
receipt,
status_obj,
log_data,
created_at,
)
break

Expand All @@ -194,7 +203,6 @@ async def handle_streamer_message(streamer_message: near_primitives.StreamerMess
receipt,
status_obj,
log_data,
created_at,
)
break

Expand Down
22 changes: 22 additions & 0 deletions indexer_app/migrations/0002_blockheight_block_timestamp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 5.0.4 on 2024-05-03 10:29

from django.db import migrations, models


class Migration(migrations.Migration):

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

operations = [
migrations.AddField(
model_name="blockheight",
name="block_timestamp",
field=models.DateTimeField(
help_text="date equivalent of the block height.",
null=True,
verbose_name="block timestamp",
),
),
]
5 changes: 5 additions & 0 deletions indexer_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ class BlockHeight(models.Model):
_("blockheight value"),
help_text=_("the last blockheight saved to db."),
)
block_timestamp = models.DateTimeField(
_("block timestamp"),
help_text=_("date equivalent of the block height."),
null=True,
)
updated_at = models.DateTimeField(
_("updated at"),
help_text=_("block height last update at."),
Expand Down
3 changes: 2 additions & 1 deletion indexer_app/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ async def indexer(network: str, from_block: int, to_block: int):
"current_block_height",
streamer_message.block.header.height,
block_count,
streamer_message.block.header.timestamp,
) # current block height
await handle_streamer_message(streamer_message)
except Exception as e:
Expand All @@ -50,7 +51,7 @@ def listen_to_near_events():
try:
# Update below with desired network & block height
# start_block = get_block_height('current_block_height')
start_block = 105_923_501 # manually setting for debugging TODO: remove this
start_block = 104_858_762 # manually setting for debugging TODO: remove this
logger.info(f"what's the start block, pray tell? {start_block-1}")
loop.run_until_complete(indexer("mainnet", start_block - 1, None))
finally:
Expand Down
Loading

0 comments on commit a615839

Please sign in to comment.