@bitflowlabs/core-sdk 2.2.0 → 2.3.1-beta.0

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 (41) hide show
  1. package/README.md +12 -3
  2. package/dist/src/BitflowSDK.js +4 -1
  3. package/dist/src/BitflowSDK.js.map +1 -1
  4. package/dist/src/config.d.ts +1 -1
  5. package/dist/src/config.js +16 -12
  6. package/dist/src/config.js.map +1 -1
  7. package/dist/src/helpers/callReadOnlyHelper.js +15 -0
  8. package/dist/src/helpers/callReadOnlyHelper.js.map +1 -1
  9. package/dist/src/helpers/fetchContractInterfaceHelper.js +8 -1
  10. package/dist/src/helpers/fetchContractInterfaceHelper.js.map +1 -1
  11. package/dist/src/helpers/fetchDataHelper.js +9 -2
  12. package/dist/src/helpers/fetchDataHelper.js.map +1 -1
  13. package/dist/src/helpers/fetchPossibleSwap.js +8 -2
  14. package/dist/src/helpers/fetchPossibleSwap.js.map +1 -1
  15. package/dist/src/types.d.ts +1 -0
  16. package/dist/test-api-keys.d.ts +2 -0
  17. package/dist/test-api-keys.js +82 -0
  18. package/dist/test-api-keys.js.map +1 -0
  19. package/package.json +5 -3
  20. package/src/BitflowSDK.ts +0 -1241
  21. package/src/config.ts +0 -37
  22. package/src/helpers/callGetSwapParams.ts +0 -122
  23. package/src/helpers/callReadOnlyHelper.ts +0 -474
  24. package/src/helpers/callSwapHelper.ts +0 -67
  25. package/src/helpers/constructFunctionArgs.ts +0 -24
  26. package/src/helpers/convertValuesHelper.ts +0 -220
  27. package/src/helpers/fetchContractInterfaceHelper.ts +0 -19
  28. package/src/helpers/fetchDataHelper.ts +0 -80
  29. package/src/helpers/fetchPossibleSwap.ts +0 -32
  30. package/src/helpers/getContractInterfaceAndFunction.ts +0 -20
  31. package/src/helpers/getFunctionArgs.ts +0 -12
  32. package/src/helpers/getTokenDecimalsHelper.ts +0 -33
  33. package/src/helpers/getTokenNameHelper.ts +0 -26
  34. package/src/helpers/handleResultHelper.ts +0 -84
  35. package/src/helpers/newPostConditionsHelper.ts +0 -172
  36. package/src/helpers/postConditionsHelper.ts +0 -298
  37. package/src/index.ts +0 -3
  38. package/src/keeper/keeperAPI.ts +0 -365
  39. package/src/keeper/types.ts +0 -310
  40. package/src/test/testMethods.ts +0 -246
  41. package/src/types.ts +0 -167
