From 40c6ff9ed1863f032c7e402c0c96994ca5c94467 Mon Sep 17 00:00:00 2001 From: Anxo Rodriguez Date: Sun, 20 Aug 2023 13:51:55 +0100 Subject: [PATCH] Handle cancelations --- actions/checkForAndPlaceOrder.ts | 50 +++++++++++++++--- actions/utils.ts | 87 ++++++++++++++++---------------- 2 files changed, 86 insertions(+), 51 deletions(-) diff --git a/actions/checkForAndPlaceOrder.ts b/actions/checkForAndPlaceOrder.ts index dc9dd50..9cbd0cb 100644 --- a/actions/checkForAndPlaceOrder.ts +++ b/actions/checkForAndPlaceOrder.ts @@ -116,8 +116,35 @@ async function _processConditionalOrder( ): Promise<{ deleteConditionalOrder: boolean; error: boolean }> { let error = false; try { + // Do a basic auth check (for singleOrders) // TODO: Check also Merkle auth + // Check in case the user invalidated it (this reduces errors in logs) + if (!conditionalOrder.proof) { + const ctx = await contract.callStatic.hash(conditionalOrder.params); + const authorised = await contract.callStatic + .singleOrders(owner, ctx) + .catch((error) => { + console.log( + "[processConditionalOrder] Error checking singleOrders auth", + error + ); + return undefined; // returns undefined if it cannot be checked + }); + + // Return early if the order is not authorised (if its not authorised) + // Note we continue in case of an error (this is just to let _getTradeableOrderWithSignature handle the error and log the Tenderly simulation link) + if (authorised === false) { + console.log( + `[processConditionalOrder] Single order not authed. Deleting order...`, + { owner, ctx, conditionalOrder } + ); + return { deleteConditionalOrder: true, error: false }; + } + } + + // Get GPv2 Order const tradeableOrderResult = await _getTradeableOrderWithSignature( owner, + network, conditionalOrder, contract ); @@ -316,6 +343,7 @@ type GetTradeableOrderWithSignatureError = { async function _getTradeableOrderWithSignature( owner: string, + network: string, conditionalOrder: ConditionalOrder, contract: ComposableCoW ): Promise { @@ -329,10 +357,9 @@ async function _getTradeableOrderWithSignature( proof ); - console.log("[getTradeableOrderWithSignature] Simulate", { - to, - data, - }); + console.log( + `[getTradeableOrderWithSignature] Simulate: https://dashboard.tenderly.co/gp-v2/watch-tower-prod/simulator/new?network=${network}&contractAddress=${to}&rawFunctionInput=${data}` + ); try { const data = await contract.callStatic.getTradeableOrderWithSignature( @@ -400,11 +427,18 @@ function _handleGetTradableOrderCall( result: CallResult.FailedButIsExpected, deleteConditionalOrder: true, }; + default: + // If there's no authorization we delete the order + // - One reason could be, because the user CANCELLED the order + // - for now it doesn't support more advanced cases where the order is auth during a pre-interaction + const errorName = error.errorName ? ` (${error.errorName})` : ""; + console.error( + `${errorMessagePrefix} for unexpected reasons${errorName}`, + error + ); + // If we don't know the reason, is better to not delete the order + return { result: CallResult.Failed, deleteConditionalOrder: false }; } - - console.error(errorMessagePrefix + " for unexpected reasons", error); - // If we don't know the reason, is better to not delete the order - return { result: CallResult.Failed, deleteConditionalOrder: false }; } console.error("[getTradeableOrderWithSignature] Unexpected error", error); diff --git a/actions/utils.ts b/actions/utils.ts index 404dc2d..a1cf5cf 100644 --- a/actions/utils.ts +++ b/actions/utils.ts @@ -244,52 +244,53 @@ var consoleOriginal = { debug: console.debug, }; -/** - * Tenderly has a limit of 4Kb per log message. When you surpass this limit, the log is not printed any more making it super hard to debug anything - * - * This tool will print - * - * @param data T - */ -const logWithLimit = - (level: "log" | "warn" | "error" | "debug") => - (...data: any[]) => { - const bigLogText = data - .map((item) => { - if (typeof item === "string") { - return item; - } - return JSON.stringify(item, null, 2); - }) - .join(" "); - - const numChunks = Math.ceil(bigLogText.length / TENDERLY_LOG_LIMIT); - - for (let i = 0; i < numChunks; i += 1) { - const chartStart = i * TENDERLY_LOG_LIMIT; - const prefix = numChunks > 1 ? `[${i + 1}/${numChunks}] ` : ""; - const message = - prefix + - bigLogText.substring(chartStart, chartStart + TENDERLY_LOG_LIMIT); - consoleOriginal[level](message); - - // if (level === "error") { - // sendSlack(message); - // } - - // // Used to debug the Tenderly log Limit issues - // consoleOriginal[level]( - // prefix + "TEST for bigLogText of " + bigLogText.length + " bytes" - // ); - } - }; +// TODO: Delete this code after we sort out the Tenderly log limit issue +// /** +// * Tenderly has a limit of 4Kb per log message. When you surpass this limit, the log is not printed any more making it super hard to debug anything +// * +// * This tool will print +// * +// * @param data T +// */ +// const logWithLimit = +// (level: "log" | "warn" | "error" | "debug") => +// (...data: any[]) => { +// const bigLogText = data +// .map((item) => { +// if (typeof item === "string") { +// return item; +// } +// return JSON.stringify(item, null, 2); +// }) +// .join(" "); + +// const numChunks = Math.ceil(bigLogText.length / TENDERLY_LOG_LIMIT); + +// for (let i = 0; i < numChunks; i += 1) { +// const chartStart = i * TENDERLY_LOG_LIMIT; +// const prefix = numChunks > 1 ? `[${i + 1}/${numChunks}] ` : ""; +// const message = +// prefix + +// bigLogText.substring(chartStart, chartStart + TENDERLY_LOG_LIMIT); +// consoleOriginal[level](message); + +// // if (level === "error") { +// // sendSlack(message); +// // } + +// // // Used to debug the Tenderly log Limit issues +// // consoleOriginal[level]( +// // prefix + "TEST for bigLogText of " + bigLogText.length + " bytes" +// // ); +// } +// }; // Override the log function since some internal libraries might print something and breaks Tenderly -console.warn = logWithLimit("warn"); -console.error = logWithLimit("error"); -console.debug = logWithLimit("debug"); -console.log = logWithLimit("log"); +// console.warn = logWithLimit["warn"]; +// console.error = logWithLimit("error"); +// console.debug = logWithLimit("debug"); +// console.log = logWithLimit("log"); export function sendSlack(message: string): boolean { if (!executionContext) {