@nradko/metric-omm-sdk-v1 0.3.0 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/README.md +98 -111
  2. package/dist/abis/MetricOmmPool.d.ts +14 -0
  3. package/dist/abis/MetricOmmPool.d.ts.map +1 -1
  4. package/dist/abis/MetricOmmPool.js +18 -0
  5. package/dist/abis/MetricOmmPool.js.map +1 -1
  6. package/dist/abis/MetricOmmPoolDataProvider.d.ts +0 -136
  7. package/dist/abis/MetricOmmPoolDataProvider.d.ts.map +1 -1
  8. package/dist/abis/MetricOmmPoolDataProvider.js +0 -177
  9. package/dist/abis/MetricOmmPoolDataProvider.js.map +1 -1
  10. package/dist/abis/MetricOmmPoolStateView.d.ts +238 -0
  11. package/dist/abis/MetricOmmPoolStateView.d.ts.map +1 -0
  12. package/dist/abis/MetricOmmPoolStateView.js +315 -0
  13. package/dist/abis/MetricOmmPoolStateView.js.map +1 -0
  14. package/dist/abis/MetricOmmSimpleRouter.d.ts +619 -0
  15. package/dist/abis/{MetricOmmPoolSwapper.d.ts.map → MetricOmmSimpleRouter.d.ts.map} +1 -1
  16. package/dist/abis/MetricOmmSimpleRouter.js +805 -0
  17. package/dist/abis/MetricOmmSimpleRouter.js.map +1 -0
  18. package/dist/abis/MetricOmmSwapQuoter.d.ts +576 -0
  19. package/dist/abis/{PriceProviderUi.d.ts.map → MetricOmmSwapQuoter.d.ts.map} +1 -1
  20. package/dist/abis/MetricOmmSwapQuoter.js +748 -0
  21. package/dist/abis/MetricOmmSwapQuoter.js.map +1 -0
  22. package/dist/abis/OracleProvider.d.ts +106 -0
  23. package/dist/abis/OracleProvider.d.ts.map +1 -0
  24. package/dist/abis/OracleProvider.js +62 -0
  25. package/dist/abis/OracleProvider.js.map +1 -0
  26. package/dist/abis/PriceProvider.d.ts +8 -2
  27. package/dist/abis/PriceProvider.d.ts.map +1 -1
  28. package/dist/abis/PriceProvider.js +9 -1
  29. package/dist/abis/PriceProvider.js.map +1 -1
  30. package/dist/abis/index.d.ts +4 -2
  31. package/dist/abis/index.d.ts.map +1 -1
  32. package/dist/abis/index.js +4 -2
  33. package/dist/abis/index.js.map +1 -1
  34. package/dist/addresses.d.ts +3 -3
  35. package/dist/addresses.d.ts.map +1 -1
  36. package/dist/addresses.js +10 -10
  37. package/dist/addresses.js.map +1 -1
  38. package/dist/constants.d.ts +4 -1
  39. package/dist/constants.d.ts.map +1 -1
  40. package/dist/constants.js +7 -2
  41. package/dist/constants.js.map +1 -1
  42. package/dist/index.d.ts +5 -3
  43. package/dist/index.d.ts.map +1 -1
  44. package/dist/index.js +8 -4
  45. package/dist/index.js.map +1 -1
  46. package/dist/oracle/index.d.ts +5 -0
  47. package/dist/oracle/index.d.ts.map +1 -0
  48. package/dist/oracle/index.js +5 -0
  49. package/dist/oracle/index.js.map +1 -0
  50. package/dist/oracle/registry.d.ts +53 -0
  51. package/dist/oracle/registry.d.ts.map +1 -0
  52. package/dist/oracle/registry.js +70 -0
  53. package/dist/oracle/registry.js.map +1 -0
  54. package/dist/quoter/index.d.ts +6 -0
  55. package/dist/quoter/index.d.ts.map +1 -0
  56. package/dist/quoter/index.js +6 -0
  57. package/dist/quoter/index.js.map +1 -0
  58. package/dist/quoter/quote.d.ts +58 -0
  59. package/dist/quoter/quote.d.ts.map +1 -0
  60. package/dist/quoter/quote.js +74 -0
  61. package/dist/quoter/quote.js.map +1 -0
  62. package/dist/quoter/quotePath.d.ts +65 -0
  63. package/dist/quoter/quotePath.d.ts.map +1 -0
  64. package/dist/quoter/quotePath.js +108 -0
  65. package/dist/quoter/quotePath.js.map +1 -0
  66. package/dist/router/index.d.ts +2 -1
  67. package/dist/router/index.d.ts.map +1 -1
  68. package/dist/router/index.js +2 -1
  69. package/dist/router/index.js.map +1 -1
  70. package/dist/router/swap.d.ts +37 -92
  71. package/dist/router/swap.d.ts.map +1 -1
  72. package/dist/router/swap.js +96 -298
  73. package/dist/router/swap.js.map +1 -1
  74. package/dist/router/swapPath.d.ts +56 -0
  75. package/dist/router/swapPath.d.ts.map +1 -0
  76. package/dist/router/swapPath.js +98 -0
  77. package/dist/router/swapPath.js.map +1 -0
  78. package/dist/swap/path.d.ts +13 -0
  79. package/dist/swap/path.d.ts.map +1 -0
  80. package/dist/swap/path.js +74 -0
  81. package/dist/swap/path.js.map +1 -0
  82. package/package.json +4 -5
  83. package/src/abis/MetricOmmPool.ts +18 -0
  84. package/src/abis/MetricOmmPoolDataProvider.ts +0 -177
  85. package/src/abis/MetricOmmPoolStateView.ts +314 -0
  86. package/src/abis/MetricOmmSimpleRouter.ts +804 -0
  87. package/src/abis/MetricOmmSwapQuoter.ts +747 -0
  88. package/src/abis/OracleProvider.ts +61 -0
  89. package/src/abis/PriceProvider.ts +9 -1
  90. package/src/abis/index.ts +4 -2
  91. package/src/addresses.ts +13 -13
  92. package/src/constants.ts +7 -2
  93. package/src/index.ts +62 -23
  94. package/src/oracle/index.ts +14 -0
  95. package/src/oracle/registry.ts +146 -0
  96. package/src/quoter/index.ts +28 -0
  97. package/src/quoter/quote.ts +193 -0
  98. package/src/quoter/quotePath.ts +240 -0
  99. package/src/router/index.ts +24 -25
  100. package/src/router/swap.ts +142 -480
  101. package/src/router/swapPath.ts +181 -0
  102. package/src/swap/path.ts +107 -0
  103. package/dist/abis/MetricOmmPoolSwapper.d.ts +0 -917
  104. package/dist/abis/MetricOmmPoolSwapper.js +0 -1185
  105. package/dist/abis/MetricOmmPoolSwapper.js.map +0 -1
  106. package/dist/abis/PriceProviderUi.d.ts +0 -433
  107. package/dist/abis/PriceProviderUi.js +0 -318
  108. package/dist/abis/PriceProviderUi.js.map +0 -1
  109. package/src/abis/MetricOmmPoolSwapper.ts +0 -1184
  110. package/src/abis/PriceProviderUi.ts +0 -317
