Skip to content

Commit

Permalink
V2 options
Browse files Browse the repository at this point in the history
  • Loading branch information
gmbronco committed Dec 7, 2023
1 parent 3792147 commit a4e1d69
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 14 deletions.
9 changes: 9 additions & 0 deletions modules/beethoven/balancer-sdk.gql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extend type Query {
swapType: GqlSorSwapType!
swapAmount: BigDecimal! #expected in human readable form
swapOptions: GqlSorSwapOptionsInput!
swapOptionsV2: GqlV2SwapOptionsInput
): GqlSorGetSwapsResponse!
sorGetBatchSwapForTokensIn(
tokensIn: [GqlTokenAmountHumanReadable!]!
Expand All @@ -25,6 +26,14 @@ input GqlSorSwapOptionsInput {
forceRefresh: Boolean #don't use any cached responses
}

input GqlV2SwapOptionsInput {
maxDepth: Int # default 6
maxNonBoostedPathDepth: Int # default 3
maxNonBoostedHopTokensInBoostedPath: Int # default 2
approxPathsToReturn: Int # default 5
poolIdsToInclude: [String]
}

type GqlSorGetSwapsResponse {
tokenIn: String!
tokenOut: String!
Expand Down
22 changes: 21 additions & 1 deletion modules/beethoven/balancer-sdk.resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { balancerSorService } from './balancer-sor.service';
import { tokenService } from '../token/token.service';
import { sorService } from '../sor/sor.service';
import { getTokenAmountHuman } from '../sor/utils';
import { SorV2Options } from '../sor/types';

type V2Options = SorV2Options['graphTraversalConfig'];

const balancerSdkResolvers: Resolvers = {
Query: {
Expand All @@ -11,7 +14,24 @@ 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 swaps = await sorService.getBeetsSwaps({ ...args, swapAmount: amount });
const { swapOptionsV2, ...cleanArgs } = args;

const graphTraversalConfig: V2Options = {};
if (swapOptionsV2 && swapOptionsV2.approxPathsToReturn)
graphTraversalConfig['approxPathsToReturn'] = swapOptionsV2.approxPathsToReturn;
if (swapOptionsV2 && swapOptionsV2.maxDepth) graphTraversalConfig['maxDepth'] = swapOptionsV2.maxDepth;
if (swapOptionsV2 && swapOptionsV2.maxNonBoostedHopTokensInBoostedPath)
graphTraversalConfig['maxNonBoostedHopTokensInBoostedPath'] =
swapOptionsV2.maxNonBoostedHopTokensInBoostedPath;
if (swapOptionsV2 && swapOptionsV2.maxNonBoostedPathDepth)
graphTraversalConfig['maxNonBoostedPathDepth'] = swapOptionsV2.maxNonBoostedPathDepth;

const v2Options = Object.keys(graphTraversalConfig).length > 0 ? { graphTraversalConfig } : undefined;
const swaps = await sorService.getBeetsSwaps({
...cleanArgs,
swapOptionsV2: v2Options,
swapAmount: amount,
});
return { ...swaps, __typename: 'GqlSorGetSwapsResponse' };
},
sorGetBatchSwapForTokensIn: async (parent, args, context) => {
Expand Down
5 changes: 2 additions & 3 deletions modules/sor/sor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,8 @@ export class SorService {
v2Time: number,
) {
const sorMetricsPublisher = getSorMetricsPublisher(chain);

await sorMetricsPublisher.publish(`SOR_VALID_V1`, v1.isValid ? 10 : 1);
await sorMetricsPublisher.publish(`SOR_VALID_V2`, v2.isValid ? 10 : 1);
await sorMetricsPublisher.publish(`SOR_VALID_V1`, v1.isValid ? 1 : 0);
await sorMetricsPublisher.publish(`SOR_VALID_V2`, v2.isValid ? 1 : 0);

if (!version) return;

Expand Down
18 changes: 9 additions & 9 deletions modules/sor/sorV2/sorV2.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,20 +270,20 @@ export class SorV2Service implements SwapService {
this.cache = new Cache<string, BasePool[]>();
}

public async getSwapResult({ chain, tokenIn, tokenOut, swapType, swapAmount }: GetSwapsInput): Promise<SwapResult> {
public async getSwapResult({
chain,
tokenIn,
tokenOut,
swapType,
swapAmount,
swapOptionsV2,
}: GetSwapsInput): Promise<SwapResult> {
try {
const poolsFromDb = await this.getBasePools(chain);
const tIn = await this.getToken(tokenIn as Address, chain);
const tOut = await this.getToken(tokenOut as Address, chain);
const swapKind = this.mapSwapType(swapType);
const swap = await sorGetSwapsWithPools(
tIn,
tOut,
swapKind,
swapAmount,
poolsFromDb,
// swapOptions, // I don't think we need specific swapOptions for this?
);
const swap = await sorGetSwapsWithPools(tIn, tOut, swapKind, swapAmount, poolsFromDb, swapOptionsV2);
return new SwapResultV2(swap);
} catch (err: any) {
console.error(
Expand Down
18 changes: 17 additions & 1 deletion modules/sor/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Chain } from '@prisma/client';
import { GqlCowSwapApiResponse, GqlSorSwapType, GqlSorGetSwapsResponse, GqlSorSwapOptionsInput } from '../../schema';
import {
GqlCowSwapApiResponse,
GqlSorSwapType,
GqlSorGetSwapsResponse,
GqlSorSwapOptionsInput,
GqlV2SwapOptionsInput,
} from '../../schema';
import { TokenAmount } from '@balancer/sdk';
export interface GetSwapsInput {
chain: Chain;
Expand All @@ -8,6 +14,16 @@ export interface GetSwapsInput {
swapType: GqlSorSwapType;
swapAmount: TokenAmount;
swapOptions: GqlSorSwapOptionsInput;
swapOptionsV2?: SorV2Options;
}

export interface SorV2Options {
graphTraversalConfig?: {
approxPathsToReturn?: number;
maxDepth?: number;
maxNonBoostedHopTokensInBoostedPath?: number;
maxNonBoostedPathDepth?: number;
};
}

export interface SwapResult {
Expand Down

0 comments on commit a4e1d69

Please sign in to comment.