Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete order listener #67

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions bulla-contracts/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,17 @@ type OrderExecutedEvent implements IEventLog @entity {
timestamp: BigInt!
}

type OrderDeletedEvent implements IEventLog @entity {
id: ID!
order: OrderERC20!
signerWallet: Bytes! #address
eventName: String!
blockNumber: BigInt!
transactionHash: Bytes!
logIndex: BigInt!
timestamp: BigInt!
}

type LoanOfferedEvent implements IEventLog @entity {
id: ID!
loanId: String!
Expand Down
11 changes: 9 additions & 2 deletions bulla-contracts/src/functions/BullaSwap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BigInt, ethereum } from "@graphprotocol/graph-ts";
import { OrderCreated, OrderExecuted } from "../../generated/BullaSwap/BullaSwap";
import { OrderCreatedEvent, OrderExecutedEvent } from "../../generated/schema";
import { OrderCreated, OrderDeleted, OrderExecuted } from "../../generated/BullaSwap/BullaSwap";
import { OrderCreatedEvent, OrderDeletedEvent, OrderExecutedEvent } from "../../generated/schema";

export const getOrderCreatedEventId = (orderId: BigInt, event: ethereum.Event): string =>
"OrderCreated-" + orderId.toString() + "-" + event.transaction.hash.toHexString() + "-" + event.logIndex.toString();
Expand All @@ -15,3 +15,10 @@ export const getOrderExecutedEventId = (orderId: BigInt, event: ethereum.Event):
export const createOrderExecutedEvent = (orderId: BigInt, event: OrderExecuted): OrderExecutedEvent => {
return new OrderExecutedEvent(getOrderExecutedEventId(orderId, event));
};

export const getOrderDeletedEventId = (orderId: BigInt, event: ethereum.Event): string =>
"OrderDeleted-" + orderId.toString() + "-" + event.transaction.hash.toHexString() + "-" + event.logIndex.toString();

export const createOrderDeletedEvent = (orderId: BigInt, event: OrderDeleted): OrderDeletedEvent => {
return new OrderDeletedEvent(getOrderDeletedEventId(orderId, event));
};
42 changes: 40 additions & 2 deletions bulla-contracts/src/mappings/BullaSwap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OrderCreated, OrderExecuted } from "../../generated/BullaSwap/BullaSwap";
import { OrderCreated, OrderDeleted, OrderExecuted } from "../../generated/BullaSwap/BullaSwap";
import { OrderERC20 } from "../../generated/schema";
import { createOrderCreatedEvent, createOrderExecutedEvent } from "../functions/BullaSwap";
import { createOrderCreatedEvent, createOrderDeletedEvent, createOrderExecutedEvent } from "../functions/BullaSwap";
import { getOrCreateToken, getOrCreateUser } from "../functions/common";

