Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cables fix volumes + partner&fee encoding optimization #891

Merged
merged 16 commits into from
Feb 12, 2025
Merged
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@paraswap/dex-lib",
"version": "4.0.28",
"version": "4.0.28-cables",
"main": "build/index.js",
"types": "build/index.d.ts",
"repository": "https://github.com/paraswap/paraswap-dex-lib",
Expand Down
80 changes: 40 additions & 40 deletions src/dex/cables/cables-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Cables E2E', () => {

const sideToContractMethods = new Map([
[SwapSide.SELL, [ContractMethod.swapExactAmountIn]],
[SwapSide.BUY, [ContractMethod.swapExactAmountOut]],
// [SwapSide.BUY, [ContractMethod.swapExactAmountOut]],
]);

describe('Arbitrum', () => {
Expand All @@ -30,7 +30,7 @@ describe('Cables E2E', () => {
[
{
name: 'USDC',
sellAmount: '500000',
sellAmount: '50000000000000',
buyAmount: '700000',
},
{
Expand All @@ -39,18 +39,18 @@ describe('Cables E2E', () => {
buyAmount: '850000',
},
],
[
{
name: 'WETH',
sellAmount: '100000000000000000',
buyAmount: '200000000000000000',
},
{
name: 'USDT',
sellAmount: '6000000',
buyAmount: '8000000',
},
],
// [
// {
// name: 'WETH',
// sellAmount: '100000000000000000',
// buyAmount: '200000000000000000',
// },
// {
// name: 'USDT',
// sellAmount: '6000000',
// buyAmount: '8000000',
// },
// ],
];

sideToContractMethods.forEach((contractMethods, side) =>
Expand Down Expand Up @@ -84,32 +84,32 @@ describe('Cables E2E', () => {
sleepMs,
);
});
it(`${pair[1].name} -> ${pair[0].name}`, async () => {
await testE2E(
side === SwapSide.SELL
? tokens[pair[1].name]
: tokens[pair[0].name],
side === SwapSide.SELL
? tokens[pair[0].name]
: tokens[pair[1].name],
side === SwapSide.SELL
? holders[pair[1].name]
: holders[pair[0].name],
side === SwapSide.SELL
? pair[1].sellAmount
: pair[1].buyAmount,
side,
dexKey,
contractMethod,
network,
provider,
undefined,
undefined,
undefined,
slippage,
sleepMs,
);
});
// it(`${pair[1].name} -> ${pair[0].name}`, async () => {
// await testE2E(
// side === SwapSide.SELL
// ? tokens[pair[1].name]
// : tokens[pair[0].name],
// side === SwapSide.SELL
// ? tokens[pair[0].name]
// : tokens[pair[1].name],
// side === SwapSide.SELL
// ? holders[pair[1].name]
// : holders[pair[0].name],
// side === SwapSide.SELL
// ? pair[1].sellAmount
// : pair[1].buyAmount,
// side,
// dexKey,
// contractMethod,
// network,
// provider,
// undefined,
// undefined,
// undefined,
// slippage,
// sleepMs,
// );
// });
});
});
});
Expand Down
47 changes: 37 additions & 10 deletions src/dex/cables/cables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,15 @@ export class Cables extends SimpleExchange implements IDex<any> {
let decimals = baseToken.decimals;
let out_decimals = quoteToken.decimals;

let price = this.calculatePriceSwap(
orderbook,
Number(amt) / 10 ** decimals,
isInputQuote,
);
let price = 0;

try {
price = this.calculatePriceSwap(
orderbook,
Number(amt) / 10 ** decimals,
isInputQuote,
);
} catch {}
result.push(BigInt(Math.round(price * 10 ** out_decimals)));
}
return result;
Expand All @@ -416,11 +420,17 @@ export class Cables extends SimpleExchange implements IDex<any> {
) {
let sumBaseQty = 0;
let sumQuoteQty = 0;

const selectedRows: string[][] = [];

const isBase = qtyMode;
const isQuote = !qtyMode;

const totalVolume: number = prices.reduce(
(sum, [, volume]) => sum + Number(volume),
0,
);

for (const [price, volume] of prices) {
if (isBase) {
if (sumBaseQty >= requiredQty) {
Expand Down Expand Up @@ -460,6 +470,10 @@ export class Cables extends SimpleExchange implements IDex<any> {
selectedRows.push([price, currentBaseQty.toString()]);
}

if (sumBaseQty == 0) {
return 0;
}

const vSumBase = selectedRows.reduce((sum: number, [price, volume]) => {
return sum + Number(price) * Number(volume);
}, 0);
Expand All @@ -468,11 +482,24 @@ export class Cables extends SimpleExchange implements IDex<any> {
.dividedBy(new BigNumber(sumBaseQty))
.toNumber();

let result: any;
if (isBase) {
return requiredQty / price;
result = requiredQty / price;
} else {
return requiredQty * price;
result = requiredQty * price;
}

if (isQuote) {
if (requiredQty > totalVolume) {
result = 0;
}
} else {
if (result > totalVolume) {
result = 0;
}
}

return result;
}

async getPricesVolume(
Expand Down Expand Up @@ -538,10 +565,10 @@ export class Cables extends SimpleExchange implements IDex<any> {
const priceData = priceMap[pairKey];

let orderbook: any[] = [];
if (side === SwapSide.BUY) {
orderbook = priceData.bids;
// reverse orderbook if isInputQuote
if (isInputQuote) {
orderbook = priceData.asks;
} else {
orderbook = priceData.bids;
}
if (orderbook?.length === 0) {
throw new Error(`Empty orderbook for ${pairKey}`);
Expand Down