Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
solidoracle committed Sep 5, 2024
1 parent 3c49da0 commit c2f2e0f
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 45 deletions.
174 changes: 132 additions & 42 deletions bulla-contracts/abis/BullaFactoring.json
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@
"name": "ImpairReserveMustBeGreater",
"type": "error"
},
{
"inputs": [],
"name": "ImpairReserveNotSet",
"type": "error"
},
{
"inputs": [],
"name": "InvalidAddress",
Expand All @@ -332,6 +337,11 @@
"name": "InvoiceCanceled",
"type": "error"
},
{
"inputs": [],
"name": "InvoiceCannotBePaid",
"type": "error"
},
{
"inputs": [],
"name": "InvoiceCreditorChanged",
Expand Down Expand Up @@ -526,6 +536,43 @@
"name": "ApprovalDurationChanged",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "withdrawer",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "receiver",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "assets",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "shares",
"type": "uint256"
}
],
"name": "AssetsWithdrawn",
"type": "event"
},
{
"anonymous": false,
"inputs": [
Expand Down Expand Up @@ -826,6 +873,55 @@
"name": "InvoiceKickbackAmountSent",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "invoiceId",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "trueInterest",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "trueProtocolFee",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "adminFee",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "fundedAmountNet",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "kickbackAmount",
"type": "uint256"
},
{
"indexed": true,
"internalType": "address",
"name": "originalCreditor",
"type": "address"
}
],
"name": "InvoicePaid",
"type": "event"
},
{
"anonymous": false,
"inputs": [
Expand Down Expand Up @@ -1379,15 +1475,15 @@
"name": "fundedAmountNet",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "adminFee",
"type": "uint256"
},
{
"internalType": "uint16",
"name": "minDaysInterestApplied",
"type": "uint16"
},
{
"internalType": "uint256",
"name": "trueFaceValue",
"type": "uint256"
}
],
"stateMutability": "view",
Expand Down Expand Up @@ -1464,6 +1560,19 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "calculateAccruedProfits",
"outputs": [
{
"internalType": "uint256",
"name": "accruedProfits",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "calculateCapitalAccount",
Expand Down Expand Up @@ -1504,7 +1613,7 @@
},
{
"internalType": "uint256",
"name": "taxAmount",
"name": "trueAdminFee",
"type": "uint256"
}
],
Expand Down Expand Up @@ -1568,25 +1677,6 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "calculateTax",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -2450,19 +2540,6 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "contract IInvoiceProviderAdapter",
"name": "_newInvoiceProviderAdapter",
"type": "address"
}
],
"name": "setInvoiceProviderAdapter",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -2515,6 +2592,19 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "sumTargetFeesForActiveInvoices",
"outputs": [
{
"internalType": "uint256",
"name": "targetFees",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "symbol",
Expand Down Expand Up @@ -2720,17 +2810,17 @@
"inputs": [
{
"internalType": "uint256",
"name": "",
"name": "assets",
"type": "uint256"
},
{
"internalType": "address",
"name": "",
"name": "receiver",
"type": "address"
},
{
"internalType": "address",
"name": "",
"name": "owner",
"type": "address"
}
],
Expand All @@ -2742,7 +2832,7 @@
"type": "uint256"
}
],
"stateMutability": "pure",
"stateMutability": "nonpayable",
"type": "function"
},
{
Expand Down
19 changes: 19 additions & 0 deletions bulla-contracts/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,25 @@ type InvoiceKickbackAmountSentEvent implements IEventLog & IPoolTransaction @ent
claim: Claim!
}

type InvoicePaidEvent implements IEventLog & IPoolTransaction @entity {
id: ID!
invoiceId: String!
fundedAmount: BigInt!
kickbackAmount: BigInt!
trueInterest: BigInt!
trueProtocolFee: BigInt!
trueAdminFee: BigInt!
originalCreditor: Bytes! #address
eventName: String!
blockNumber: BigInt!
transactionHash: Bytes!
logIndex: BigInt!
timestamp: BigInt!
poolAddress: Bytes!
priceAfterTransaction: BigInt!
claim: Claim!
}