package/src/config.ts DELETED
@@ -1,37 +0,0 @@
1
- import { BitflowSDKConfig } from "./types";
2
-
3
- export const configs: BitflowSDKConfig = {
4
- BITFLOW_API_HOST:
5
- (process.env.NEXT_PUBLIC_BITFLOW_API_HOST ??
6
- process.env.BITFLOW_API_HOST) ||
7
- "",
8
- BITFLOW_API_KEY:
9
- (process.env.NEXT_PUBLIC_BITFLOW_API_KEY ?? process.env.BITFLOW_API_KEY) ||
10
- "",
11
- BITFLOW_PROVIDER_ADDRESS:
12
- (process.env.NEXT_PUBLIC_BITFLOW_PROVIDER_ADDRESS ??
13
- process.env.BITFLOW_PROVIDER_ADDRESS) ||
14
- "",
15
- READONLY_CALL_API_HOST:
16
- (process.env.NEXT_PUBLIC_READONLY_CALL_API_HOST ??
17
- process.env.READONLY_CALL_API_HOST) ||
18
- "",
19
- KEEPER_API_KEY:
20
- (process.env.NEXT_PUBLIC_KEEPER_API_KEY ?? process.env.KEEPER_API_KEY) ||
21
- "",
22
- KEEPER_API_HOST:
23
- (process.env.NEXT_PUBLIC_KEEPER_API_HOST ?? process.env.KEEPER_API_HOST) ||
24
- "",
25
- };
26
-
27
- export function validateConfig() {
28
- const optionalEnvVars: (keyof BitflowSDKConfig)[] = ['BITFLOW_PROVIDER_ADDRESS'];
29
- const requiredEnvVars = (Object.keys(configs) as (keyof BitflowSDKConfig)[])
30
- .filter((key) => !optionalEnvVars.includes(key));
31
-
32
- for (const envVar of requiredEnvVars) {
33
- if (!configs[envVar]) {
34
- throw new Error(`Missing required configuration: ${envVar}`);
35
- }
36
- }
37
- }
@@ -1,122 +0,0 @@
1
- import { createSwapPostConditions } from "./postConditionsHelper";
2
- import {
3
- SwapContext,
4
- SwapDataParamsAndPostConditions,
5
- SwapExecutionData,
6
- } from "../types";
7
- import { constructFunctionArgs } from "./constructFunctionArgs";
8
- import { getContractInterfaceAndFunction } from "./getContractInterfaceAndFunction";
9
- import { stringifyWithBigInt } from "./callReadOnlyHelper";
10
-
11
- const applySlippage = (
12
- value: string | number | bigint,
13
- slippageTolerance: number
14
- ): bigint => {
15
- const bigIntValue = BigInt(value);
16
- const slippageFactor = BigInt(Math.floor((1 - slippageTolerance) * 10000));
17
- return (bigIntValue * slippageFactor) / BigInt(10000);
18
- };
19
-
20
- export const executeGetParams = async (
21
- swapExecutionData: SwapExecutionData,
22
- senderAddress: string,
23
- slippageTolerance: number,
24
- context: SwapContext
25
- ): Promise<SwapDataParamsAndPostConditions> => {
26
- const network = context.network;
27
- const { route, amount, tokenXDecimals, tokenYDecimals } = swapExecutionData;
28
- const [contractAddress, contractName] = route.swapData.contract.split(".");
29
-
30
- try {
31
- const contractKey = `${contractAddress}.${contractName}`;
32
- let contractInterface = context.contractInterfaces[contractKey];
33
- let functionArgsDefinition =
34
- context.functionArgs[contractKey]?.[route.swapData.function];
35
-
36
- if (!contractInterface || !functionArgsDefinition) {
37
- const result = await getContractInterfaceAndFunction(
38
- contractAddress,
39
- contractName,
40
- route.swapData.function
41
- );
42
-
43
- if (!contractInterface) {
44
- contractInterface = result.contractInterface;
45
- context.contractInterfaces[contractKey] = contractInterface;
46
- }
47
-
48
- if (!functionArgsDefinition) {
49
- functionArgsDefinition = result.functionArgs;
50
- if (!context.functionArgs[contractKey]) {
51
- context.functionArgs[contractKey] = {};
52
- }
53
- context.functionArgs[contractKey][route.swapData.function] =
54
- functionArgsDefinition;
55
- }
56
- }
57
-
58
- const swapParameters = { ...route.swapData.parameters };
59
-
60
- // Apply slippage to various parameters
61
- const slippageParams = [
62
- "min-received",
63
- "min-dy",
64
- "min-dx",
65
- "min-dz",
66
- "min-dw",
67
- "amt-out",
68
- "amt-out-min",
69
- "min-x-amount",
70
- "min-dv",
71
- "min-y-amount",
72
- ];
73
-
74
- slippageParams.forEach((param) => {
75
- if (swapParameters[param]) {
76
- swapParameters[param] = applySlippage(
77
- swapParameters[param],
78
- slippageTolerance
79
- );
80
- }
81
- });
82
-
83
- // Convert input amounts to BigInt
84
- const inputParams = [
85
- "amount",
86
- "dx",
87
- "amt-in",
88
- "amt-in-max",
89
- "y-amount",
90
- "dy",
91
- ];
92
- inputParams.forEach((param) => {
93
- if (param in swapParameters) {
94
- swapParameters[param] = BigInt(swapParameters[param]);
95
- }
96
- });
97
-
98
- const functionArgs = constructFunctionArgs(
99
- swapParameters,
100
- functionArgsDefinition
101
- );
102
-
103
- const postConditions = await createSwapPostConditions(
104
- swapParameters,
105
- route.postConditions,
106
- senderAddress,
107
- tokenXDecimals,
108
- tokenYDecimals
109
- );
110
-
111
- return {
112
- functionArgs,
113
- postConditions,
114
- contractAddress,
115
- contractName,
116
- functionName: route.swapData.function,
117
- };
118
- } catch (error) {
119
- console.error("Error getting swap parameters:", error);
120
- throw error;
121
- }
122
- };
@@ -1,474 +0,0 @@
1
- import { fetchCallReadOnlyFunction } from '@stacks/transactions';
2
- import { SwapContext } from '../types';
3
- import { getTokenDecimals } from './getTokenDecimalsHelper';
4
- import { constructFunctionArgs } from './constructFunctionArgs';
5
- import { configs } from '../config';
6
- import { getContractInterfaceAndFunction } from './getContractInterfaceAndFunction';
7
- import { handleResult } from './handleResultHelper';
8
-
9
- export const stringifyWithBigInt = (obj: any): string => {
10
- return JSON.stringify(obj, (_, v) =>
11
- typeof v === 'bigint' ? v.toString() : v
12
- );
13
- };
14
-
15
- export const callReadOnlyFunctionHelper = async (
16
- contractDeployer: string,
17
- contractName: string,
18
- functionName: string,
19
- parameters: any,
20
- senderAddress: string,
21
- tokenXId: string,
22
- tokenYId: string,
23
- swapData: any,
24
- context: SwapContext
25
- ): Promise<{
26
- convertedResult: number;
27
- rawResult: number;
28
- tokenXDecimals: number;
29
- tokenYDecimals: number;
30
- }> => {
31
- const network = 'mainnet';
32
-
33
- const client = {
34
- baseUrl: configs.READONLY_CALL_API_HOST,
35
- };
36
-
37
- try {
38
- const contractKey = `${contractDeployer}.${contractName}`;
39
- let contractInterface = context.contractInterfaces[contractKey];
40
- let functionArgsDefinition =
41
- context.functionArgs[contractKey]?.[functionName];
42
-
43
- if (!contractInterface || !functionArgsDefinition) {
44
- const result = await getContractInterfaceAndFunction(
45
- contractDeployer,
46
- contractName,
47
- functionName
48
- );
49
-
50
- if (!contractInterface) {
51
- contractInterface = result.contractInterface;
52
- context.contractInterfaces[contractKey] = contractInterface;
53
- }
54
-
55
- if (!functionArgsDefinition) {
56
- functionArgsDefinition = result.functionArgs;
57
- if (!context.functionArgs[contractKey]) {
58
- context.functionArgs[contractKey] = {};
59
- }
60
- context.functionArgs[contractKey][functionName] =
61
- functionArgsDefinition;
62
- }
63
- }
64
-
65
- const tokenXDecimals = getTokenDecimals(tokenXId, context);
66
- let tokenXMatchingDecimal:
67
- | { tokenContract: string; tokenDecimals: number }
68
- | undefined;
69
-
70
- // Attempt to find matching token decimals from parameters
71
- for (const key in parameters) {
72
- if (typeof parameters[key] === 'object') {
73
- for (const subKey in parameters[key]) {
74
- const contractValue = parameters[key][subKey];
75
- tokenXMatchingDecimal = tokenXDecimals.find(
76
- (d) => d.tokenContract === contractValue
77
- );
78
- if (tokenXMatchingDecimal) break;
79
- }
80
- } else if (typeof parameters[key] === 'string') {
81
- tokenXMatchingDecimal = tokenXDecimals.find(
82
- (d) => d.tokenContract === parameters[key]
83
- );
84
- }
85
- if (tokenXMatchingDecimal) break;
86
- }
87
-
88
- // If not found yet, also check swapData parameters
89
- if (!tokenXMatchingDecimal && swapData && swapData.parameters) {
90
- const swapParameters = swapData.parameters;
91
- for (const key in swapParameters) {
92
- if (typeof swapParameters[key] === 'object') {
93
- for (const subKey in swapParameters[key]) {
94
- const contractValue = swapParameters[key][subKey];
95
- tokenXMatchingDecimal = tokenXDecimals.find(
96
- (d) => d.tokenContract === contractValue
97
- );
98
- if (tokenXMatchingDecimal) break;
99
- }
100
- } else if (typeof swapParameters[key] === 'string') {
101
- tokenXMatchingDecimal = tokenXDecimals.find(
102
- (d) => d.tokenContract === swapParameters[key]
103
- );
104
- }
105
- if (tokenXMatchingDecimal) break;
106
- }
107
- }
108
-
109
- // Scale parameters if we find a matching decimal for tokenX
110
- if (tokenXMatchingDecimal) {
111
- const scaleAmount = (amount: number | string) => {
112
- const scaledAmount =
113
- parseFloat(amount.toString()) *
114
- 10 ** tokenXMatchingDecimal!.tokenDecimals;
115
- return BigInt(Math.floor(scaledAmount));
116
- };
117
-
118
- if (parameters.dx !== undefined && parameters.dx !== null) {
119
- parameters.dx = scaleAmount(parameters.dx);
120
- } else if (
121
- parameters.amount !== undefined &&
122
- parameters.amount !== null
123
- ) {
124
- parameters.amount = scaleAmount(parameters.amount);
125
- } else if (
126
- parameters['amt-in'] !== undefined &&
127
- parameters['amt-in'] !== null
128
- ) {
129
- parameters['amt-in'] = scaleAmount(parameters['amt-in']);
130
- } else if (
131
- parameters['amt-in-max'] !== undefined &&
132
- parameters['amt-in-max'] !== null
133
- ) {
134
- parameters['amt-in-max'] = scaleAmount(parameters['amt-in-max']);
135
- } else if (
136
- parameters['y-amount'] !== undefined &&
137
- parameters['y-amount'] !== null
138
- ) {
139
- parameters['y-amount'] = scaleAmount(parameters['y-amount']);
140
- parameters['x-amount'] = scaleAmount(parameters['x-amount']);
141
- } else if (
142
- parameters['x-amount'] !== undefined &&
143
- parameters['x-amount'] !== null
144
- ) {
145
- parameters['x-amount'] = scaleAmount(parameters['x-amount']);
146
- } else if (parameters.dy !== undefined && parameters.dy !== null) {
147
- parameters.dy = scaleAmount(parameters.dy);
148
- }
149
- } else {
150
- console.warn(`No matching decimal found for tokenX: ${tokenXId}`);
151
- }
152
-
153
- const functionArgs = constructFunctionArgs(
154
- parameters,
155
- functionArgsDefinition
156
- );
157
-
158
- const result = await fetchCallReadOnlyFunction({
159
- contractAddress: contractDeployer,
160
- contractName,
161
- functionName,
162
- functionArgs,
163
- network,
164
- client,
165
- senderAddress: senderAddress,
166
- });
167
-
168
- const { rawResult, convertedResult } = handleResult(result);
169
-
170
- try {
171
- const tokenYDecimals = getTokenDecimals(tokenYId, context);
172
-
173
- let tokenYMatchingDecimal:
174
- | { tokenContract: string; tokenDecimals: number }
175
- | undefined;
176
-
177
- for (const key in parameters) {
178
- if (typeof parameters[key] === 'object') {
179
- for (const subKey in parameters[key]) {
180
- const contractValue = parameters[key][subKey];
181
- tokenYMatchingDecimal = tokenYDecimals.find(
182
- (d) => d.tokenContract === contractValue
183
- );
184
- if (tokenYMatchingDecimal) break;
185
- }
186
- } else if (typeof parameters[key] === 'string') {
187
- tokenYMatchingDecimal = tokenYDecimals.find(
188
- (d) => d.tokenContract === parameters[key]
189
- );
190
- }
191
- if (tokenYMatchingDecimal) break;
192
- }
193
-
194
- if (!tokenYMatchingDecimal && swapData && swapData.parameters) {
195
- const swapParameters = swapData.parameters;
196
- for (const key in swapParameters) {
197
- if (typeof swapParameters[key] === 'object') {
198
- for (const subKey in swapParameters[key]) {
199
- const contractValue = swapParameters[key][subKey];
200
- tokenYMatchingDecimal = tokenYDecimals.find(
201
- (d) => d.tokenContract === contractValue
202
- );
203
- if (tokenYMatchingDecimal) break;
204
- }
205
- } else if (typeof swapParameters[key] === 'string') {
206
- tokenYMatchingDecimal = tokenYDecimals.find(
207
- (d) => d.tokenContract === swapParameters[key]
208
- );
209
- }
210
- if (tokenYMatchingDecimal) break;
211
- }
212
- }
213
-
214
- if (tokenYMatchingDecimal && typeof convertedResult === 'number') {
215
- const adjustedResult =
216
- convertedResult / 10 ** tokenYMatchingDecimal.tokenDecimals;
217
- return {
218
- convertedResult: adjustedResult,
219
- rawResult,
220
- tokenXDecimals: tokenXMatchingDecimal?.tokenDecimals || 0,
221
- tokenYDecimals: tokenYMatchingDecimal.tokenDecimals,
222
- };
223
- } else {
224
- console.warn(
225
- `No matching decimal found for tokenY: ${tokenYId} or result is not a number`
226
- );
227
- return {
228
- convertedResult,
229
- rawResult,
230
- tokenXDecimals: tokenXMatchingDecimal?.tokenDecimals || 0,
231
- tokenYDecimals: 0,
232
- };
233
- }
234
- } catch (error) {
235
- console.warn(`Couldn't apply decimal conversion: ${error}`);
236
- console.warn('Using raw result without decimal conversion');
237
- return {
238
- convertedResult,
239
- rawResult,
240
- tokenXDecimals: tokenXMatchingDecimal?.tokenDecimals || 0,
241
- tokenYDecimals: 0,
242
- };
243
- }
244
- } catch (error) {
245
- console.error(`Error calling read-only function ${functionName}:`, error);
246
- throw error;
247
- }
248
- };
249
-
250
- export const callReadOnlyFunctionHelperWithoutScaling = async (
251
- contractDeployer: string,
252
- contractName: string,
253
- functionName: string,
254
- parameters: any,
255
- senderAddress: string,
256
- tokenXId: string,
257
- tokenYId: string,
258
- swapData: any,
259
- context: SwapContext
260
- ): Promise<{
261
- convertedResult: number;
262
- rawResult: number;
263
- tokenXDecimals: number;
264
- tokenYDecimals: number;
265
- }> => {
266
- const network = 'mainnet';
267
-
268
- const client = {
269
- baseUrl: configs.READONLY_CALL_API_HOST,
270
- };
271
-
272
- try {
273
- const contractKey = `${contractDeployer}.${contractName}`;
274
- let contractInterface = context.contractInterfaces[contractKey];
275
- let functionArgsDefinition =
276
- context.functionArgs[contractKey]?.[functionName];
277
-
278
- if (!contractInterface || !functionArgsDefinition) {
279
- const result = await getContractInterfaceAndFunction(
280
- contractDeployer,
281
- contractName,
282
- functionName
283
- );
284
-
285
- if (!contractInterface) {
286
- contractInterface = result.contractInterface;
287
- context.contractInterfaces[contractKey] = contractInterface;
288
- }
289
-
290
- if (!functionArgsDefinition) {
291
- functionArgsDefinition = result.functionArgs;
292
- if (!context.functionArgs[contractKey]) {
293
- context.functionArgs[contractKey] = {};
294
- }
295
- context.functionArgs[contractKey][functionName] =
296
- functionArgsDefinition;
297
- }
298
- }
299
-
300
- const tokenXDecimals = getTokenDecimals(tokenXId, context);
301
- let tokenXMatchingDecimal:
302
- | { tokenContract: string; tokenDecimals: number }
303
- | undefined;
304
-
305
- // Attempt to find matching token decimals from parameters
306
- for (const key in parameters) {
307
- if (typeof parameters[key] === 'object') {
308
- for (const subKey in parameters[key]) {
309
- const contractValue = parameters[key][subKey];
310
- tokenXMatchingDecimal = tokenXDecimals.find(
311
- (d) => d.tokenContract === contractValue
312
- );
313
- if (tokenXMatchingDecimal) break;
314
- }
315
- } else if (typeof parameters[key] === 'string') {
316
- tokenXMatchingDecimal = tokenXDecimals.find(
317
- (d) => d.tokenContract === parameters[key]
318
- );
319
- }
320
- if (tokenXMatchingDecimal) break;
321
- }
322
-
323
- // If not found yet, also check swapData parameters
324
- if (!tokenXMatchingDecimal && swapData && swapData.parameters) {
325
- const swapParameters = swapData.parameters;
326
- for (const key in swapParameters) {
327
- if (typeof swapParameters[key] === 'object') {
328
- for (const subKey in swapParameters[key]) {
329
- const contractValue = swapParameters[key][subKey];
330
- tokenXMatchingDecimal = tokenXDecimals.find(
331
- (d) => d.tokenContract === contractValue
332
- );
333
- if (tokenXMatchingDecimal) break;
334
- }
335
- } else if (typeof swapParameters[key] === 'string') {
336
- tokenXMatchingDecimal = tokenXDecimals.find(
337
- (d) => d.tokenContract === swapParameters[key]
338
- );
339
- }
340
- if (tokenXMatchingDecimal) break;
341
- }
342
- }
343
-
344
- // Skip scaling and use raw input values
345
- // Just convert any numerical values to BigInt to maintain type compatibility
346
- if (parameters.dx !== undefined && parameters.dx !== null) {
347
- parameters.dx = BigInt(Math.floor(parameters.dx));
348
- } else if (parameters.amount !== undefined && parameters.amount !== null) {
349
- parameters.amount = BigInt(Math.floor(parameters.amount));
350
- } else if (
351
- parameters['amt-in'] !== undefined &&
352
- parameters['amt-in'] !== null
353
- ) {
354
- parameters['amt-in'] = BigInt(Math.floor(parameters['amt-in']));
355
- } else if (
356
- parameters['amt-in-max'] !== undefined &&
357
- parameters['amt-in-max'] !== null
358
- ) {
359
- parameters['amt-in-max'] = BigInt(Math.floor(parameters['amt-in-max']));
360
- } else if (
361
- parameters['y-amount'] !== undefined &&
362
- parameters['y-amount'] !== null
363
- ) {
364
- parameters['y-amount'] = BigInt(Math.floor(parameters['y-amount']));
365
- if (
366
- parameters['x-amount'] !== undefined &&
367
- parameters['x-amount'] !== null
368
- ) {
369
- parameters['x-amount'] = BigInt(Math.floor(parameters['x-amount']));
370
- }
371
- } else if (
372
- parameters['x-amount'] !== undefined &&
373
- parameters['x-amount'] !== null
374
- ) {
375
- parameters['x-amount'] = BigInt(Math.floor(parameters['x-amount']));
376
- } else if (parameters.dy !== undefined && parameters.dy !== null) {
377
- parameters.dy = BigInt(Math.floor(parameters.dy));
378
- }
379
-
380
- const functionArgs = constructFunctionArgs(
381
- parameters,
382
- functionArgsDefinition
383
- );
384
-
385
- const result = await fetchCallReadOnlyFunction({
386
- contractAddress: contractDeployer,
387
- contractName,
388
- functionName,
389
- functionArgs,
390
- network,
391
- client,
392
- senderAddress: senderAddress,
393
- });
394
-
395
- const { rawResult, convertedResult } = handleResult(result);
396
-
397
- try {
398
- const tokenYDecimals = getTokenDecimals(tokenYId, context);
399
-
400
- let tokenYMatchingDecimal:
401
- | { tokenContract: string; tokenDecimals: number }
402
- | undefined;
403
-
404
- for (const key in parameters) {
405
- if (typeof parameters[key] === 'object') {
406
- for (const subKey in parameters[key]) {
407
- const contractValue = parameters[key][subKey];
408
- tokenYMatchingDecimal = tokenYDecimals.find(
409
- (d) => d.tokenContract === contractValue
410
- );
411
- if (tokenYMatchingDecimal) break;
412
- }
413
- } else if (typeof parameters[key] === 'string') {
414
- tokenYMatchingDecimal = tokenYDecimals.find(
415
- (d) => d.tokenContract === parameters[key]
416
- );
417
- }
418
- if (tokenYMatchingDecimal) break;
419
- }
420
-
421
- if (!tokenYMatchingDecimal && swapData && swapData.parameters) {
422
- const swapParameters = swapData.parameters;
423
- for (const key in swapParameters) {
424
- if (typeof swapParameters[key] === 'object') {
425
- for (const subKey in swapParameters[key]) {
426
- const contractValue = swapParameters[key][subKey];
427
- tokenYMatchingDecimal = tokenYDecimals.find(
428
- (d) => d.tokenContract === contractValue
429
- );
430
- if (tokenYMatchingDecimal) break;
431
- }
432
- } else if (typeof swapParameters[key] === 'string') {
433
- tokenYMatchingDecimal = tokenYDecimals.find(
434
- (d) => d.tokenContract === swapParameters[key]
435
- );
436
- }
437
- if (tokenYMatchingDecimal) break;
438
- }
439
- }
440
-
441
- if (tokenYMatchingDecimal && typeof convertedResult === 'number') {
442
- const adjustedResult =
443
- convertedResult / 10 ** tokenYMatchingDecimal.tokenDecimals;
444
- return {
445
- convertedResult: adjustedResult,
446
- rawResult,
447
- tokenXDecimals: tokenXMatchingDecimal?.tokenDecimals || 0,
448
- tokenYDecimals: tokenYMatchingDecimal.tokenDecimals,
449
- };
450
- } else {
451
- console.warn(
452
- `No matching decimal found for tokenY: ${tokenYId} or result is not a number`
453
- );
454
- return {
455
- convertedResult,
456
- rawResult,
457
- tokenXDecimals: tokenXMatchingDecimal?.tokenDecimals || 0,
458
- tokenYDecimals: 0,
459
- };
460
- }
461
- } catch (error) {
462
- console.warn(`Couldn't apply decimal conversion: ${error}`);
463
- return {
464
- convertedResult,
465
- rawResult,
466
- tokenXDecimals: tokenXMatchingDecimal?.tokenDecimals || 0,
467
- tokenYDecimals: 0,
468
- };
469
- }
470
- } catch (error) {
471
- console.error(`Error calling read-only function ${functionName}:`, error);
472
- throw error;
473
- }
474
- };
@@ -1,67 +0,0 @@
1
- import { AnchorMode, PostConditionMode } from "@stacks/transactions";
2
- import type { StacksProvider } from "@stacks/connect";
3
- import { SwapContext } from "../types";
4
-
5
- const loadStacksConnect = async () => {
6
- if (typeof window === "undefined") {
7
- throw new Error(
8
- "Stacks Connect functionality is only available in browser environments"
9
- );
10
- }
11
-
12
- try {
13
- const { openContractCall } = await import("@stacks/connect");
14
- return { openContractCall };
15
- } catch (error) {
16
- console.error("Error loading Stacks Connect:", error);
17
- throw new Error("Failed to load Stacks Connect dependencies");
18
- }
19
- };
20
-
21
- export const executeSwapHelper = async (
22
- swapParams: {
23
- functionArgs: any[];
24
- postConditions: any[];
25
- contractAddress: string;
26
- contractName: string;
27
- functionName: string;
28
- },
29
- senderAddress: string,
30
- context: SwapContext,
31
- stacksProvider: StacksProvider,
32
- onFinish?: (data: any) => void,
33
- onCancel?: () => void
34
- ): Promise<void> => {
35
- const network = context.network;
36
-
37
- try {
38
- const { openContractCall } = await loadStacksConnect();
39
-
40
- const txOptions = {
41
- contractAddress: swapParams.contractAddress,
42
- contractName: swapParams.contractName,
43
- functionName: swapParams.functionName,
44
- functionArgs: swapParams.functionArgs,
45
- senderAddress,
46
- network,
47
- anchorMode: AnchorMode.Any,
48
- postConditionMode: PostConditionMode.Deny,
49
- postConditions: swapParams.postConditions,
50
- onFinish:
51
- onFinish ||
52
- ((data: any) => {
53
- console.log("Transaction submitted:", data);
54
- }),
55
- onCancel:
56
- onCancel ||
57
- (() => {
58
- console.log("Transaction canceled");
59
- }),
60
- };
61
-
62
- await openContractCall({ ...txOptions }, stacksProvider);
63
- } catch (error) {
64
- console.error("Error executing swap:", error);
65
- throw error;
66
- }
67
- };