Skip to content

Commit

Permalink
Merge pull request #891 from Birua/cables-fix-volumes
Browse files Browse the repository at this point in the history
Cables fix volumes
  • Loading branch information
JackieJoo authored Feb 12, 2025
2 parents 75fe1cc + b355295 commit 698732f
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 52 deletions.
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.1.0",
"version": "4.1.1",
"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
8 changes: 7 additions & 1 deletion src/generic-swap-transaction-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,14 @@ export class GenericSwapTransactionBuilder {
isReferral,
isSkipBlacklist,
}: FeeParams): string {
const partnerAddress =
feePercent === '0' && !isTakeSurplus && !isReferral
? NULL_ADDRESS
: partner;

// Partner address shifted left to make room for flags and fee percent
const partialFeeCodeWithPartnerAddress = BigNumber.from(partner).shl(96);
const partialFeeCodeWithPartnerAddress =
BigNumber.from(partnerAddress).shl(96);
let partialFeeCodeWithBitFlags = BigNumber.from(0); // default 0 is safe if none the conditions pass

const isFixedFees = !BigNumber.from(feePercent).isZero();
Expand Down

0 comments on commit 698732f

Please sign in to comment.