From 9f2c120a379684ec6793593b43c40a5c1daa7047 Mon Sep 17 00:00:00 2001 From: solidoracle <105349716+solidoracle@users.noreply.github.com> Date: Wed, 10 Apr 2024 16:53:43 +0200 Subject: [PATCH] Setup other factoring events in thegraph (#30) * InvoiceKickbackAmountSentEvent + tests * InvoiceUnfactored * deposit and depositwithattachment events * shares redeemed events with attachment --- bulla-contracts/schema.graphql | 81 ++++++++++ .../src/functions/BullaFactoring.ts | 62 +++++++- bulla-contracts/src/functions/common.ts | 13 ++ .../src/mappings/BullaFactoring.ts | 138 +++++++++++++++++- bulla-contracts/template.yaml | 18 +++ bulla-contracts/tests/BullaFactoring.test.ts | 136 ++++++++++++++++- .../functions/BullaFactoring.testtools.ts | 105 ++++++++++++- 7 files changed, 541 insertions(+), 12 deletions(-) diff --git a/bulla-contracts/schema.graphql b/bulla-contracts/schema.graphql index e1bcc94..7fc9972 100644 --- a/bulla-contracts/schema.graphql +++ b/bulla-contracts/schema.graphql @@ -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! diff --git a/bulla-contracts/src/functions/BullaFactoring.ts b/bulla-contracts/src/functions/BullaFactoring.ts index db09f21..995e7bf 100644 --- a/bulla-contracts/src/functions/BullaFactoring.ts +++ b/bulla-contracts/src/functions/BullaFactoring.ts @@ -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)); diff --git a/bulla-contracts/src/functions/common.ts b/bulla-contracts/src/functions/common.ts index 3db4c5c..7132b25 100644 --- a/bulla-contracts/src/functions/common.ts +++ b/bulla-contracts/src/functions/common.ts @@ -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"; @@ -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) { diff --git a/bulla-contracts/src/mappings/BullaFactoring.ts b/bulla-contracts/src/mappings/BullaFactoring.ts index f99ead7..9f1836e 100644 --- a/bulla-contracts/src/mappings/BullaFactoring.ts +++ b/bulla-contracts/src/mappings/BullaFactoring.ts @@ -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; @@ -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(); +} diff --git a/bulla-contracts/template.yaml b/bulla-contracts/template.yaml index c41456c..9f918a4 100644 --- a/bulla-contracts/template.yaml +++ b/bulla-contracts/template.yaml @@ -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 diff --git a/bulla-contracts/tests/BullaFactoring.test.ts b/bulla-contracts/tests/BullaFactoring.test.ts index 8ff6db1..07f5dd7 100644 --- a/bulla-contracts/tests/BullaFactoring.test.ts +++ b/bulla-contracts/tests/BullaFactoring.test.ts @@ -2,11 +2,35 @@ 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 { + handleDepositMade, + handleDepositMadeWithAttachment, + handleInvoiceFunded, + handleInvoiceKickbackAmountSent, + handleInvoiceUnfactored, + handleSharesRedeemed, + handleSharesRedeemedWithAttachment +} 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"; +import { ADDRESS_1, ADDRESS_2, ADDRESS_3, IPFS_HASH, afterEach, setupContracts } from "./helpers"; +import { + newDepositMadeEvent, + newDepositMadeWithAttachmentEvent, + newInvoiceFundedEvent, + newInvoiceKickbackAmountSentEvent, + newInvoiceUnfactoredEvent, + newSharesRedeemedEvent, + newSharesRedeemedWithAttachmentEvent +} from "./functions/BullaFactoring.testtools"; +import { + getDepositMadeEventId, + getDepositMadeWithAttachmentEventId, + getInvoiceFundedEventId, + getInvoiceKickbackAmountSentEventId, + getInvoiceUnfactoredEventId, + getSharesRedeemedEventId, + getSharesRedeemedWithAttachmentEventId +} from "../src/functions/BullaFactoring"; test("it handles BullaFactoring events", () => { setupContracts(); @@ -36,8 +60,110 @@ test("it handles BullaFactoring events", () => { log.info("✅ should create a InvoiceFunded event", []); + const kickbackAmount = BigInt.fromI32(2000); + + const invoiceKickbackAmountSentEvent = newInvoiceKickbackAmountSentEvent(claimId, kickbackAmount, originalCreditor); + invoiceKickbackAmountSentEvent.block.timestamp = timestamp; + invoiceKickbackAmountSentEvent.block.number = blockNum; + + handleInvoiceKickbackAmountSent(invoiceKickbackAmountSentEvent); + + const invoiceKickbackAmountSentEventId = getInvoiceKickbackAmountSentEventId(claimId, invoiceKickbackAmountSentEvent); + assert.fieldEquals("InvoiceKickbackAmountSentEvent", invoiceKickbackAmountSentEventId, "invoiceId", invoiceKickbackAmountSentEvent.params.invoiceId.toString()); + assert.fieldEquals( + "InvoiceKickbackAmountSentEvent", + invoiceKickbackAmountSentEventId, + "kickbackAmount", + invoiceKickbackAmountSentEvent.params.kickbackAmount.toString() + ); + assert.fieldEquals( + "InvoiceKickbackAmountSentEvent", + invoiceKickbackAmountSentEventId, + "originalCreditor", + invoiceKickbackAmountSentEvent.params.originalCreditor.toHexString() + ); + + log.info("✅ should create a InvoiceKickbackAmountSent event", []); + + const invoiceUnfactoredEvent = newInvoiceUnfactoredEvent(claimId, originalCreditor); + invoiceUnfactoredEvent.block.timestamp = timestamp; + invoiceUnfactoredEvent.block.number = blockNum; + + handleInvoiceUnfactored(invoiceUnfactoredEvent); + + const invoiceUnfactoredEventId = getInvoiceUnfactoredEventId(claimId, invoiceUnfactoredEvent); + assert.fieldEquals("InvoiceUnfactoredEvent", invoiceUnfactoredEventId, "invoiceId", invoiceUnfactoredEvent.params.invoiceId.toString()); + assert.fieldEquals("InvoiceUnfactoredEvent", invoiceUnfactoredEventId, "originalCreditor", invoiceUnfactoredEvent.params.originalCreditor.toHexString()); + + log.info("✅ should create a InvoiceUnfactored event", []); + + const depositor = ADDRESS_2; + const assets = BigInt.fromI32(10000); + const shares = BigInt.fromI32(10000); + + const depositMadeEvent = newDepositMadeEvent(depositor, assets, shares); + depositMadeEvent.block.timestamp = timestamp; + depositMadeEvent.block.number = blockNum; + + handleDepositMade(depositMadeEvent); + + const depositMadeEventId = getDepositMadeEventId(depositMadeEvent); + 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()); + + log.info("✅ should create a DepositMade event", []); + + const depositMadeWithAttachmentEvent = newDepositMadeWithAttachmentEvent(depositor, assets, shares); + depositMadeEvent.block.timestamp = timestamp; + depositMadeEvent.block.number = blockNum; + + handleDepositMadeWithAttachment(depositMadeWithAttachmentEvent); + + const depositMadeWithAttachmentEventId = getDepositMadeWithAttachmentEventId(depositMadeWithAttachmentEvent); + assert.fieldEquals("DepositMadeWithAttachmentEvent", depositMadeWithAttachmentEventId, "depositor", depositMadeWithAttachmentEvent.params.depositor.toHexString()); + 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); + + log.info("✅ should create a DepositMadeWithAttachment event", []); + + const redeemer = ADDRESS_3; + + const sharesRedeemedEvent = newSharesRedeemedEvent(redeemer, shares, assets); + sharesRedeemedEvent.block.timestamp = timestamp; + sharesRedeemedEvent.block.number = blockNum; + + handleSharesRedeemed(sharesRedeemedEvent); + + const sharesRedeemedEventId = getSharesRedeemedEventId(sharesRedeemedEvent); + 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()); + + log.info("✅ should create a SharesRedeemed event", []); + + const sharesRedeemedWithAttachmentEvent = newSharesRedeemedWithAttachmentEvent(redeemer, shares, assets); + sharesRedeemedWithAttachmentEvent.block.timestamp = timestamp; + sharesRedeemedWithAttachmentEvent.block.number = blockNum; + + handleSharesRedeemedWithAttachment(sharesRedeemedWithAttachmentEvent); + + const sharesRedeemedWithAttachmentEventId = getSharesRedeemedWithAttachmentEventId(sharesRedeemedWithAttachmentEvent); + assert.fieldEquals( + "SharesRedeemedWithAttachmentEvent", + sharesRedeemedWithAttachmentEventId, + "redeemer", + sharesRedeemedWithAttachmentEvent.params.redeemer.toHexString() + ); + 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); + + log.info("✅ should create a SharesRedeemedAttachment event", []); + afterEach(); }); // exporting for test coverage -export { handleInvoiceFunded }; +export { handleInvoiceFunded, handleClaimCreated, handleInvoiceKickbackAmountSent, handleInvoiceUnfactored }; diff --git a/bulla-contracts/tests/functions/BullaFactoring.testtools.ts b/bulla-contracts/tests/functions/BullaFactoring.testtools.ts index c6e4d0d..3079d92 100644 --- a/bulla-contracts/tests/functions/BullaFactoring.testtools.ts +++ b/bulla-contracts/tests/functions/BullaFactoring.testtools.ts @@ -1,7 +1,15 @@ -import { Address, BigInt, ethereum } from "@graphprotocol/graph-ts"; +import { Address, BigInt, Bytes, ethereum } from "@graphprotocol/graph-ts"; import { newMockEvent } from "matchstick-as"; -import { InvoiceFunded } from "../../generated/BullaFactoring/BullaFactoring"; -import { toEthAddress, toUint256 } from "../helpers"; +import { + DepositMade, + DepositMadeWithAttachment, + InvoiceFunded, + InvoiceKickbackAmountSent, + InvoiceUnfactored, + SharesRedeemed, + SharesRedeemedWithAttachment +} from "../../generated/BullaFactoring/BullaFactoring"; +import { MULTIHASH_BYTES, MULTIHASH_FUNCTION, MULTIHASH_SIZE, toEthAddress, toUint256 } from "../helpers"; export const newInvoiceFundedEvent = (originatingClaimId: BigInt, fundedAmount: BigInt, originalCreditor: Address): InvoiceFunded => { const event: InvoiceFunded = changetype(newMockEvent()); @@ -14,3 +22,94 @@ export const newInvoiceFundedEvent = (originatingClaimId: BigInt, fundedAmount: return event; }; + +export const newInvoiceKickbackAmountSentEvent = (originatingClaimId: BigInt, kickbackAmount: BigInt, originalCreditor: Address): InvoiceKickbackAmountSent => { + const event: InvoiceKickbackAmountSent = changetype(newMockEvent()); + + const invoiceId = new ethereum.EventParam("invoiceId", toUint256(originatingClaimId)); + const kickbackAmountParam = new ethereum.EventParam("kickbackAmount", toUint256(kickbackAmount)); + const originalCreditorParam = new ethereum.EventParam("originalCreditor", toEthAddress(originalCreditor)); + + event.parameters = [invoiceId, kickbackAmountParam, originalCreditorParam]; + + return event; +}; + +export const newInvoiceUnfactoredEvent = (originatingClaimId: BigInt, originalCreditor: Address): InvoiceUnfactored => { + const event: InvoiceUnfactored = changetype(newMockEvent()); + + const invoiceId = new ethereum.EventParam("invoiceId", toUint256(originatingClaimId)); + const originalCreditorParam = new ethereum.EventParam("originalCreditor", toEthAddress(originalCreditor)); + + event.parameters = [invoiceId, originalCreditorParam]; + + return event; +}; + +export const newDepositMadeEvent = (depositor: Address, assets: BigInt, shares: BigInt): DepositMade => { + const event: DepositMade = changetype(newMockEvent()); + + const depositorParam = new ethereum.EventParam("depositor", toEthAddress(depositor)); + const assetsParam = new ethereum.EventParam("assets", toUint256(assets)); + const sharesParam = new ethereum.EventParam("sharesIssued", toUint256(shares)); + + event.parameters = [depositorParam, assetsParam, sharesParam]; + + return event; +}; + +export const newDepositMadeWithAttachmentEvent = (depositor: Address, assets: BigInt, shares: BigInt): DepositMadeWithAttachment => { + const event: DepositMadeWithAttachment = changetype(newMockEvent()); + + const hash: Bytes = changetype(Bytes.fromHexString(MULTIHASH_BYTES)); + + const multihashArray: Array = [ + ethereum.Value.fromBytes(hash), // hash + toUint256(BigInt.fromU32(MULTIHASH_FUNCTION)), // hashFunction + toUint256(BigInt.fromU32(MULTIHASH_SIZE)) // size + ]; + const multihashTuple: ethereum.Tuple = changetype(multihashArray); + + const depositorParam = new ethereum.EventParam("depositor", toEthAddress(depositor)); + const assetsParam = new ethereum.EventParam("assets", toUint256(assets)); + const sharesParam = new ethereum.EventParam("shares", toUint256(shares)); + const attachmentParam = new ethereum.EventParam("attachment", ethereum.Value.fromTuple(multihashTuple)); // change name? + + event.parameters = [depositorParam, assetsParam, sharesParam, attachmentParam]; + + return event; +}; + +export const newSharesRedeemedEvent = (redeemer: Address, assets: BigInt, shares: BigInt): SharesRedeemed => { + const event: SharesRedeemed = changetype(newMockEvent()); + + const redeemerParam = new ethereum.EventParam("redeemer", toEthAddress(redeemer)); + const sharesParam = new ethereum.EventParam("shares", toUint256(shares)); + const assetsParam = new ethereum.EventParam("assets", toUint256(assets)); + + event.parameters = [redeemerParam, sharesParam, assetsParam]; + + return event; +}; + +export const newSharesRedeemedWithAttachmentEvent = (redeemer: Address, assets: BigInt, shares: BigInt): SharesRedeemedWithAttachment => { + const event: SharesRedeemedWithAttachment = changetype(newMockEvent()); + + const hash: Bytes = changetype(Bytes.fromHexString(MULTIHASH_BYTES)); + + const multihashArray: Array = [ + ethereum.Value.fromBytes(hash), // hash + toUint256(BigInt.fromU32(MULTIHASH_FUNCTION)), // hashFunction + toUint256(BigInt.fromU32(MULTIHASH_SIZE)) // size + ]; + const multihashTuple: ethereum.Tuple = changetype(multihashArray); + + const depositorParam = new ethereum.EventParam("depositor", toEthAddress(redeemer)); + const sharesParam = new ethereum.EventParam("shares", toUint256(shares)); + const assetsParam = new ethereum.EventParam("assets", toUint256(assets)); + const attachmentParam = new ethereum.EventParam("attachment", ethereum.Value.fromTuple(multihashTuple)); // change name? + + event.parameters = [depositorParam, sharesParam, assetsParam, attachmentParam]; + + return event; +};