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

Use SDK to poll and create smart order and handle result #16

Merged
merged 23 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ node_modules/
/actions/types/

# Compiled production code
/actions/out/
/actions/out/
.yalc
yalc.lock
anxolin marked this conversation as resolved.
Show resolved Hide resolved
39 changes: 28 additions & 11 deletions actions/addContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ import type {
} from "./types/ComposableCoW";
import { ComposableCoW__factory } from "./types/factories/ComposableCoW__factory";

import { isComposableCowCompatible, handleExecutionError, init, writeRegistry } from "./utils";
import {
isComposableCowCompatible,
handleExecutionError,
initContext,
writeRegistry,
toChainId,
} from "./utils";
import { ChainContext, Owner, Proof, Registry } from "./model";

/**
Expand All @@ -32,26 +38,34 @@ const _addContract: ActionFn = async (context: Context, event: Event) => {
const transactionEvent = event as TransactionEvent;
const tx = transactionEvent.hash;
const composableCow = ComposableCoW__factory.createInterface();
const { provider } = await ChainContext.create(context, transactionEvent.network);
const { registry } = await init(
"addContract",
transactionEvent.network,
context
);

const chainId = toChainId(transactionEvent.network);
const { provider } = await ChainContext.create(context, chainId);
const { registry } = await initContext("addContract", chainId, context);

// Process the logs
let hasErrors = false;
let numContractsAdded = 0;
for (const log of transactionEvent.logs) {
// Do not process logs that are not from a `ComposableCoW`-compatible contract
// This is a *normal* case, if the contract is not `ComposableCoW`-compatible
// then we do not need to do anything, and therefore don't flag as an error.
if (!isComposableCowCompatible(await provider.getCode(log.address))) {
continue;
}
const { error } = await _registerNewOrder(tx, log, composableCow, registry);
const { error, added } = await _registerNewOrder(
tx,
log,
composableCow,
registry
);
if (added) {
numContractsAdded++;
}
hasErrors ||= error;
}

console.log(`[addContract] Added ${numContractsAdded} contracts`);
hasErrors ||= !(await writeRegistry());
// Throw execution error if there was at least one error
if (hasErrors) {
Expand All @@ -66,7 +80,8 @@ export async function _registerNewOrder(
log: Log,
composableCow: ComposableCoWInterface,
registry: Registry
): Promise<{ error: boolean }> {
): Promise<{ error: boolean; added: boolean }> {
let added = false;
try {
// Check if the log is a ConditionalOrderCreated event
if (
Expand All @@ -80,6 +95,7 @@ export async function _registerNewOrder(

// Attempt to add the conditional order to the registry
await add(tx, owner, params, null, log.address, registry);
added = true;
} else if (log.topics[0] == composableCow.getEventTopic("MerkleRootSet")) {
const [owner, root, proof] = composableCow.decodeEventLog(
"MerkleRootSet",
Expand Down Expand Up @@ -116,6 +132,7 @@ export async function _registerNewOrder(
log.address,
registry
);
added = true;
}
}
}
Expand All @@ -124,10 +141,10 @@ export async function _registerNewOrder(
"[addContract] Error handling ConditionalOrderCreated/MerkleRootSet event" +
error
);
return { error: true };
return { error: true, added };
}

return { error: false };
return { error: false, added };
}

/**
Expand Down
Loading