@quantabit/swap-sdk 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +36 -0
- package/dist/index.cjs +159 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.esm.js +148 -0
- package/dist/index.esm.js.map +1 -0
- package/package.json +72 -0
- package/types/index.d.ts +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 QuantaBit Team
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# @quantabit/swap-sdk
|
|
2
|
+
|
|
3
|
+
QuantaBit Swap SDK facilitates instant token swap operations, optimal path routing calculations, automated market maker (AMM) pool interactions, and slippage monitoring on the QuantaBit network.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Swap Quote Generation**: Retrieve real-time pricing feeds with LP fee and price impact indicators.
|
|
8
|
+
- **Slippage Enforcement**: Strict client-side validation to abort transactions under high price impact.
|
|
9
|
+
- **Liquidity Pool Queries**: Fetch liquidity details, trade rates and token assets.
|
|
10
|
+
- **State Synchronization Hooks**: React hooks to fetch real-time quotes on token pair inputs.
|
|
11
|
+
- **Multilingual dictionary**: Built-in support for localized notifications in EN, ZH, JA, and KO.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install @quantabit/swap-sdk @quantabit/sdk-config
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
import { getSwapQuote, executeSwap, getLiquidityPools } from '@quantabit/swap-sdk';
|
|
23
|
+
|
|
24
|
+
// Get swap route/quote
|
|
25
|
+
const quote = await getSwapQuote('QBT', 'USDT', 100);
|
|
26
|
+
|
|
27
|
+
// Execute swap with 1% max slippage tolerance
|
|
28
|
+
const result = await executeSwap(quote, 'user_wallet_address', 0.01);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Brand & Links
|
|
32
|
+
|
|
33
|
+
- **Official Website**: [https://quantabit.io](https://quantabit.io)
|
|
34
|
+
- **Documentation**: [https://docs.quantabit.io](https://docs.quantabit.io)
|
|
35
|
+
- **Explorer**: [https://explorer.mg.qbitchain.io](https://explorer.mg.qbitchain.io)
|
|
36
|
+
- **Source Code**: [https://github.com/quantabit-chain/qbit-sdk](https://github.com/quantabit-chain/qbit-sdk)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
var sdkConfig = require('@quantabit/sdk-config');
|
|
5
|
+
|
|
6
|
+
const messages = {
|
|
7
|
+
en: {
|
|
8
|
+
swap_success: "Swap transaction executed successfully",
|
|
9
|
+
swap_failed: "Swap transaction failed",
|
|
10
|
+
slippage_exceeded: "Price slippage limit exceeded",
|
|
11
|
+
insufficient_liquidity: "Insufficient liquidity in pool",
|
|
12
|
+
invalid_token_pair: "Invalid token pair specified"
|
|
13
|
+
},
|
|
14
|
+
zh: {
|
|
15
|
+
swap_success: "兑换交易成功执行",
|
|
16
|
+
swap_failed: "兑换交易失败",
|
|
17
|
+
slippage_exceeded: "价格滑点超出限制",
|
|
18
|
+
insufficient_liquidity: "资金池流动性不足",
|
|
19
|
+
invalid_token_pair: "指定的代币交易对无效"
|
|
20
|
+
},
|
|
21
|
+
ja: {
|
|
22
|
+
swap_success: "スワップ取引が正常に実行されました",
|
|
23
|
+
swap_failed: "スワップ取引に失敗しました",
|
|
24
|
+
slippage_exceeded: "許容スリッページ制限を超過しました",
|
|
25
|
+
insufficient_liquidity: "プール内の流動性が不足しています",
|
|
26
|
+
invalid_token_pair: "無効なトークンペアが指定されました"
|
|
27
|
+
},
|
|
28
|
+
ko: {
|
|
29
|
+
swap_success: "스왑 트랜잭션이 성공적으로 실행되었습니다",
|
|
30
|
+
swap_failed: "스왑 트랜잭션 실패",
|
|
31
|
+
slippage_exceeded: "허용 슬리피지 한도 초과",
|
|
32
|
+
insufficient_liquidity: "풀 내 유동성이 부족합니다",
|
|
33
|
+
invalid_token_pair: "유효하지 않은 토큰 쌍입니다"
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
let currentLang = null;
|
|
37
|
+
function getLanguage() {
|
|
38
|
+
return currentLang || sdkConfig.getLanguage() || 'en';
|
|
39
|
+
}
|
|
40
|
+
function setLanguage(lang) {
|
|
41
|
+
if (messages[lang]) {
|
|
42
|
+
currentLang = lang;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function t(key) {
|
|
46
|
+
const lang = getLanguage();
|
|
47
|
+
return messages[lang]?.[key] || messages['en']?.[key] || key;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// 模拟 AMM 资金池兑换率
|
|
51
|
+
const EXCHANGE_RATES = {
|
|
52
|
+
"QBT/USDT": 1.25,
|
|
53
|
+
"XWIN/USDT": 0.88,
|
|
54
|
+
"wQBT/QBT": 1.00
|
|
55
|
+
};
|
|
56
|
+
async function getSwapQuote(fromToken, toToken, amount) {
|
|
57
|
+
if (amount <= 0) {
|
|
58
|
+
throw new Error("Amount must be positive");
|
|
59
|
+
}
|
|
60
|
+
const pair1 = `${fromToken}/${toToken}`;
|
|
61
|
+
const pair2 = `${toToken}/${fromToken}`;
|
|
62
|
+
let rate;
|
|
63
|
+
if (EXCHANGE_RATES[pair1]) {
|
|
64
|
+
rate = EXCHANGE_RATES[pair1];
|
|
65
|
+
} else if (EXCHANGE_RATES[pair2]) {
|
|
66
|
+
rate = 1 / EXCHANGE_RATES[pair2];
|
|
67
|
+
} else {
|
|
68
|
+
throw new Error(t('invalid_token_pair'));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// 模拟少量价差/手续费/滑点 (0.3% LP fee)
|
|
72
|
+
const lpFee = amount * 0.003;
|
|
73
|
+
const outputAmount = (amount - lpFee) * rate;
|
|
74
|
+
const priceImpact = parseFloat((amount * 0.0001).toFixed(4)); // 0.01% price impact per unit of input
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
fromToken,
|
|
78
|
+
toToken,
|
|
79
|
+
inputAmount: amount,
|
|
80
|
+
outputAmount: parseFloat(outputAmount.toFixed(6)),
|
|
81
|
+
rate: parseFloat(rate.toFixed(6)),
|
|
82
|
+
lpFee: parseFloat(lpFee.toFixed(6)),
|
|
83
|
+
priceImpact: priceImpact > 0.1 ? 0.1 : priceImpact
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
async function executeSwap(quote, userWallet, maxSlippage = 0.01) {
|
|
87
|
+
if (!quote || !userWallet) {
|
|
88
|
+
throw new Error("Missing swap quote or user wallet");
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// 模拟滑点判定
|
|
92
|
+
if (quote.priceImpact > maxSlippage) {
|
|
93
|
+
throw new Error(t('slippage_exceeded'));
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
txHash: `swap_tx_${Math.random().toString(36).substr(2, 9)}`,
|
|
97
|
+
fromToken: quote.fromToken,
|
|
98
|
+
toToken: quote.toToken,
|
|
99
|
+
inputAmount: quote.inputAmount,
|
|
100
|
+
outputAmount: quote.outputAmount,
|
|
101
|
+
status: "success",
|
|
102
|
+
message: t('swap_success')
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
async function getLiquidityPools() {
|
|
106
|
+
return Object.keys(EXCHANGE_RATES).map(pair => {
|
|
107
|
+
const [tokenA, tokenB] = pair.split('/');
|
|
108
|
+
return {
|
|
109
|
+
pair,
|
|
110
|
+
tokenA,
|
|
111
|
+
tokenB,
|
|
112
|
+
rate: EXCHANGE_RATES[pair],
|
|
113
|
+
liquidityUSD: Math.floor(Math.random() * 5000000) + 1000000
|
|
114
|
+
};
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
function useSwapQuote(fromToken, toToken, amount) {
|
|
118
|
+
const [quote, setQuote] = react.useState(null);
|
|
119
|
+
const [error, setError] = react.useState(null);
|
|
120
|
+
const [loading, setLoading] = react.useState(false);
|
|
121
|
+
react.useEffect(() => {
|
|
122
|
+
if (!fromToken || !toToken || !amount || amount <= 0) {
|
|
123
|
+
setQuote(null);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
setLoading(true);
|
|
127
|
+
setError(null);
|
|
128
|
+
const timer = setTimeout(async () => {
|
|
129
|
+
try {
|
|
130
|
+
const q = await getSwapQuote(fromToken, toToken, amount);
|
|
131
|
+
setQuote(q);
|
|
132
|
+
} catch (err) {
|
|
133
|
+
setError(err.message);
|
|
134
|
+
setQuote(null);
|
|
135
|
+
} finally {
|
|
136
|
+
setLoading(false);
|
|
137
|
+
}
|
|
138
|
+
}, 500);
|
|
139
|
+
return () => clearTimeout(timer);
|
|
140
|
+
}, [fromToken, toToken, amount]);
|
|
141
|
+
return {
|
|
142
|
+
quote,
|
|
143
|
+
error,
|
|
144
|
+
loading
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
const SUPPORTED_LANGUAGES = ['en', 'zh', 'ja', 'ko'];
|
|
148
|
+
|
|
149
|
+
exports.EXCHANGE_RATES = EXCHANGE_RATES;
|
|
150
|
+
exports.SUPPORTED_LANGUAGES = SUPPORTED_LANGUAGES;
|
|
151
|
+
exports.executeSwap = executeSwap;
|
|
152
|
+
exports.getLanguage = getLanguage;
|
|
153
|
+
exports.getLiquidityPools = getLiquidityPools;
|
|
154
|
+
exports.getSwapQuote = getSwapQuote;
|
|
155
|
+
exports.messages = messages;
|
|
156
|
+
exports.setLanguage = setLanguage;
|
|
157
|
+
exports.t = t;
|
|
158
|
+
exports.useSwapQuote = useSwapQuote;
|
|
159
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/i18n/index.js","../src/index.js"],"sourcesContent":["import { getLanguage as getGlobalLang } from '@quantabit/sdk-config';\n\nexport const messages = {\n en: {\n swap_success: \"Swap transaction executed successfully\",\n swap_failed: \"Swap transaction failed\",\n slippage_exceeded: \"Price slippage limit exceeded\",\n insufficient_liquidity: \"Insufficient liquidity in pool\",\n invalid_token_pair: \"Invalid token pair specified\"\n },\n zh: {\n swap_success: \"兑换交易成功执行\",\n swap_failed: \"兑换交易失败\",\n slippage_exceeded: \"价格滑点超出限制\",\n insufficient_liquidity: \"资金池流动性不足\",\n invalid_token_pair: \"指定的代币交易对无效\"\n },\n ja: {\n swap_success: \"スワップ取引が正常に実行されました\",\n swap_failed: \"スワップ取引に失敗しました\",\n slippage_exceeded: \"許容スリッページ制限を超過しました\",\n insufficient_liquidity: \"プール内の流動性が不足しています\",\n invalid_token_pair: \"無効なトークンペアが指定されました\"\n },\n ko: {\n swap_success: \"스왑 트랜잭션이 성공적으로 실행되었습니다\",\n swap_failed: \"스왑 트랜잭션 실패\",\n slippage_exceeded: \"허용 슬리피지 한도 초과\",\n insufficient_liquidity: \"풀 내 유동성이 부족합니다\",\n invalid_token_pair: \"유효하지 않은 토큰 쌍입니다\"\n }\n};\n\nlet currentLang = null;\n\nexport function getLanguage() {\n return currentLang || getGlobalLang() || 'en';\n}\n\nexport function setLanguage(lang) {\n if (messages[lang]) {\n currentLang = lang;\n }\n}\n\nexport function t(key) {\n const lang = getLanguage();\n return messages[lang]?.[key] || messages['en']?.[key] || key;\n}\n","import { useState, useEffect } from 'react';\nimport { getConfig } from '@quantabit/sdk-config';\nimport { t, setLanguage, getLanguage, messages } from './i18n/index.js';\n\n// 模拟 AMM 资金池兑换率\nconst EXCHANGE_RATES = {\n \"QBT/USDT\": 1.25,\n \"XWIN/USDT\": 0.88,\n \"wQBT/QBT\": 1.00\n};\n\nexport async function getSwapQuote(fromToken, toToken, amount) {\n if (amount <= 0) {\n throw new Error(\"Amount must be positive\");\n }\n \n const pair1 = `${fromToken}/${toToken}`;\n const pair2 = `${toToken}/${fromToken}`;\n let rate;\n \n if (EXCHANGE_RATES[pair1]) {\n rate = EXCHANGE_RATES[pair1];\n } else if (EXCHANGE_RATES[pair2]) {\n rate = 1 / EXCHANGE_RATES[pair2];\n } else {\n throw new Error(t('invalid_token_pair'));\n }\n \n // 模拟少量价差/手续费/滑点 (0.3% LP fee)\n const lpFee = amount * 0.003;\n const outputAmount = (amount - lpFee) * rate;\n const priceImpact = parseFloat((amount * 0.0001).toFixed(4)); // 0.01% price impact per unit of input\n \n return {\n fromToken,\n toToken,\n inputAmount: amount,\n outputAmount: parseFloat(outputAmount.toFixed(6)),\n rate: parseFloat(rate.toFixed(6)),\n lpFee: parseFloat(lpFee.toFixed(6)),\n priceImpact: priceImpact > 0.1 ? 0.1 : priceImpact\n };\n}\n\nexport async function executeSwap(quote, userWallet, maxSlippage = 0.01) {\n if (!quote || !userWallet) {\n throw new Error(\"Missing swap quote or user wallet\");\n }\n \n // 模拟滑点判定\n if (quote.priceImpact > maxSlippage) {\n throw new Error(t('slippage_exceeded'));\n }\n \n return {\n txHash: `swap_tx_${Math.random().toString(36).substr(2, 9)}`,\n fromToken: quote.fromToken,\n toToken: quote.toToken,\n inputAmount: quote.inputAmount,\n outputAmount: quote.outputAmount,\n status: \"success\",\n message: t('swap_success')\n };\n}\n\nexport async function getLiquidityPools() {\n return Object.keys(EXCHANGE_RATES).map(pair => {\n const [tokenA, tokenB] = pair.split('/');\n return {\n pair,\n tokenA,\n tokenB,\n rate: EXCHANGE_RATES[pair],\n liquidityUSD: Math.floor(Math.random() * 5000000) + 1000000\n };\n });\n}\n\nexport function useSwapQuote(fromToken, toToken, amount) {\n const [quote, setQuote] = useState(null);\n const [error, setError] = useState(null);\n const [loading, setLoading] = useState(false);\n\n useEffect(() => {\n if (!fromToken || !toToken || !amount || amount <= 0) {\n setQuote(null);\n return;\n }\n setLoading(true);\n setError(null);\n \n const timer = setTimeout(async () => {\n try {\n const q = await getSwapQuote(fromToken, toToken, amount);\n setQuote(q);\n } catch (err) {\n setError(err.message);\n setQuote(null);\n } finally {\n setLoading(false);\n }\n }, 500);\n\n return () => clearTimeout(timer);\n }, [fromToken, toToken, amount]);\n\n return { quote, error, loading };\n}\n\nexport { t, setLanguage, getLanguage, messages };\nexport const SUPPORTED_LANGUAGES = ['en', 'zh', 'ja', 'ko'];\nexport { EXCHANGE_RATES };\n"],"names":["messages","en","swap_success","swap_failed","slippage_exceeded","insufficient_liquidity","invalid_token_pair","zh","ja","ko","currentLang","getLanguage","getGlobalLang","setLanguage","lang","t","key","EXCHANGE_RATES","getSwapQuote","fromToken","toToken","amount","Error","pair1","pair2","rate","lpFee","outputAmount","priceImpact","parseFloat","toFixed","inputAmount","executeSwap","quote","userWallet","maxSlippage","txHash","Math","random","toString","substr","status","message","getLiquidityPools","Object","keys","map","pair","tokenA","tokenB","split","liquidityUSD","floor","useSwapQuote","setQuote","useState","error","setError","loading","setLoading","useEffect","timer","setTimeout","q","err","clearTimeout","SUPPORTED_LANGUAGES"],"mappings":";;;;;AAEO,MAAMA,QAAQ,GAAG;AACtBC,EAAAA,EAAE,EAAE;AACFC,IAAAA,YAAY,EAAE,wCAAwC;AACtDC,IAAAA,WAAW,EAAE,yBAAyB;AACtCC,IAAAA,iBAAiB,EAAE,+BAA+B;AAClDC,IAAAA,sBAAsB,EAAE,gCAAgC;AACxDC,IAAAA,kBAAkB,EAAE;GACrB;AACDC,EAAAA,EAAE,EAAE;AACFL,IAAAA,YAAY,EAAE,UAAU;AACxBC,IAAAA,WAAW,EAAE,QAAQ;AACrBC,IAAAA,iBAAiB,EAAE,UAAU;AAC7BC,IAAAA,sBAAsB,EAAE,UAAU;AAClCC,IAAAA,kBAAkB,EAAE;GACrB;AACDE,EAAAA,EAAE,EAAE;AACFN,IAAAA,YAAY,EAAE,mBAAmB;AACjCC,IAAAA,WAAW,EAAE,eAAe;AAC5BC,IAAAA,iBAAiB,EAAE,mBAAmB;AACtCC,IAAAA,sBAAsB,EAAE,kBAAkB;AAC1CC,IAAAA,kBAAkB,EAAE;GACrB;AACDG,EAAAA,EAAE,EAAE;AACFP,IAAAA,YAAY,EAAE,wBAAwB;AACtCC,IAAAA,WAAW,EAAE,YAAY;AACzBC,IAAAA,iBAAiB,EAAE,eAAe;AAClCC,IAAAA,sBAAsB,EAAE,gBAAgB;AACxCC,IAAAA,kBAAkB,EAAE;AACtB;AACF;AAEA,IAAII,WAAW,GAAG,IAAI;AAEf,SAASC,WAAWA,GAAG;AAC5B,EAAA,OAAOD,WAAW,IAAIE,qBAAa,EAAE,IAAI,IAAI;AAC/C;AAEO,SAASC,WAAWA,CAACC,IAAI,EAAE;AAChC,EAAA,IAAId,QAAQ,CAACc,IAAI,CAAC,EAAE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tB,OAAO,CAAC;QACrBY,QAAQ,CAAC,IAAI,CAAC;AAChB,MAAA,CAAC,SAAS;QACRK,UAAU,CAAC,KAAK,CAAC;AACnB,MAAA;IACF,CAAC,EAAE,GAAG,CAAC;AAEP,IAAA,OAAO,MAAMM,YAAY,CAACJ,KAAK,CAAC;EAClC,CAAC,EAAE,CAAC1C,SAAS,EAAEC,OAAO,EAAEC,MAAM,CAAC,CAAC;EAEhC,OAAO;IAAEY,KAAK;IAAEuB,KAAK;AAAEE,IAAAA;GAAS;AAClC;AAGO,MAAMQ,mBAAmB,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { useState, useEffect } from 'react';
|
|
2
|
+
import { getLanguage as getLanguage$1 } from '@quantabit/sdk-config';
|
|
3
|
+
|
|
4
|
+
const messages = {
|
|
5
|
+
en: {
|
|
6
|
+
swap_success: "Swap transaction executed successfully",
|
|
7
|
+
swap_failed: "Swap transaction failed",
|
|
8
|
+
slippage_exceeded: "Price slippage limit exceeded",
|
|
9
|
+
insufficient_liquidity: "Insufficient liquidity in pool",
|
|
10
|
+
invalid_token_pair: "Invalid token pair specified"
|
|
11
|
+
},
|
|
12
|
+
zh: {
|
|
13
|
+
swap_success: "兑换交易成功执行",
|
|
14
|
+
swap_failed: "兑换交易失败",
|
|
15
|
+
slippage_exceeded: "价格滑点超出限制",
|
|
16
|
+
insufficient_liquidity: "资金池流动性不足",
|
|
17
|
+
invalid_token_pair: "指定的代币交易对无效"
|
|
18
|
+
},
|
|
19
|
+
ja: {
|
|
20
|
+
swap_success: "スワップ取引が正常に実行されました",
|
|
21
|
+
swap_failed: "スワップ取引に失敗しました",
|
|
22
|
+
slippage_exceeded: "許容スリッページ制限を超過しました",
|
|
23
|
+
insufficient_liquidity: "プール内の流動性が不足しています",
|
|
24
|
+
invalid_token_pair: "無効なトークンペアが指定されました"
|
|
25
|
+
},
|
|
26
|
+
ko: {
|
|
27
|
+
swap_success: "스왑 트랜잭션이 성공적으로 실행되었습니다",
|
|
28
|
+
swap_failed: "스왑 트랜잭션 실패",
|
|
29
|
+
slippage_exceeded: "허용 슬리피지 한도 초과",
|
|
30
|
+
insufficient_liquidity: "풀 내 유동성이 부족합니다",
|
|
31
|
+
invalid_token_pair: "유효하지 않은 토큰 쌍입니다"
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
let currentLang = null;
|
|
35
|
+
function getLanguage() {
|
|
36
|
+
return currentLang || getLanguage$1() || 'en';
|
|
37
|
+
}
|
|
38
|
+
function setLanguage(lang) {
|
|
39
|
+
if (messages[lang]) {
|
|
40
|
+
currentLang = lang;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function t(key) {
|
|
44
|
+
const lang = getLanguage();
|
|
45
|
+
return messages[lang]?.[key] || messages['en']?.[key] || key;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 模拟 AMM 资金池兑换率
|
|
49
|
+
const EXCHANGE_RATES = {
|
|
50
|
+
"QBT/USDT": 1.25,
|
|
51
|
+
"XWIN/USDT": 0.88,
|
|
52
|
+
"wQBT/QBT": 1.00
|
|
53
|
+
};
|
|
54
|
+
async function getSwapQuote(fromToken, toToken, amount) {
|
|
55
|
+
if (amount <= 0) {
|
|
56
|
+
throw new Error("Amount must be positive");
|
|
57
|
+
}
|
|
58
|
+
const pair1 = `${fromToken}/${toToken}`;
|
|
59
|
+
const pair2 = `${toToken}/${fromToken}`;
|
|
60
|
+
let rate;
|
|
61
|
+
if (EXCHANGE_RATES[pair1]) {
|
|
62
|
+
rate = EXCHANGE_RATES[pair1];
|
|
63
|
+
} else if (EXCHANGE_RATES[pair2]) {
|
|
64
|
+
rate = 1 / EXCHANGE_RATES[pair2];
|
|
65
|
+
} else {
|
|
66
|
+
throw new Error(t('invalid_token_pair'));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// 模拟少量价差/手续费/滑点 (0.3% LP fee)
|
|
70
|
+
const lpFee = amount * 0.003;
|
|
71
|
+
const outputAmount = (amount - lpFee) * rate;
|
|
72
|
+
const priceImpact = parseFloat((amount * 0.0001).toFixed(4)); // 0.01% price impact per unit of input
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
fromToken,
|
|
76
|
+
toToken,
|
|
77
|
+
inputAmount: amount,
|
|
78
|
+
outputAmount: parseFloat(outputAmount.toFixed(6)),
|
|
79
|
+
rate: parseFloat(rate.toFixed(6)),
|
|
80
|
+
lpFee: parseFloat(lpFee.toFixed(6)),
|
|
81
|
+
priceImpact: priceImpact > 0.1 ? 0.1 : priceImpact
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
async function executeSwap(quote, userWallet, maxSlippage = 0.01) {
|
|
85
|
+
if (!quote || !userWallet) {
|
|
86
|
+
throw new Error("Missing swap quote or user wallet");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// 模拟滑点判定
|
|
90
|
+
if (quote.priceImpact > maxSlippage) {
|
|
91
|
+
throw new Error(t('slippage_exceeded'));
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
txHash: `swap_tx_${Math.random().toString(36).substr(2, 9)}`,
|
|
95
|
+
fromToken: quote.fromToken,
|
|
96
|
+
toToken: quote.toToken,
|
|
97
|
+
inputAmount: quote.inputAmount,
|
|
98
|
+
outputAmount: quote.outputAmount,
|
|
99
|
+
status: "success",
|
|
100
|
+
message: t('swap_success')
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
async function getLiquidityPools() {
|
|
104
|
+
return Object.keys(EXCHANGE_RATES).map(pair => {
|
|
105
|
+
const [tokenA, tokenB] = pair.split('/');
|
|
106
|
+
return {
|
|
107
|
+
pair,
|
|
108
|
+
tokenA,
|
|
109
|
+
tokenB,
|
|
110
|
+
rate: EXCHANGE_RATES[pair],
|
|
111
|
+
liquidityUSD: Math.floor(Math.random() * 5000000) + 1000000
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
function useSwapQuote(fromToken, toToken, amount) {
|
|
116
|
+
const [quote, setQuote] = useState(null);
|
|
117
|
+
const [error, setError] = useState(null);
|
|
118
|
+
const [loading, setLoading] = useState(false);
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
if (!fromToken || !toToken || !amount || amount <= 0) {
|
|
121
|
+
setQuote(null);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
setLoading(true);
|
|
125
|
+
setError(null);
|
|
126
|
+
const timer = setTimeout(async () => {
|
|
127
|
+
try {
|
|
128
|
+
const q = await getSwapQuote(fromToken, toToken, amount);
|
|
129
|
+
setQuote(q);
|
|
130
|
+
} catch (err) {
|
|
131
|
+
setError(err.message);
|
|
132
|
+
setQuote(null);
|
|
133
|
+
} finally {
|
|
134
|
+
setLoading(false);
|
|
135
|
+
}
|
|
136
|
+
}, 500);
|
|
137
|
+
return () => clearTimeout(timer);
|
|
138
|
+
}, [fromToken, toToken, amount]);
|
|
139
|
+
return {
|
|
140
|
+
quote,
|
|
141
|
+
error,
|
|
142
|
+
loading
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
const SUPPORTED_LANGUAGES = ['en', 'zh', 'ja', 'ko'];
|
|
146
|
+
|
|
147
|
+
export { EXCHANGE_RATES, SUPPORTED_LANGUAGES, executeSwap, getLanguage, getLiquidityPools, getSwapQuote, messages, setLanguage, t, useSwapQuote };
|
|
148
|
+
//# sourceMappingURL=index.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../src/i18n/index.js","../src/index.js"],"sourcesContent":["import { getLanguage as getGlobalLang } from '@quantabit/sdk-config';\n\nexport const messages = {\n en: {\n swap_success: \"Swap transaction executed successfully\",\n swap_failed: \"Swap transaction failed\",\n slippage_exceeded: \"Price slippage limit exceeded\",\n insufficient_liquidity: \"Insufficient liquidity in pool\",\n invalid_token_pair: \"Invalid token pair specified\"\n },\n zh: {\n swap_success: \"兑换交易成功执行\",\n swap_failed: \"兑换交易失败\",\n slippage_exceeded: \"价格滑点超出限制\",\n insufficient_liquidity: \"资金池流动性不足\",\n invalid_token_pair: \"指定的代币交易对无效\"\n },\n ja: {\n swap_success: \"スワップ取引が正常に実行されました\",\n swap_failed: \"スワップ取引に失敗しました\",\n slippage_exceeded: \"許容スリッページ制限を超過しました\",\n insufficient_liquidity: \"プール内の流動性が不足しています\",\n invalid_token_pair: \"無効なトークンペアが指定されました\"\n },\n ko: {\n swap_success: \"스왑 트랜잭션이 성공적으로 실행되었습니다\",\n swap_failed: \"스왑 트랜잭션 실패\",\n slippage_exceeded: \"허용 슬리피지 한도 초과\",\n insufficient_liquidity: \"풀 내 유동성이 부족합니다\",\n invalid_token_pair: \"유효하지 않은 토큰 쌍입니다\"\n }\n};\n\nlet currentLang = null;\n\nexport function getLanguage() {\n return currentLang || getGlobalLang() || 'en';\n}\n\nexport function setLanguage(lang) {\n if (messages[lang]) {\n currentLang = lang;\n }\n}\n\nexport function t(key) {\n const lang = getLanguage();\n return messages[lang]?.[key] || messages['en']?.[key] || key;\n}\n","import { useState, useEffect } from 'react';\nimport { getConfig } from '@quantabit/sdk-config';\nimport { t, setLanguage, getLanguage, messages } from './i18n/index.js';\n\n// 模拟 AMM 资金池兑换率\nconst EXCHANGE_RATES = {\n \"QBT/USDT\": 1.25,\n \"XWIN/USDT\": 0.88,\n \"wQBT/QBT\": 1.00\n};\n\nexport async function getSwapQuote(fromToken, toToken, amount) {\n if (amount <= 0) {\n throw new Error(\"Amount must be positive\");\n }\n \n const pair1 = `${fromToken}/${toToken}`;\n const pair2 = `${toToken}/${fromToken}`;\n let rate;\n \n if (EXCHANGE_RATES[pair1]) {\n rate = EXCHANGE_RATES[pair1];\n } else if (EXCHANGE_RATES[pair2]) {\n rate = 1 / EXCHANGE_RATES[pair2];\n } else {\n throw new Error(t('invalid_token_pair'));\n }\n \n // 模拟少量价差/手续费/滑点 (0.3% LP fee)\n const lpFee = amount * 0.003;\n const outputAmount = (amount - lpFee) * rate;\n const priceImpact = parseFloat((amount * 0.0001).toFixed(4)); // 0.01% price impact per unit of input\n \n return {\n fromToken,\n toToken,\n inputAmount: amount,\n outputAmount: parseFloat(outputAmount.toFixed(6)),\n rate: parseFloat(rate.toFixed(6)),\n lpFee: parseFloat(lpFee.toFixed(6)),\n priceImpact: priceImpact > 0.1 ? 0.1 : priceImpact\n };\n}\n\nexport async function executeSwap(quote, userWallet, maxSlippage = 0.01) {\n if (!quote || !userWallet) {\n throw new Error(\"Missing swap quote or user wallet\");\n }\n \n // 模拟滑点判定\n if (quote.priceImpact > maxSlippage) {\n throw new Error(t('slippage_exceeded'));\n }\n \n return {\n txHash: `swap_tx_${Math.random().toString(36).substr(2, 9)}`,\n fromToken: quote.fromToken,\n toToken: quote.toToken,\n inputAmount: quote.inputAmount,\n outputAmount: quote.outputAmount,\n status: \"success\",\n message: t('swap_success')\n };\n}\n\nexport async function getLiquidityPools() {\n return Object.keys(EXCHANGE_RATES).map(pair => {\n const [tokenA, tokenB] = pair.split('/');\n return {\n pair,\n tokenA,\n tokenB,\n rate: EXCHANGE_RATES[pair],\n liquidityUSD: Math.floor(Math.random() * 5000000) + 1000000\n };\n });\n}\n\nexport function useSwapQuote(fromToken, toToken, amount) {\n const [quote, setQuote] = useState(null);\n const [error, setError] = useState(null);\n const [loading, setLoading] = useState(false);\n\n useEffect(() => {\n if (!fromToken || !toToken || !amount || amount <= 0) {\n setQuote(null);\n return;\n }\n setLoading(true);\n setError(null);\n \n const timer = setTimeout(async () => {\n try {\n const q = await getSwapQuote(fromToken, toToken, amount);\n setQuote(q);\n } catch (err) {\n setError(err.message);\n setQuote(null);\n } finally {\n setLoading(false);\n }\n }, 500);\n\n return () => clearTimeout(timer);\n }, [fromToken, toToken, amount]);\n\n return { quote, error, loading };\n}\n\nexport { t, setLanguage, getLanguage, messages };\nexport const SUPPORTED_LANGUAGES = ['en', 'zh', 'ja', 'ko'];\nexport { EXCHANGE_RATES };\n"],"names":["messages","en","swap_success","swap_failed","slippage_exceeded","insufficient_liquidity","invalid_token_pair","zh","ja","ko","currentLang","getLanguage","getGlobalLang","setLanguage","lang","t","key","EXCHANGE_RATES","getSwapQuote","fromToken","toToken","amount","Error","pair1","pair2","rate","lpFee","outputAmount","priceImpact","parseFloat","toFixed","inputAmount","executeSwap","quote","userWallet","maxSlippage","txHash","Math","random","toString","substr","status","message","getLiquidityPools","Object","keys","map","pair","tokenA","tokenB","split","liquidityUSD","floor","useSwapQuote","setQuote","useState","error","setError","loading","setLoading","useEffect","timer","setTimeout","q","err","clearTimeout","SUPPORTED_LANGUAGES"],"mappings":";;;AAEO,MAAMA,QAAQ,GAAG;AACtBC,EAAAA,EAAE,EAAE;AACFC,IAAAA,YAAY,EAAE,wCAAwC;AACtDC,IAAAA,WAAW,EAAE,yBAAyB;AACtCC,IAAAA,iBAAiB,EAAE,+BAA+B;AAClDC,IAAAA,sBAAsB,EAAE,gCAAgC;AACxDC,IAAAA,kBAAkB,EAAE;GACrB;AACDC,EAAAA,EAAE,EAAE;AACFL,IAAAA,YAAY,EAAE,UAAU;AACxBC,IAAAA,WAAW,EAAE,QAAQ;AACrBC,IAAAA,iBAAiB,EAAE,UAAU;AAC7BC,IAAAA,sBAAsB,EAAE,UAAU;AAClCC,IAAAA,kBAAkB,EAAE;GACrB;AACDE,EAAAA,EAAE,EAAE;AACFN,IAAAA,YAAY,EAAE,mBAAmB;AACjCC,IAAAA,WAAW,EAAE,eAAe;AAC5BC,IAAAA,iBAAiB,EAAE,mBAAmB;AACtCC,IAAAA,sBAAsB,EAAE,kBAAkB;AAC1CC,IAAAA,kBAAkB,EAAE;GACrB;AACDG,EAAAA,EAAE,EAAE;AACFP,IAAAA,YAAY,EAAE,wBAAwB;AACtCC,IAAAA,WAAW,EAAE,YAAY;AACzBC,IAAAA,iBAAiB,EAAE,eAAe;AAClCC,IAAAA,sBAAsB,EAAE,gBAAgB;AACxCC,IAAAA,kBAAkB,EAAE;A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U,CAAC,YAAY;MACnC,IAAI;QACF,MAAMC,CAAC,GAAG,MAAM7C,YAAY,CAACC,SAAS,EAAEC,OAAO,EAAEC,MAAM,CAAC;QACxDiC,QAAQ,CAACS,CAAC,CAAC;MACb,CAAC,CAAC,OAAOC,GAAG,EAAE;AACZP,QAAAA,QAAQ,CAACO,GAAG,CAACtB,OAAO,CAAC;QACrBY,QAAQ,CAAC,IAAI,CAAC;AAChB,MAAA,CAAC,SAAS;QACRK,UAAU,CAAC,KAAK,CAAC;AACnB,MAAA;IACF,CAAC,EAAE,GAAG,CAAC;AAEP,IAAA,OAAO,MAAMM,YAAY,CAACJ,KAAK,CAAC;EAClC,CAAC,EAAE,CAAC1C,SAAS,EAAEC,OAAO,EAAEC,MAAM,CAAC,CAAC;EAEhC,OAAO;IAAEY,KAAK;IAAEuB,KAAK;AAAEE,IAAAA;GAAS;AAClC;AAGO,MAAMQ,mBAAmB,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;;;;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@quantabit/swap-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "QuantaBit Swap SDK - AMM liquidity integration, routing and smart contract swaps",
|
|
6
|
+
"main": "dist/index.cjs",
|
|
7
|
+
"module": "dist/index.esm.js",
|
|
8
|
+
"types": "types/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./types/index.d.ts",
|
|
12
|
+
"import": "./dist/index.esm.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"sideEffects": [
|
|
17
|
+
"*.css"
|
|
18
|
+
],
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"types",
|
|
22
|
+
"README.md",
|
|
23
|
+
"LICENSE"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "rollup -c",
|
|
27
|
+
"dev": "rollup -c -w",
|
|
28
|
+
"test": "jest --passWithNoTests",
|
|
29
|
+
"prepublishOnly": "npm run build"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "https://github.com/quantabit-chain/qbit-sdk.git",
|
|
34
|
+
"directory": "packages/swap-sdk"
|
|
35
|
+
},
|
|
36
|
+
"keywords": [
|
|
37
|
+
"swap",
|
|
38
|
+
"amm",
|
|
39
|
+
"dex",
|
|
40
|
+
"routing",
|
|
41
|
+
"web3",
|
|
42
|
+
"react",
|
|
43
|
+
"quantabit"
|
|
44
|
+
],
|
|
45
|
+
"author": "QuantaBit Team",
|
|
46
|
+
"license": "MIT",
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=18.0.0"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"react": ">=17.0.0",
|
|
52
|
+
"react-dom": ">=17.0.0"
|
|
53
|
+
},
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"@quantabit/sdk-config": "^1.0.10"
|
|
56
|
+
},
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"access": "public"
|
|
59
|
+
},
|
|
60
|
+
"qbit": {
|
|
61
|
+
"privacy": {
|
|
62
|
+
"level": "functional",
|
|
63
|
+
"dataCollection": "Fetches liquidity pool rates. No personal records stored.",
|
|
64
|
+
"gdprCompliant": true,
|
|
65
|
+
"ccpaCompliant": true
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
"homepage": "https://github.com/quantabit-chain/qbit-sdk/tree/main/packages/swap-sdk#readme",
|
|
69
|
+
"bugs": {
|
|
70
|
+
"url": "https://github.com/quantabit-chain/qbit-sdk/issues"
|
|
71
|
+
}
|
|
72
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export interface SwapQuote {
|
|
2
|
+
fromToken: string;
|
|
3
|
+
toToken: string;
|
|
4
|
+
inputAmount: number;
|
|
5
|
+
outputAmount: number;
|
|
6
|
+
rate: number;
|
|
7
|
+
lpFee: number;
|
|
8
|
+
priceImpact: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface SwapResult {
|
|
12
|
+
txHash: string;
|
|
13
|
+
fromToken: string;
|
|
14
|
+
toToken: string;
|
|
15
|
+
inputAmount: number;
|
|
16
|
+
outputAmount: number;
|
|
17
|
+
status: 'success' | 'failed';
|
|
18
|
+
message: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface LiquidityPool {
|
|
22
|
+
pair: string;
|
|
23
|
+
tokenA: string;
|
|
24
|
+
tokenB: string;
|
|
25
|
+
rate: number;
|
|
26
|
+
liquidityUSD: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function getSwapQuote(fromToken: string, toToken: string, amount: number): Promise<SwapQuote>;
|
|
30
|
+
export function executeSwap(quote: SwapQuote, userWallet: string, maxSlippage?: number): Promise<SwapResult>;
|
|
31
|
+
export function getLiquidityPools(): Promise<LiquidityPool[]>;
|
|
32
|
+
|
|
33
|
+
export function useSwapQuote(fromToken: string, toToken: string, amount: number): {
|
|
34
|
+
quote: SwapQuote | null;
|
|
35
|
+
error: string | null;
|
|
36
|
+
loading: boolean;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export function getLanguage(): string;
|
|
40
|
+
export function setLanguage(lang: string): void;
|
|
41
|
+
export function t(key: string): string;
|
|
42
|
+
export const SUPPORTED_LANGUAGES: string[];
|
|
43
|
+
export const messages: Record<string, Record<string, string>>;
|