Skip to content

Commit

Permalink
setup IPoolTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
solidoracle committed Jul 11, 2024
1 parent 715e5b3 commit f30ebf2
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 100 deletions.
2 changes: 1 addition & 1 deletion bulla-contracts/config/sepolia.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"bullaFinance": { "address": "0xB219ecd037E8A5410d2e8839586D9F3996685cfB", "startBlock": 5096555 },
"frendLend": { "address": "0x3E058834CE20A54F0755889c008D3fF62D33cE85", "startBlock": 5096555 },
"instantPayment": { "address": "0x1cD1A83C2965CB7aD55d60551877Eb390e9C3d7A", "startBlock": 5096555 },
"bullaFactoring": { "address": "0xe90D1384Aeb44212dBd959498592D6327B45609B", "startBlock": 5960360 }
"bullaFactoring": { "address": "0x58dc9404f7188A65C9EaF9e95dDA4F5080e52484", "startBlock": 5960360 }
}
40 changes: 19 additions & 21 deletions bulla-contracts/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,12 @@ type FinancingAcceptedEvent implements IEventLog @entity {
timestamp: BigInt!
}

type PoolTransaction @entity {
id: ID!
pool: Bytes! # address
timestamp: BigInt!
blockNumber: BigInt!
transactionHash: Bytes!
interface IPoolTransaction {
poolAddress: Bytes! # address
priceAfterTransaction: PriceHistoryEntry!
}

type InvoiceFundedEvent implements IEventLog @entity {
type InvoiceFundedEvent implements IEventLog & IPoolTransaction @entity {
id: ID!
invoiceId: String!
fundedAmount: BigInt!
Expand All @@ -183,10 +179,11 @@ type InvoiceFundedEvent implements IEventLog @entity {
transactionHash: Bytes!
logIndex: BigInt!
timestamp: BigInt!
poolTransaction: PoolTransaction!
poolAddress: Bytes!
priceAfterTransaction: PriceHistoryEntry!
}

type InvoiceKickbackAmountSentEvent implements IEventLog @entity {
type InvoiceKickbackAmountSentEvent implements IEventLog & IPoolTransaction @entity {
id: ID!
invoiceId: String!
kickbackAmount: BigInt!
Expand All @@ -196,10 +193,11 @@ type InvoiceKickbackAmountSentEvent implements IEventLog @entity {
transactionHash: Bytes!
logIndex: BigInt!
timestamp: BigInt!
poolTransaction: PoolTransaction!
poolAddress: Bytes!
priceAfterTransaction: PriceHistoryEntry!
}

type InvoiceUnfactoredEvent implements IEventLog @entity {
type InvoiceUnfactoredEvent implements IEventLog & IPoolTransaction @entity {
id: ID!
invoiceId: String!
originalCreditor: Bytes! #address
Expand All @@ -210,10 +208,11 @@ type InvoiceUnfactoredEvent implements IEventLog @entity {
transactionHash: Bytes!
logIndex: BigInt!
timestamp: BigInt!
poolTransaction: PoolTransaction!
poolAddress: Bytes!
priceAfterTransaction: PriceHistoryEntry!
}

type DepositMadeEvent implements IEventLog @entity {
type DepositMadeEvent implements IEventLog & IPoolTransaction @entity {
id: ID!
depositor: Bytes! #address
assets: BigInt!
Expand All @@ -224,10 +223,10 @@ type DepositMadeEvent implements IEventLog @entity {
transactionHash: Bytes!
logIndex: BigInt!
timestamp: BigInt!
poolTransaction: PoolTransaction!
priceAfterTransaction: PriceHistoryEntry!
}

type DepositMadeWithAttachmentEvent implements IEventLog @entity {
type DepositMadeWithAttachmentEvent implements IEventLog & IPoolTransaction @entity {
id: ID!
depositor: Bytes! #address
assets: BigInt!
Expand All @@ -239,10 +238,10 @@ type DepositMadeWithAttachmentEvent implements IEventLog @entity {
transactionHash: Bytes!
logIndex: BigInt!
timestamp: BigInt!
poolTransaction: PoolTransaction!
priceAfterTransaction: PriceHistoryEntry!
}

type SharesRedeemedEvent implements IEventLog @entity {
type SharesRedeemedEvent implements IEventLog & IPoolTransaction @entity {
id: ID!
redeemer: Bytes! #address
assets: BigInt!
Expand All @@ -253,10 +252,10 @@ type SharesRedeemedEvent implements IEventLog @entity {
transactionHash: Bytes!
logIndex: BigInt!
timestamp: BigInt!
poolTransaction: PoolTransaction!
priceAfterTransaction: PriceHistoryEntry!
}

type SharesRedeemedWithAttachmentEvent implements IEventLog @entity {
type SharesRedeemedWithAttachmentEvent implements IEventLog & IPoolTransaction @entity {
id: ID!
redeemer: Bytes! #address
assets: BigInt!
Expand All @@ -268,7 +267,7 @@ type SharesRedeemedWithAttachmentEvent implements IEventLog @entity {
transactionHash: Bytes!
logIndex: BigInt!
timestamp: BigInt!
poolTransaction: PoolTransaction!
priceAfterTransaction: PriceHistoryEntry!
}

type LoanOfferedEvent implements IEventLog @entity {
Expand Down Expand Up @@ -445,7 +444,6 @@ type PriceHistoryEntry @entity {
id: ID!
timestamp: BigInt!
price: BigInt!
transaction: PoolTransaction!
}

type BullaManager @entity {
Expand Down
8 changes: 5 additions & 3 deletions bulla-contracts/src/functions/BullaFactoring.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigInt, ethereum } from "@graphprotocol/graph-ts";
import { Address, BigInt, ethereum } from "@graphprotocol/graph-ts";
import {
DepositMadeEvent,
DepositMadeWithAttachmentEvent,
Expand All @@ -17,12 +17,14 @@ import {
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();

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

export const getInvoiceKickbackAmountSentEventId = (underlyingClaimId: BigInt, event: ethereum.Event): string =>
"InvoiceKickbackAmountSent-" + underlyingClaimId.toString() + "-" + event.transaction.hash.toHexString() + "-" + event.logIndex.toString();
Expand Down
26 changes: 26 additions & 0 deletions bulla-contracts/src/mappings/BullaFactoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export function handleInvoiceFunded(event: InvoiceFunded): void {
InvoiceFundedEvent.transactionHash = event.transaction.hash;
InvoiceFundedEvent.logIndex = event.logIndex;
InvoiceFundedEvent.timestamp = event.block.timestamp;
InvoiceFundedEvent.poolAddress = event.address;
InvoiceFundedEvent.priceAfterTransaction = price_per_share.id;

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

Expand All @@ -56,19 +58,23 @@ export function handleInvoiceKickbackAmountSent(event: InvoiceKickbackAmountSent
InvoiceKickbackAmountSentEvent.kickbackAmount = ev.kickbackAmount;
InvoiceKickbackAmountSentEvent.originalCreditor = ev.originalCreditor;
const original_creditor = getOrCreateUser(ev.originalCreditor);
const price_per_share = getOrCreatePricePerShare(event);

InvoiceKickbackAmountSentEvent.eventName = "InvoiceKickbackAmountSent";
InvoiceKickbackAmountSentEvent.blockNumber = event.block.number;
InvoiceKickbackAmountSentEvent.transactionHash = event.transaction.hash;
InvoiceKickbackAmountSentEvent.logIndex = event.logIndex;
InvoiceKickbackAmountSentEvent.timestamp = event.block.timestamp;
InvoiceKickbackAmountSentEvent.poolAddress = event.address;
InvoiceKickbackAmountSentEvent.priceAfterTransaction = price_per_share.id;

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

InvoiceKickbackAmountSentEvent.save();
original_creditor.save();
price_per_share.save();
}

export function handleInvoiceUnfactored(event: InvoiceUnfactored): void {
Expand All @@ -81,6 +87,7 @@ export function handleInvoiceUnfactored(event: InvoiceUnfactored): void {
InvoiceUnfactoredEvent.invoiceId = underlyingClaim.id;
InvoiceUnfactoredEvent.originalCreditor = ev.originalCreditor;
const original_creditor = getOrCreateUser(ev.originalCreditor);
const price_per_share = getOrCreatePricePerShare(event);

InvoiceUnfactoredEvent.eventName = "InvoiceUnfactored";
InvoiceUnfactoredEvent.blockNumber = event.block.number;
Expand All @@ -89,13 +96,16 @@ export function handleInvoiceUnfactored(event: InvoiceUnfactored): void {
InvoiceUnfactoredEvent.totalRefundAmount = ev.totalRefundAmount;
InvoiceUnfactoredEvent.interestToCharge = ev.interestToCharge;
InvoiceUnfactoredEvent.timestamp = event.block.timestamp;
InvoiceUnfactoredEvent.poolAddress = event.address;
InvoiceUnfactoredEvent.priceAfterTransaction = price_per_share.id;

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

InvoiceUnfactoredEvent.save();
original_creditor.save();
price_per_share.save();
}

export function handleDepositMade(event: DepositMade): void {
Expand All @@ -108,17 +118,21 @@ export function handleDepositMade(event: DepositMade): void {
DepositMadeEvent.assets = ev.assets;
DepositMadeEvent.sharesIssued = ev.sharesIssued;
const investor = getOrCreateUser(ev.depositor);
const price_per_share = getOrCreatePricePerShare(event);

DepositMadeEvent.eventName = "DepositMade";
DepositMadeEvent.blockNumber = event.block.number;
DepositMadeEvent.transactionHash = event.transaction.hash;
DepositMadeEvent.logIndex = event.logIndex;
DepositMadeEvent.timestamp = event.block.timestamp;
DepositMadeEvent.poolAddress = event.address;
DepositMadeEvent.priceAfterTransaction = price_per_share.id;

investor.factoringEvents = investor.factoringEvents ? investor.factoringEvents.concat([DepositMadeEvent.id]) : [DepositMadeEvent.id];

DepositMadeEvent.save();
investor.save();
price_per_share.save();
}

export function handleDepositMadeWithAttachment(event: DepositMadeWithAttachment): void {
Expand All @@ -132,17 +146,21 @@ export function handleDepositMadeWithAttachment(event: DepositMadeWithAttachment
DepositMadeWithAttachmentEvent.sharesIssued = ev.shares;
DepositMadeWithAttachmentEvent.ipfsHash = getIPFSHash_depositWithAttachment(ev.attachment);
const investor = getOrCreateUser(ev.depositor);
const price_per_share = getOrCreatePricePerShare(event);

DepositMadeWithAttachmentEvent.eventName = "DepositMadeWithAttachment";
DepositMadeWithAttachmentEvent.blockNumber = event.block.number;
DepositMadeWithAttachmentEvent.transactionHash = event.transaction.hash;
DepositMadeWithAttachmentEvent.logIndex = event.logIndex;
DepositMadeWithAttachmentEvent.timestamp = event.block.timestamp;
DepositMadeWithAttachmentEvent.poolAddress = event.address;
DepositMadeWithAttachmentEvent.priceAfterTransaction = price_per_share.id;

investor.factoringEvents = investor.factoringEvents ? investor.factoringEvents.concat([DepositMadeWithAttachmentEvent.id]) : [DepositMadeWithAttachmentEvent.id];

DepositMadeWithAttachmentEvent.save();
investor.save();
price_per_share.save();
}

export function handleSharesRedeemed(event: SharesRedeemed): void {
Expand All @@ -155,17 +173,21 @@ export function handleSharesRedeemed(event: SharesRedeemed): void {
SharesRedeemedEvent.assets = ev.assets;
SharesRedeemedEvent.shares = ev.shares;
const investor = getOrCreateUser(ev.redeemer);
const price_per_share = getOrCreatePricePerShare(event);

SharesRedeemedEvent.eventName = "SharesRedeemed";
SharesRedeemedEvent.blockNumber = event.block.number;
SharesRedeemedEvent.transactionHash = event.transaction.hash;
SharesRedeemedEvent.logIndex = event.logIndex;
SharesRedeemedEvent.timestamp = event.block.timestamp;
SharesRedeemedEvent.poolAddress = event.address;
SharesRedeemedEvent.priceAfterTransaction = price_per_share.id;

investor.factoringEvents = investor.factoringEvents ? investor.factoringEvents.concat([SharesRedeemedEvent.id]) : [SharesRedeemedEvent.id];

SharesRedeemedEvent.save();
investor.save();
price_per_share.save();
}

export function handleSharesRedeemedWithAttachment(event: SharesRedeemedWithAttachment): void {
Expand All @@ -179,15 +201,19 @@ export function handleSharesRedeemedWithAttachment(event: SharesRedeemedWithAtta
SharesRedeemedWithAttachmentEvent.shares = ev.shares;
SharesRedeemedWithAttachmentEvent.ipfsHash = getIPFSHash_redeemWithAttachment(ev.attachment);
const investor = getOrCreateUser(ev.redeemer);
const price_per_share = getOrCreatePricePerShare(event);

SharesRedeemedWithAttachmentEvent.eventName = "SharesRedeemedWithAttachment";
SharesRedeemedWithAttachmentEvent.blockNumber = event.block.number;
SharesRedeemedWithAttachmentEvent.transactionHash = event.transaction.hash;
SharesRedeemedWithAttachmentEvent.logIndex = event.logIndex;
SharesRedeemedWithAttachmentEvent.timestamp = event.block.timestamp;
SharesRedeemedWithAttachmentEvent.poolAddress = event.address;
SharesRedeemedWithAttachmentEvent.priceAfterTransaction = price_per_share.id;

investor.factoringEvents = investor.factoringEvents ? investor.factoringEvents.concat([SharesRedeemedWithAttachmentEvent.id]) : [SharesRedeemedWithAttachmentEvent.id];

SharesRedeemedWithAttachmentEvent.save();
investor.save();
price_per_share.save();
}
13 changes: 7 additions & 6 deletions bulla-contracts/tests/BullaFactoring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ test("it handles BullaFactoring events", () => {
assert.fieldEquals("InvoiceFundedEvent", invoiceFundedEventId, "invoiceId", invoiceFundedEvent.params.invoiceId.toString());
assert.fieldEquals("InvoiceFundedEvent", invoiceFundedEventId, "fundedAmount", invoiceFundedEvent.params.fundedAmount.toString());
assert.fieldEquals("InvoiceFundedEvent", invoiceFundedEventId, "originalCreditor", invoiceFundedEvent.params.originalCreditor.toHexString());
assert.fieldEquals("InvoiceFundedEvent", invoiceFundedEventId, "poolAddress", MOCK_BULLA_FACTORING_ADDRESS.toHexString());

log.info("✅ should create a InvoiceFunded event", []);

Expand All @@ -83,6 +84,7 @@ test("it handles BullaFactoring events", () => {
"originalCreditor",
invoiceKickbackAmountSentEvent.params.originalCreditor.toHexString()
);
assert.fieldEquals("InvoiceKickbackAmountSentEvent", invoiceKickbackAmountSentEventId, "poolAddress", MOCK_BULLA_FACTORING_ADDRESS.toHexString());

log.info("✅ should create a InvoiceKickbackAmountSent event", []);

Expand All @@ -100,6 +102,7 @@ test("it handles BullaFactoring events", () => {
assert.fieldEquals("InvoiceUnfactoredEvent", invoiceUnfactoredEventId, "originalCreditor", invoiceUnfactoredEvent.params.originalCreditor.toHexString());
assert.fieldEquals("InvoiceUnfactoredEvent", invoiceUnfactoredEventId, "totalRefundAmount", invoiceUnfactoredEvent.params.totalRefundAmount.toString());
assert.fieldEquals("InvoiceUnfactoredEvent", invoiceUnfactoredEventId, "interestToCharge", invoiceUnfactoredEvent.params.interestToCharge.toString());
assert.fieldEquals("InvoiceUnfactoredEvent", invoiceUnfactoredEventId, "poolAddress", MOCK_BULLA_FACTORING_ADDRESS.toHexString());

log.info("✅ should create a InvoiceUnfactored event", []);

Expand All @@ -117,6 +120,7 @@ test("it handles BullaFactoring events", () => {
assert.fieldEquals("DepositMadeEvent", depositMadeEventId, "depositor", depositMadeEvent.params.depositor.toHexString());
assert.fieldEquals("DepositMadeEvent", depositMadeEventId, "assets", depositMadeEvent.params.assets.toString());
assert.fieldEquals("DepositMadeEvent", depositMadeEventId, "sharesIssued", depositMadeEvent.params.sharesIssued.toString());
assert.fieldEquals("DepositMadeEvent", depositMadeEventId, "poolAddress", MOCK_BULLA_FACTORING_ADDRESS.toHexString());

log.info("✅ should create a DepositMade event", []);

Expand All @@ -131,6 +135,7 @@ test("it handles BullaFactoring events", () => {
assert.fieldEquals("DepositMadeWithAttachmentEvent", depositMadeWithAttachmentEventId, "assets", depositMadeWithAttachmentEvent.params.assets.toString());
assert.fieldEquals("DepositMadeWithAttachmentEvent", depositMadeWithAttachmentEventId, "sharesIssued", depositMadeWithAttachmentEvent.params.shares.toString());
assert.fieldEquals("DepositMadeWithAttachmentEvent", depositMadeWithAttachmentEventId, "ipfsHash", IPFS_HASH);
assert.fieldEquals("DepositMadeWithAttachmentEvent", depositMadeWithAttachmentEventId, "poolAddress", MOCK_BULLA_FACTORING_ADDRESS.toHexString());

log.info("✅ should create a DepositMadeWithAttachment event", []);

Expand All @@ -146,6 +151,7 @@ test("it handles BullaFactoring events", () => {
assert.fieldEquals("SharesRedeemedEvent", sharesRedeemedEventId, "redeemer", sharesRedeemedEvent.params.redeemer.toHexString());
assert.fieldEquals("SharesRedeemedEvent", sharesRedeemedEventId, "shares", sharesRedeemedEvent.params.shares.toString());
assert.fieldEquals("SharesRedeemedEvent", sharesRedeemedEventId, "assets", sharesRedeemedEvent.params.assets.toString());
assert.fieldEquals("SharesRedeemedEvent", sharesRedeemedEventId, "poolAddress", MOCK_BULLA_FACTORING_ADDRESS.toHexString());

log.info("✅ should create a SharesRedeemed event", []);

Expand All @@ -165,6 +171,7 @@ test("it handles BullaFactoring events", () => {
assert.fieldEquals("SharesRedeemedWithAttachmentEvent", sharesRedeemedWithAttachmentEventId, "shares", sharesRedeemedWithAttachmentEvent.params.shares.toString());
assert.fieldEquals("SharesRedeemedWithAttachmentEvent", sharesRedeemedWithAttachmentEventId, "assets", sharesRedeemedWithAttachmentEvent.params.assets.toString());
assert.fieldEquals("SharesRedeemedWithAttachmentEvent", sharesRedeemedWithAttachmentEventId, "ipfsHash", IPFS_HASH);
assert.fieldEquals("SharesRedeemedWithAttachmentEvent", sharesRedeemedWithAttachmentEventId, "poolAddress", MOCK_BULLA_FACTORING_ADDRESS.toHexString());

log.info("✅ should create a SharesRedeemedAttachment event", []);

Expand Down Expand Up @@ -214,13 +221,7 @@ test("it handles BullaFactoring events and stores price history", () => {
// Update the mock to return a new price
updatePricePerShareMock(BigInt.fromI32(1100000));

// Second InvoiceFunded event
const newTimestamp = timestamp.plus(BigInt.fromI32(3600)); // 1 hour later
const newBlockNum = blockNum.plus(BigInt.fromI32(100));

const invoiceFundedEvent2 = newInvoiceFundedEvent(claimId2, fundedAmount, originalCreditor);
invoiceFundedEvent2.block.timestamp = newTimestamp;
invoiceFundedEvent2.block.number = newBlockNum;

handleInvoiceFunded(invoiceFundedEvent2);

Expand Down
Loading

0 comments on commit f30ebf2

Please sign in to comment.