Skip to content

Commit

Permalink
Fix/sor (#564)
Browse files Browse the repository at this point in the history
* metrics by ENV

* default maxNonBoostedPathDepth

* GqlGraphTraversalConfigInput docs
  • Loading branch information
gmbronco authored Dec 8, 2023
1 parent f58ac63 commit 52ef7f9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
29 changes: 26 additions & 3 deletions modules/beethoven/balancer-sdk.gql
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,33 @@ input GqlSorSwapOptionsInput {
forceRefresh: Boolean #don't use any cached responses
}

"""
Configuration options for SOR V2
"""
input GqlGraphTraversalConfigInput {
maxDepth: Int # default 6
maxNonBoostedPathDepth: Int # default 3
maxNonBoostedHopTokensInBoostedPath: Int # default 2
"""
The max hops in a path.
Default: 6
"""
maxDepth: Int
"""
Limit of "non-boosted" pools for efficiency.
Default: 6
"""
maxNonBoostedPathDepth: Int
"""
Limit non boosted hop tokens in a boosted path.
Default: 2
"""
maxNonBoostedHopTokensInBoostedPath: Int
"""
Max number of paths to return (can be less)
Default: 5
"""
approxPathsToReturn: Int # default 5
poolIdsToInclude: [String]
}
Expand Down
10 changes: 8 additions & 2 deletions modules/beethoven/balancer-sdk.resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const balancerSdkResolvers: Resolvers = {
if (!args.chain && currentChain) {
args.chain = currentChain;
} else if (!args.chain) {
throw new Error('poolGetPool error: Provide "chain" param');
throw new Error('sorGetSwaps error: Provide "chain" param');
}
const chain = args.chain;
const tokenIn = args.tokenIn.toLowerCase();
Expand All @@ -22,7 +22,13 @@ const balancerSdkResolvers: Resolvers = {
// Use TokenAmount to help follow scaling requirements in later logic
// args.swapAmount is HumanScale
const amount = await getTokenAmountHuman(amountToken, args.swapAmount, args.chain);
const graphTraversalConfig = args.graphTraversalConfig as GraphTraversalConfig;
const graphTraversalConfig = (
args.graphTraversalConfig
? args.graphTraversalConfig
: {
maxNonBoostedPathDepth: 6,
}
) as GraphTraversalConfig;

const swaps = await sorService.getBeetsSwaps({
...args,
Expand Down
8 changes: 7 additions & 1 deletion modules/metrics/sor.metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { CloudwatchMetricsPublisher } from './metrics.client';

const publishers: Record<string, CloudwatchMetricsPublisher> = {};

export function getSorMetricsPublisher(chain: Chain): CloudwatchMetricsPublisher {
export const publishMetric = (chain: Chain, metricName: string, value: number) => {
if (process.env.NODE_ENV !== 'production') return Promise.resolve();

return getSorMetricsPublisher(chain).publish(metricName, value);
};

function getSorMetricsPublisher(chain: Chain): CloudwatchMetricsPublisher {
if (!publishers[chain]) {
console.log(`Creating new SOR publisher for ${chain}`);
publishers[chain] = new CloudwatchMetricsPublisher(`Backend-${chain}/Sor`);
Expand Down
13 changes: 6 additions & 7 deletions modules/sor/sor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { sorV1BeetsService } from './sorV1Beets/sorV1Beets.service';
import { sorV2Service } from './sorV2/sorV2.service';
import { GetSwapsInput, SwapResult, SwapService } from './types';
import { EMPTY_COWSWAP_RESPONSE } from './constants';
import { getSorMetricsPublisher } from '../metrics/sor.metric';
import { publishMetric } from '../metrics/sor.metric';
import { Chain } from '@prisma/client';
import { parseUnits, formatUnits } from '@ethersproject/units';
import { tokenService } from '../token/token.service';
Expand Down Expand Up @@ -121,9 +121,8 @@ export class SorService {
v1Time: number,
v2Time: number,
) {
const sorMetricsPublisher = getSorMetricsPublisher(chain);
await sorMetricsPublisher.publish(`SOR_VALID_V1`, v1.isValid ? 1 : 0);
await sorMetricsPublisher.publish(`SOR_VALID_V2`, v2.isValid ? 1 : 0);
await publishMetric(chain, `SOR_VALID_V1`, v1.isValid ? 1 : 0);
await publishMetric(chain, `SOR_VALID_V2`, v2.isValid ? 1 : 0);

if (!version) return;

Expand Down Expand Up @@ -154,9 +153,9 @@ export class SorService {
let diff = bn(diffN.toFixed(decimals), decimals);
let bestResultAmount = version === 'V1' ? v1ResultAmount : v2ResultAmount;

await sorMetricsPublisher.publish(`SOR_TIME_V1`, v1Time);
await sorMetricsPublisher.publish(`SOR_TIME_V2`, v2Time);
await sorMetricsPublisher.publish(`SOR_V2_PERFORMACE`, v2Perf);
await publishMetric(chain, `SOR_TIME_V1`, v1Time);
await publishMetric(chain, `SOR_TIME_V2`, v2Time);
await publishMetric(chain, `SOR_V2_PERFORMACE`, v2Perf);

console.log(
[
Expand Down

0 comments on commit 52ef7f9

Please sign in to comment.