Skip to content

Commit

Permalink
SOV-4239: sdex logger (#939)
Browse files Browse the repository at this point in the history
* feat: sdex logger

* fix: add logs to calcImpact

* feat: update swap logger

* fix: add service name to logger

* fix: remove logs

* fix: log calcImpact in ambient utils

* fix: error logs

* feat: add dataFormatted logs

* rebuild

* test: logger

* fix: remove log

* chore: add changeset

---------

Co-authored-by: soulBit <[email protected]>
  • Loading branch information
rick23p and soulBit authored Jun 21, 2024
1 parent 9aefb81 commit 746f3d6
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 49 deletions.
7 changes: 7 additions & 0 deletions .changeset/little-avocados-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"frontend": patch
"@sovryn/sdex": patch
"@sovryn/sdk": patch
---

SOV-4239: add SDEX swap error logging for debugging
2 changes: 2 additions & 0 deletions apps/frontend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ REACT_APP_ENABLE_SERVICE_WORKER=false
# All transactions will be simulated on forked mainnet, REACT_APP_ESTIMATOR_URI is required
REACT_APP_SIMULATE_TX=false
REACT_APP_ESTIMATOR_URI=https://simulator.sovryn.app

REACT_APP_DATADOG_CLIENT_TOKEN=
1 change: 1 addition & 0 deletions packages/sdex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"typescript": "^5.3.3"
},
"dependencies": {
"@datadog/browser-logs": "^5.21.0",
"ethers": "^5.5.3"
}
}
19 changes: 10 additions & 9 deletions packages/sdex/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
export * from "./constants";
export * from "./utils";
export * from "./abis";
export * from "./pool";
export * from "./position";
export * from "./swap";
export * from "./croc";
export * from './constants';
export * from './utils';
export * from './abis';
export * from './pool';
export * from './position';
export * from './swap';
export * from './croc';
export * from './logger';

export * from "./encoding/liquidity";
export * from './encoding/liquidity';

export * from "./recipes/reposition";
export * from './recipes/reposition';
17 changes: 17 additions & 0 deletions packages/sdex/src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { datadogLogs } from '@datadog/browser-logs';

datadogLogs.init({
clientToken: process.env.REACT_APP_DATADOG_CLIENT_TOKEN || '',
site: 'datadoghq.com',
forwardErrorsToLogs: true,
sessionSampleRate: 100,
service: 'd2',
});

