Skip to content

Commit

Permalink
Swap failed events: only reject committed txs with code !== 0 (#3301)
Browse files Browse the repository at this point in the history
* check for tx code for rejecting promise

* only reject with failed txs

* make error handling more consistent

* fixes
  • Loading branch information
jonator authored Jun 6, 2024
1 parent 253d32b commit 994085f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
9 changes: 3 additions & 6 deletions packages/web/components/swap-tool/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,10 @@ export const SwapTool: FunctionComponent<SwapToolProps> = observer(
});
}
})
.catch((error) => {
console.error("swap failed", error);
if (error instanceof Error && error.message === "Request rejected") {
// don't log when the user rejects in wallet
return;
.catch((e) => {
if (e instanceof Error && e.message.includes("Failed to send")) {
logEvent([EventName.Swap.swapFailed, baseEvent]);
}
logEvent([EventName.Swap.swapFailed, baseEvent]);
})
.finally(() => {
setIsSendingTx(false);
Expand Down
52 changes: 32 additions & 20 deletions packages/web/hooks/use-swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,18 @@ export function useSwap(
() =>
new Promise<"multiroute" | "multihop" | "exact-in">(
async (resolve, reject) => {
if (!maxSlippage) return reject("No max slippage");
if (!inAmountInput.amount) return reject("No input amount");
if (!account) return reject("No account");
if (!swapAssets.fromAsset) return reject("No from asset");
if (!swapAssets.toAsset) return reject("No to asset");
if (!maxSlippage)
return reject(new Error("Max slippage is not defined."));
if (!inAmountInput.amount)
return reject(new Error("Input amount is not specified."));
if (!account)
return reject(new Error("Account information is missing."));
if (!swapAssets.fromAsset)
return reject(new Error("From asset is not specified."));
if (!swapAssets.toAsset)
return reject(new Error("To asset is not specified."));

let txParams: ReturnType<typeof getSwapTxParameters>;

try {
txParams = getSwapTxParameters({
coinAmount: inAmountInput.amount.toCoin().amount,
Expand All @@ -290,7 +294,9 @@ export function useSwap(
});
} catch (e) {
const error = e as Error;
return reject(error.message);
return reject(
new Error(`Transaction preparation failed: ${error.message}`)
);
}

const { routes, tokenIn, tokenOutMinAmount } = txParams;
Expand Down Expand Up @@ -347,7 +353,7 @@ export function useSwap(
);
});
} catch (e) {
return reject("Rejected manual approval");
return reject(new Error("Rejected manual approval"));
}
}

Expand Down Expand Up @@ -376,14 +382,15 @@ export function useSwap(
tokenOutMinAmount,
undefined,
signOptions,
() => {
resolve(pools.length === 1 ? "exact-in" : "multihop");
({ code }) => {
if (code)
reject(
new Error("Failed to send swap exact amount in message")
);
else resolve(pools.length === 1 ? "exact-in" : "multihop");
}
)
.catch((reason) => {
reject(reason);
});
return pools.length === 1 ? "exact-in" : "multihop";
.catch(reject);
} else if (routes.length > 1) {
account.osmosis
.sendSplitRouteSwapExactAmountInMsg(
Expand All @@ -392,15 +399,20 @@ export function useSwap(
tokenOutMinAmount,
undefined,
signOptions,
() => {
resolve("multiroute");
({ code }) => {
if (code)
reject(
new Error(
"Failed to send split route swap exact amount in message"
)
);
else resolve("multiroute");
}
)
.catch((reason) => {
reject(reason);
});
.catch(reject);
} else {
reject("No routes given");
// should not be possible because button should be disabled
reject(new Error("No routes given"));
}
}
).finally(() => {
Expand Down

0 comments on commit 994085f

Please sign in to comment.