-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* setup bulla swap mappings * fix tests * addressed comments * add OrderCreated test * OrderExecuted test
- Loading branch information
1 parent
beab830
commit 0e1110d
Showing
11 changed files
with
869 additions
and
12 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { BigInt, ethereum } from "@graphprotocol/graph-ts"; | ||
import { OrderCreated, OrderExecuted } from "../../generated/BullaSwap/BullaSwap"; | ||
import { OrderCreatedEvent, OrderExecutedEvent } from "../../generated/schema"; | ||
|
||
export const getOrderCreatedEventId = (orderId: BigInt, event: ethereum.Event): string => | ||
"OrderCreated-" + orderId.toString() + "-" + event.transaction.hash.toHexString() + "-" + event.logIndex.toString(); | ||
|
||
export const createOrderCreatedEvent = (orderId: BigInt, event: OrderCreated): OrderCreatedEvent => { | ||
return new OrderCreatedEvent(getOrderCreatedEventId(orderId, event)); | ||
}; | ||
|
||
export const getOrderExecutedEventId = (orderId: BigInt, event: ethereum.Event): string => | ||
"OrderExecuted-" + orderId.toString() + "-" + event.transaction.hash.toHexString() + "-" + event.logIndex.toString(); | ||
|
||
export const createOrderExecutedEvent = (orderId: BigInt, event: OrderExecuted): OrderExecutedEvent => { | ||
return new OrderExecutedEvent(getOrderExecutedEventId(orderId, event)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { OrderCreated, OrderExecuted } from "../../generated/BullaSwap/BullaSwap"; | ||
import { OrderERC20 } from "../../generated/schema"; | ||
import { createOrderCreatedEvent, createOrderExecutedEvent } from "../functions/BullaSwap"; | ||
import { getOrCreateToken, getOrCreateUser } from "../functions/common"; | ||
|
||
export function handleOrderCreated(event: OrderCreated): 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 orderCreatedEvent = createOrderCreatedEvent(orderId, event); | ||
orderCreatedEvent.order = order.id; | ||
orderCreatedEvent.sender = ev.sender; | ||
orderCreatedEvent.signerWallet = ev.order.signerWallet; | ||
orderCreatedEvent.eventName = "OrderCreated"; | ||
orderCreatedEvent.blockNumber = event.block.number; | ||
orderCreatedEvent.transactionHash = event.transaction.hash; | ||
orderCreatedEvent.logIndex = event.logIndex; | ||
orderCreatedEvent.timestamp = event.block.timestamp; | ||
|
||
// Update user entities | ||
const sender = getOrCreateUser(ev.sender); | ||
sender.swapEvents = sender.swapEvents ? sender.swapEvents.concat([orderCreatedEvent.id]) : [orderCreatedEvent.id]; | ||
sender.save(); | ||
|
||
const signer = getOrCreateUser(ev.signerWallet); | ||
signer.swapEvents = signer.swapEvents ? signer.swapEvents.concat([orderCreatedEvent.id]) : [orderCreatedEvent.id]; | ||
signer.save(); | ||
|
||
orderCreatedEvent.save(); | ||
} | ||
|
||
export function handleOrderExecuted(event: OrderExecuted): 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 orderExecutedEvent = createOrderExecutedEvent(orderId, event); | ||
orderExecutedEvent.order = order.id; | ||
orderExecutedEvent.sender = ev.sender; | ||
orderExecutedEvent.signerWallet = ev.order.signerWallet; | ||
orderExecutedEvent.eventName = "OrderExecuted"; | ||
orderExecutedEvent.blockNumber = event.block.number; | ||
orderExecutedEvent.transactionHash = event.transaction.hash; | ||
orderExecutedEvent.logIndex = event.logIndex; | ||
orderExecutedEvent.timestamp = event.block.timestamp; | ||
|
||
// Update user entities | ||
const sender = getOrCreateUser(ev.sender); | ||
sender.swapEvents = sender.swapEvents ? sender.swapEvents.concat([orderExecutedEvent.id]) : [orderExecutedEvent.id]; | ||
sender.save(); | ||
|
||
const recipient = getOrCreateUser(ev.recipient); | ||
recipient.swapEvents = recipient.swapEvents ? recipient.swapEvents.concat([orderExecutedEvent.id]) : [orderExecutedEvent.id]; | ||
recipient.save(); | ||
|
||
orderExecutedEvent.save(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { BigInt, log } from "@graphprotocol/graph-ts"; | ||
import { assert, test } from "matchstick-as/assembly/index"; | ||
|
||
import { handleOrderCreated, 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 { User } from "../generated/schema"; | ||
import { getOrderCreatedEventId, getOrderExecutedEventId } from "../src/functions/BullaSwap"; | ||
|
||
test("it handles OrderCreated 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 orderCreatedEvent = newOrderCreatedEvent(orderId, signerWallet, signerToken, signerAmount, senderWallet, senderToken, senderAmount, expiry); | ||
orderCreatedEvent.block.timestamp = timestamp; | ||
orderCreatedEvent.block.number = blockNum; | ||
|
||
handleOrderCreated(orderCreatedEvent); | ||
|
||
const orderCreatedEventId = getOrderCreatedEventId(orderId, orderCreatedEvent); | ||
assert.fieldEquals("OrderCreatedEvent", orderCreatedEventId, "signerWallet", signerWallet.toHexString()); | ||
assert.fieldEquals("OrderCreatedEvent", orderCreatedEventId, "sender", senderWallet.toHexString()); | ||
assert.fieldEquals("OrderERC20", orderId.toString(), "orderId", orderId.toString()); | ||
|
||
log.info("✅ should create a OrderCreated event", []); | ||
|
||
afterEach(); | ||
}); | ||
|
||
test("it handles OrderExecuted 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 orderExecutedEvent = newOrderExecutedEvent(orderId, signerWallet, signerToken, signerAmount, senderWallet, senderToken, senderAmount, expiry); | ||
orderExecutedEvent.block.timestamp = timestamp; | ||
orderExecutedEvent.block.number = blockNum; | ||
|
||
handleOrderExecuted(orderExecutedEvent); | ||
|
||
const orderExecutedEventId = getOrderExecutedEventId(orderId, orderExecutedEvent); | ||
assert.fieldEquals("OrderExecutedEvent", orderExecutedEventId, "signerWallet", signerWallet.toHexString()); | ||
assert.fieldEquals("OrderExecutedEvent", orderExecutedEventId, "sender", senderWallet.toHexString()); | ||
assert.fieldEquals("OrderERC20", orderId.toString(), "orderId", orderId.toString()); | ||
|
||
log.info("✅ should create a OrderExecuted event", []); | ||
|
||
afterEach(); | ||
}); | ||
|
||
// exporting for test coverage | ||
export { handleOrderCreated, handleOrderExecuted }; |
Oops, something went wrong.