Skip to content

Commit

Permalink
Don't fail if simulation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
anxolin committed Aug 18, 2023
1 parent 62ab490 commit 6f0b6f2
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions actions/checkForAndPlaceOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
writeRegistry,
} from "./utils";
import { ChainContext, ConditionalOrder, OrderStatus } from "./model";
import { GPv2Order } from "./types/ComposableCoW";

const GPV2SETTLEMENT = "0x9008D19f58AAbD9eD0D60971565AA8510560ab41";

Expand Down Expand Up @@ -115,12 +116,19 @@ async function _processConditionalOrder(
): Promise<{ deleteConditionalOrder: boolean; error: boolean }> {
let error = false;
try {
const { order, signature } = await _getTradeableOrderWithSignature(
const result = await _getTradeableOrderWithSignature(
owner,
conditionalOrder,
contract
);

if (!result.success) {
// The simulation failed, this only means the order is not ready to be placed in the orderbook. Will check again in the next block
return { error: false, deleteConditionalOrder: false };
}

const { order, signature } = result.data;

const orderToSubmit: Order = {
...order,
kind: kindToString(order.kind),
Expand Down Expand Up @@ -299,11 +307,21 @@ function _handleOrderBookError(
return { shouldThrow: true };
}

type GetTradeableOrderWithSignatureResult =
| {
success: true;
data: {
order: GPv2Order.DataStructOutput;
signature: string;
};
}
| { success: false; error: any };

async function _getTradeableOrderWithSignature(
owner: string,
conditionalOrder: ConditionalOrder,
contract: ComposableCoW
) {
): Promise<GetTradeableOrderWithSignatureResult> {
const proof = conditionalOrder.proof ? conditionalOrder.proof.path : [];
const offchainInput = "0x";
const { to, data } =
Expand All @@ -319,20 +337,22 @@ async function _getTradeableOrderWithSignature(
data,
});

return contract.callStatic
.getTradeableOrderWithSignature(
try {
const data = await contract.callStatic.getTradeableOrderWithSignature(
owner,
conditionalOrder.params,
offchainInput,
proof
)
.catch((e) => {
console.error(
'[getTradeableOrderWithSignature] Error during "getTradeableOrderWithSignature" call: ',
e
);
throw e;
});
);

return { success: true, data };
} catch (error) {
console.error(
'[getTradeableOrderWithSignature] "getTradeableOrderWithSignature" failed. Order might not be ready to be placed in the orderbook yet',
error
);
return { success: false, error };
}
}

/**
Expand Down

0 comments on commit 6f0b6f2

Please sign in to comment.