Skip to content

Commit

Permalink
Setup other factoring events in thegraph (#30)
Browse files Browse the repository at this point in the history
* InvoiceKickbackAmountSentEvent + tests

* InvoiceUnfactored

* deposit and depositwithattachment events

* shares redeemed events with attachment
  • Loading branch information
solidoracle authored Apr 10, 2024
1 parent 413803c commit 9f2c120
Show file tree
Hide file tree
Showing 7 changed files with 541 additions and 12 deletions.
81 changes: 81 additions & 0 deletions bulla-contracts/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,87 @@ type InvoiceFundedEvent implements IEventLog @entity {
timestamp: BigInt!
}

type InvoiceKickbackAmountSentEvent implements IEventLog @entity {
id: ID!
invoiceId: String!
kickbackAmount: BigInt!
originalCreditor: Bytes! #address
eventName: String!
blockNumber: BigInt!
transactionHash: Bytes!
logIndex: BigInt!
timestamp: BigInt!
}

type InvoiceUnfactoredEvent implements IEventLog @entity {
id: ID!
invoiceId: String!
originalCreditor: Bytes! #address
eventName: String!
blockNumber: BigInt!
transactionHash: Bytes!
logIndex: BigInt!
timestamp: BigInt!
}

type DepositMadeEvent implements IEventLog @entity {
id: ID!
invoiceId: String!
depositor: Bytes! #address
assets: BigInt!
sharesIssued: BigInt!
poolAddress: Bytes! #address
eventName: String!
blockNumber: BigInt!
transactionHash: Bytes!
logIndex: BigInt!
timestamp: BigInt!
}

type DepositMadeWithAttachmentEvent implements IEventLog @entity {
id: ID!
invoiceId: String!
depositor: Bytes! #address
assets: BigInt!
sharesIssued: BigInt!
ipfsHash: String
poolAddress: Bytes! #address
eventName: String!
blockNumber: BigInt!
transactionHash: Bytes!
logIndex: BigInt!
timestamp: BigInt!
}

type SharesRedeemedEvent implements IEventLog @entity {
id: ID!
invoiceId: String!
redeemer: Bytes! #address
assets: BigInt!
shares: BigInt!
poolAddress: Bytes! #address
eventName: String!
blockNumber: BigInt!
transactionHash: Bytes!
logIndex: BigInt!
timestamp: BigInt!
}

type SharesRedeemedWithAttachmentEvent implements IEventLog @entity {
id: ID!
invoiceId: String!
redeemer: Bytes! #address
assets: BigInt!
shares: BigInt!
ipfsHash: String
poolAddress: Bytes! #address
eventName: String!
blockNumber: BigInt!
transactionHash: Bytes!
logIndex: BigInt!
timestamp: BigInt!
}

type LoanOfferedEvent implements IEventLog @entity {
id: ID!
loanId: String!
Expand Down
62 changes: 60 additions & 2 deletions bulla-contracts/src/functions/BullaFactoring.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,67 @@
import { BigInt, ethereum } from "@graphprotocol/graph-ts";
import { InvoiceFundedEvent } from "../../generated/schema";
import { InvoiceFunded } from "../../generated/BullaFactoring/BullaFactoring";
import {
DepositMadeEvent,
DepositMadeWithAttachmentEvent,
InvoiceFundedEvent,
InvoiceKickbackAmountSentEvent,
InvoiceUnfactoredEvent,
SharesRedeemedEvent,
SharesRedeemedWithAttachmentEvent
} from "../../generated/schema";
import {
DepositMade,
DepositMadeWithAttachment,
InvoiceFunded,
InvoiceKickbackAmountSent,
InvoiceUnfactored,
SharesRedeemed,
SharesRedeemedWithAttachment
} from "../../generated/BullaFactoring/BullaFactoring";

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

export const createInvoiceFundedEvent = (underlyingTokenId: BigInt, event: InvoiceFunded): InvoiceFundedEvent =>
new InvoiceFundedEvent(getInvoiceFundedEventId(underlyingTokenId, event));

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

export const createInvoiceKickbackAmountSentEvent = (underlyingTokenId: BigInt, event: InvoiceKickbackAmountSent): InvoiceKickbackAmountSentEvent =>
new InvoiceKickbackAmountSentEvent(getInvoiceKickbackAmountSentEventId(underlyingTokenId, event));

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

export const createInvoiceUnfactoredEvent = (underlyingTokenId: BigInt, event: InvoiceUnfactored): InvoiceUnfactoredEvent =>
new InvoiceUnfactoredEvent(getInvoiceUnfactoredEventId(underlyingTokenId, event));

export const getDepositMadeEventId = (event: ethereum.Event): string => {
const poolAddress = event.address;
return "DepositMade-" + poolAddress.toString() + "-" + event.transaction.hash.toHexString() + "-" + event.logIndex.toString();
};

export const createDepositMadeEvent = (event: DepositMade): DepositMadeEvent => new DepositMadeEvent(getDepositMadeEventId(event));

export const getDepositMadeWithAttachmentEventId = (event: ethereum.Event): string => {
const poolAddress = event.address;
return "DepositMadeWithAttachment-" + poolAddress.toString() + "-" + event.transaction.hash.toHexString() + "-" + event.logIndex.toString();
};

export const createDepositMadeWithAttachmentEvent = (event: DepositMadeWithAttachment): DepositMadeWithAttachmentEvent =>
new DepositMadeWithAttachmentEvent(getDepositMadeWithAttachmentEventId(event));

export const getSharesRedeemedEventId = (event: ethereum.Event): string => {
const poolAddress = event.address;
return "SharesRedeemed-" + poolAddress.toString() + "-" + event.transaction.hash.toHexString() + "-" + event.logIndex.toString();
};

export const createSharesRedeemedEvent = (event: SharesRedeemed): SharesRedeemedEvent => new SharesRedeemedEvent(getSharesRedeemedEventId(event));

export const getSharesRedeemedWithAttachmentEventId = (event: ethereum.Event): string => {
const poolAddress = event.address;
return "SharesRedeemedWithAttachment-" + poolAddress.toString() + "-" + event.transaction.hash.toHexString() + "-" + event.logIndex.toString();
};

export const createSharesRedeemedWithAttachmentEvent = (event: SharesRedeemedWithAttachment): SharesRedeemedWithAttachmentEvent =>
new SharesRedeemedWithAttachmentEvent(getSharesRedeemedWithAttachmentEventId(event));
13 changes: 13 additions & 0 deletions bulla-contracts/src/functions/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ERC20 } from "../../generated/BullaClaimERC721/ERC20";
import { BullaManager as BullaManagerContract } from "../../generated/BullaManager/BullaManager";
import { LoanOfferedLoanOfferAttachmentStruct } from "../../generated/FrendLend/FrendLend";
import { BullaManager, Token, User } from "../../generated/schema";
import { DepositMadeWithAttachmentAttachmentStruct, SharesRedeemedWithAttachmentAttachmentStruct } from "../../generated/BullaFactoring/BullaFactoring";

export const ADDRESS_ZERO = "0x0000000000000000000000000000000000000000";

Expand Down Expand Up @@ -57,6 +58,18 @@ export const getIPFSHash_loanOffered = (attachment: LoanOfferedLoanOfferAttachme
return ipfsHash;
};

export const getIPFSHash_depositWithAttachment = (attachment: DepositMadeWithAttachmentAttachmentStruct): string | null => {
if (attachment.hash.equals(Bytes.fromHexString(EMPTY_BYTES32))) return null;
const ipfsHash = multihashStructToBase58(attachment.hash, attachment.size, attachment.hashFunction);
return ipfsHash;
};

export const getIPFSHash_redeemWithAttachment = (attachment: SharesRedeemedWithAttachmentAttachmentStruct): string | null => {
if (attachment.hash.equals(Bytes.fromHexString(EMPTY_BYTES32))) return null;
const ipfsHash = multihashStructToBase58(attachment.hash, attachment.size, attachment.hashFunction);
return ipfsHash;
};

export const getOrCreateUser = (address: Address): User => {
let user = User.load(address.toHexString());
if (!user) {
Expand Down
138 changes: 136 additions & 2 deletions bulla-contracts/src/mappings/BullaFactoring.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import { InvoiceFunded } from "../../generated/BullaFactoring/BullaFactoring";
import {
DepositMade,
DepositMadeWithAttachment,
InvoiceFunded,
InvoiceKickbackAmountSent,
InvoiceUnfactored,
SharesRedeemed,
SharesRedeemedWithAttachment
} from "../../generated/BullaFactoring/BullaFactoring";
import { getClaim } from "../functions/BullaClaimERC721";
import { createInvoiceFundedEvent } from "../functions/BullaFactoring";
import {
createDepositMadeEvent,
createDepositMadeWithAttachmentEvent,
createInvoiceFundedEvent,
createInvoiceKickbackAmountSentEvent,
createInvoiceUnfactoredEvent,
createSharesRedeemedEvent,
createSharesRedeemedWithAttachmentEvent
} from "../functions/BullaFactoring";
import { getIPFSHash_depositWithAttachment, getIPFSHash_redeemWithAttachment } from "../functions/common";

export function handleInvoiceFunded(event: InvoiceFunded): void {
const ev = event.params;
Expand All @@ -21,3 +38,120 @@ export function handleInvoiceFunded(event: InvoiceFunded): void {

InvoiceFundedEvent.save();
}

export function handleInvoiceKickbackAmountSent(event: InvoiceKickbackAmountSent): void {
const ev = event.params;
const originatingClaimId = ev.invoiceId;

const underlyingClaim = getClaim(originatingClaimId.toString());
const InvoiceKickbackAmountSentEvent = createInvoiceKickbackAmountSentEvent(originatingClaimId, event);

InvoiceKickbackAmountSentEvent.invoiceId = underlyingClaim.id;
InvoiceKickbackAmountSentEvent.kickbackAmount = ev.kickbackAmount;
InvoiceKickbackAmountSentEvent.originalCreditor = ev.originalCreditor;

InvoiceKickbackAmountSentEvent.eventName = "InvoiceKickbackAmountSent";
InvoiceKickbackAmountSentEvent.blockNumber = event.block.number;
InvoiceKickbackAmountSentEvent.transactionHash = event.transaction.hash;
InvoiceKickbackAmountSentEvent.logIndex = event.logIndex;
InvoiceKickbackAmountSentEvent.timestamp = event.block.timestamp;

InvoiceKickbackAmountSentEvent.save();
}

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

const underlyingClaim = getClaim(originatingClaimId.toString());
const InvoiceUnfactoredEvent = createInvoiceUnfactoredEvent(originatingClaimId, event);

InvoiceUnfactoredEvent.invoiceId = underlyingClaim.id;
InvoiceUnfactoredEvent.originalCreditor = ev.originalCreditor;

InvoiceUnfactoredEvent.eventName = "InvoiceUnfactored";
InvoiceUnfactoredEvent.blockNumber = event.block.number;
InvoiceUnfactoredEvent.transactionHash = event.transaction.hash;
InvoiceUnfactoredEvent.logIndex = event.logIndex;
InvoiceUnfactoredEvent.timestamp = event.block.timestamp;

InvoiceUnfactoredEvent.save();
}

export function handleDepositMade(event: DepositMade): void {
const ev = event.params;

const DepositMadeEvent = createDepositMadeEvent(event);

DepositMadeEvent.poolAddress = event.address;
DepositMadeEvent.depositor = ev.depositor;
DepositMadeEvent.assets = ev.assets;
DepositMadeEvent.sharesIssued = ev.sharesIssued;

DepositMadeEvent.eventName = "DepositMade";
DepositMadeEvent.blockNumber = event.block.number;
DepositMadeEvent.transactionHash = event.transaction.hash;
DepositMadeEvent.logIndex = event.logIndex;
DepositMadeEvent.timestamp = event.block.timestamp;

DepositMadeEvent.save();
}

export function handleDepositMadeWithAttachment(event: DepositMadeWithAttachment): void {
const ev = event.params;

const DepositMadeWithAttachmentEvent = createDepositMadeWithAttachmentEvent(event);

DepositMadeWithAttachmentEvent.poolAddress = event.address;
DepositMadeWithAttachmentEvent.depositor = ev.depositor;
DepositMadeWithAttachmentEvent.assets = ev.assets;
DepositMadeWithAttachmentEvent.sharesIssued = ev.shares;
DepositMadeWithAttachmentEvent.ipfsHash = getIPFSHash_depositWithAttachment(ev.attachment);

DepositMadeWithAttachmentEvent.eventName = "DepositMadeWithAttachment";
DepositMadeWithAttachmentEvent.blockNumber = event.block.number;
DepositMadeWithAttachmentEvent.transactionHash = event.transaction.hash;
DepositMadeWithAttachmentEvent.logIndex = event.logIndex;
DepositMadeWithAttachmentEvent.timestamp = event.block.timestamp;

DepositMadeWithAttachmentEvent.save();
}

export function handleSharesRedeemed(event: SharesRedeemed): void {
const ev = event.params;

const SharesRedeemedEvent = createSharesRedeemedEvent(event);

SharesRedeemedEvent.poolAddress = event.address;
SharesRedeemedEvent.redeemer = ev.redeemer;
SharesRedeemedEvent.assets = ev.assets;
SharesRedeemedEvent.shares = ev.shares;

SharesRedeemedEvent.eventName = "SharesRedeemed";
SharesRedeemedEvent.blockNumber = event.block.number;
SharesRedeemedEvent.transactionHash = event.transaction.hash;
SharesRedeemedEvent.logIndex = event.logIndex;
SharesRedeemedEvent.timestamp = event.block.timestamp;

SharesRedeemedEvent.save();
}

export function handleSharesRedeemedWithAttachment(event: SharesRedeemedWithAttachment): void {
const ev = event.params;

const SharesRedeemedWithAttachmentEvent = createSharesRedeemedWithAttachmentEvent(event);

SharesRedeemedWithAttachmentEvent.poolAddress = event.address;
SharesRedeemedWithAttachmentEvent.redeemer = ev.redeemer;
SharesRedeemedWithAttachmentEvent.assets = ev.assets;
SharesRedeemedWithAttachmentEvent.shares = ev.shares;
SharesRedeemedWithAttachmentEvent.ipfsHash = getIPFSHash_redeemWithAttachment(ev.attachment);

SharesRedeemedWithAttachmentEvent.eventName = "SharesRedeemedWithAttachment";
SharesRedeemedWithAttachmentEvent.blockNumber = event.block.number;
SharesRedeemedWithAttachmentEvent.transactionHash = event.transaction.hash;
SharesRedeemedWithAttachmentEvent.logIndex = event.logIndex;
SharesRedeemedWithAttachmentEvent.timestamp = event.block.timestamp;

SharesRedeemedWithAttachmentEvent.save();
}
18 changes: 18 additions & 0 deletions bulla-contracts/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,28 @@ dataSources:
language: wasm/assemblyscript
entities:
- InvoiceFundedEvent
- InvoiceKickbackAmountSentEvent
- InvoiceUnfactoredEvent
- DepositMadeEvent
- DepositMadeWithAttachmentEvent
- SharesRedeemedEvent
- SharesRedeemedWithAttachmentEvent
abis:
- name: BullaFactoring
file: ./abis/BullaFactoring.json
eventHandlers:
- event: InvoiceFunded(indexed uint256,uint256,indexed address)
handler: handleInvoiceFunded
- event: InvoiceKickbackAmountSent(indexed uint256,uint256,indexed address)
handler: handleInvoiceKickbackAmountSent
- event: InvoiceUnfactored(indexed uint256,address)
handler: handleInvoiceUnfactored
- event: DepositMade(indexed address,uint256,uint256)
handler: handleDepositMade
- event: DepositMadeWithAttachment(indexed address,uint256,uint256,(bytes32,uint8,uint8))
handler: handleDepositMadeWithAttachment
- event: SharesRedeemed(indexed address,uint256,uint256)
handler: handleSharesRedeemed
- event: SharesRedeemedWithAttachment(indexed address,uint256,uint256,(bytes32,uint8,uint8))
handler: handleSharesRedeemedWithAttachment
file: ./src/mappings/BullaFactoring.ts
Loading

0 comments on commit 9f2c120

Please sign in to comment.