@@ -0,0 +1,240 @@
1
+ /**
2
+ * MetricAMM SDK - MetricOmmSwapQuoter multihop quotes and shared helpers.
3
+ */
4
+
5
+ import type { Address, PublicClient, Hex } from "viem";
6
+ import { getAddressesOrThrow } from "../addresses.js";
7
+ import { MAX_UINT128, QUOTER_ABI } from "../constants.js";
8
+ import { EMPTY_EXTENSION_DATA } from "../extensions/types.js";
9
+ import { resolveSwapPath } from "../swap/path.js";
10
+
11
+ export type QuoteSwapResult = {
12
+ amountIn: bigint;
13
+ amountOut: bigint;
14
+ };
15
+
16
+ export interface QuoteLiveExactInParams {
17
+ publicClient: PublicClient;
18
+ tokenIn: Address;
19
+ tokenOut: Address;
20
+ pools: readonly Address[];
21
+ extensionDatas?: readonly Hex[];
22
+ recipient: Address;
23
+ amountIn: bigint;
24
+ quoterAddress?: Address;
25
+ }
26
+
27
+ export interface QuoteLiveExactOutParams {
28
+ publicClient: PublicClient;
29
+ tokenIn: Address;
30
+ tokenOut: Address;
31
+ pools: readonly Address[];
32
+ extensionDatas?: readonly Hex[];
33
+ recipient: Address;
34
+ amountOut: bigint;
35
+ quoterAddress?: Address;
36
+ }
37
+
38
+ export interface QuoteHypotheticalExactInputParams {
39
+ publicClient: PublicClient;
40
+ tokenIn: Address;
41
+ tokenOut: Address;
42
+ pools: readonly Address[];
43
+ extensionDatas?: readonly Hex[];
44
+ bidPricesX64: readonly bigint[];
45
+ askPricesX64: readonly bigint[];
46
+ recipient: Address;
47
+ amountIn: bigint;
48
+ quoterAddress?: Address;
49
+ }
50
+
51
+ export interface QuoteHypotheticalExactOutputParams {
52
+ publicClient: PublicClient;
53
+ tokenIn: Address;
54
+ tokenOut: Address;
55
+ pools: readonly Address[];
56
+ extensionDatas?: readonly Hex[];
57
+ bidPricesX64: readonly bigint[];
58
+ askPricesX64: readonly bigint[];
59
+ recipient: Address;
60
+ amountOut: bigint;
61
+ quoterAddress?: Address;
62
+ }
63
+
64
+ export async function resolveQuoterAddress(params: {
65
+ publicClient?: PublicClient;
66
+ quoterAddress?: Address;
67
+ }): Promise<Address> {
68
+ if (params.quoterAddress) return params.quoterAddress;
69
+ if (!params.publicClient) {
70
+ throw new Error("Either publicClient or quoterAddress must be provided");
71
+ }
72
+ const chainId = params.publicClient.chain?.id ?? (await params.publicClient.getChainId());
73
+ const addresses = getAddressesOrThrow(chainId);
74
+ if (addresses.quoter === "0x0000000000000000000000000000000000000000" || !addresses.quoter) {
75
+ throw new Error(
76
+ `Chain ${chainId} has no default quoter address. Pass a quoterAddress to override.`,
77
+ );
78
+ }
79
+ return addresses.quoter;
80
+ }
81
+
82
+ export function defaultPriceLimitX64(zeroForOne: boolean): bigint {
83
+ return zeroForOne ? 0n : MAX_UINT128;
84
+ }
85
+
86
+ export function withQuoteExtensionData(extensionData?: Hex): Hex {
87
+ return extensionData ?? EMPTY_EXTENSION_DATA;
88
+ }
89
+
90
+ export function validateHypotheticalPrices(
91
+ pools: readonly Address[],
92
+ bidPricesX64: readonly bigint[],
93
+ askPricesX64: readonly bigint[],
94
+ ): void {
95
+ if (bidPricesX64.length !== pools.length || askPricesX64.length !== pools.length) {
96
+ throw new Error("bidPricesX64 and askPricesX64 length must match pools length");
97
+ }
98
+ }
99
+
100
+ export async function simulateQuote(
101
+ publicClient: PublicClient,
102
+ quoterAddress: Address,
103
+ recipient: Address,
104
+ functionName:
105
+ | "quoteLiveExactInSingle"
106
+ | "quoteLiveExactOutSingle"
107
+ | "quoteLiveExactIn"
108
+ | "quoteLiveExactOut"
109
+ | "quoteHypotheticalExactInputSingle"
110
+ | "quoteHypotheticalExactOutputSingle"
111
+ | "quoteHypotheticalExactInput"
112
+ | "quoteHypotheticalExactOutput",
113
+ args: readonly unknown[],
114
+ ): Promise<QuoteSwapResult> {
115
+ const {
116
+ result: [amountIn, amountOut],
117
+ } = (await publicClient.simulateContract({
118
+ address: quoterAddress,
119
+ abi: QUOTER_ABI,
120
+ functionName,
121
+ args,
122
+ account: recipient,
123
+ })) as unknown as { result: [bigint, bigint] };
124
+
125
+ return { amountIn, amountOut };
126
+ }
127
+
128
+ export async function quoteLiveExactIn(params: QuoteLiveExactInParams): Promise<QuoteSwapResult> {
129
+ const quoterAddress = await resolveQuoterAddress({
130
+ publicClient: params.publicClient,
131
+ quoterAddress: params.quoterAddress,
132
+ });
133
+ const { extensionDatas, zeroForOneBitMap } = await resolveSwapPath(
134
+ params.publicClient,
135
+ params.tokenIn,
136
+ params.tokenOut,
137
+ params.pools,
138
+ params.extensionDatas,
139
+ );
140
+
141
+ return simulateQuote(params.publicClient, quoterAddress, params.recipient, "quoteLiveExactIn", [
142
+ {
143
+ pools: [...params.pools],
144
+ extensionDatas,
145
+ zeroForOneBitMap,
146
+ amountIn: params.amountIn,
147
+ },
148
+ ]);
149
+ }
150
+
151
+ export async function quoteLiveExactOut(params: QuoteLiveExactOutParams): Promise<QuoteSwapResult> {
152
+ const quoterAddress = await resolveQuoterAddress({
153
+ publicClient: params.publicClient,
154
+ quoterAddress: params.quoterAddress,
155
+ });
156
+ const { extensionDatas, zeroForOneBitMap } = await resolveSwapPath(
157
+ params.publicClient,
158
+ params.tokenIn,
159
+ params.tokenOut,
160
+ params.pools,
161
+ params.extensionDatas,
162
+ );
163
+
164
+ return simulateQuote(params.publicClient, quoterAddress, params.recipient, "quoteLiveExactOut", [
165
+ {
166
+ pools: [...params.pools],
167
+ extensionDatas,
168
+ zeroForOneBitMap,
169
+ amountOut: params.amountOut,
170
+ },
171
+ ]);
172
+ }
173
+
174
+ export async function quoteHypotheticalExactInput(
175
+ params: QuoteHypotheticalExactInputParams,
176
+ ): Promise<QuoteSwapResult> {
177
+ validateHypotheticalPrices(params.pools, params.bidPricesX64, params.askPricesX64);
178
+ const quoterAddress = await resolveQuoterAddress({
179
+ publicClient: params.publicClient,
180
+ quoterAddress: params.quoterAddress,
181
+ });
182
+ const { extensionDatas, zeroForOneBitMap } = await resolveSwapPath(
183
+ params.publicClient,
184
+ params.tokenIn,
185
+ params.tokenOut,
186
+ params.pools,
187
+ params.extensionDatas,
188
+ );
189
+
190
+ return simulateQuote(
191
+ params.publicClient,
192
+ quoterAddress,
193
+ params.recipient,
194
+ "quoteHypotheticalExactInput",
195
+ [
196
+ {
197
+ pools: [...params.pools],
198
+ extensionDatas,
199
+ zeroForOneBitMap,
200
+ amountIn: params.amountIn,
201
+ bidPricesX64: [...params.bidPricesX64],
202
+ askPricesX64: [...params.askPricesX64],
203
+ },
204
+ ],
205
+ );
206
+ }
207
+
208
+ export async function quoteHypotheticalExactOutput(
209
+ params: QuoteHypotheticalExactOutputParams,
210
+ ): Promise<QuoteSwapResult> {
211
+ validateHypotheticalPrices(params.pools, params.bidPricesX64, params.askPricesX64);
212
+ const quoterAddress = await resolveQuoterAddress({
213
+ publicClient: params.publicClient,
214
+ quoterAddress: params.quoterAddress,
215
+ });
216
+ const { extensionDatas, zeroForOneBitMap } = await resolveSwapPath(
217
+ params.publicClient,
218
+ params.tokenIn,
219
+ params.tokenOut,
220
+ params.pools,
221
+ params.extensionDatas,
222
+ );
223
+
224
+ return simulateQuote(
225
+ params.publicClient,
226
+ quoterAddress,
227
+ params.recipient,
228
+ "quoteHypotheticalExactOutput",
229
+ [
230
+ {
231
+ pools: [...params.pools],
232
+ extensionDatas,
233
+ zeroForOneBitMap,
234
+ amountOut: params.amountOut,
235
+ bidPricesX64: [...params.bidPricesX64],
236
+ askPricesX64: [...params.askPricesX64],
237
+ },
238
+ ],
239
+ );
240
+ }
@@ -3,29 +3,28 @@
3
3
  */