export function handleOrderCreated(event: OrderCreated): void {
Expand Down Expand Up @@ -80,3 +80,41 @@ export function handleOrderExecuted(event: OrderExecuted): void {

orderExecutedEvent.save();
}

export function handleOrderDeleted(event: OrderDeleted): void {
const ev = event.params;
const orderId = ev.orderId;

// Create the order entity
const order = new OrderERC20(orderId.toString());
order.orderId = orderId;
order.expiry = ev.order.expiry;
order.signerWallet = ev.order.signerWallet;
order.signerToken = getOrCreateToken(ev.order.signerToken).id;
order.signerAmount = ev.order.signerAmount;
order.senderWallet = ev.order.senderWallet;
order.senderToken = getOrCreateToken(ev.order.senderToken).id;
order.senderAmount = ev.order.senderAmount;
order.save();

// Create the event entity
const orderDeletedEvent = createOrderDeletedEvent(orderId, event);
orderDeletedEvent.order = order.id;
orderDeletedEvent.signerWallet = ev.signerWallet;
orderDeletedEvent.eventName = "OrderDeleted";
orderDeletedEvent.blockNumber = event.block.number;
orderDeletedEvent.transactionHash = event.transaction.hash;
orderDeletedEvent.logIndex = event.logIndex;
orderDeletedEvent.timestamp = event.block.timestamp;

// Update user entities
const signer = getOrCreateUser(ev.order.signerWallet);
signer.swapEvents = signer.swapEvents ? signer.swapEvents.concat([orderDeletedEvent.id]) : [orderDeletedEvent.id];
signer.save();

const sender = getOrCreateUser(ev.order.senderWallet);
sender.swapEvents = sender.swapEvents ? sender.swapEvents.concat([orderDeletedEvent.id]) : [orderDeletedEvent.id];
sender.save();

orderDeletedEvent.save();
}
2 changes: 2 additions & 0 deletions bulla-contracts/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,6 @@ dataSources:
handler: handleOrderCreated
- event: OrderExecuted(indexed uint256,indexed address,indexed address,(uint256,uint256,address,address,uint256,address,address,uint256))
handler: handleOrderExecuted
- event: OrderDeleted(indexed uint256,indexed address,(uint256,uint256,address,address,uint256,address,address,uint256))
handler: handleOrderDeleted
file: ./src/mappings/BullaSwap.ts
43 changes: 40 additions & 3 deletions bulla-contracts/tests/BullaSwap.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { BigInt, log } from "@graphprotocol/graph-ts";
import { assert, test } from "matchstick-as/assembly/index";

import { handleOrderCreated, handleOrderExecuted } from "../src/mappings/BullaSwap";
import { handleOrderCreated, handleOrderDeleted, handleOrderExecuted } from "../src/mappings/BullaSwap";
import { ADDRESS_1, ADDRESS_2, ADDRESS_3, ADDRESS_4, afterEach, setupContracts } from "./helpers";
import { newOrderCreatedEvent, newOrderExecutedEvent } from "./functions/BullaSwap.testtools";
import { newOrderCreatedEvent, newOrderDeletedEvent, newOrderExecutedEvent } from "./functions/BullaSwap.testtools";
import { User } from "../generated/schema";
import { getOrderCreatedEventId, getOrderExecutedEventId } from "../src/functions/BullaSwap";
import { getOrderCreatedEventId, getOrderDeletedEventId, getOrderExecutedEventId } from "../src/functions/BullaSwap";

test("it handles OrderCreated event", () => {
const orderId = BigInt.fromI32(3);
Expand Down Expand Up @@ -83,5 +83,42 @@ test("it handles OrderExecuted event", () => {
afterEach();
});

test("it handles OrderDeleted event", () => {
const orderId = BigInt.fromI32(3);
const expiry = BigInt.fromI32(100);
const signerWallet = ADDRESS_1;
const signerToken = ADDRESS_2;
const signerAmount = BigInt.fromI32(10000);
const senderWallet = ADDRESS_3;
const senderToken = ADDRESS_4;
const senderAmount = BigInt.fromI32(10000);

setupContracts();
const signerUser = new User(signerWallet.toHexString());
signerUser.swapEvents = [];
signerUser.save();

const senderUser = new User(senderWallet.toHexString());
senderUser.swapEvents = [];
senderUser.save();

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

const orderDeletedEvent = newOrderDeletedEvent(orderId, signerWallet, signerToken, signerAmount, senderWallet, senderToken, senderAmount, expiry);
orderDeletedEvent.block.timestamp = timestamp;
orderDeletedEvent.block.number = blockNum;

handleOrderDeleted(orderDeletedEvent);

const orderDeletedEventId = getOrderDeletedEventId(orderId, orderDeletedEvent);
assert.fieldEquals("OrderDeletedEvent", orderDeletedEventId, "signerWallet", signerWallet.toHexString());
assert.fieldEquals("OrderERC20", orderId.toString(), "orderId", orderId.toString());

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

afterEach();
});

// exporting for test coverage
export { handleOrderCreated, handleOrderExecuted };
47 changes: 46 additions & 1 deletion bulla-contracts/tests/functions/BullaSwap.testtools.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Address, BigInt, ethereum } from "@graphprotocol/graph-ts";
import { newMockEvent } from "matchstick-as";
import { OrderCreated, OrderExecuted } from "../../generated/BullaSwap/BullaSwap";
import { OrderCreated, OrderDeleted, OrderExecuted } from "../../generated/BullaSwap/BullaSwap";
import { MOCK_BULLA_SWAP_ADDRESS } from "../helpers";

/// @NOTICE: event parameters should be in the same order as the event declaration in the contract
Expand Down Expand Up @@ -96,3 +96,48 @@ export function newOrderExecutedEvent(

return orderExecutedEvent;
}

export function newOrderDeletedEvent(
orderId: BigInt,
signerWallet: Address,
signerToken: Address,
signerAmount: BigInt,
senderWallet: Address,
senderToken: Address,
senderAmount: BigInt,
expiry: BigInt
): OrderDeleted {
const mockEvent = newMockEvent();
const orderDeletedEvent = new OrderDeleted(
mockEvent.address,
mockEvent.logIndex,
mockEvent.transactionLogIndex,
mockEvent.logType,
mockEvent.block,
mockEvent.transaction,
mockEvent.parameters,
mockEvent.receipt
);

orderDeletedEvent.address = MOCK_BULLA_SWAP_ADDRESS;
orderDeletedEvent.parameters = new Array();
orderDeletedEvent.parameters.push(new ethereum.EventParam("orderId", ethereum.Value.fromUnsignedBigInt(orderId)));
orderDeletedEvent.parameters.push(new ethereum.EventParam("signerWallet", ethereum.Value.fromAddress(signerWallet)));

const orderTupleArray = [
ethereum.Value.fromUnsignedBigInt(orderId),
ethereum.Value.fromUnsignedBigInt(expiry),
ethereum.Value.fromAddress(signerWallet),
ethereum.Value.fromAddress(signerToken),
ethereum.Value.fromUnsignedBigInt(signerAmount),
ethereum.Value.fromAddress(senderWallet),
ethereum.Value.fromAddress(senderToken),
ethereum.Value.fromUnsignedBigInt(senderAmount)
];

const orderTuple: ethereum.Tuple = changetype<ethereum.Tuple>(orderTupleArray);

orderDeletedEvent.parameters.push(new ethereum.EventParam("order", ethereum.Value.fromTuple(orderTuple)));

return orderDeletedEvent;
}
Loading