Skip to content

Commit

Permalink
setup factoring events (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
solidoracle authored Apr 7, 2024
1 parent b72547b commit 413803c
Show file tree
Hide file tree
Showing 10 changed files with 2,313 additions and 5 deletions.
2,182 changes: 2,182 additions & 0 deletions bulla-contracts/abis/BullaFactoring.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion bulla-contracts/config/sepolia.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"bullaBanker": { "address": "0x5369F71e1Fe238f0e6D938c734E2D2aE7296F362", "startBlock": 5096555 },
"bullaFinance": { "address": "0xB219ecd037E8A5410d2e8839586D9F3996685cfB", "startBlock": 5096555 },
"frendLend": { "address": "0x3E058834CE20A54F0755889c008D3fF62D33cE85", "startBlock": 5096555 },
"instantPayment": { "address": "0x1cD1A83C2965CB7aD55d60551877Eb390e9C3d7A", "startBlock": 5096555 }
"instantPayment": { "address": "0x1cD1A83C2965CB7aD55d60551877Eb390e9C3d7A", "startBlock": 5096555 },
"bullaFactoring": { "address": "0xbdefcafade1d86b451e5aa44b896ef29432dce76", "startBlock": 5532095 }
}
4 changes: 2 additions & 2 deletions bulla-contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "bulla-contracts-subgraph",
"license": "BSL-1.1",
"scripts": {
"codegen": "yarn prep:goerli && graph codegen",
"build": "yarn prep:goerli && graph codegen && graph build",
"codegen": "yarn prep:sepolia && graph codegen",
"build": "yarn prep:sepolia && graph codegen && graph build",
"test": "yarn && yarn prep:goerli && graph test -v 0.2.2",
"coverage": "yarn prep:goerli && graph test -- -c",
"prep:mainnet": "mustache config/mainnet.json template.yaml > subgraph.yaml",
Expand Down
12 changes: 12 additions & 0 deletions bulla-contracts/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ type FinancingAcceptedEvent implements IEventLog @entity {
timestamp: BigInt!
}

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

type LoanOfferedEvent implements IEventLog @entity {
id: ID!
loanId: String!
Expand Down
9 changes: 9 additions & 0 deletions bulla-contracts/src/functions/BullaFactoring.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { BigInt, ethereum } from "@graphprotocol/graph-ts";
import { InvoiceFundedEvent } from "../../generated/schema";
import { InvoiceFunded } 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));
23 changes: 23 additions & 0 deletions bulla-contracts/src/mappings/BullaFactoring.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { InvoiceFunded } from "../../generated/BullaFactoring/BullaFactoring";
import { getClaim } from "../functions/BullaClaimERC721";
import { createInvoiceFundedEvent } from "../functions/BullaFactoring";

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

const underlyingClaim = getClaim(originatingClaimId.toString());
const InvoiceFundedEvent = createInvoiceFundedEvent(originatingClaimId, event);

InvoiceFundedEvent.invoiceId = underlyingClaim.id;
InvoiceFundedEvent.fundedAmount = ev.fundedAmount;
InvoiceFundedEvent.originalCreditor = ev.originalCreditor;

InvoiceFundedEvent.eventName = "InvoiceFunded";
InvoiceFundedEvent.blockNumber = event.block.number;
InvoiceFundedEvent.transactionHash = event.transaction.hash;
InvoiceFundedEvent.logIndex = event.logIndex;
InvoiceFundedEvent.timestamp = event.block.timestamp;

InvoiceFundedEvent.save();
}
20 changes: 20 additions & 0 deletions bulla-contracts/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,23 @@ dataSources:
- event: BullaBankerModuleDeploy(string,indexed address,indexed address,indexed address)
handler: handleBullaBankerModuleDeploy
file: ./src/mappings/BullaBankerModule.ts
- kind: ethereum/contract
name: BullaFactoring
network: {{ network }}
source:
address: "{{ bullaFactoring.address }}"
startBlock: {{ bullaFactoring.startBlock }}
abi: BullaFactoring
mapping:
kind: ethereum/events
apiVersion: {{ apiVersion }}
language: wasm/assemblyscript
entities:
- InvoiceFundedEvent
abis:
- name: BullaFactoring
file: ./abis/BullaFactoring.json
eventHandlers:
- event: InvoiceFunded(indexed uint256,uint256,indexed address)
handler: handleInvoiceFunded
file: ./src/mappings/BullaFactoring.ts
43 changes: 43 additions & 0 deletions bulla-contracts/tests/BullaFactoring.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { BigInt, log } from "@graphprotocol/graph-ts";
import { assert, test } from "matchstick-as/assembly/index";
import { CLAIM_TYPE_INVOICE } from "../src/functions/common";
import { handleClaimCreated } from "../src/mappings/BullaClaimERC721";
import { handleInvoiceFunded } from "../src/mappings/BullaFactoring";
import { newClaimCreatedEvent } from "./functions/BullaClaimERC721.testtools";
import { ADDRESS_1, afterEach, setupContracts } from "./helpers";
import { newInvoiceFundedEvent } from "./functions/BullaFactoring.testtools";
import { getInvoiceFundedEventId } from "../src/functions/BullaFactoring";

test("it handles BullaFactoring events", () => {
setupContracts();

const claimId = BigInt.fromI32(1);
const fundedAmount = BigInt.fromI32(10000);
const originalCreditor = ADDRESS_1;

const timestamp = BigInt.fromI32(100);
const blockNum = BigInt.fromI32(100);

const invoiceFundedEvent = newInvoiceFundedEvent(claimId, fundedAmount, originalCreditor);
invoiceFundedEvent.block.timestamp = timestamp;
invoiceFundedEvent.block.number = blockNum;

const claimCreatedEvent = newClaimCreatedEvent(claimId.toU32(), CLAIM_TYPE_INVOICE);
claimCreatedEvent.block.timestamp = timestamp;
claimCreatedEvent.block.number = blockNum;

handleClaimCreated(claimCreatedEvent);
handleInvoiceFunded(invoiceFundedEvent);

const invoiceFundedEventId = getInvoiceFundedEventId(claimId, invoiceFundedEvent);
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());

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

afterEach();
});

// exporting for test coverage
export { handleInvoiceFunded };
16 changes: 16 additions & 0 deletions bulla-contracts/tests/functions/BullaFactoring.testtools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Address, BigInt, ethereum } from "@graphprotocol/graph-ts";
import { newMockEvent } from "matchstick-as";
import { InvoiceFunded } from "../../generated/BullaFactoring/BullaFactoring";
import { toEthAddress, toUint256 } from "../helpers";

export const newInvoiceFundedEvent = (originatingClaimId: BigInt, fundedAmount: BigInt, originalCreditor: Address): InvoiceFunded => {
const event: InvoiceFunded = changetype<InvoiceFunded>(newMockEvent());

const invoiceId = new ethereum.EventParam("invoiceId", toUint256(originatingClaimId));
const fundedAmountParam = new ethereum.EventParam("fundedAmount", toUint256(fundedAmount));
const originalCreditorParam = new ethereum.EventParam("originalCreditor", toEthAddress(originalCreditor));

event.parameters = [invoiceId, fundedAmountParam, originalCreditorParam];

return event;
};
6 changes: 4 additions & 2 deletions node_modules/.yarn-integrity

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 413803c

Please sign in to comment.