bb53beb22d
Skip 0 balances in header display; fill max funding/trading amount on 全部兑换/全部划转. Co-authored-by: Cursor <cursoragent@cursor.com>
180 lines
6.3 KiB
JavaScript
180 lines
6.3 KiB
JavaScript
(function () {
|
|
"use strict";
|
|
|
|
const root = document.getElementById("options-settings-root");
|
|
if (!root) return;
|
|
|
|
async function apiJson(url, opts) {
|
|
const r = await fetch(url, Object.assign({ credentials: "same-origin" }, opts || {}));
|
|
return r.json();
|
|
}
|
|
|
|
function setMsg(id, text, isErr) {
|
|
const el = document.getElementById(id);
|
|
if (!el) return;
|
|
el.textContent = text || "";
|
|
el.classList.toggle("opt-error", !!isErr);
|
|
}
|
|
|
|
function floorAmount(v) {
|
|
const n = Number(v);
|
|
if (!Number.isFinite(n) || n <= 0) return null;
|
|
return Math.floor(n * 100) / 100;
|
|
}
|
|
|
|
async function loadBalances(force) {
|
|
const q = force ? "?force=1" : "";
|
|
const d = await apiJson("/api/options/balances" + q);
|
|
if (!d.ok) throw new Error(d.msg || "余额拉取失败");
|
|
return d;
|
|
}
|
|
|
|
function pickBalance(bal, account, ccy) {
|
|
const acct = account === "trading" ? "trading" : "funding";
|
|
const key = acct + "_" + String(ccy || "").toLowerCase();
|
|
return floorAmount(bal[key]);
|
|
}
|
|
|
|
async function resolveMaxAmount(account, ccy) {
|
|
const bal = await loadBalances(true);
|
|
return pickBalance(bal, account, ccy);
|
|
}
|
|
|
|
async function submitSwap(amount) {
|
|
const d = await apiJson("/api/options/spot/swap", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
direction: document.getElementById("opt-set-swap-dir").value,
|
|
amount: amount,
|
|
}),
|
|
});
|
|
setMsg("opt-set-swap-msg", d.ok ? "兑换单已提交" : (d.msg || "失败"), !d.ok);
|
|
if (d.ok && typeof refreshAccountSnapshot === "function") refreshAccountSnapshot();
|
|
}
|
|
|
|
const swapBtn = document.getElementById("opt-set-swap-btn");
|
|
if (swapBtn) {
|
|
swapBtn.addEventListener("click", async function () {
|
|
const amount = parseFloat(document.getElementById("opt-set-swap-amount").value);
|
|
if (!amount || amount <= 0) {
|
|
setMsg("opt-set-swap-msg", "请输入有效数量", true);
|
|
return;
|
|
}
|
|
await submitSwap(amount);
|
|
});
|
|
}
|
|
|
|
const swapAllBtn = document.getElementById("opt-set-swap-all-btn");
|
|
if (swapAllBtn) {
|
|
swapAllBtn.addEventListener("click", async function () {
|
|
try {
|
|
const dir = document.getElementById("opt-set-swap-dir").value;
|
|
const ccy = dir === "usdc_to_usdt" ? "USDC" : "USDT";
|
|
const amount = await resolveMaxAmount("funding", ccy);
|
|
if (!amount) {
|
|
setMsg("opt-set-swap-msg", "资金账户可用余额不足", true);
|
|
return;
|
|
}
|
|
document.getElementById("opt-set-swap-amount").value = String(amount);
|
|
await submitSwap(amount);
|
|
} catch (e) {
|
|
setMsg("opt-set-swap-msg", e.message || "余额拉取失败", true);
|
|
}
|
|
});
|
|
}
|
|
|
|
async function submitInternalTransfer(amount) {
|
|
const d = await apiJson("/api/options/transfer", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
ccy: document.getElementById("opt-set-int-ccy").value,
|
|
from: document.getElementById("opt-set-int-from").value,
|
|
to: document.getElementById("opt-set-int-to").value,
|
|
amount: amount,
|
|
}),
|
|
});
|
|
setMsg("opt-set-int-msg", d.ok ? "划转成功" : (d.msg || "失败"), !d.ok);
|
|
if (d.ok && typeof refreshAccountSnapshot === "function") refreshAccountSnapshot();
|
|
}
|
|
|
|
const intBtn = document.getElementById("opt-set-int-btn");
|
|
if (intBtn) {
|
|
intBtn.addEventListener("click", async function () {
|
|
const amount = parseFloat(document.getElementById("opt-set-int-amount").value);
|
|
if (!amount || amount <= 0) {
|
|
setMsg("opt-set-int-msg", "请输入有效数量", true);
|
|
return;
|
|
}
|
|
await submitInternalTransfer(amount);
|
|
});
|
|
}
|
|
|
|
const intAllBtn = document.getElementById("opt-set-int-all-btn");
|
|
if (intAllBtn) {
|
|
intAllBtn.addEventListener("click", async function () {
|
|
try {
|
|
const ccy = document.getElementById("opt-set-int-ccy").value;
|
|
const from = document.getElementById("opt-set-int-from").value;
|
|
const amount = await resolveMaxAmount(from, ccy);
|
|
if (!amount) {
|
|
setMsg("opt-set-int-msg", "划出账户可用余额不足", true);
|
|
return;
|
|
}
|
|
document.getElementById("opt-set-int-amount").value = String(amount);
|
|
await submitInternalTransfer(amount);
|
|
} catch (e) {
|
|
setMsg("opt-set-int-msg", e.message || "余额拉取失败", true);
|
|
}
|
|
});
|
|
}
|
|
|
|
async function submitCrossTransfer(amount) {
|
|
const d = await apiJson("/api/options/cross-transfer", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
ccy: document.getElementById("opt-set-cross-ccy").value,
|
|
amount: amount,
|
|
from_account: document.getElementById("opt-set-cross-from").value,
|
|
to_account: document.getElementById("opt-set-cross-to").value,
|
|
direction: document.getElementById("opt-set-cross-dir").value,
|
|
}),
|
|
});
|
|
setMsg("opt-set-cross-msg", d.ok ? "划转成功" : (d.msg || "失败"), !d.ok);
|
|
if (d.ok && typeof refreshAccountSnapshot === "function") refreshAccountSnapshot();
|
|
}
|
|
|
|
const crossBtn = document.getElementById("opt-set-cross-btn");
|
|
if (crossBtn) {
|
|
crossBtn.addEventListener("click", async function () {
|
|
const amount = parseFloat(document.getElementById("opt-set-cross-amount").value);
|
|
if (!amount || amount <= 0) {
|
|
setMsg("opt-set-cross-msg", "请输入有效数量", true);
|
|
return;
|
|
}
|
|
await submitCrossTransfer(amount);
|
|
});
|
|
}
|
|
|
|
const crossAllBtn = document.getElementById("opt-set-cross-all-btn");
|
|
if (crossAllBtn) {
|
|
crossAllBtn.addEventListener("click", async function () {
|
|
try {
|
|
const ccy = document.getElementById("opt-set-cross-ccy").value;
|
|
const from = document.getElementById("opt-set-cross-from").value;
|
|
const amount = await resolveMaxAmount(from, ccy);
|
|
if (!amount) {
|
|
setMsg("opt-set-cross-msg", "划出账户可用余额不足", true);
|
|
return;
|
|
}
|
|
document.getElementById("opt-set-cross-amount").value = String(amount);
|
|
await submitCrossTransfer(amount);
|
|
} catch (e) {
|
|
setMsg("opt-set-cross-msg", e.message || "余额拉取失败", true);
|
|
}
|
|
});
|
|
}
|
|
})();
|