Skip to content

Commit

Permalink
Merge pull request #69 from bulla-network/bgobeil/associate-pool-events
Browse files Browse the repository at this point in the history
Associate pool events with pool address
  • Loading branch information
solidoracle authored Nov 22, 2024
2 parents 3b85c1b + e949d8f commit 5b007aa
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions bulla-contracts/src/mappings/BullaFactoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
}
Expand All @@ -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);
Expand All @@ -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();
}
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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");
Expand All @@ -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();
Expand Down Expand Up @@ -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");

Expand All @@ -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();
Expand All @@ -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");
Expand All @@ -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();
Expand All @@ -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");
Expand All @@ -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();
Expand All @@ -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");
Expand All @@ -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();
Expand Down Expand Up @@ -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");
Expand All @@ -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();
Expand All @@ -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");
Expand All @@ -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();
Expand All @@ -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");
Expand All @@ -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();
Expand Down Expand Up @@ -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";
Expand All @@ -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 {
Expand All @@ -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];
Expand Down Expand Up @@ -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();
Expand All @@ -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 {
Expand Down

0 comments on commit 5b007aa

Please sign in to comment.