Merge pull request #467 from zhouyongyou/fix/binance-multi-assets-api-error

fix: 智能处理币安多资产模式和统一账户API错误
This commit is contained in:
Icyoung
2025-11-05 16:23:32 +08:00
committed by GitHub
3 changed files with 135 additions and 33 deletions

View File

@@ -866,6 +866,21 @@ func (t *AsterTrader) SetMarginMode(symbol string, isCrossMargin bool) error {
log.Printf(" ✓ %s 仓位模式已是 %s 或有持仓无法更改", symbol, marginType)
return nil
}
// 检测多资产模式(错误码 -4168
if strings.Contains(err.Error(), "Multi-Assets mode") ||
strings.Contains(err.Error(), "-4168") ||
strings.Contains(err.Error(), "4168") {
log.Printf(" ⚠️ %s 检测到多资产模式,强制使用全仓模式", symbol)
log.Printf(" 💡 提示:如需使用逐仓模式,请在交易所关闭多资产模式")
return nil
}
// 检测统一账户 API
if strings.Contains(err.Error(), "unified") ||
strings.Contains(err.Error(), "portfolio") ||
strings.Contains(err.Error(), "Portfolio") {
log.Printf(" ❌ %s 检测到统一账户 API无法进行合约交易", symbol)
return fmt.Errorf("请使用「现货与合约交易」API 权限,不要使用「统一账户 API」")
}
log.Printf(" ⚠️ 设置仓位模式失败: %v", err)
// 不返回错误,让交易继续
return nil

View File

@@ -193,6 +193,17 @@ func (t *FuturesTrader) SetMarginMode(symbol string, isCrossMargin bool) error {
log.Printf(" ⚠️ %s 有持仓,无法更改仓位模式,继续使用当前模式", symbol)
return nil
}
// 检测多资产模式(错误码 -4168
if contains(err.Error(), "Multi-Assets mode") || contains(err.Error(), "-4168") || contains(err.Error(), "4168") {
log.Printf(" ⚠️ %s 检测到多资产模式,强制使用全仓模式", symbol)
log.Printf(" 💡 提示:如需使用逐仓模式,请在币安关闭多资产模式")
return nil
}
// 检测统一账户 APIPortfolio Margin
if contains(err.Error(), "unified") || contains(err.Error(), "portfolio") || contains(err.Error(), "Portfolio") {
log.Printf(" ❌ %s 检测到统一账户 API无法进行合约交易", symbol)
return fmt.Errorf("请使用「现货与合约交易」API 权限,不要使用「统一账户 API」")
}
log.Printf(" ⚠️ 设置仓位模式失败: %v", err)
// 不返回错误,让交易继续
return nil

View File

@@ -1530,14 +1530,18 @@ function ExchangeConfigModal({
onClose: () => void
language: Language
}) {
const [selectedExchangeId, setSelectedExchangeId] = useState(
editingExchangeId || ''
)
const [apiKey, setApiKey] = useState('')
const [secretKey, setSecretKey] = useState('')
const [passphrase, setPassphrase] = useState('')
const [testnet, setTestnet] = useState(false)
const [showGuide, setShowGuide] = useState(false)
const [selectedExchangeId, setSelectedExchangeId] = useState(editingExchangeId || '');
const [apiKey, setApiKey] = useState('');
const [secretKey, setSecretKey] = useState('');
const [passphrase, setPassphrase] = useState('');
const [testnet, setTestnet] = useState(false);
const [showGuide, setShowGuide] = useState(false);
// 币安配置指南展开状态
const [showBinanceGuide, setShowBinanceGuide] = useState(false);
// Hyperliquid 特定字段
const [hyperliquidWalletAddr, setHyperliquidWalletAddr] = useState('');
// Aster 特定字段
const [asterUser, setAsterUser] = useState('')
@@ -1708,32 +1712,104 @@ function ExchangeConfigModal({
{selectedExchange && (
<>
{/* Binance 和其他 CEX 交易所的字段 */}
{(selectedExchange.id === 'binance' ||
selectedExchange.type === 'cex') &&
selectedExchange.id !== 'hyperliquid' &&
selectedExchange.id !== 'aster' && (
<>
<div>
<label
className="block text-sm font-semibold mb-2"
style={{ color: '#EAECEF' }}
>
{t('apiKey', language)}
</label>
<input
type="password"
value={apiKey}
onChange={(e) => setApiKey(e.target.value)}
placeholder={t('enterAPIKey', language)}
className="w-full px-3 py-2 rounded"
style={{
background: '#0B0E11',
border: '1px solid #2B3139',
color: '#EAECEF',
}}
required
/>
{(selectedExchange.id === 'binance' || selectedExchange.type === 'cex') && selectedExchange.id !== 'hyperliquid' && selectedExchange.id !== 'aster' && (
<>
{/* 币安用户配置提示 (D1 方案) */}
{selectedExchange.id === 'binance' && (
<div
className="mb-4 p-3 rounded cursor-pointer transition-colors"
style={{
background: '#1a3a52',
border: '1px solid #2b5278',
}}
onClick={() => setShowBinanceGuide(!showBinanceGuide)}
>
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<span style={{ color: '#58a6ff' }}></span>
<span className="text-sm font-medium" style={{ color: '#EAECEF' }}>
<strong></strong>
使API API
</span>
</div>
<span style={{ color: '#8b949e' }}>
{showBinanceGuide ? '▲' : '▼'}
</span>
</div>
{/* 展开的详细说明 */}
{showBinanceGuide && (
<div
className="mt-3 pt-3"
style={{
borderTop: '1px solid #2b5278',
fontSize: '0.875rem',
color: '#c9d1d9'
}}
onClick={(e) => e.stopPropagation()}
>
<p className="mb-2" style={{ color: '#8b949e' }}>
<strong></strong> API
</p>
<p className="font-semibold mb-1" style={{ color: '#EAECEF' }}>
</p>
<ol className="list-decimal list-inside space-y-1 mb-3" style={{ paddingLeft: '0.5rem' }}>
<li> <strong>API </strong></li>
<li> API <strong> API </strong></li>
<li><strong></strong><span style={{ color: '#f85149' }}></span></li>
<li>IP <strong></strong> IP</li>
</ol>
<p className="mb-2 p-2 rounded" style={{ background: '#3d2a00', border: '1px solid #9e6a03' }}>
💡 <strong></strong>
使
</p>
<a
href="https://www.binance.com/zh-CN/support/faq/how-to-create-api-keys-on-binance-360002502072"
target="_blank"
rel="noopener noreferrer"
className="inline-block text-sm hover:underline"
style={{ color: '#58a6ff' }}
>
📖
</a>
</div>
)}
</div>
)}
<div>
<label className="block text-sm font-semibold mb-2" style={{ color: '#EAECEF' }}>
{t('apiKey', language)}
</label>
<input
type="password"
value={apiKey}
onChange={(e) => setApiKey(e.target.value)}
placeholder={t('enterAPIKey', language)}
className="w-full px-3 py-2 rounded"
style={{ background: '#0B0E11', border: '1px solid #2B3139', color: '#EAECEF' }}
required
/>
</div>
<div>
<label className="block text-sm font-semibold mb-2" style={{ color: '#EAECEF' }}>
{t('secretKey', language)}
</label>
<input
type="password"
value={secretKey}
onChange={(e) => setSecretKey(e.target.value)}
placeholder={t('enterSecretKey', language)}
className="w-full px-3 py-2 rounded"
style={{ background: '#0B0E11', border: '1px solid #2B3139', color: '#EAECEF' }}
required
/>
</div>
<div>
<label