Skip to content

Commit 746f3d6

Browse files
rick23psoulBit
andauthored
SOV-4239: sdex logger (#939)
* 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 <its.soulBit@gmail.com>
1 parent 9aefb81 commit 746f3d6

8 files changed

Lines changed: 186 additions & 49 deletions

File tree

.changeset/little-avocados-buy.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"frontend": patch
3+
"@sovryn/sdex": patch
4+
"@sovryn/sdk": patch
5+
---
6+
7+
SOV-4239: add SDEX swap error logging for debugging

apps/frontend/.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ REACT_APP_ENABLE_SERVICE_WORKER=false
2222
# All transactions will be simulated on forked mainnet, REACT_APP_ESTIMATOR_URI is required
2323
REACT_APP_SIMULATE_TX=false
2424
REACT_APP_ESTIMATOR_URI=https://simulator.sovryn.app
25+
26+
REACT_APP_DATADOG_CLIENT_TOKEN=

packages/sdex/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"typescript": "^5.3.3"
3737
},
3838
"dependencies": {
39+
"@datadog/browser-logs": "^5.21.0",
3940
"ethers": "^5.5.3"
4041
}
4142
}

packages/sdex/src/index.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
export * from "./constants";
2-
export * from "./utils";
3-
export * from "./abis";
4-
export * from "./pool";
5-
export * from "./position";
6-
export * from "./swap";
7-
export * from "./croc";
1+
export * from './constants';
2+
export * from './utils';
3+
export * from './abis';
4+
export * from './pool';
5+
export * from './position';
6+
export * from './swap';
7+
export * from './croc';
8+
export * from './logger';
89

9-
export * from "./encoding/liquidity";
10+
export * from './encoding/liquidity';
1011

11-
export * from "./recipes/reposition";
12+
export * from './recipes/reposition';

packages/sdex/src/logger.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { datadogLogs } from '@datadog/browser-logs';
2+
3+
datadogLogs.init({
4+
clientToken: process.env.REACT_APP_DATADOG_CLIENT_TOKEN || '',
5+
site: 'datadoghq.com',
6+
forwardErrorsToLogs: true,
7+
sessionSampleRate: 100,
8+
service: 'd2',
9+
});
10+
11+
export const logger = (type: 'info' | 'error', title: string, data: object) => {
12+
if (type === 'error') {
13+
datadogLogs.logger.error(title, data);
14+
} else if (type === 'info') {
15+
datadogLogs.logger.info(title, data);
16+
}
17+
};

packages/sdex/src/swap.ts

