From e949d8ff06ac1ad3f61a6276b8693f59f2a3ee2c Mon Sep 17 00:00:00 2001 From: Benjamin Gobeil Date: Thu, 21 Nov 2024 14:28:08 -0500 Subject: [PATCH] Associate pool events with pool address --- .../src/mappings/BullaFactoring.ts | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/bulla-contracts/src/mappings/BullaFactoring.ts b/bulla-contracts/src/mappings/BullaFactoring.ts index db12f7b..72ed2a5 100644 --- a/bulla-contracts/src/mappings/BullaFactoring.ts +++ b/bulla-contracts/src/mappings/BullaFactoring.ts @@ -62,6 +62,7 @@ export function handleInvoiceFunded(event: InvoiceFunded, version: string): void InvoiceFundedEvent.originalCreditor = ev.originalCreditor; InvoiceFundedEvent.upfrontBps = upfrontBps; const original_creditor = getOrCreateUser(ev.originalCreditor); + const pool = getOrCreateUser(event.address); // Update the price history const price_per_share = getOrCreatePricePerShare(event, version); @@ -92,9 +93,11 @@ export function handleInvoiceFunded(event: InvoiceFunded, version: string): void InvoiceFundedEvent.targetTax = targetTax; original_creditor.factoringEvents = original_creditor.factoringEvents ? original_creditor.factoringEvents.concat([InvoiceFundedEvent.id]) : [InvoiceFundedEvent.id]; + pool.factoringEvents = pool.factoringEvents ? pool.factoringEvents.concat([InvoiceFundedEvent.id]) : [InvoiceFundedEvent.id]; InvoiceFundedEvent.save(); original_creditor.save(); + pool.save(); price_per_share.save(); historical_factoring_statistics.save(); } @@ -118,6 +121,7 @@ export function handleInvoiceKickbackAmountSent(event: InvoiceKickbackAmountSent InvoiceKickbackAmountSentEvent.kickbackAmount = ev.kickbackAmount; InvoiceKickbackAmountSentEvent.originalCreditor = ev.originalCreditor; const original_creditor = getOrCreateUser(ev.originalCreditor); + const pool = getOrCreateUser(event.address); const price_per_share = getOrCreatePricePerShare(event, version); const latestPrice = getLatestPrice(event, version); const historical_factoring_statistics = getOrCreateHistoricalFactoringStatistics(event, version); @@ -135,8 +139,13 @@ export function handleInvoiceKickbackAmountSent(event: InvoiceKickbackAmountSent ? original_creditor.factoringEvents.concat([InvoiceKickbackAmountSentEvent.id]) : [InvoiceKickbackAmountSentEvent.id]; + pool.factoringEvents = pool.factoringEvents + ? pool.factoringEvents.concat([InvoiceKickbackAmountSentEvent.id]) + : [InvoiceKickbackAmountSentEvent.id]; + InvoiceKickbackAmountSentEvent.save(); original_creditor.save(); + pool.save(); price_per_share.save(); historical_factoring_statistics.save(); } @@ -166,6 +175,7 @@ export function handleInvoicePaid(event: InvoicePaid, version: string): void { InvoiceReconciledEvent.trueTax = trueTax; const original_creditor = getOrCreateUser(ev.originalCreditor); + const pool = getOrCreateUser(event.address); const price_per_share = getOrCreatePricePerShare(event, version); const latestPrice = getLatestPrice(event, version); const historical_factoring_statistics = getOrCreateHistoricalFactoringStatistics(event, version); @@ -184,8 +194,13 @@ export function handleInvoicePaid(event: InvoicePaid, version: string): void { ? original_creditor.factoringEvents.concat([InvoiceReconciledEvent.id]) : [InvoiceReconciledEvent.id]; + pool.factoringEvents = pool.factoringEvents + ? pool.factoringEvents.concat([InvoiceReconciledEvent.id]) + : [InvoiceReconciledEvent.id]; + InvoiceReconciledEvent.save(); original_creditor.save(); + pool.save(); price_per_share.save(); historical_factoring_statistics.save(); pool_pnl.save(); @@ -213,6 +228,7 @@ export function handleInvoiceUnfactoredV1(event: InvoiceUnfactoredV1): void { InvoiceUnfactoredEvent.invoiceId = underlyingClaim.id; InvoiceUnfactoredEvent.originalCreditor = ev.originalCreditor; const original_creditor = getOrCreateUser(ev.originalCreditor); + const pool = getOrCreateUser(event.address); const price_per_share = getOrCreatePricePerShare(event, "v1"); const latestPrice = getLatestPrice(event, "v1"); const historical_factoring_statistics = getOrCreateHistoricalFactoringStatistics(event, "v1"); @@ -236,10 +252,15 @@ export function handleInvoiceUnfactoredV1(event: InvoiceUnfactoredV1): void { ? original_creditor.factoringEvents.concat([InvoiceUnfactoredEvent.id]) : [InvoiceUnfactoredEvent.id]; + pool.factoringEvents = pool.factoringEvents + ? pool.factoringEvents.concat([InvoiceUnfactoredEvent.id]) + : [InvoiceUnfactoredEvent.id]; + const pool_pnl = getOrCreatePoolProfitAndLoss(event, ev.interestToCharge.minus(trueTax)); InvoiceUnfactoredEvent.save(); original_creditor.save(); + pool.save(); price_per_share.save(); historical_factoring_statistics.save(); pool_pnl.save(); @@ -274,6 +295,7 @@ export function handleInvoiceUnfactoredV2(event: InvoiceUnfactored): void { InvoiceUnfactoredEvent.invoiceId = underlyingClaim.id; InvoiceUnfactoredEvent.originalCreditor = ev.originalCreditor; const original_creditor = getOrCreateUser(ev.originalCreditor); + const pool = getOrCreateUser(event.address); const price_per_share = getOrCreatePricePerShare(event, "v2"); const latestPrice = getLatestPrice(event, "v2"); @@ -298,10 +320,15 @@ export function handleInvoiceUnfactoredV2(event: InvoiceUnfactored): void { ? original_creditor.factoringEvents.concat([InvoiceUnfactoredEvent.id]) : [InvoiceUnfactoredEvent.id]; + pool.factoringEvents = pool.factoringEvents + ? pool.factoringEvents.concat([InvoiceUnfactoredEvent.id]) + : [InvoiceUnfactoredEvent.id]; + const pool_pnl = getOrCreatePoolProfitAndLoss(event, ev.interestToCharge.minus(trueTax)); InvoiceUnfactoredEvent.save(); original_creditor.save(); + pool.save(); price_per_share.save(); historical_factoring_statistics.save(); pool_pnl.save(); @@ -318,6 +345,7 @@ export function handleDepositV2(event: Deposit): void { DepositMadeEvent.sharesIssued = ev.shares; const investor = getOrCreateUser(ev.sender); + const pool = getOrCreateUser(event.address); const price_per_share = getOrCreatePricePerShare(event, "v2"); const latestPrice = getLatestPrice(event, "v2"); const historical_factoring_statistics = getOrCreateHistoricalFactoringStatistics(event, "v2"); @@ -331,8 +359,10 @@ export function handleDepositV2(event: Deposit): void { DepositMadeEvent.priceAfterTransaction = latestPrice; investor.factoringEvents = investor.factoringEvents ? investor.factoringEvents.concat([DepositMadeEvent.id]) : [DepositMadeEvent.id]; + pool.factoringEvents = pool.factoringEvents ? pool.factoringEvents.concat([DepositMadeEvent.id]) : [DepositMadeEvent.id]; DepositMadeEvent.save(); + pool.save(); investor.save(); price_per_share.save(); historical_factoring_statistics.save(); @@ -349,6 +379,7 @@ export function handleDepositMadeV1(event: DepositMade): void { DepositMadeEvent.sharesIssued = ev.sharesIssued; const investor = getOrCreateUser(ev.depositor); + const pool = getOrCreateUser(event.address); const price_per_share = getOrCreatePricePerShare(event, "v1"); const latestPrice = getLatestPrice(event, "v1"); const historical_factoring_statistics = getOrCreateHistoricalFactoringStatistics(event, "v1"); @@ -362,8 +393,10 @@ export function handleDepositMadeV1(event: DepositMade): void { DepositMadeEvent.priceAfterTransaction = latestPrice; investor.factoringEvents = investor.factoringEvents ? investor.factoringEvents.concat([DepositMadeEvent.id]) : [DepositMadeEvent.id]; + pool.factoringEvents = pool.factoringEvents ? pool.factoringEvents.concat([DepositMadeEvent.id]) : [DepositMadeEvent.id]; DepositMadeEvent.save(); + pool.save(); investor.save(); price_per_share.save(); historical_factoring_statistics.save(); @@ -381,6 +414,7 @@ export function handleDepositMadeWithAttachmentV1(event: DepositMadeWithAttachme DepositMadeEvent.ipfsHash = getIPFSHash_depositWithAttachment(ev.attachment); const investor = getOrCreateUser(ev.depositor); + const pool = getOrCreateUser(ev.depositor); const price_per_share = getOrCreatePricePerShare(event, "v1"); const latestPrice = getLatestPrice(event, "v1"); const historical_factoring_statistics = getOrCreateHistoricalFactoringStatistics(event, "v1"); @@ -394,8 +428,10 @@ export function handleDepositMadeWithAttachmentV1(event: DepositMadeWithAttachme DepositMadeEvent.priceAfterTransaction = latestPrice; investor.factoringEvents = investor.factoringEvents ? investor.factoringEvents.concat([DepositMadeEvent.id]) : [DepositMadeEvent.id]; + pool.factoringEvents = pool.factoringEvents ? pool.factoringEvents.concat([DepositMadeEvent.id]) : [DepositMadeEvent.id]; DepositMadeEvent.save(); + pool.save(); investor.save(); price_per_share.save(); historical_factoring_statistics.save(); @@ -425,6 +461,7 @@ export function handleWithdraw(event: Withdraw): void { SharesRedeemedEvent.assets = ev.assets; SharesRedeemedEvent.shares = ev.shares; const investor = getOrCreateUser(ev.receiver); + const pool = getOrCreateUser(ev.receiver); const price_per_share = getOrCreatePricePerShare(event, "v2"); const latestPrice = getLatestPrice(event, "v2"); const historical_factoring_statistics = getOrCreateHistoricalFactoringStatistics(event, "v2"); @@ -438,8 +475,10 @@ export function handleWithdraw(event: Withdraw): void { SharesRedeemedEvent.priceAfterTransaction = latestPrice; investor.factoringEvents = investor.factoringEvents ? investor.factoringEvents.concat([SharesRedeemedEvent.id]) : [SharesRedeemedEvent.id]; + pool.factoringEvents = pool.factoringEvents ? pool.factoringEvents.concat([SharesRedeemedEvent.id]) : [SharesRedeemedEvent.id]; SharesRedeemedEvent.save(); + pool.save(); investor.save(); price_per_share.save(); historical_factoring_statistics.save(); @@ -456,6 +495,7 @@ export function handleSharesRedeemed(event: SharesRedeemed): void { SharesRedeemedEvent.assets = ev.assets; SharesRedeemedEvent.shares = ev.shares; const investor = getOrCreateUser(ev.redeemer); + const pool = getOrCreateUser(ev.redeemer); const price_per_share = getOrCreatePricePerShare(event, "v1"); const latestPrice = getLatestPrice(event, "v1"); const historical_factoring_statistics = getOrCreateHistoricalFactoringStatistics(event, "v1"); @@ -469,8 +509,10 @@ export function handleSharesRedeemed(event: SharesRedeemed): void { SharesRedeemedEvent.priceAfterTransaction = latestPrice; investor.factoringEvents = investor.factoringEvents ? investor.factoringEvents.concat([SharesRedeemedEvent.id]) : [SharesRedeemedEvent.id]; + pool.factoringEvents = pool.factoringEvents ? pool.factoringEvents.concat([SharesRedeemedEvent.id]) : [SharesRedeemedEvent.id]; SharesRedeemedEvent.save(); + pool.save(); investor.save(); price_per_share.save(); historical_factoring_statistics.save(); @@ -488,6 +530,7 @@ export function handleSharesRedeemedWithAttachmentV1(event: SharesRedeemedWithAt SharesRedeemedEvent.ipfsHash = getIPFSHash_redeemWithAttachment(ev.attachment); const investor = getOrCreateUser(ev.redeemer); + const pool = getOrCreateUser(ev.redeemer); const price_per_share = getOrCreatePricePerShare(event, "v1"); const latestPrice = getLatestPrice(event, "v1"); const historical_factoring_statistics = getOrCreateHistoricalFactoringStatistics(event, "v1"); @@ -501,8 +544,10 @@ export function handleSharesRedeemedWithAttachmentV1(event: SharesRedeemedWithAt SharesRedeemedEvent.priceAfterTransaction = latestPrice; investor.factoringEvents = investor.factoringEvents ? investor.factoringEvents.concat([SharesRedeemedEvent.id]) : [SharesRedeemedEvent.id]; + pool.factoringEvents = pool.factoringEvents ? pool.factoringEvents.concat([SharesRedeemedEvent.id]) : [SharesRedeemedEvent.id]; SharesRedeemedEvent.save(); + pool.save(); investor.save(); price_per_share.save(); historical_factoring_statistics.save(); @@ -532,6 +577,7 @@ export function handleInvoiceImpaired(event: InvoiceImpaired, version: string): InvoiceImpairedEvent.invoiceId = underlyingClaim.id; const price_per_share = getOrCreatePricePerShare(event, version); const latestPrice = getLatestPrice(event, version); + const pool = getOrCreateUser(event.address); const historical_factoring_statistics = getOrCreateHistoricalFactoringStatistics(event, version); InvoiceImpairedEvent.eventName = "InvoiceImpaired"; @@ -549,11 +595,14 @@ export function handleInvoiceImpaired(event: InvoiceImpaired, version: string): .minus(ev.gainAmount) .neg(); const pool_pnl = getOrCreatePoolProfitAndLoss(event, lossAccrued); + + pool.factoringEvents = pool.factoringEvents ? pool.factoringEvents.concat([InvoiceImpairedEvent.id]) : [InvoiceImpairedEvent.id]; InvoiceImpairedEvent.save(); price_per_share.save(); historical_factoring_statistics.save(); pool_pnl.save(); + pool.save(); } export function handleInvoiceImpairedV1(event: InvoiceImpaired): void { @@ -567,6 +616,7 @@ export function handleInvoiceImpairedV2(event: InvoiceImpaired): void { export function handleActivePaidInvoicesReconciled(event: ActivePaidInvoicesReconciled, version: string): void { const ev = event.params; let pnlTotal = BigInt.fromI32(0); + const pool = getOrCreateUser(event.address); for (let i = 0; i < ev.paidInvoiceIds.length; i++) { const invoiceId = ev.paidInvoiceIds[i]; @@ -600,6 +650,7 @@ export function handleActivePaidInvoicesReconciled(event: ActivePaidInvoicesReco InvoiceReconciled.trueTax = trueTax; originalCreditor.factoringEvents = originalCreditor.factoringEvents ? originalCreditor.factoringEvents.concat([InvoiceReconciled.id]) : [InvoiceReconciled.id]; + pool.factoringEvents = pool.factoringEvents ? pool.factoringEvents.concat([InvoiceReconciled.id]) : [InvoiceReconciled.id]; InvoiceReconciled.save(); originalCreditor.save(); @@ -613,6 +664,7 @@ export function handleActivePaidInvoicesReconciled(event: ActivePaidInvoicesReco price_per_share.save(); historical_factoring_statistics.save(); pool_pnl.save(); + pool.save(); } export function handleActivePaidInvoicesReconciledV1(event: ActivePaidInvoicesReconciled): void {