Skip to content

Commit

Permalink
Use SDK to poll and create smart order and handle result (#16)
Browse files Browse the repository at this point in the history
* Do polling and handle polling result

* Delete unused types

* Show result in logs

* Improve log and show errors

* Improve logs

* Fix issue checkign settmelemt

* Add small refactor to utils

* Add todos and point to issue

* Let the SDK know the block yhou are indexing

* Update versions

* Improve types

Co-authored-by: Leandro <[email protected]>

* Rename var

* Dont check twice

* Remove dead code

* Dont override the console

* Delete unnecesary call

* Improve logs and dont fail for handled errors

* Change method name

* Move util to misc

* Pass proof and offchain input

* Comment out deletion of owners

* Rename counters

* Rename logging message

---------

Co-authored-by: Leandro <[email protected]>
  • Loading branch information
anxolin and alfetopito authored Aug 30, 2023
1 parent b7f6055 commit b05a187
Show file tree
Hide file tree
Showing 20 changed files with 5,676 additions and 776 deletions.
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
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

0 comments on commit b05a187

Please sign in to comment.