@runesx/api-client 0.1.0 → 0.2.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/package.json +1 -1
- package/src/utils/swapUtils.mjs +34 -58
package/package.json
CHANGED
package/src/utils/swapUtils.mjs
CHANGED
|
@@ -10,9 +10,11 @@ export const validatePositiveNumber = (value, fieldName) => {
|
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
// Get RUNES price in USD using RUNES/USDC pool
|
|
13
|
-
export const getRunesPriceUSD =
|
|
13
|
+
export const getRunesPriceUSD = (pools) => {
|
|
14
14
|
try {
|
|
15
|
-
const runesUsdcPool = pools.find((p) =>
|
|
15
|
+
const runesUsdcPool = pools.find((p) =>
|
|
16
|
+
(p.coinA.ticker === 'RUNES' && p.coinB.ticker === 'USDC') ||
|
|
17
|
+
(p.coinA.ticker === 'USDC' && p.coinB.ticker === 'RUNES'));
|
|
16
18
|
|
|
17
19
|
if (!runesUsdcPool) {
|
|
18
20
|
console.warn('RUNES/USDC pool not found, using fallback price of $0.01');
|
|
@@ -63,8 +65,8 @@ export const getTokenPriceInRunes = (token, pools) => {
|
|
|
63
65
|
const reserveB = new BigNumber(pool.reserveB).shiftedBy(-pool.coinB.dp);
|
|
64
66
|
|
|
65
67
|
const priceInRunes = isRunesA
|
|
66
|
-
? reserveA.div(reserveB).toString() // RUNES
|
|
67
|
-
: reserveB.div(reserveA).toString(); //
|
|
68
|
+
? reserveA.div(reserveB).toString() // RUNES is coinA: price = RUNES_reserve / TOKEN_reserve
|
|
69
|
+
: reserveB.div(reserveA).toString(); // RUNES is coinB: price = RUNES_reserve / TOKEN_reserve
|
|
68
70
|
|
|
69
71
|
return priceInRunes;
|
|
70
72
|
};
|
|
@@ -74,6 +76,18 @@ const findAllPathsDFS = (startCoin, endCoin, pools, maxHops = 6) => {
|
|
|
74
76
|
const paths = [];
|
|
75
77
|
const visited = new Set();
|
|
76
78
|
|
|
79
|
+
// Build adjacency map for O(1) lookups (matches BFS approach)
|
|
80
|
+
const poolMap = new Map();
|
|
81
|
+
pools.forEach((pool) => {
|
|
82
|
+
if (!pool.runesCompliant || new BigNumber(pool.reserveA).isZero() || new BigNumber(pool.reserveB).isZero()) {return;}
|
|
83
|
+
const keyA = pool.coinA.ticker;
|
|
84
|
+
const keyB = pool.coinB.ticker;
|
|
85
|
+
if (!poolMap.has(keyA)) {poolMap.set(keyA, []);}
|
|
86
|
+
if (!poolMap.has(keyB)) {poolMap.set(keyB, []);}
|
|
87
|
+
poolMap.get(keyA).push({ pool, nextCoin: pool.coinB });
|
|
88
|
+
poolMap.get(keyB).push({ pool, nextCoin: pool.coinA });
|
|
89
|
+
});
|
|
90
|
+
|
|
77
91
|
function dfs(currentCoin, currentPath, hops) {
|
|
78
92
|
if (hops > maxHops) {return;}
|
|
79
93
|
if (currentCoin.ticker === endCoin.ticker) {
|
|
@@ -81,13 +95,8 @@ const findAllPathsDFS = (startCoin, endCoin, pools, maxHops = 6) => {
|
|
|
81
95
|
return;
|
|
82
96
|
}
|
|
83
97
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
const isCoinA = pool.coinA.ticker === currentCoin.ticker;
|
|
87
|
-
const isCoinB = pool.coinB.ticker === currentCoin.ticker;
|
|
88
|
-
if (!isCoinA && !isCoinB) {continue;}
|
|
89
|
-
|
|
90
|
-
const nextCoin = isCoinA ? pool.coinB : pool.coinA;
|
|
98
|
+
const connectedPools = poolMap.get(currentCoin.ticker) || [];
|
|
99
|
+
for (const { pool, nextCoin } of connectedPools) {
|
|
91
100
|
const poolKey = pool.id;
|
|
92
101
|
|
|
93
102
|
if (visited.has(poolKey)) {continue;}
|
|
@@ -175,7 +184,11 @@ export const simulateSwap = (pool, inputCoin, amountInBN, isCoinAInput) => {
|
|
|
175
184
|
const totalFeeRate = lpFeeRate.plus(treasuryFeeRate);
|
|
176
185
|
|
|
177
186
|
// Calculate total fee and split proportionally
|
|
178
|
-
|
|
187
|
+
let totalFeeAmount = amountInBN.times(totalFeeRate).integerValue(BigNumber.ROUND_DOWN);
|
|
188
|
+
// Minimum fee floor: match backend - ensure at least 1 smallest unit when fees are configured
|
|
189
|
+
if (totalFeeRate.gt(0) && totalFeeAmount.isZero()) {
|
|
190
|
+
totalFeeAmount = new BigNumber(1);
|
|
191
|
+
}
|
|
179
192
|
let treasuryFeeAmount = new BigNumber(0);
|
|
180
193
|
let lpFeeAmount = new BigNumber(0);
|
|
181
194
|
if (!totalFeeRate.eq(0)) {
|
|
@@ -197,7 +210,7 @@ export const simulateSwap = (pool, inputCoin, amountInBN, isCoinAInput) => {
|
|
|
197
210
|
}
|
|
198
211
|
amountOutBN = amountOutBN.integerValue(BigNumber.ROUND_DOWN);
|
|
199
212
|
|
|
200
|
-
if (amountOutBN.isNaN() || amountOutBN.lt(1)) {return null;}
|
|
213
|
+
if (amountOutBN.isNaN() || !amountOutBN.isFinite() || amountOutBN.lt(1)) {return null;}
|
|
201
214
|
|
|
202
215
|
// Simulate reserve updates (mimicking backend)
|
|
203
216
|
const updatedPool = { ...pool };
|
|
@@ -362,7 +375,7 @@ export const estimateSwap = async (
|
|
|
362
375
|
const runesPriceUSD = await getRunesPriceUSD(pools);
|
|
363
376
|
const priceAInRunes = getTokenPriceInRunes(inputCoinData, pools);
|
|
364
377
|
const priceBInRunes = getTokenPriceInRunes(outputCoinData, pools);
|
|
365
|
-
if (!priceAInRunes || !priceBInRunes) {
|
|
378
|
+
if (!priceAInRunes || priceAInRunes === '0' || !priceBInRunes || priceBInRunes === '0') {
|
|
366
379
|
throw new Error('Pool not initialized or invalid token');
|
|
367
380
|
}
|
|
368
381
|
|
|
@@ -375,48 +388,11 @@ export const estimateSwap = async (
|
|
|
375
388
|
.times(priceBUSD)
|
|
376
389
|
.toString();
|
|
377
390
|
|
|
378
|
-
//
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
let updatedPools = [...pools]; // Clone pools for after-swap price simulation
|
|
384
|
-
|
|
385
|
-
for (const step of bestPath) {
|
|
386
|
-
const poolIndex = updatedPools.findIndex(
|
|
387
|
-
(p) =>
|
|
388
|
-
(p.coinA.ticker === step.from && p.coinB.ticker === step.to) ||
|
|
389
|
-
(p.coinB.ticker === step.from && p.coinA.ticker === step.to)
|
|
390
|
-
);
|
|
391
|
-
const pool = updatedPools[poolIndex];
|
|
392
|
-
const isCoinAInput = pool.coinA.ticker === step.from;
|
|
393
|
-
const nextCoin = coins.find((c) => c.ticker === step.to);
|
|
394
|
-
const swapResult = simulateSwap(pool, currentCoin, currentAmount, isCoinAInput);
|
|
395
|
-
|
|
396
|
-
const reserveA = new BigNumber(pool.reserveA);
|
|
397
|
-
const reserveB = new BigNumber(pool.reserveB);
|
|
398
|
-
if (isCoinAInput) {
|
|
399
|
-
const newRunesReserve = reserveA.plus(currentAmount);
|
|
400
|
-
const newTokenReserve = reserveB.minus(swapResult.amountOut);
|
|
401
|
-
if (step.from === 'RUNES') {
|
|
402
|
-
priceAInRunesAfter = newRunesReserve.div(newTokenReserve);
|
|
403
|
-
} else if (step.to === 'RUNES') {
|
|
404
|
-
priceBInRunesAfter = newRunesReserve.div(newTokenReserve);
|
|
405
|
-
}
|
|
406
|
-
} else {
|
|
407
|
-
const newTokenReserve = reserveB.plus(currentAmount);
|
|
408
|
-
const newRunesReserve = reserveA.minus(swapResult.amountOut);
|
|
409
|
-
if (step.from === 'RUNES') {
|
|
410
|
-
priceAInRunesAfter = newRunesReserve.div(newTokenReserve);
|
|
411
|
-
} else if (step.to === 'RUNES') {
|
|
412
|
-
priceBInRunesAfter = newRunesReserve.div(newTokenReserve);
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
currentAmount = swapResult.amountOut;
|
|
417
|
-
currentCoin = nextCoin;
|
|
418
|
-
updatedPools[poolIndex] = swapResult.updatedPool; // Update pool for next iteration
|
|
419
|
-
}
|
|
391
|
+
// After-swap prices: use the already-simulated updated pools from bestResult
|
|
392
|
+
// (estimatePath already tracks post-swap reserve states, no need to re-simulate)
|
|
393
|
+
const runesPriceUSDAfter = getRunesPriceUSD(bestResult.updatedPools);
|
|
394
|
+
const priceAInRunesAfter = new BigNumber(getTokenPriceInRunes(inputCoinData, bestResult.updatedPools));
|
|
395
|
+
const priceBInRunesAfter = new BigNumber(getTokenPriceInRunes(outputCoinData, bestResult.updatedPools));
|
|
420
396
|
|
|
421
397
|
return {
|
|
422
398
|
input: {
|
|
@@ -438,8 +414,8 @@ export const estimateSwap = async (
|
|
|
438
414
|
intermediateAmounts: bestResult.intermediateAmounts,
|
|
439
415
|
},
|
|
440
416
|
afterSwapPrices: {
|
|
441
|
-
[inputCoin.ticker]: priceAInRunesAfter.times(
|
|
442
|
-
[outputCoin.ticker]: priceBInRunesAfter.times(
|
|
417
|
+
[inputCoin.ticker]: priceAInRunesAfter.times(runesPriceUSDAfter).toString(),
|
|
418
|
+
[outputCoin.ticker]: priceBInRunesAfter.times(runesPriceUSDAfter).toString(),
|
|
443
419
|
},
|
|
444
420
|
path: bestPath,
|
|
445
421
|
algorithm, // Include algorithm in the response
|