Skip to content

Commit

Permalink
cables prices fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Birua committed Nov 28, 2024
1 parent 21133a5 commit d475e3b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 48 deletions.
54 changes: 39 additions & 15 deletions src/dex/cables/cables-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ describe('Cables E2E', () => {
[
{
name: 'WETH',
sellAmount: '1000000000000000000',
buyAmount: '1000000000000000000',
sellAmount: '100000000000000000',
buyAmount: '200000000000000000',
},
{
name: 'USDT',
sellAmount: '6000000',
buyAmount: '6000000',
buyAmount: '8000000',
},
],
];
Expand All @@ -60,9 +60,15 @@ describe('Cables E2E', () => {
describe(`${contractMethod}`, () => {
it(`${pair[0].name} -> ${pair[1].name}`, async () => {
await testE2E(
tokens[pair[0].name],
tokens[pair[1].name],
holders[pair[0].name],
side === SwapSide.SELL
? tokens[pair[0].name]
: tokens[pair[1].name],
side === SwapSide.SELL
? tokens[pair[1].name]
: tokens[pair[0].name],
side === SwapSide.SELL
? holders[pair[0].name]
: holders[pair[1].name],
side === SwapSide.SELL
? pair[0].sellAmount
: pair[0].buyAmount,
Expand All @@ -80,9 +86,15 @@ describe('Cables E2E', () => {
});
it(`${pair[1].name} -> ${pair[0].name}`, async () => {
await testE2E(
tokens[pair[1].name],
tokens[pair[0].name],
holders[pair[1].name],
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,
Expand Down Expand Up @@ -136,9 +148,15 @@ describe('Cables E2E', () => {
describe(`${contractMethod}`, () => {
it(`${pair[0].name} -> ${pair[1].name}`, async () => {
await testE2E(
tokens[pair[0].name],
tokens[pair[1].name],
holders[pair[0].name],
side === SwapSide.SELL
? tokens[pair[0].name]
: tokens[pair[1].name],
side === SwapSide.SELL
? tokens[pair[1].name]
: tokens[pair[0].name],
side === SwapSide.SELL
? holders[pair[0].name]
: holders[pair[1].name],
side === SwapSide.SELL
? pair[0].sellAmount
: pair[0].buyAmount,
Expand All @@ -156,9 +174,15 @@ describe('Cables E2E', () => {
});
it(`${pair[1].name} -> ${pair[0].name}`, async () => {
await testE2E(
tokens[pair[1].name],
tokens[pair[0].name],
holders[pair[1].name],
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,
Expand Down
65 changes: 32 additions & 33 deletions src/dex/cables/cables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ export class Cables extends SimpleExchange implements IDex<any> {
const swapIdentifier = `${this.dexKey}_${normalizedSrcToken.address}_${normalizedDestToken.address}_${side}`;

try {
const makerToken = normalizedDestToken;
const takerToken = normalizedSrcToken;
let makerToken = normalizedDestToken;
let takerToken = normalizedSrcToken;

const isSell = side === SwapSide.SELL;
const isBuy = side === SwapSide.BUY;
Expand Down Expand Up @@ -201,32 +201,32 @@ export class Cables extends SimpleExchange implements IDex<any> {
const expiryAsBigInt = BigInt(order.expiry);
const minDeadline = expiryAsBigInt > 0 ? expiryAsBigInt : BI_MAX_UINT256;

if (side === SwapSide.SELL) {
const requiredAmount = BigInt(optimalSwapExchange.destAmount);
const quoteAmount = BigInt(order.makerAmount);
if (side === SwapSide.BUY) {
const requiredAmount = BigInt(optimalSwapExchange.srcAmount);
const quoteAmount = BigInt(order.takerAmount);
const requiredAmountWithSlippage = new BigNumber(
requiredAmount.toString(),
)
.times(options.slippageFactor)
.multipliedBy(options.slippageFactor)
.toFixed(0);
if (quoteAmount < BigInt(requiredAmountWithSlippage)) {
if (quoteAmount > BigInt(requiredAmountWithSlippage)) {
throw new SlippageError(
`Slipped, factor: ${quoteAmount.toString()} < ${requiredAmountWithSlippage}`,
`Slipped, factor: ${quoteAmount.toString()} > ${requiredAmountWithSlippage}`,
);
}
} else {
const requiredAmount = BigInt(optimalSwapExchange.srcAmount);
const quoteAmount = BigInt(order.takerAmount);
const requiredAmount = BigInt(optimalSwapExchange.destAmount);
const quoteAmount = BigInt(order.makerAmount);
const requiredAmountWithSlippage = new BigNumber(
requiredAmount.toString(),
)
.times(options.slippageFactor)
.multipliedBy(options.slippageFactor)
.toFixed(0);
if (quoteAmount > BigInt(requiredAmountWithSlippage)) {
if (quoteAmount < BigInt(requiredAmountWithSlippage)) {
throw new SlippageError(
`Slipped, factor: ${
options.slippageFactor
} ${quoteAmount.toString()} > ${requiredAmountWithSlippage}`,
} ${quoteAmount.toString()} < ${requiredAmountWithSlippage}`,
);
}
}
Expand Down Expand Up @@ -276,7 +276,7 @@ export class Cables extends SimpleExchange implements IDex<any> {
`${this.dexKey}-${this.network}: quoteData undefined`,
);

const swapFunction = 'partialSwap';
const swapFunction = 'simpleSwap';
const swapFunctionParams = [
[
quoteData.nonceAndMeta,
Expand All @@ -289,7 +289,6 @@ export class Cables extends SimpleExchange implements IDex<any> {
quoteData.takerAmount,
],
quoteData.signature,
quoteData.takerAmount,
];

const exchangeData = this.rfqInterface.encodeFunctionData(
Expand Down Expand Up @@ -431,15 +430,14 @@ export class Cables extends SimpleExchange implements IDex<any> {
}

let decimals = baseToken.decimals;
if (isInputQuote) {
decimals = quoteToken.decimals;
}
let out_decimals = quoteToken.decimals;

let price = this.calculatePriceSwap(
orderbook,
Number(amt) / 10 ** decimals,
isInputQuote,
);
result.push(BigInt(Math.round(price * 10 ** decimals)));
result.push(BigInt(Math.round(price * 10 ** out_decimals)));
}
return result;
}
Expand All @@ -453,8 +451,8 @@ export class Cables extends SimpleExchange implements IDex<any> {
let sumQuoteQty = 0;
const selectedRows: string[][] = [];

const isBase = !qtyMode;
const isQuote = qtyMode;
const isBase = qtyMode;
const isQuote = !qtyMode;

for (const [price, volume] of prices) {
if (isBase) {
Expand Down Expand Up @@ -499,10 +497,14 @@ export class Cables extends SimpleExchange implements IDex<any> {
return sum + Number(price) * Number(volume);
}, 0);

const price = new BigNumber(vSumBase)
.dividedBy(new BigNumber(sumBaseQty))
.toNumber();

if (isBase) {
return sumBaseQty;
return requiredQty / price;
} else {
return sumQuoteQty;
return requiredQty * price;
}
}

Expand Down Expand Up @@ -565,11 +567,13 @@ export class Cables extends SimpleExchange implements IDex<any> {

if (!priceMap) return null;

let isInputQuote = false;
let pairKey = `${normalizedSrcToken.symbol}/${normalizedDestToken.symbol}`;
const pairsKeys = Object.keys(priceMap);

if (!pairsKeys.includes(pairKey)) {
// Revert
isInputQuote = true;
pairKey = `${normalizedDestToken.symbol}/${normalizedSrcToken.symbol}`;
if (!pairsKeys.includes(pairKey)) {
return null;
Expand All @@ -591,30 +595,25 @@ export class Cables extends SimpleExchange implements IDex<any> {
throw new Error(`Empty orderbook for ${pairKey}`);
}

const isInputQuote = side === SwapSide.BUY;

const prices = this.calculateOrderPrice(
amounts,
orderbook,
srcToken,
destToken,
isInputQuote,
side === SwapSide.SELL ? srcToken : destToken,
side === SwapSide.SELL ? destToken : srcToken,
side === SwapSide.SELL ? isInputQuote : !isInputQuote,
);

const outDecimals =
side === SwapSide.BUY
? normalizedSrcToken.decimals
: normalizedDestToken.decimals;
const result = [
{
prices: prices,
unit: BigInt(outDecimals),
unit: BigInt(normalizedDestToken.decimals),
exchange: this.dexKey,
gasCost: CABLES_GAS_COST,
poolAddresses: [this.mainnetRFQAddress],
data: {},
},
];

return result;
} catch (e: unknown) {
this.logger.error(
Expand Down

0 comments on commit d475e3b

Please sign in to comment.