Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 18 additions & 3 deletions app/_components/trade/order/forms/limit.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,8 @@ const OrderLimitForm = () => {

setIsSubmitting(true);
toast({
title: "Placing order",
description: "Order is being placed, please do not close this page.",
title: "Step 1/3: Preparing account",
description: "Transferring funds to a new trading account...",
});

const { account: newTradingAccount } = await createZkAccountWithBalance({
Expand Down Expand Up @@ -540,6 +540,11 @@ const OrderLimitForm = () => {
updatedAddress: updatedTradingAccountAddress,
} = privateTxSingleResult.data;

toast({
title: "Step 1/3: Transfer broadcast",
description: "Waiting for on-chain confirmation...",
});

const newZkAccount: ZkAccount = {
scalar: updatedTradingAccountScalar,
type: "Coin",
Expand Down Expand Up @@ -626,6 +631,11 @@ const OrderLimitForm = () => {
const { newZkAccount } = queueResult;
const leverageNum = parseInt(leverage || "1", 10);

toast({
title: "Step 2/3: Submitting order",
description: "Sending limit order to the relayer...",
});

const { success, msg } = await createZkOrder({
leverage: leverageNum,
orderType: "LIMIT",
Expand All @@ -646,6 +656,11 @@ const OrderLimitForm = () => {
if (!data.result || !data.result.id_key)
throw "Error with creating limit order";

toast({
title: "Step 3/3: Confirming order",
description: "Waiting for order confirmation...",
});

const transactionHashCondition = (
txHashResult: Awaited<ReturnType<typeof queryTransactionHashes>>
) => {
Expand All @@ -670,7 +685,7 @@ const OrderLimitForm = () => {
queryTransactionHashes,
30,
newZkAccount.address,
1000,
100,
transactionHashCondition,
transactionHashFailCondition
);
Expand Down
21 changes: 18 additions & 3 deletions app/_components/trade/order/forms/market.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,8 @@ const OrderMarketForm = () => {
setIsSubmitting(true);

toast({
title: "Placing order",
description: "Order is being placed, please do not close this page.",
title: "Step 1/3: Preparing account",
description: "Transferring funds to a new trading account...",
});

// Generate the new order account before entering the queue — this is
Expand Down Expand Up @@ -515,6 +515,11 @@ const OrderMarketForm = () => {
updatedAddress: updatedTradingAccountAddress,
} = privateTxSingleResult.data;

toast({
title: "Step 1/3: Transfer broadcast",
description: "Waiting for on-chain confirmation...",
});

const newZkAccount: ZkAccount = {
scalar: updatedTradingAccountScalar,
type: "Coin",
Expand Down Expand Up @@ -605,6 +610,11 @@ const OrderMarketForm = () => {

const { newZkAccount } = queueResult;

toast({
title: "Step 2/3: Submitting order",
description: "Sending trade order to the relayer...",
});

const { success, msg } = await createZkOrder({
leverage: leverageVal,
orderType: "MARKET",
Expand Down Expand Up @@ -641,6 +651,11 @@ const OrderMarketForm = () => {

console.log(data);

toast({
title: "Step 3/3: Confirming order",
description: "Waiting for order confirmation...",
});

const transactionHashCondition = (
txHashResult: Awaited<ReturnType<typeof queryTransactionHashes>>
) => {
Expand Down Expand Up @@ -672,7 +687,7 @@ const OrderMarketForm = () => {
queryTransactionHashes,
30,
newZkAccount.address,
1000,
100,
transactionHashCondition,
transactionHashFailCondition
);
Expand Down
12 changes: 8 additions & 4 deletions lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ type RetryErrorResponse = {

const sleep = (delay: number) => new Promise((res) => setTimeout(res, delay));

/** Calculate progressive delay: starts at `base`, grows by 1.2x per attempt, caps at `base * 10` */
function progressiveDelay(base: number, attempt: number): number {
const scaled = base * Math.pow(1.2, attempt);
return Math.min(scaled, base * 10);
}

export async function retry<QueryReturn, QueryArgs = void>(
query: (args: QueryArgs, ...rest: any[]) => QueryReturn,
retries: number,
Expand All @@ -29,12 +35,10 @@ export async function retry<QueryReturn, QueryArgs = void>(
try {
tryCount += 1;

console.log(`retrying ${tryCount} / ${retries}`);

const response = await query(args);

if (!response) {
await sleep(delay);
await sleep(progressiveDelay(delay, tryCount));
continue;
}

Expand All @@ -58,7 +62,7 @@ export async function retry<QueryReturn, QueryArgs = void>(
break;
}

await sleep(delay);
await sleep(progressiveDelay(delay, tryCount));
continue;
} catch (err) {
console.error("retry >> error", err);
Expand Down
6 changes: 3 additions & 3 deletions lib/utils/waitForUtxoUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { queryUtxoForAddress } from "@/lib/api/zkos";
import { UtxoData } from "@/lib/types";

const DEFAULT_TIMEOUT_MS = 60_000;
const DEFAULT_POLL_INTERVAL_MS = 1_000;
/** Minimum wait before returning success. Relayer/chain UTXO availability ~5–6s. */
const DEFAULT_MIN_WAIT_MS = 5_000;
const DEFAULT_POLL_INTERVAL_MS = 200;
/** Minimum wait before returning success. Relayer/chain UTXO availability ~2–6s. */
const DEFAULT_MIN_WAIT_MS = 1_500;

type WaitResult =
| { success: true }
Expand Down
4 changes: 2 additions & 2 deletions lib/zk/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async function getCoinOutputFromUtxo(
const outputResult = await retry<
ReturnType<typeof queryUtxoForOutput>,
string
>(queryUtxoForOutput, 30, utxoHex, 1000, (outputObj) =>
>(queryUtxoForOutput, 30, utxoHex, 100, (outputObj) =>
Object.hasOwn(outputObj, "out_type")
);

Expand Down Expand Up @@ -77,7 +77,7 @@ async function getUtxoFromAddress(
const utxoDataResult = await retry<
ReturnType<typeof queryUtxoForAddress>,
string
>(queryUtxoForAddress, 30, address, 1000, (utxoObj) =>
>(queryUtxoForAddress, 30, address, 100, (utxoObj) =>
Object.hasOwn(utxoObj, "output_index")
);

Expand Down
6 changes: 3 additions & 3 deletions lib/zk/trade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export async function settleOrder(
queryTransactionHashes,
30,
trade.accountAddress,
1000,
100,
transactionHashCondition
);

Expand Down Expand Up @@ -165,7 +165,7 @@ export async function settleOrder(
queryTransactionHashes,
30,
trade.accountAddress,
1000,
100,
transactionHashCondition,
transactionHashFailCondition
);
Expand Down Expand Up @@ -293,7 +293,7 @@ export async function settleOrderSltp(
queryTransactionHashes,
30,
trade.accountAddress,
1000,
100,
transactionHashCondition
);

Expand Down