Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/sor update #190

Merged
merged 12 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/network/mainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const data: NetworkData = {
rpcUrl:
env.INFURA_API_KEY && (env.DEPLOYMENT_ENV as DeploymentEnv) === 'main'
? `https://mainnet.infura.io/v3/${env.INFURA_API_KEY}`
: 'https://rpc.eth.gateway.fm',
: 'https://eth.llamarpc.com',
rpcMaxBlockRange: 700,
protocolToken: 'bal',
bal: {
Expand Down
187 changes: 170 additions & 17 deletions modules/sor/sor.gql
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,29 @@ extend type Query {
Get swap quote from the SOR, queries both the old and new SOR
"""
sorGetSwaps(
chain: GqlChain
"""
The Chain to query
"""
chain: GqlChain!
"""
Token address of the tokenIn
"""
tokenIn: String!
"""
Token address of the tokenOut
"""
tokenOut: String!
"""
SwapType either exact_in or exact_out (also givenIn or givenOut)
"""
swapType: GqlSorSwapType!
"""
The amount to swap, in human form.
"""
swapAmount: BigDecimal! #expected in human readable form
"""
Options for the swap
"""
swapOptions: GqlSorSwapOptionsInput!
): GqlSorGetSwapsResponse!
"""
Expand All @@ -22,15 +40,36 @@ extend type Query {
Token address of the tokenIn
"""
tokenIn: String!
"""
Token address of the tokenOut
"""
tokenOut: String!
"""
SwapType either exact_in or exact_out (also givenIn or givenOut)
"""
swapType: GqlSorSwapType!
"""
The amount to swap, in human form.
"""
swapAmount: BigDecimal! #expected in human readable form
queryBatchSwap: Boolean #run queryBatchSwap to update with onchain values, default: true
useVaultVersion: Int #defaults that it gets the best swap from v2 and v3, can force to use only one vault version
"""
Whether to run queryBatchSwap to update with onchain values, default: true
"""
queryBatchSwap: Boolean
"""
Which vault version to use. If none provided, will chose the better return from either version
"""
useVaultVersion: Int
): GqlSorGetSwapPaths!
}

"""
The swap paths for a swap
"""
type GqlSorGetSwapPaths {
"""
The version of the vault these paths are from
"""
vaultVersion: Int!
"""
The token address of the tokenIn provided
Expand All @@ -40,26 +79,87 @@ type GqlSorGetSwapPaths {
The token address of the tokenOut provided
"""
tokenOut: String!
"""
The swapType that was provided, exact_in vs exact_out (givenIn vs givenOut)
"""
swapType: GqlSorSwapType!
"""
Swaps as needed for the vault swap input to execute the swap
"""
swaps: [GqlSorSwap!]! #used by cowswap
"""
All token addresses (or assets) as needed for the vault swap input to execute the swap
"""
tokenAddresses: [String!]! #used by cowswap
"""
The found paths as needed as input for the b-sdk to execute the swap
"""
paths: [GqlSorPath!]! #used by b-sdk
"""
The amount of tokenIn in human form
"""
tokenInAmount: AmountHumanReadable!
"""
The amount of tokenOut in human form
"""
tokenOutAmount: AmountHumanReadable!
"""
The swap amount in human form. Swap amount is either tokenInAmount (if swapType is exactIn) or tokenOutAmount (if swapType is exactOut)
"""
swapAmount: AmountHumanReadable!
swapAmountScaled: BigDecimal!
"""
The swap amount in a raw form
"""
swapAmountRaw: BigDecimal!
"""
The return amount in human form. Return amount is either tokenOutAmount (if swapType is exactIn) or tokenInAmount (if swapType is exactOut)
"""
returnAmount: AmountHumanReadable!
returnAmountScaled: BigDecimal!
"""
The return amount in a raw form
"""
returnAmountRaw: BigDecimal!
"""
The price of tokenOut in tokenIn.
"""
effectivePrice: AmountHumanReadable!
"""
The price of tokenIn in tokenOut.
"""
effectivePriceReversed: AmountHumanReadable!
"""
The swap routes including pool information. Used to display by the UI
"""
routes: [GqlSorSwapRoute!]!
"""
Price impact in percent. 0.01 -> 0.01%
"""
priceImpact: AmountHumanReadable!
}

"""
A path of a swap. A swap can have multiple paths. Used as input to execute the swap via b-sdk
"""
type GqlSorPath {
"""
Vault version of this path.
"""
vaultVersion: Int!
pools: [String]! #list of pool Ids
"""
A sorted list of pool ids that are used in this path
"""
pools: [String]!
"""
A sorted list of tokens that are ussed in this path
"""
tokens: [Token]!
"""
Output amount of this path in scaled form
"""
outputAmountRaw: String!
"""
Input amount of this path in scaled form
"""
inputAmountRaw: String!
}

Expand Down Expand Up @@ -133,35 +233,88 @@ type GqlSorGetSwapsResponse {
priceImpact: AmountHumanReadable!
}

#used by cowswap
"""
A single swap step as used for input to the vault to execute a swap
"""
type GqlSorSwap {
"""
Pool id used in this swap step
"""
poolId: String!
"""
Index of the asset used in the tokenAddress array.
"""
assetInIndex: Int!
"""
Index of the asset used in the tokenAddress array.
"""
assetOutIndex: Int!
"""
Amount to be swapped in this step. 0 for chained swap.
"""
amount: String!
"""
UserData used in this swap, generally uses defaults.
"""
userData: String!
}

"""
The swap routes including pool information. Used to display by the UI
"""
type GqlSorSwapRoute {
"""
Address of the tokenIn
"""
tokenIn: String!
tokenInAmount: BigDecimal!
"""
Amount of the tokenIn in human form
"""
tokenInAmount: AmountHumanReadable!
"""
Address of the tokenOut
"""
tokenOut: String!
tokenOutAmount: BigDecimal!
"""
Amount of the tokenOut in human form
"""
tokenOutAmount: AmountHumanReadable!
"""
Share of this route of the total swap
"""
share: Float!
"""
The hops this route takes
"""
hops: [GqlSorSwapRouteHop!]!
}

"""
A hop of a route. A route can have many hops meaning it traverses more than one pool.
"""
type GqlSorSwapRouteHop {
"""
Address of the tokenIn
"""
tokenIn: String!
tokenInAmount: BigDecimal!
"""
Amount of the tokenIn in human form
"""
tokenInAmount: AmountHumanReadable!
"""
Address of the tokenOut
"""
tokenOut: String!
tokenOutAmount: BigDecimal!
"""
Amount of the tokenOut in human form
"""
tokenOutAmount: AmountHumanReadable!
"""
The pool id of this hop.
"""
poolId: String!
"""
The pool entity of this hop.
"""
pool: GqlPoolMinimal!
}

type GqlSorGetBatchSwapForTokensInResponse {
tokenOutAmount: AmountHumanReadable!
swaps: [GqlSorSwap!]!
assets: [String!]!
}
10 changes: 6 additions & 4 deletions modules/sor/sor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
GqlSorGetSwapPaths,
} from '../../schema';
import { sorV1BeetsService } from './sorV1Beets/sorV1Beets.service';
import { sorV2Service } from './sorV2/sorV2.service';
import { sorV2Service } from './sorV2/sorPathService';
import { GetSwapsInput, SwapResult } from './types';
import * as Sentry from '@sentry/node';
import { Chain } from '@prisma/client';
Expand Down Expand Up @@ -74,7 +74,7 @@ export class SorService {
// args.swapAmount is HumanScale
const amount = await getTokenAmountHuman(amountToken, args.swapAmount, args.chain!);

const swap = await this.getComparingSwap({
const swapResult = await this.getComparingSwap({
chain: args.chain!,
swapAmount: amount,
swapOptions: args.swapOptions,
Expand All @@ -83,11 +83,13 @@ export class SorService {
tokenOut: tokenOut,
});

if (!swap) return emptyResponse;
if (!swapResult) return emptyResponse;

try {
// Updates with latest onchain data before returning
return swap.getSorSwapResponse(args.swapOptions.queryBatchSwap ? args.swapOptions.queryBatchSwap : false);
return swapResult.getSorSwapResponse(
args.swapOptions.queryBatchSwap ? args.swapOptions.queryBatchSwap : false,
);
} catch (err) {
console.log(`Error Retrieving QuerySwap`, err);
return emptyResponse;
Expand Down
4 changes: 2 additions & 2 deletions modules/sor/sorV1Beets/balancer-sor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { AllNetworkConfigsKeyedOnChain } from '../../network/network-config';
import { DeploymentEnv } from '../../network/network-config-types';
import _ from 'lodash';
import { SwapInfoRoute } from '@balancer-labs/sor';
import { NATIVE_ADDRESS, ZERO_ADDRESS } from '@balancer/sdk';
import { ZERO_ADDRESS } from '@balancer/sdk';

interface GetSwapsInput {
tokenIn: string;
Expand Down Expand Up @@ -228,7 +228,7 @@ export class BalancerSorService {
private getTokenDecimals(tokenAddress: string, tokens: PrismaToken[]): number {
if (
tokenAddress === ZERO_ADDRESS ||
tokenAddress === NATIVE_ADDRESS ||
tokenAddress === '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' ||
tokenAddress === '0x0000000000000000000000000000000000001010'
) {
return 18;
Expand Down
Loading
Loading