Skip to content

Commit

Permalink
Handle quote too low (#4982)
Browse files Browse the repository at this point in the history
  • Loading branch information
megrogan authored Dec 11, 2023
1 parent ac5f7f3 commit b174fcf
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 40 deletions.
75 changes: 46 additions & 29 deletions frontend/app/src/components/home/profile/SwapCrypto.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
const client = getContext<OpenChat>("client");
const dispatch = createEventDispatcher();
type State = "quote" | "swap" | "finished";
type Result = "success" | "rateChanged" | "insufficientFunds" | "error" | undefined;
let error: string | undefined = undefined;
let amountIn: bigint = BigInt(0);
let busy = false;
let valid = false;
let state: "quote" | "swap" | "finished" = "quote";
let result: "success" | "rateChanged" | "insufficientFunds" | "error" | undefined = undefined;
let state: State = "quote";
let result: Result = undefined;
let validAmount = false;
let ledgerOut: string | undefined;
let swaps = {} as Record<string, DexId[]>;
Expand Down Expand Up @@ -60,20 +63,22 @@
$: remainingBalance =
amountIn > BigInt(0) ? balanceIn - amountIn - detailsIn.transferFee : balanceIn;
$: primaryButtonText = $_(
`tokenSwap.${
state === "quote"
? "quote"
: state === "swap"
? "swap"
: result === "insufficientFunds"
? "back"
: "requote"
}`,
);
$: primaryButtonText = getPrimaryButtonText(state, result);
onMount(() => loadSwaps(ledgerIn));
function getPrimaryButtonText(state: State, result: Result): string {
let label;
if (state === "finished") {
label = result === "insufficientFunds" ? "back" : "requote";
} else {
label = state;
}
return $_(`tokenSwap.${label}`);
}
function quote() {
if (!valid) return;
Expand All @@ -89,24 +94,36 @@
error = $_("tokenSwap.noQuotes", { values: { tokenIn: detailsIn.symbol } });
} else {
bestQuote = response[0];
state = "swap";
const [dexId, quote] = bestQuote!;
const amountOutText = client.formatTokens(quote, 0, detailsOut!.decimals);
const rate = (Number(amountOutText) / Number(amountInText)).toPrecision(3);
const dex = dexName(dexId);
const swapText = $_("tokenSwap.swap");
message = $_("tokenSwap.swapInfo", {
values: {
amountIn: amountInText,
tokenIn: detailsIn.symbol,
rate,
amountOut: amountOutText,
tokenOut: detailsOut!.symbol,
dex,
swap: swapText,
},
});
const minAmountOut = BigInt(10) * detailsOut!.transferFee;
const minAmountOutText = client.formatTokens(
minAmountOut,
0,
detailsOut!.decimals,
);
let values = {
amountIn: amountInText,
tokenIn: detailsIn.symbol,
rate,
amountOut: amountOutText,
tokenOut: detailsOut!.symbol,
dex,
swap: swapText,
minAmountOut: minAmountOutText,
};
if (quote > minAmountOut) {
state = "swap";
message = $_("tokenSwap.swapInfo", { values });
} else {
error = $_("tokenSwap.quoteTooLow", { values });
}
}
})
.catch((err) => {
Expand Down Expand Up @@ -159,13 +176,13 @@
}
function onPrimaryClick() {
if (state === "quote" || result === "rateChanged") {
if (state === "finished" && result === "insufficientFunds") {
amountIn = BigInt(0);
state = "quote";
} else if (state === "quote" || result === "rateChanged") {
quote();
} else if (state === "swap") {
swap();
} else if (result === "insufficientFunds") {
amountIn = BigInt(0);
state = "quote";
}
}
Expand Down
7 changes: 4 additions & 3 deletions frontend/app/src/i18n/cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,8 @@
"insufficientFunds": "兑换失败-资金不足",
"error": "兑换错误 - 没有资金损失"
},
"swapToken": "兑换{tokenIn}",
"swapTokenTo": "将 {tokenIn} 兑换为 {tokenOut}"
"swapToken": "交换{tokenIn}",
"swapTokenTo": "将 {tokenIn} 交换为 {tokenOut}",
"quoteTooLow": "所报的最佳费率是 {rate} {tokenOut} / {tokenIn} @{dex}。这将从 {amountIn} {tokenIn} 兑换 {amountOut} {tokenOut} 。仅当报价金额超过 {minAmountOut} 时,您才可以进行交换。"
}
}
}
3 changes: 2 additions & 1 deletion frontend/app/src/i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,7 @@
"error": "Swap-Fehler – es sind keine Gelder verloren gegangen"
},
"swapToken": "Tausche {tokenIn}",
"swapTokenTo": "Tauschen Sie {tokenIn} gegen {tokenOut} aus"
"swapTokenTo": "Tauschen Sie {tokenIn} gegen {tokenOut} aus",
"quoteTooLow": "Der beste angegebene Tarif ist {rate} {tokenOut} pro {tokenIn} ab {dex}. Dies ergibt {amountOut} {tokenOut} für {amountIn} {tokenIn}. Sie können nur tauschen, wenn der angegebene Betrag {minAmountOut} überschreitet."
}
}
1 change: 1 addition & 0 deletions frontend/app/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@
"quoteError": "Error getting quotes to swap {tokenIn}",
"findingAvailableTokens": "finding available tokens to swap...",
"swapInfo": "The best rate quoted is **{rate}** {tokenOut} per {tokenIn} from {dex}. This gives **{amountOut}** {tokenOut} for **{amountIn}** {tokenIn}. The swap will only go ahead if the amount of {tokenOut} to be received is at least 98% of the amount quoted. Would you like to go ahead and attempt the `{swap}`?",
"quoteTooLow": "The best rate quoted is {rate} {tokenOut} per {tokenIn} from {dex}. This gives {amountOut} {tokenOut} for {amountIn} {tokenIn}. You can only swap if the quoted amount out exceeds {minAmountOut}.",
"progress": {
"get": "Get deposit account",
"deposit": "Transfer {amountIn} {tokenIn} to {dex}",
Expand Down
3 changes: 2 additions & 1 deletion frontend/app/src/i18n/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,7 @@
"error": "Error de intercambio: no se han perdido fondos"
},
"swapToken": "Intercambiar {tokenIn}",
"swapTokenTo": "Intercambiar {tokenIn} por {tokenOut}"
"swapTokenTo": "Intercambiar {tokenIn} por {tokenOut}",
"quoteTooLow": "La mejor tarifa cotizada es {rate} {tokenOut} por {tokenIn} de {dex}. Esto da {amountOut} {tokenOut} para {amountIn} {tokenIn}. Solo puede intercambiar si el monto cotizado excede {minAmountOut}."
}
}
3 changes: 2 additions & 1 deletion frontend/app/src/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,7 @@
"error": "Erreur d'échange - aucun fonds n'a été perdu"
},
"swapToken": "Échanger {tokenIn}",
"swapTokenTo": "Remplacer {tokenIn} par {tokenOut}"
"swapTokenTo": "Remplacer {tokenIn} par {tokenOut}",
"quoteTooLow": "Le meilleur tarif indiqué est de {rate} {tokenOut} pour {tokenIn} à partir de {dex}. Cela donne {amountOut} {tokenOut} pour {amountIn} {tokenIn}. Vous ne pouvez échanger que si le montant indiqué dépasse {minAmountOut}."
}
}
3 changes: 2 additions & 1 deletion frontend/app/src/i18n/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,7 @@
"error": "Errore di scambio: nessun fondo è stato perso"
},
"swapToken": "Scambia {tokenIn}",
"swapTokenTo": "Scambia {tokenIn} in {tokenOut}"
"swapTokenTo": "Scambia {tokenIn} in {tokenOut}",
"quoteTooLow": "La migliore tariffa indicata è {rate} {tokenOut} per {tokenIn} da {dex}. Questo dà {amountOut} {tokenOut} per {amountIn} {tokenIn}. Puoi scambiare solo se l'importo indicato supera {minAmountOut}."
}
}
3 changes: 2 additions & 1 deletion frontend/app/src/i18n/iw.json
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,7 @@
"error": "שגיאת החלפה - לא אבדו כספים"
},
"swapToken": "החלפה {tokenIn}",
"swapTokenTo": "החלף את {tokenIn} ל-{tokenOut}"
"swapTokenTo": "החלף את {tokenIn} ל-{tokenOut}",
"quoteTooLow": "התעריף הטוב ביותר שצוטט הוא {rate} {tokenOut} לכל {tokenIn} מ-{dex}. זה נותן {amountOut} {tokenOut} עבור {amountIn} {tokenIn}. אתה יכול להחליף רק אם הסכום הנקוב עולה על {minAmountOut}."
}
}
3 changes: 2 additions & 1 deletion frontend/app/src/i18n/jp.json
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,7 @@
"error": "スワップエラー - 資金は失われていません"
},
"swapToken": "{tokenIn}を交換する",
"swapTokenTo": "{tokenIn} を {tokenOut} に交換します"
"swapTokenTo": "{tokenIn} を {tokenOut} に交換します",
"quoteTooLow": "見積もられた最良レートは、{rate} からの {tokenOut} あたり {tokenIn} {dex} です。これにより、{amountOut} {tokenOut} の {amountIn} {tokenIn} が得られます。見積金額が {minAmountOut} を超える場合にのみ交換できます。"
}
}
3 changes: 2 additions & 1 deletion frontend/app/src/i18n/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,7 @@
"error": "Ошибка свопа - средства не потеряны"
},
"swapToken": "Обмен {tokenIn}",
"swapTokenTo": "Замените {tokenIn} на {tokenOut}"
"swapTokenTo": "Замените {tokenIn} на {tokenOut}",
"quoteTooLow": "Лучшая заявленная ставка составляет {rate} {tokenOut} за {tokenIn} от {dex}. Это дает {amountOut} {tokenOut} для {amountIn} {tokenIn}. Вы можете обменять только в том случае, если заявленная сумма превышает {minAmountOut}."
}
}
3 changes: 2 additions & 1 deletion frontend/app/src/i18n/vi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,7 @@
"error": "Lỗi hoán đổi - không có khoản tiền nào bị mất"
},
"swapToken": "Hoán đổi {tokenIn}",
"swapTokenTo": "Hoán đổi {tokenIn} thành {tokenOut}"
"swapTokenTo": "Hoán đổi {tokenIn} thành {tokenOut}",
"quoteTooLow": "Tỷ giá được trích dẫn tốt nhất là {rate} {tokenOut} trên {tokenIn} từ {dex}. Điều này mang lại {amountOut} {tokenOut} cho {amountIn} {tokenIn}. Bạn chỉ có thể hoán đổi nếu số tiền trích dẫn vượt quá {minAmountOut}."
}
}

0 comments on commit b174fcf

Please sign in to comment.