Skip to content

Commit

Permalink
feat: apply filter policy before adding an order
Browse files Browse the repository at this point in the history
  • Loading branch information
anxolin committed Dec 10, 2024
1 parent 57965f1 commit c87f05b
Showing 1 changed file with 51 additions and 30 deletions.
81 changes: 51 additions & 30 deletions src/domain/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ import {
Proof,
Registry,
} from "../../types";
import {
ConditionalOrder,
ConditionalOrderParams,
SupportedChainId,
} from "@cowprotocol/cow-sdk";
import { ConditionalOrder, ConditionalOrderParams } from "@cowprotocol/cow-sdk";

import { ChainContext } from "../../services/chain";
import { policy } from "../polling/filtering";

const composableCow = ComposableCoW__factory.createInterface();

Expand All @@ -39,10 +36,9 @@ export async function processNewOrderEvent(
context: ChainContext,
event: ConditionalOrderCreatedEvent
) {
const { chainId } = context;
const { chainId, registry } = context;

const action = async () => {
const { registry, chainId } = context;
const { transactionHash: tx, blockNumber } = event;

const log = getLogger({
Expand All @@ -55,10 +51,12 @@ export async function processNewOrderEvent(
let hasErrors = false;
let numContractsAdded = 0;

const { error, added } = await decodeAndAddOrder(event, registry, chainId);
const { error, added } = await decodeAndAddOrder(event, context);

if (added) {
metrics.ownersTotal.labels(context.chainId.toString()).inc();
const network = context.chainId.toString();
metrics.ownersTotal.labels(network).inc();
metrics.activeOrdersTotal.labels(network).inc();
numContractsAdded++;
} else {
log.error(
Expand Down Expand Up @@ -97,9 +95,9 @@ export async function processNewOrderEvent(

async function decodeAndAddOrder(
event: ConditionalOrderCreatedEvent | MerkleRootSetEvent,
registry: Registry,
chainId: SupportedChainId
context: ChainContext
): Promise<{ error: boolean; added: boolean }> {
const { chainId, registry } = context;
const log = getLogger({
name: "decodeAndAddOrder",
chainId,
Expand All @@ -123,18 +121,18 @@ async function decodeAndAddOrder(
) as [string, IConditionalOrder.ConditionalOrderParamsStruct];

// Attempt to add the conditional order to the registry
addOrder(
added = addOrder(
eventLog.transactionHash,
owner,
toConditionalOrderParams(params),
null,
eventLog.address,
registry,
chainId,
event.blockNumber
event.blockNumber,
context
);
added = true;
metrics.singleOrdersTotal.labels(network).inc();
if (added) {
metrics.singleOrdersTotal.labels(network).inc();
}
} else if (
event.topics[0] == composableCow.getEventTopic("MerkleRootSet")
) {
Expand Down Expand Up @@ -166,18 +164,19 @@ async function decodeAndAddOrder(
order as BytesLike
);
// Attempt to add the conditional order to the registry
addOrder(

added = addOrder(
event.transactionHash,
owner,
toConditionalOrderParams(decodedOrder[1]),
{ merkleRoot: root, path: decodedOrder[0] },
eventLog.address,
registry,
chainId,
event.blockNumber
event.blockNumber,
context
);
added = true;
metrics.merkleRootTotal.labels(network).inc();
if (added) {
metrics.merkleRootTotal.labels(network).inc();
}
}
}
}
Expand Down Expand Up @@ -208,15 +207,33 @@ function addOrder(
params: ConditionalOrderParams,
proof: Proof | null,
composableCow: string,
registry: Registry,
chainId: SupportedChainId,
blockNumber: number
) {
blockNumber: number,
context: ChainContext
): boolean {
const { chainId, registry, filterPolicy } = context;
const log = getLogger({ name: "addOrder", chainId, blockNumber });
const { handler, salt, staticInput } = params;
const { network, ownerOrders } = registry;

const conditionalOrderParams = toConditionalOrderParams(params);
const conditionalOrderId = ConditionalOrder.leafToId(params);

// Apply the filter policy, in case this order should be dropped
if (filterPolicy) {
const filterResult = filterPolicy.preFilter({
conditionalOrderId,
transaction: tx,
owner,
conditionalOrderParams,
});

if (filterResult === policy.FilterAction.DROP) {
// Drop the order
log.info("Not adding the conditional order. Reason: AcceptPolicy: DROP");
return false;
}
}

if (ownerOrders.has(owner)) {
const conditionalOrders = ownerOrders.get(owner);
log.info(`Adding conditional order to already existing owner ${owner}`, {
Expand All @@ -240,8 +257,9 @@ function addOrder(
}
}

// If the params are not in the conditionalOrder, add them
// If the params are not in the conditionalOrder
if (!exists) {
// Add the order for existing owner
conditionalOrders?.add({
id: conditionalOrderId,
tx,
Expand All @@ -250,9 +268,10 @@ function addOrder(
orders: new Map(),
composableCow,
});
metrics.activeOrdersTotal.labels(network).inc();
return true;
}
} else {
// Add the order for new owner
log.info(`Adding conditional order to new owner ${owner}:`, {
conditionalOrderId,
tx,
Expand All @@ -275,8 +294,10 @@ function addOrder(
);

metrics.activeOwnersTotal.labels(network).inc();
metrics.activeOrdersTotal.labels(network).inc();
return true;
}

return false;
}

/**
Expand Down

0 comments on commit c87f05b

Please sign in to comment.