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

Add conditional order id to our model #145

Merged
merged 4 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 14 additions & 2 deletions src/domain/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import {
Proof,
Registry,
} from "../../types";
import { ConditionalOrder, ConditionalOrderParams } from "@cowprotocol/cow-sdk";

import { ChainContext } from "../../services/chain";
import { ConditionalOrderParams } from "@cowprotocol/cow-sdk";

/**
* Listens to these events on the `ComposableCoW` contract:
Expand Down Expand Up @@ -201,6 +201,8 @@ export function add(
const log = getLogger("addContract:add");
const { handler, salt, staticInput } = params;
const { network, ownerOrders } = registry;

const conditionalOrderId = ConditionalOrder.leafToId(params);
if (ownerOrders.has(owner)) {
const conditionalOrders = ownerOrders.get(owner);
log.info(
Expand All @@ -220,6 +222,7 @@ export function add(
// If the params are not in the conditionalOrder, add them
if (!exists) {
conditionalOrders?.add({
id: conditionalOrderId,
tx,
params: { handler, salt, staticInput },
proof,
Expand All @@ -237,7 +240,16 @@ export function add(
});
registry.ownerOrders.set(
owner,
new Set([{ tx, params, proof, orders: new Map(), composableCow }])
new Set([
{
id: conditionalOrderId,
tx,
params,
proof,
orders: new Map(),
composableCow,
},
])
);
metrics.activeOwnersTotal.labels(network).inc();
metrics.activeOrdersTotal.labels(network).inc();
Expand Down
11 changes: 8 additions & 3 deletions src/domain/polling/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export async function checkForAndPlaceOrder(
blockNumber.toString(),
ownerRef
);
const logOrderDetails = `Processing order from TX ${conditionalOrder.tx} with params:`;
const logOrderDetails = `Processing order ${conditionalOrder.id} from TX ${conditionalOrder.tx} with params:`;

const { result: lastHint } = conditionalOrder.pollResult || {};

Expand Down Expand Up @@ -259,8 +259,12 @@ async function _processConditionalOrder(
"checkForAndPlaceOrder:_processConditionalOrder",
orderRef
);
const id = ConditionalOrderSDK.leafToId(conditionalOrder.params);
const metricLabels = [chainId.toString(), handler, owner, id];
const metricLabels = [
chainId.toString(),
handler,
owner,
conditionalOrder.id,
];
try {
metrics.pollingRunsTotal.labels(...metricLabels).inc();

Expand Down Expand Up @@ -289,6 +293,7 @@ async function _processConditionalOrder(
},
};
let pollResult = await pollConditionalOrder(
conditionalOrder.id,
pollParams,
conditionalOrder.params,
orderRef
Expand Down
3 changes: 2 additions & 1 deletion src/domain/polling/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const ordersFactory = new ConditionalOrderFactory(
);

export async function pollConditionalOrder(
conditionalOrderId: string,
pollParams: PollParams,
conditionalOrderParams: ConditionalOrderParams,
orderRef: string
Expand All @@ -30,7 +31,7 @@ export async function pollConditionalOrder(
: pollParams;

log.debug(
`Polling for ${order.toString()} using block (${
`Polling id ${conditionalOrderId}. Order ${order.toString()} using block (${
actualPollParams.blockInfo === undefined
? "latest"
: actualPollParams.blockInfo.blockNumber
Expand Down
41 changes: 33 additions & 8 deletions src/types/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import type { ConditionalOrderCreatedEvent } from "./generated/ComposableCoW";
import { ConditionalOrderParams, PollResult } from "@cowprotocol/cow-sdk";
import { DBService } from "../services";
import { metrics } from "../utils";
import { ConditionalOrder as ConditionalOrderSdk } from "@cowprotocol/cow-sdk";

// Standardise the storage key
const LAST_NOTIFIED_ERROR_STORAGE_KEY = "LAST_NOTIFIED_ERROR";
const LAST_PROCESSED_BLOCK_STORAGE_KEY = "LAST_PROCESSED_BLOCK";
const CONDITIONAL_ORDER_REGISTRY_STORAGE_KEY = "CONDITIONAL_ORDER_REGISTRY";
const CONDITIONAL_ORDER_REGISTRY_VERSION_KEY =
"CONDITIONAL_ORDER_REGISTRY_VERSION";
const CONDITIONAL_ORDER_REGISTRY_VERSION = 1;
const CONDITIONAL_ORDER_REGISTRY_VERSION = 2;

export const getNetworkStorageKey = (key: string, network: string): string => {
return `${key}_${network}`;
Expand Down Expand Up @@ -49,6 +50,13 @@ export enum OrderStatus {
}

export type ConditionalOrder = {
/**
* Id of the conditional order (which also happens to be the key used for `ctx` in the ComposableCoW contract).
*
* This is a `keccak256` hash of the serialized conditional order.
*/
id: string;

/**
* The transaction hash that created the conditional order (useful for debugging purposes)
*/
Expand Down Expand Up @@ -279,15 +287,15 @@ async function loadOwnerOrders(
getNetworkStorageKey(CONDITIONAL_ORDER_REGISTRY_STORAGE_KEY, network)
);
// Get the persisted registry version
const version = await db.get(
getNetworkStorageKey(CONDITIONAL_ORDER_REGISTRY_VERSION_KEY, network)

const version = Number(
await db.get(
getNetworkStorageKey(CONDITIONAL_ORDER_REGISTRY_VERSION_KEY, network)
)
);

// Parse conditional orders registry (for the persisted version, converting it to the last version)
const ownerOrders = parseConditionalOrders(
!!str ? str : undefined,
Number(version)
);
const ownerOrders = parseConditionalOrders(!!str ? str : undefined, version);

return ownerOrders;
}
Expand All @@ -299,7 +307,24 @@ function parseConditionalOrders(
if (!serializedConditionalOrders) {
return createNewOrderMap();
}
return JSON.parse(serializedConditionalOrders, _reviver);
const ownerOrders: Map<
Owner,
Set<Omit<ConditionalOrder, "id"> & { id?: string }>
> = JSON.parse(serializedConditionalOrders, _reviver);

// The conditional order id was added in version 2
if (_version && _version < 2) {
// Migrate model: Add the missing order Id
for (const orders of ownerOrders.values()) {
for (const order of orders.values()) {
if (!order.id) {
order.id = ConditionalOrderSdk.leafToId(order.params);
}
}
}
}

return ownerOrders as Map<Owner, Set<ConditionalOrder>>;
}

function createNewOrderMap(): Map<Owner, Set<ConditionalOrder>> {
Expand Down
Loading