export const logger = (type: 'info' | 'error', title: string, data: object) => {
if (type === 'error') {
datadogLogs.logger.error(title, data);
} else if (type === 'info') {
datadogLogs.logger.info(title, data);
}
};
123 changes: 95 additions & 28 deletions packages/sdex/src/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
decodeSurplusFlag,
encodeSurplusArg,
} from './encoding/flags';
import { logger } from './logger';
import { CrocPoolView } from './pool';
import { CrocSlotReader } from './slots';
import {
Expand Down Expand Up @@ -401,34 +402,100 @@ export class CrocSwapPlan {
async calcImpact(): Promise<CrocImpact> {
const TIP = 0;
const limitPrice = this.sellBase ? MAX_SQRT_PRICE : MIN_SQRT_PRICE;

const impact = await (
await this.context
).slipQuery.calcImpact(
this.baseToken.tokenAddr,
this.quoteToken.tokenAddr,
this.poolIndex,
this.sellBase,
this.qtyInBase,
await this.qty,
TIP,
limitPrice,
);

const baseQty = this.baseToken.toDisplay(impact.baseFlow.abs());
const quoteQty = this.quoteToken.toDisplay(impact.quoteFlow.abs());
const spotPrice = decodeCrocPrice(impact.finalPrice);

const startPrice = this.poolView.displayPrice();
const finalPrice = this.poolView.toDisplayPrice(spotPrice);

return {
sellQty: this.sellBase ? await baseQty : await quoteQty,
buyQty: this.sellBase ? await quoteQty : await baseQty,
finalPrice: await finalPrice,
percentChange:
((await finalPrice) - (await startPrice)) / (await startPrice),
};
const qty = await this.qty;

try {
console.log(
`this.baseToken.tokenAddr: ${
this.baseToken.tokenAddr
} , this.quoteToken.tokenAddr: ${
this.quoteToken.tokenAddr
} , this.poolIndex: ${this.poolIndex} , this.sellBase: ${
this.sellBase
} , this.qtyInBase: ${this.qtyInBase} , qty: ${await this
.qty} , TIP: ${TIP} , limitPrice: ${limitPrice}`,
);

const impact = await (
await this.context
).slipQuery.calcImpact(
this.baseToken.tokenAddr,
this.quoteToken.tokenAddr,
this.poolIndex,
this.sellBase,
this.qtyInBase,
qty,
TIP,
limitPrice,
);

const baseQty = this.baseToken.toDisplay(impact.baseFlow.abs());
const quoteQty = this.quoteToken.toDisplay(impact.quoteFlow.abs());
const spotPrice = decodeCrocPrice(impact.finalPrice);

const startPrice = this.poolView.displayPrice();
const finalPrice = this.poolView.toDisplayPrice(spotPrice);

logger('info', 'calcImpact', {
data: {
baseToken: this.baseToken.tokenAddr,
quoteToken: this.quoteToken.tokenAddr,
poolIndex: this.poolIndex,
sellBase: this.sellBase,
qtyInBase: this.qtyInBase,
qty,
TIP,
limitPrice,
network: process.env.REACT_APP_NETWORK,
},
dataFormatted: {
baseToken: this.baseToken.tokenAddr,
quoteToken: this.quoteToken.tokenAddr,
poolIndex: this.poolIndex,
sellBase: this.sellBase,
qtyInBase: this.qtyInBase,
qty: qty.toString(),
TIP,
limitPrice: limitPrice.toString(),
network: process.env.REACT_APP_NETWORK,
},
});
return {
sellQty: this.sellBase ? await baseQty : await quoteQty,
buyQty: this.sellBase ? await quoteQty : await baseQty,
finalPrice: await finalPrice,
percentChange:
((await finalPrice) - (await startPrice)) / (await startPrice),
};
} catch (error) {
logger('error', 'calcImpact', {
data: {
baseToken: this.baseToken.tokenAddr,
quoteToken: this.quoteToken.tokenAddr,
poolIndex: this.poolIndex,
sellBase: this.sellBase,
qtyInBase: this.qtyInBase,
qty,
TIP,
limitPrice,
network: process.env.REACT_APP_NETWORK,
error,
},
dataFormatted: {
baseToken: this.baseToken.tokenAddr,
quoteToken: this.quoteToken.tokenAddr,
poolIndex: this.poolIndex,
sellBase: this.sellBase,
qtyInBase: this.qtyInBase,
qty: qty.toString(),
TIP,
limitPrice: limitPrice.toString(),
network: process.env.REACT_APP_NETWORK,
error,
},
});
throw error;
}
}

public maskSurplusArgs(args?: CrocSwapExecOpts): number {
Expand Down
54 changes: 42 additions & 12 deletions packages/sdk/src/swaps/smart-router/utils/ambient-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BigNumber } from 'ethers';

import { getAssetContract, getAssetDataByAddress } from '@sovryn/contracts';
import { ChainId, ChainIds } from '@sovryn/ethers-provider';
import { CrocEnv, MAX_SQRT_PRICE, MIN_SQRT_PRICE } from '@sovryn/sdex';
import { CrocEnv, logger, MAX_SQRT_PRICE, MIN_SQRT_PRICE } from '@sovryn/sdex';

export type PoolWithIndex = [string, string, number];
export type Pool = [string, string];
Expand Down Expand Up @@ -73,17 +73,47 @@ export const calcImpact = async (
inBaseQty: boolean,
qty: BigNumber,
) => {
const context = await env.context;
return await context.slipQuery.calcImpact(
base,
quote,
poolIdx,
isBuy,
inBaseQty,
qty,
0,
initialLimitPrice(isBuy),
);
try {
const context = await env.context;
const result = await context.slipQuery.calcImpact(
base,
quote,
poolIdx,
isBuy,
inBaseQty,
qty,
0,
initialLimitPrice(isBuy),
);

return result;
} catch (error) {
logger('error', 'calcImpact', {
data: {
error,
base,
quote,
poolIdx,
isBuy,
inBaseQty,
qty,
initialLimitPrice: initialLimitPrice(isBuy),
env,
},
dataFormatted: {
error,
base,
quote,
poolIdx,
isBuy,
inBaseQty,
qty: qty.toString(),
initialLimitPrice: initialLimitPrice(isBuy).toString(),
network: process.env.REACT_APP_NETWORK,
env,
},
});
}
};

const INDEXER = {
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1976,6 +1976,18 @@
resolved "https://registry.yarnpkg.com/@csstools/selector-specificity/-/selector-specificity-2.0.2.tgz#1bfafe4b7ed0f3e4105837e056e0a89b108ebe36"
integrity sha512-IkpVW/ehM1hWKln4fCA3NzJU8KwD+kIOvPZA4cqxoJHtE21CCzjyp+Kxbu0i5I4tBNOlXPL9mjwnWlL0VEG4Fg==

"@datadog/[email protected]":
version "5.21.0"
resolved "https://registry.yarnpkg.com/@datadog/browser-core/-/browser-core-5.21.0.tgz#980e43d036a4c2074d37b887bd0ba748cca75e15"
integrity sha512-F0lZCwHwGCryhyRAg9x9Aeq8FsYMxA41vwyBeyFHbh+er5UrQl/LuKdrRMSxz3kpaBdHppLP920voTSvooHiug==

"@datadog/[email protected]", "@datadog/browser-logs@^5.21.0":
version "5.21.0"
resolved "https://registry.yarnpkg.com/@datadog/browser-logs/-/browser-logs-5.21.0.tgz#86afe2595a6bb5ddbc095500a2f35733b35e5aad"
integrity sha512-Nb1F2M0tQzSnDU3iI61U+fnqPskuYMuYTyMLD9KwvcKru4cGj3+k5nYVmszvwEKZZ+etYff+1iBvnpYIo2w6hw==
dependencies:
"@datadog/browser-core" "5.21.0"

"@design-systems/[email protected]":
version "2.12.0"
resolved "https://registry.yarnpkg.com/@design-systems/utils/-/utils-2.12.0.tgz#955c108be07cb8f01532207cbfea8f848fa760c9"
Expand Down

0 comments on commit 746f3d6

Please sign in to comment.