Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
rouzwelt committed Oct 26, 2024
1 parent 8d90b7a commit a11dbb2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/abis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export const ClearConfig =
*/
export const orderbookAbi = [
`event AddOrderV2(address sender, bytes32 orderHash, ${OrderV3} order)`,
`event AfterClear(address sender, ${ClearStateChange} clearStateChange)`,
`event RemoveOrderV2(address sender, bytes32 orderHash, ${OrderV3} order)`,
`event AfterClear(address sender, ${ClearStateChange} clearStateChange)`,
"function vaultBalance(address owner, address token, uint256 vaultId) external view returns (uint256 balance)",
`function deposit2(address token, uint256 vaultId, uint256 amount, ${TaskV1}[] calldata tasks) external`,
`function addOrder2(${OrderConfigV3} calldata config, ${TaskV1}[] calldata tasks) external returns (bool stateChanged)`,
Expand Down
50 changes: 9 additions & 41 deletions src/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ export function toOrder(orderLog: any): Order {
};
}

export const orderbookAbi = parseAbi(abi);
export type UnwatchOrderbook = {
unwatchAddOrder: WatchContractEventReturnType;
unwatchRemoveOrder: WatchContractEventReturnType;
};
export const orderbookAbi = parseAbi([abi[0], abi[1]]);

/**
* Applies an event watcher for a specified orderbook
Expand All @@ -92,17 +88,16 @@ export function watchOrderbook(
orderbook: string,
viemClient: ViemClient,
watchedOrderbookOrders: WatchedOrderbookOrders,
): UnwatchOrderbook {
const unwatchAddOrder = viemClient.watchContractEvent({
): WatchContractEventReturnType {
return viemClient.watchContractEvent({
address: orderbook as `0x${string}`,
abi: orderbookAbi,
eventName: "AddOrderV2",
pollingInterval: 30_000,
onLogs: (logs) => {
logs.forEach((log) => {
if (log) {
watchedOrderbookOrders.orderLogs.push({
type: "add",
type: log.eventName === "AddOrderV2" ? "add" : "remove",
logIndex: log.logIndex,
block: Number(log.blockNumber),
txHash: log.transactionHash,
Expand All @@ -112,31 +107,6 @@ export function watchOrderbook(
});
},
});

const unwatchRemoveOrder = viemClient.watchContractEvent({
address: orderbook as `0x${string}`,
abi: orderbookAbi,
eventName: "RemoveOrderV2",
pollingInterval: 30_000,
onLogs: (logs) => {
logs.forEach((log) => {
if (log) {
watchedOrderbookOrders.orderLogs.push({
type: "remove",
logIndex: log.logIndex,
block: Number(log.blockNumber),
txHash: log.transactionHash,
order: logToOrder(log.args as any as OrderEventLog),
});
}
});
},
});

return {
unwatchAddOrder,
unwatchRemoveOrder,
};
}

/**
Expand All @@ -147,26 +117,24 @@ export function watchAllOrderbooks(
orderbooks: string[],
viemClient: ViemClient,
watchedOrderbooksOrders: Record<string, WatchedOrderbookOrders>,
): Record<string, UnwatchOrderbook> {
const allUnwatchers: Record<string, UnwatchOrderbook> = {};
): Record<string, WatchContractEventReturnType> {
const allUnwatchers: Record<string, WatchContractEventReturnType> = {};
for (const v of orderbooks) {
const ob = v.toLowerCase();
if (!watchedOrderbooksOrders[ob]) {
watchedOrderbooksOrders[ob] = { orderLogs: [] };
}
const unwatcher = watchOrderbook(ob, viemClient, watchedOrderbooksOrders[ob]);
allUnwatchers[ob] = unwatcher;
allUnwatchers[ob] = watchOrderbook(ob, viemClient, watchedOrderbooksOrders[ob]);
}
return allUnwatchers;
}

/**
* Unwatches all orderbooks event watchers
*/
export function unwatchAllOrderbooks(unwatchers: Record<string, UnwatchOrderbook>) {
export function unwatchAllOrderbooks(unwatchers: Record<string, WatchContractEventReturnType>) {
for (const ob in unwatchers) {
unwatchers[ob].unwatchAddOrder();
unwatchers[ob].unwatchRemoveOrder();
unwatchers[ob]?.();
}
}

Expand Down

0 comments on commit a11dbb2

Please sign in to comment.