Skip to content

Commit

Permalink
shares redeemed events with attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
solidoracle committed Apr 9, 2024
1 parent 7c8c1a8 commit d1dbf25
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 13 deletions.
29 changes: 29 additions & 0 deletions bulla-contracts/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,35 @@ type DepositMadeWithAttachmentEvent implements IEventLog @entity {
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
35 changes: 33 additions & 2 deletions bulla-contracts/src/functions/BullaFactoring.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import { BigInt, ethereum } from "@graphprotocol/graph-ts";
import { DepositMadeEvent, DepositMadeWithAttachmentEvent, InvoiceFundedEvent, InvoiceKickbackAmountSentEvent, InvoiceUnfactoredEvent } from "../../generated/schema";
import { DepositMade, DepositMadeWithAttachment, InvoiceFunded, InvoiceKickbackAmountSent, InvoiceUnfactored } 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();
Expand Down Expand Up @@ -34,3 +50,18 @@ export const getDepositMadeWithAttachmentEventId = (event: ethereum.Event): stri

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));
8 changes: 7 additions & 1 deletion bulla-contracts/src/functions/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +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 } from "../../generated/BullaFactoring/BullaFactoring";
import { DepositMadeWithAttachmentAttachmentStruct, SharesRedeemedWithAttachmentAttachmentStruct } from "../../generated/BullaFactoring/BullaFactoring";

export const ADDRESS_ZERO = "0x0000000000000000000000000000000000000000";

Expand Down Expand Up @@ -64,6 +64,12 @@ export const getIPFSHash_depositWithAttachment = (attachment: DepositMadeWithAtt
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
55 changes: 52 additions & 3 deletions bulla-contracts/src/mappings/BullaFactoring.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { DepositMade, DepositMadeWithAttachment, InvoiceFunded, InvoiceKickbackAmountSent, InvoiceUnfactored } from "../../generated/BullaFactoring/BullaFactoring";
import {
DepositMade,
DepositMadeWithAttachment,
InvoiceFunded,
InvoiceKickbackAmountSent,
InvoiceUnfactored,
SharesRedeemed,
SharesRedeemedWithAttachment
} from "../../generated/BullaFactoring/BullaFactoring";
import { getClaim } from "../functions/BullaClaimERC721";
import {
createDepositMadeEvent,
createDepositMadeWithAttachmentEvent,
createInvoiceFundedEvent,
createInvoiceKickbackAmountSentEvent,
createInvoiceUnfactoredEvent
createInvoiceUnfactoredEvent,
createSharesRedeemedEvent,
createSharesRedeemedWithAttachmentEvent
} from "../functions/BullaFactoring";
import { getIPFSHash_depositWithAttachment } from "../functions/common";
import { getIPFSHash_depositWithAttachment, getIPFSHash_redeemWithAttachment } from "../functions/common";

export function handleInvoiceFunded(event: InvoiceFunded): void {
const ev = event.params;
Expand Down Expand Up @@ -106,3 +116,42 @@ export function handleDepositMadeWithAttachment(event: DepositMadeWithAttachment

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();
}
6 changes: 6 additions & 0 deletions bulla-contracts/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ dataSources:
- InvoiceUnfactoredEvent
- DepositMadeEvent
- DepositMadeWithAttachmentEvent
- SharesRedeemedEvent
- SharesRedeemedWithAttachmentEvent
abis:
- name: BullaFactoring
file: ./abis/BullaFactoring.json
Expand All @@ -260,4 +262,8 @@ dataSources:
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
50 changes: 45 additions & 5 deletions bulla-contracts/tests/BullaFactoring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,29 @@ import {
handleDepositMadeWithAttachment,
handleInvoiceFunded,
handleInvoiceKickbackAmountSent,
handleInvoiceUnfactored
handleInvoiceUnfactored,
handleSharesRedeemed,
handleSharesRedeemedWithAttachment
} from "../src/mappings/BullaFactoring";
import { newClaimCreatedEvent } from "./functions/BullaClaimERC721.testtools";
import { ADDRESS_1, ADDRESS_2, IPFS_HASH, afterEach, setupContracts } from "./helpers";
import { ADDRESS_1, ADDRESS_2, ADDRESS_3, IPFS_HASH, afterEach, setupContracts } from "./helpers";
import {
newDepositMadeEvent,
newDepositMadeWithAttachmentEvent,
newInvoiceFundedEvent,
newInvoiceKickbackAmountSentEvent,
newInvoiceUnfactoredEvent
newInvoiceUnfactoredEvent,
newSharesRedeemedEvent,
newSharesRedeemedWithAttachmentEvent
} from "./functions/BullaFactoring.testtools";
import {
getDepositMadeEventId,
getDepositMadeWithAttachmentEventId,
getInvoiceFundedEventId,
getInvoiceKickbackAmountSentEventId,
getInvoiceUnfactoredEventId
getInvoiceUnfactoredEventId,
getSharesRedeemedEventId,
getSharesRedeemedWithAttachmentEventId
} from "../src/functions/BullaFactoring";

test("it handles BullaFactoring events", () => {
Expand Down Expand Up @@ -101,7 +107,7 @@ test("it handles BullaFactoring events", () => {

handleDepositMade(depositMadeEvent);

const depositMadeEventId = getDepositMadeEventId(invoiceUnfactoredEvent);
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());
Expand All @@ -122,6 +128,40 @@ test("it handles BullaFactoring events", () => {

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();
});

Expand Down
46 changes: 44 additions & 2 deletions bulla-contracts/tests/functions/BullaFactoring.testtools.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { Address, BigInt, Bytes, ethereum } from "@graphprotocol/graph-ts";
import { newMockEvent } from "matchstick-as";
import { DepositMade, DepositMadeWithAttachment, InvoiceFunded, InvoiceKickbackAmountSent, InvoiceUnfactored } from "../../generated/BullaFactoring/BullaFactoring";
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 => {
Expand Down Expand Up @@ -64,10 +72,44 @@ export const newDepositMadeWithAttachmentEvent = (depositor: Address, assets: Bi

const depositorParam = new ethereum.EventParam("depositor", toEthAddress(depositor));
const assetsParam = new ethereum.EventParam("assets", toUint256(assets));
const sharesParam = new ethereum.EventParam("shares", toUint256(shares)); // change name?
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<SharesRedeemed>(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<SharesRedeemedWithAttachment>(newMockEvent());

const hash: Bytes = changetype<Bytes>(Bytes.fromHexString(MULTIHASH_BYTES));

const multihashArray: Array<ethereum.Value> = [
ethereum.Value.fromBytes(hash), // hash
toUint256(BigInt.fromU32(MULTIHASH_FUNCTION)), // hashFunction
toUint256(BigInt.fromU32(MULTIHASH_SIZE)) // size
];
const multihashTuple: ethereum.Tuple = changetype<ethereum.Tuple>(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;
};

0 comments on commit d1dbf25

Please sign in to comment.