Skip to content

Commit 087d462

Browse files
authored
Merge pull request #176 from PotLock/testnet
update campaign totals
2 parents 700a538 + 768e453 commit 087d462

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

campaigns/management/commands/populatecampaigndata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313

14-
CAMPAIGN_CONTRACT_ID =f"v1.campaign.{settings.POTLOCK_TLA}" if settings.ENVIRONMENT=="testnet" else f"v1.campaigns.staging.{settings.POTLOCK_TLA}"
14+
CAMPAIGN_CONTRACT_ID =f"v1.campaign.{settings.POTLOCK_TLA}" if settings.ENVIRONMENT=="testnet" else f"v1.campaigns.{settings.POTLOCK_TLA}"
1515

1616
class Command(BaseCommand):
1717
help = "Pull campaigns data from contract & populate campaigns table."

indexer_app/utils.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,6 +2106,19 @@ async def handle_campaign_donation(data: dict, receipt_id):
21062106
# Fetch USD prices asynchronously
21072107
await donation.fetch_usd_prices_async()
21082108

2109+
# Update campaign totals
2110+
try:
2111+
total_amount = int(data["total_amount"])
2112+
net_amount = int(data["net_amount"])
2113+
2114+
campaign.total_raised_amount = str(int(campaign.total_raised_amount) + total_amount)
2115+
campaign.net_raised_amount = str(int(campaign.net_raised_amount) + net_amount)
2116+
await campaign.asave()
2117+
2118+
logger.info(f"Updated campaign {campaign.on_chain_id} totals: +{total_amount} total, +{net_amount} net")
2119+
except (ValueError, TypeError) as e:
2120+
logger.error(f"Failed to update campaign totals: {e}")
2121+
21092122
except Campaign.DoesNotExist:
21102123
logger.error(f"Campaign {data['campaign_id']} not found for donation")
21112124
except Exception as e:
@@ -2142,14 +2155,28 @@ async def handle_campaign_donation_refund(data: dict, refunded_at):
21422155
else:
21432156
logger.warning(f"No donations found for refund: {donation_ids}")
21442157

2145-
# Update campaign escrow balance
2158+
# Update campaign escrow balance and totals
21462159
try:
21472160
campaign = await Campaign.objects.aget(on_chain_id=campaign_id)
2148-
campaign.escrow_balance = int(campaign.escrow_balance) - int(escrow_balance)
2161+
campaign.escrow_balance = str(int(campaign.escrow_balance) - int(escrow_balance))
2162+
2163+
refunded_donations = CampaignDonation.objects.filter(
2164+
on_chain_id__in=donation_ids,
2165+
campaign__on_chain_id=campaign_id
2166+
).values_list('total_amount', 'net_amount')
2167+
2168+
total_refunded = sum(int(donation[0]) for donation in refunded_donations)
2169+
net_refunded = sum(int(donation[1]) for donation in refunded_donations)
2170+
2171+
campaign.total_raised_amount = str(int(campaign.total_raised_amount) - total_refunded)
2172+
campaign.net_raised_amount = str(int(campaign.net_raised_amount) - net_refunded)
2173+
21492174
await campaign.asave()
2150-
logger.info(f"Updated campaign {campaign_id} escrow balance to {escrow_balance}")
2175+
logger.info(f"Updated campaign {campaign_id}: -{total_refunded} total, -{net_refunded} net, escrow={campaign.escrow_balance}")
21512176
except Campaign.DoesNotExist:
21522177
logger.error(f"Campaign {campaign_id} not found for escrow balance update")
2178+
except (ValueError, TypeError) as e:
2179+
logger.error(f"Failed to update campaign totals after refund: {e}")
21532180

21542181
except Exception as e:
21552182
logger.error(f"Failed to index campaign donation refund: {e}")

0 commit comments

Comments
 (0)