Lines changed: 95 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
decodeSurplusFlag,
1212
encodeSurplusArg,
1313
} from './encoding/flags';
14+
import { logger } from './logger';
1415
import { CrocPoolView } from './pool';
1516
import { CrocSlotReader } from './slots';
1617
import {
@@ -401,34 +402,100 @@ export class CrocSwapPlan {
401402
async calcImpact(): Promise<CrocImpact> {
402403
const TIP = 0;
403404
const limitPrice = this.sellBase ? MAX_SQRT_PRICE : MIN_SQRT_PRICE;
404-
405-
const impact = await (
406-
await this.context
407-
).slipQuery.calcImpact(
408-
this.baseToken.tokenAddr,
409-
this.quoteToken.tokenAddr,
410-
this.poolIndex,
411-
this.sellBase,
412-
this.qtyInBase,
413-
await this.qty,
414-
TIP,
415-
limitPrice,
416-
);
417-
418-
const baseQty = this.baseToken.toDisplay(impact.baseFlow.abs());
419-
const quoteQty = this.quoteToken.toDisplay(impact.quoteFlow.abs());
420-
const spotPrice = decodeCrocPrice(impact.finalPrice);
421-
422-
const startPrice = this.poolView.displayPrice();
423-
const finalPrice = this.poolView.toDisplayPrice(spotPrice);
424-
425-
return {
426-
sellQty: this.sellBase ? await baseQty : await quoteQty,
427-
buyQty: this.sellBase ? await quoteQty : await baseQty,
428-
finalPrice: await finalPrice,
429-
percentChange:
430-
((await finalPrice) - (await startPrice)) / (await startPrice),
431-
};
405+
const qty = await this.qty;
406+
407+
try {
408+
console.log(
409+
`this.baseToken.tokenAddr: ${
410+
this.baseToken.tokenAddr
411+
} , this.quoteToken.tokenAddr: ${
412+
this.quoteToken.tokenAddr
413+
} , this.poolIndex: ${this.poolIndex} , this.sellBase: ${
414+
this.sellBase
415+
} , this.qtyInBase: ${this.qtyInBase} , qty: ${await this
416+
.qty} , TIP: ${TIP} , limitPrice: ${limitPrice}`,
417+
);
418+
419+
const impact = await (
420+
await this.context
421+
).slipQuery.calcImpact(
422+
this.baseToken.tokenAddr,
423+
this.quoteToken.tokenAddr,
424+
this.poolIndex,
425+
this.sellBase,
426+
this.qtyInBase,
427+
qty,
428+
TIP,
429+
limitPrice,
430+
);
431+
432+
const baseQty = this.baseToken.toDisplay(impact.baseFlow.abs());
433+
const quoteQty = this.quoteToken.toDisplay(impact.quoteFlow.abs());
434+
const spotPrice = decodeCrocPrice(impact.finalPrice);
435+
436+
const startPrice = this.poolView.displayPrice();
437+
const finalPrice = this.poolView.toDisplayPrice(spotPrice);
438+
439+
logger('info', 'calcImpact', {
440+
data: {
441+
baseToken: this.baseToken.tokenAddr,
442+
quoteToken: this.quoteToken.tokenAddr,
443+
poolIndex: this.poolIndex,
444+
sellBase: this.sellBase,
445+
qtyInBase: this.qtyInBase,
446+
qty,
447+
TIP,
448+
limitPrice,
449+
network: process.env.REACT_APP_NETWORK,
450+
},
451+
dataFormatted: {
452+
baseToken: this.baseToken.tokenAddr,
453+
quoteToken: this.quoteToken.tokenAddr,
454+
poolIndex: this.poolIndex,
455+
sellBase: this.sellBase,
456+
qtyInBase: this.qtyInBase,
457+
qty: qty.toString(),
458+
TIP,
459+
limitPrice: limitPrice.toString(),
460+
network: process.env.REACT_APP_NETWORK,
461+
},
462+
});
463+
return {
464+
sellQty: this.sellBase ? await baseQty : await quoteQty,
465+
buyQty: this.sellBase ? await quoteQty : await baseQty,
466+
finalPrice: await finalPrice,
467+
percentChange:
468+
((await finalPrice) - (await startPrice)) / (await startPrice),
469+
};
470+
} catch (error) {
471+
logger('error', 'calcImpact', {
472+
data: {
473+
baseToken: this.baseToken.tokenAddr,
474+
quoteToken: this.quoteToken.tokenAddr,
475+
poolIndex: this.poolIndex,
476+
sellBase: this.sellBase,
477+
qtyInBase: this.qtyInBase,
478+
qty,
479+
TIP,
480+
limitPrice,
481+
network: process.env.REACT_APP_NETWORK,
482+
error,
483+
},
484+
dataFormatted: {
485+
baseToken: this.baseToken.tokenAddr,
486+
quoteToken: this.quoteToken.tokenAddr,
487+
poolIndex: this.poolIndex,
488+
sellBase: this.sellBase,
489+
qtyInBase: this.qtyInBase,
490+
qty: qty.toString(),
491+
TIP,
492+
limitPrice: limitPrice.toString(),
493+
network: process.env.REACT_APP_NETWORK,
494+
error,
495+
},
496+
});
497+
throw error;
498+
}
432499
}
433500

434501
public maskSurplusArgs(args?: CrocSwapExecOpts): number {

packages/sdk/src/swaps/smart-router/utils/ambient-utils.ts

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { BigNumber } from 'ethers';
22

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

77
export type PoolWithIndex = [string, string, number];
88
export type Pool = [string, string];
@@ -73,17 +73,47 @@ export const calcImpact = async (
7373
inBaseQty: boolean,
7474
qty: BigNumber,
7575
) => {
76-
const context = await env.context;
77-
return await context.slipQuery.calcImpact(
78-
base,
79-
quote,
80-
poolIdx,
81-
isBuy,
82-
inBaseQty,
83-
qty,
84-
0,
85-
initialLimitPrice(isBuy),
86-
);
76+
try {
77+
const context = await env.context;
78+
const result = await context.slipQuery.calcImpact(
79+
base,
80+
quote,
81+
poolIdx,
82+
isBuy,
83+
inBaseQty,
84+
qty,
85+
0,
86+
initialLimitPrice(isBuy),
87+
);
88+
89+
return result;
90+
} catch (error) {
91+
logger('error', 'calcImpact', {
92+
data: {
93+
error,
94+
base,
95+
quote,
96+
poolIdx,
97+
isBuy,
98+
inBaseQty,
99+
qty,
100+
initialLimitPrice: initialLimitPrice(isBuy),
101+
env,
102+
},
103+
dataFormatted: {
104+
error,
105+
base,
106+
quote,
107+
poolIdx,
108+
isBuy,
109+
inBaseQty,
110+
qty: qty.toString(),
111+
initialLimitPrice: initialLimitPrice(isBuy).toString(),
112+
network: process.env.REACT_APP_NETWORK,
113+
env,
114+
},
115+
});
116+
}
87117
};
88118

89119
const INDEXER = {

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,6 +1976,18 @@
19761976
resolved "https://registry.yarnpkg.com/@csstools/selector-specificity/-/selector-specificity-2.0.2.tgz#1bfafe4b7ed0f3e4105837e056e0a89b108ebe36"
19771977
integrity sha512-IkpVW/ehM1hWKln4fCA3NzJU8KwD+kIOvPZA4cqxoJHtE21CCzjyp+Kxbu0i5I4tBNOlXPL9mjwnWlL0VEG4Fg==
19781978

1979+
"@datadog/browser-core@5.21.0":
1980+
version "5.21.0"
1981+
resolved "https://registry.yarnpkg.com/@datadog/browser-core/-/browser-core-5.21.0.tgz#980e43d036a4c2074d37b887bd0ba748cca75e15"
1982+
integrity sha512-F0lZCwHwGCryhyRAg9x9Aeq8FsYMxA41vwyBeyFHbh+er5UrQl/LuKdrRMSxz3kpaBdHppLP920voTSvooHiug==
1983+
1984+
"@datadog/browser-logs@5.21.0", "@datadog/browser-logs@^5.21.0":
1985+
version "5.21.0"
1986+
resolved "https://registry.yarnpkg.com/@datadog/browser-logs/-/browser-logs-5.21.0.tgz#86afe2595a6bb5ddbc095500a2f35733b35e5aad"
1987+
integrity sha512-Nb1F2M0tQzSnDU3iI61U+fnqPskuYMuYTyMLD9KwvcKru4cGj3+k5nYVmszvwEKZZ+etYff+1iBvnpYIo2w6hw==
1988+
dependencies:
1989+
"@datadog/browser-core" "5.21.0"
1990+
19791991
"@design-systems/utils@2.12.0":
19801992
version "2.12.0"
19811993
resolved "https://registry.yarnpkg.com/@design-systems/utils/-/utils-2.12.0.tgz#955c108be07cb8f01532207cbfea8f848fa760c9"

0 commit comments

Comments
 (0)