4
4
 
5
5
  export {
6
- quoteSwap,
7
- buildArgsForSwapExactInput,
8
- buildArgsAndExpectedAmountsForSwapExactInput,
9
- quoteExpectedAmountsForSwapExactInput,
10
- buildArgsForSwapExactOutput,
11
- buildArgsAndExpectedAmountsForSwapExactOutput,
12
- quoteExpectedAmountsForSwapExactOutput,
13
- encodeSwapExactInputCalldata,
14
- encodeSwapExactOutputCalldata,
15
- prepareSwapExactInputCalldata,
16
- prepareSwapExactOutputCalldata,
17
- prepareSwapExactInputNativeForTokensCalldata,
18
- type PreparedSwapTransaction,
19
- type BuildSwapWithFactoryParams,
20
- type BuildSwapPrices,
21
- type BuildSwapExactInputParams,
22
- type QuoteExpectedAmountsForSwapExactInputParams,
23
- type SwapExactInputParams,
24
- type BuildSwapExactOutputParams,
25
- type QuoteExpectedAmountsForSwapExactOutputParams,
26
- type SwapExactOutputParams,
27
- type SwapExactInputArgs,
28
- type SwapExactOutputArgs,
29
- type QuoteSwapParams,
30
- type QuoteSwapResult,
6
+ buildExactInputParams,
7
+ buildExactOutputParams,
8
+ encodeExactInputCalldata,
9
+ encodeExactOutputCalldata,
10
+ encodeZeroForOneBitMap,
11
+ prepareExactInputCalldata,
12
+ prepareExactOutputCalldata,
13
+ type ExactInputParams,
14
+ type ExactOutputParams,
15
+ type BuildExactInputParams,
16
+ type BuildExactOutputParams,
17
+ } from "./swapPath.js";
18
+
19
+ export {
20
+ buildExactInputSingleParams,
21
+ buildExactOutputSingleParams,
22
+ encodeExactInputSingleCalldata,
23
+ encodeExactOutputSingleCalldata,
24
+ prepareExactInputSingleCalldata,
25
+ prepareExactOutputSingleCalldata,
26
+ type ExactInputSingleParams,
27
+ type ExactOutputSingleParams,
28
+ type BuildExactInputSingleParams,
29
+ type BuildExactOutputSingleParams,
31
30
  } from "./swap.js";