-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.ts
136 lines (131 loc) · 3.83 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { BalancerApiClient } from '../../client';
import { ChainId } from '@/utils';
import { Address } from 'viem';
import { SwapKind } from '@/types';
import { TokenAmount } from '@/entities';
import { Path } from '@/entities/swap/paths/types';
export type SorInput = {
chainId: ChainId;
tokenIn: Address;
tokenOut: Address;
swapKind: SwapKind;
swapAmount: TokenAmount; // API expects input in human readable form
useProtocolVersion?: 2 | 3; // If not specified API will return best
};
export class SorSwapPaths {
readonly sorSwapPathQuery = `
query MyQuery($chain: GqlChain!, $swapType: GqlSorSwapType!, $swapAmount: AmountHumanReadable!, $tokenIn: String!, $tokenOut: String!) {
sorGetSwapPaths(
swapAmount: $swapAmount
chain: $chain
swapType: $swapType
tokenIn: $tokenIn
tokenOut: $tokenOut
) {
tokenInAmount
tokenOutAmount
returnAmount
priceImpact {
error
priceImpact
}
swapAmount
paths {
inputAmountRaw
outputAmountRaw
pools
protocolVersion
tokens {
address
decimals
}
}
}
}
`;
readonly sorSwapPathQueryWithVersion = `
query MyQuery($chain: GqlChain!, $swapType: GqlSorSwapType!, $swapAmount: AmountHumanReadable!, $tokenIn: String!, $tokenOut: String!, $useProtocolVersion: Int!) {
sorGetSwapPaths(
swapAmount: $swapAmount
chain: $chain
swapType: $swapType
tokenIn: $tokenIn
tokenOut: $tokenOut
useProtocolVersion: $useProtocolVersion
) {
tokenInAmount
tokenOutAmount
returnAmount
priceImpact {
error
priceImpact
}
swapAmount
paths {
inputAmountRaw
outputAmountRaw
pools
protocolVersion
tokens {
address
decimals
}
}
}
}
`;
constructor(private readonly balancerApiClient: BalancerApiClient) {}
async fetchSorSwapPaths(sorInput: SorInput): Promise<Path[]> {
const variables = {
chain: this.mapGqlChain(sorInput.chainId),
swapAmount: sorInput.swapAmount.toSignificant(
sorInput.swapAmount.token.decimals,
), // Must use human scale
swapType:
sorInput.swapKind === SwapKind.GivenIn
? 'EXACT_IN'
: 'EXACT_OUT',
tokenIn: sorInput.tokenIn,
tokenOut: sorInput.tokenOut,
};
const { data } = await this.balancerApiClient.fetch({
query: sorInput.useProtocolVersion
? this.sorSwapPathQueryWithVersion
: this.sorSwapPathQuery,
variables: sorInput.useProtocolVersion
? {
...variables,
useProtocolVersion: sorInput.useProtocolVersion,
}
: variables,
});
const poolGetPool: Path[] = data.sorGetSwapPaths.paths;
return poolGetPool;
}
private mapGqlChain(chainId: ChainId): string {
switch (chainId) {
case ChainId.ARBITRUM_ONE:
return 'ARBITRUM';
case ChainId.AVALANCHE:
return 'AVALANCHE';
case ChainId.FANTOM:
return 'FANTOM';
case ChainId.GNOSIS_CHAIN:
return 'GNOSIS';
case ChainId.MAINNET:
return 'MAINNET';
case ChainId.OPTIMISM:
return 'OPTIMISM';
case ChainId.POLYGON:
return 'POLYGON';
case ChainId.ZKEVM:
return 'ZKEVM';
case ChainId.SEPOLIA:
return 'SEPOLIA';
case ChainId.BASE:
return 'BASE';
default:
throw Error(`Unsupported API chain: ${chainId}`);
}
}
}