type InvoiceUnfactoredEvent implements IEventLog & IPoolTransaction @entity {
id: ID!
invoiceId: String!
Expand Down
11 changes: 9 additions & 2 deletions bulla-contracts/src/functions/BullaFactoring.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Address, BigInt, ethereum } from "@graphprotocol/graph-ts";
import { BigInt, ethereum } from "@graphprotocol/graph-ts";
import {
DepositMadeEvent,
DepositMadeWithAttachmentEvent,
InvoiceFundedEvent,
InvoiceKickbackAmountSentEvent,
InvoicePaidEvent,
InvoiceUnfactoredEvent,
SharesRedeemedEvent,
SharesRedeemedWithAttachmentEvent
Expand All @@ -13,11 +14,11 @@ import {
DepositMadeWithAttachment,
InvoiceFunded,
InvoiceKickbackAmountSent,
InvoicePaid,
InvoiceUnfactored,
SharesRedeemed,
SharesRedeemedWithAttachment
} from "../../generated/BullaFactoring/BullaFactoring";
import { getOrCreatePricePerShare } from "./common";

export const getInvoiceFundedEventId = (underlyingClaimId: BigInt, event: ethereum.Event): string =>
"InvoiceFunded-" + underlyingClaimId.toString() + "-" + event.transaction.hash.toHexString() + "-" + event.logIndex.toString();
Expand All @@ -32,6 +33,12 @@ export const getInvoiceKickbackAmountSentEventId = (underlyingClaimId: BigInt, e
export const createInvoiceKickbackAmountSentEvent = (underlyingTokenId: BigInt, event: InvoiceKickbackAmountSent): InvoiceKickbackAmountSentEvent =>
new InvoiceKickbackAmountSentEvent(getInvoiceKickbackAmountSentEventId(underlyingTokenId, event));

export const getInvoicePaidEventId = (underlyingClaimId: BigInt, event: ethereum.Event): string =>
"InvoicePaid-" + underlyingClaimId.toString() + "-" + event.transaction.hash.toHexString() + "-" + event.logIndex.toString();

export const createInvoicePaidEvent = (underlyingTokenId: BigInt, event: InvoicePaid): InvoicePaidEvent =>
new InvoicePaidEvent(getInvoicePaidEventId(underlyingTokenId, event));

export const getInvoiceUnfactoredEventId = (underlyingClaimId: BigInt, event: ethereum.Event): string =>
"InvoiceUnfactored-" + underlyingClaimId.toString() + "-" + event.transaction.hash.toHexString() + "-" + event.logIndex.toString();

Expand Down
42 changes: 42 additions & 0 deletions bulla-contracts/src/mappings/BullaFactoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
DepositMadeWithAttachment,
InvoiceFunded,
InvoiceKickbackAmountSent,
InvoicePaid,
InvoicePaid__Params,
InvoiceUnfactored,
SharesRedeemed,
SharesRedeemedWithAttachment
Expand All @@ -14,6 +16,7 @@ import {
createDepositMadeWithAttachmentEvent,
createInvoiceFundedEvent,
createInvoiceKickbackAmountSentEvent,
createInvoicePaidEvent,
createInvoiceUnfactoredEvent,
createSharesRedeemedEvent,
createSharesRedeemedWithAttachmentEvent
Expand Down Expand Up @@ -98,6 +101,45 @@ export function handleInvoiceKickbackAmountSent(event: InvoiceKickbackAmountSent
historical_factoring_statistics.save();
}

export function handleInvoicePaid(event: InvoicePaid): void {
const ev: InvoicePaid__Params = event.params;
const originatingClaimId = ev.invoiceId;

log.info("in handleInvoicePaid", []);
const underlyingClaim = getClaim(originatingClaimId.toString());
const InvoicePaidEvent = createInvoicePaidEvent(originatingClaimId, event);

InvoicePaidEvent.invoiceId = underlyingClaim.id;
InvoicePaidEvent.fundedAmount = ev.fundedAmountNet;
InvoicePaidEvent.kickbackAmount = ev.kickbackAmount;
// issue: Ethereum value is not an int or uint.
InvoicePaidEvent.trueAdminFee = ev.adminFee;
InvoicePaidEvent.trueInterest = ev.trueInterest;
InvoicePaidEvent.trueProtocolFee = ev.trueProtocolFee;
// issue: index out of range
// InvoicePaidEvent.originalCreditor = ev.originalCreditor;
// const original_creditor = getOrCreateUser(ev.originalCreditor);
const price_per_share = getOrCreatePricePerShare(event);
const latestPrice = getLatestPrice(event);
const historical_factoring_statistics = getOrCreateHistoricalFactoringStatistics(event);

InvoicePaidEvent.eventName = "InvoicePaid";
InvoicePaidEvent.blockNumber = event.block.number;
InvoicePaidEvent.transactionHash = event.transaction.hash;
InvoicePaidEvent.logIndex = event.logIndex;
InvoicePaidEvent.timestamp = event.block.timestamp;
InvoicePaidEvent.poolAddress = event.address;
InvoicePaidEvent.priceAfterTransaction = latestPrice;
InvoicePaidEvent.claim = underlyingClaim.id;

// original_creditor.factoringEvents = original_creditor.factoringEvents ? original_creditor.factoringEvents.concat([InvoicePaidEvent.id]) : [InvoicePaidEvent.id];

InvoicePaidEvent.save();
// original_creditor.save();
price_per_share.save();
historical_factoring_statistics.save();
}

export function handleInvoiceUnfactored(event: InvoiceUnfactored): void {
const ev = event.params;
const originatingClaimId = ev.invoiceId;
Expand Down
Loading

0 comments on commit c2f2e0f

Please sign in to comment.