@bitflowlabs/core-sdk 1.0.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 (78) hide show
  1. package/README.md +205 -0
  2. package/dist/src/BitflowSDK.d.ts +14 -0
  3. package/dist/src/BitflowSDK.js +275 -0
  4. package/dist/src/BitflowSDK.js.map +1 -0
  5. package/dist/src/config.d.ts +3 -0
  6. package/dist/src/config.js +20 -0
  7. package/dist/src/config.js.map +1 -0
  8. package/dist/src/helpers/callGetSwapParams.d.ts +2 -0
  9. package/dist/src/helpers/callGetSwapParams.js +85 -0
  10. package/dist/src/helpers/callGetSwapParams.js.map +1 -0
  11. package/dist/src/helpers/callReadOnlyHelper.d.ts +8 -0
  12. package/dist/src/helpers/callReadOnlyHelper.js +191 -0
  13. package/dist/src/helpers/callReadOnlyHelper.js.map +1 -0
  14. package/dist/src/helpers/callSwapHelper.d.ts +9 -0
  15. package/dist/src/helpers/callSwapHelper.js +41 -0
  16. package/dist/src/helpers/callSwapHelper.js.map +1 -0
  17. package/dist/src/helpers/constructFunctionArgs.d.ts +1 -0
  18. package/dist/src/helpers/constructFunctionArgs.js +22 -0
  19. package/dist/src/helpers/constructFunctionArgs.js.map +1 -0
  20. package/dist/src/helpers/convertValuesHelper.d.ts +2 -0
  21. package/dist/src/helpers/convertValuesHelper.js +181 -0
  22. package/dist/src/helpers/convertValuesHelper.js.map +1 -0
  23. package/dist/src/helpers/fetchContractInterfaceHelper.d.ts +1 -0
  24. package/dist/src/helpers/fetchContractInterfaceHelper.js +20 -0
  25. package/dist/src/helpers/fetchContractInterfaceHelper.js.map +1 -0
  26. package/dist/src/helpers/fetchDataHelper.d.ts +2 -0
  27. package/dist/src/helpers/fetchDataHelper.js +44 -0
  28. package/dist/src/helpers/fetchDataHelper.js.map +1 -0
  29. package/dist/src/helpers/fetchPossibleSwap.d.ts +2 -0
  30. package/dist/src/helpers/fetchPossibleSwap.js +20 -0
  31. package/dist/src/helpers/fetchPossibleSwap.js.map +1 -0
  32. package/dist/src/helpers/getContractInterfaceAndFunction.d.ts +4 -0
  33. package/dist/src/helpers/getContractInterfaceAndFunction.js +15 -0
  34. package/dist/src/helpers/getContractInterfaceAndFunction.js.map +1 -0
  35. package/dist/src/helpers/getFunctionArgs.d.ts +1 -0
  36. package/dist/src/helpers/getFunctionArgs.js +12 -0
  37. package/dist/src/helpers/getFunctionArgs.js.map +1 -0
  38. package/dist/src/helpers/getTokenDecimalsHelper.d.ts +5 -0
  39. package/dist/src/helpers/getTokenDecimalsHelper.js +28 -0
  40. package/dist/src/helpers/getTokenDecimalsHelper.js.map +1 -0
  41. package/dist/src/helpers/getTokenNameHelper.d.ts +2 -0
  42. package/dist/src/helpers/getTokenNameHelper.js +22 -0
  43. package/dist/src/helpers/getTokenNameHelper.js.map +1 -0
  44. package/dist/src/helpers/handleResultHelper.d.ts +4 -0
  45. package/dist/src/helpers/handleResultHelper.js +80 -0
  46. package/dist/src/helpers/handleResultHelper.js.map +1 -0
  47. package/dist/src/helpers/postConditionsHelper.d.ts +2 -0
  48. package/dist/src/helpers/postConditionsHelper.js +135 -0
  49. package/dist/src/helpers/postConditionsHelper.js.map +1 -0
  50. package/dist/src/index.d.ts +2 -0
  51. package/dist/src/index.js +8 -0
  52. package/dist/src/index.js.map +1 -0
  53. package/dist/src/test/testMethods.d.ts +1 -0
  54. package/dist/src/test/testMethods.js +178 -0
  55. package/dist/src/test/testMethods.js.map +1 -0
  56. package/dist/src/types.d.ts +126 -0
  57. package/dist/src/types.js +3 -0
  58. package/dist/src/types.js.map +1 -0
  59. package/package.json +57 -0
  60. package/src/BitflowSDK.ts +385 -0
  61. package/src/config.ts +22 -0
  62. package/src/helpers/callGetSwapParams.ts +122 -0
  63. package/src/helpers/callReadOnlyHelper.ts +243 -0
  64. package/src/helpers/callSwapHelper.ts +53 -0
  65. package/src/helpers/constructFunctionArgs.ts +24 -0
  66. package/src/helpers/convertValuesHelper.ts +214 -0
  67. package/src/helpers/fetchContractInterfaceHelper.ts +19 -0
  68. package/src/helpers/fetchDataHelper.ts +41 -0
  69. package/src/helpers/fetchPossibleSwap.ts +18 -0
  70. package/src/helpers/getContractInterfaceAndFunction.ts +20 -0
  71. package/src/helpers/getFunctionArgs.ts +12 -0
  72. package/src/helpers/getTokenDecimalsHelper.ts +33 -0
  73. package/src/helpers/getTokenNameHelper.ts +26 -0
  74. package/src/helpers/handleResultHelper.ts +85 -0
  75. package/src/helpers/postConditionsHelper.ts +246 -0
  76. package/src/index.ts +2 -0
  77. package/src/test/testMethods.ts +227 -0
  78. package/src/types.ts +137 -0
@@ -0,0 +1,227 @@
1
+ import { BitflowSDK } from '../BitflowSDK';
2
+ import { stringifyWithBigInt } from '../helpers/callReadOnlyHelper';
3
+ import { Token, SelectedSwapRoute, QuoteResult } from '../types';
4
+
5
+ class BitflowSDKTest {
6
+ private sdk: BitflowSDK;
7
+
8
+ constructor() {
9
+ this.sdk = new BitflowSDK();
10
+ }
11
+
12
+ public async testGetAvailableTokens(): Promise<void> {
13
+ const tokens = await this.sdk.getAvailableTokens();
14
+ console.log('Available Tokens:');
15
+ tokens.forEach(this.logToken);
16
+ }
17
+
18
+ public async testGetPossibleSwaps(tokenXName: string): Promise<void> {
19
+ try {
20
+ console.log(`Fetching possible swaps for token: ${tokenXName}`);
21
+ const possibleSwaps = await this.sdk.getPossibleSwaps(tokenXName);
22
+ console.log(
23
+ `Possible swaps for ${tokenXName}:`,
24
+ stringifyWithBigInt(possibleSwaps)
25
+ );
26
+ } catch (error) {
27
+ console.error(
28
+ `Error fetching possible swaps for token ${tokenXName}:`,
29
+ (error as Error).message
30
+ );
31
+ }
32
+ }
33
+
34
+ public async testGetAllPossibleTokenY(tokenXName: string): Promise<void> {
35
+ try {
36
+ console.log(`Fetching possible tokenY for token: ${tokenXName}`);
37
+ const possibleTokenY = await this.sdk.getAllPossibleTokenY(tokenXName);
38
+ console.log(
39
+ `Possible tokenY for ${tokenXName}:`,
40
+ stringifyWithBigInt(possibleTokenY)
41
+ );
42
+ } catch (error) {
43
+ console.error(
44
+ `Error fetching possible tokenY for token ${tokenXName}:`,
45
+ (error as Error).message
46
+ );
47
+ }
48
+ }
49
+
50
+ public async testGetAllPossibleTokenYRoutes(
51
+ tokenXName: string,
52
+ tokenYName: string
53
+ ): Promise<void> {
54
+ try {
55
+ console.log(`Fetching routes for ${tokenXName} to ${tokenYName}`);
56
+ const routes = await this.sdk.getAllPossibleTokenYRoutes(
57
+ tokenXName,
58
+ tokenYName
59
+ );
60
+ this.logRoutes(routes);
61
+ } catch (error) {
62
+ console.error(
63
+ `Error fetching routes for ${tokenXName} to ${tokenYName}:`,
64
+ (error as Error).message
65
+ );
66
+ }
67
+ }
68
+
69
+ public async testGetQuoteForRoute(
70
+ tokenXName: string,
71
+ tokenYName: string,
72
+ amountStr: string
73
+ ): Promise<void> {
74
+ try {
75
+ const amount = parseFloat(amountStr);
76
+ if (isNaN(amount)) {
77
+ throw new Error('Invalid amount input. Please provide a valid number.');
78
+ }
79
+
80
+ console.log(
81
+ `Fetching quotes for ${tokenXName} to ${tokenYName} with amount ${amount} (supports decimal input)`
82
+ );
83
+ const quoteResult = await this.sdk.getQuoteForRoute(
84
+ tokenXName,
85
+ tokenYName,
86
+ amount
87
+ );
88
+ this.logQuoteResult(quoteResult);
89
+ } catch (error) {
90
+ console.error(
91
+ 'Error getting quotes for routes:',
92
+ (error as Error).message
93
+ );
94
+ }
95
+ }
96
+
97
+ private logQuote(
98
+ quote: QuoteResult['bestRoute'] | QuoteResult['allRoutes'][0] | null
99
+ ): void {
100
+ if (quote === null) {
101
+ console.log(' No quote available');
102
+ return;
103
+ }
104
+
105
+ if ('error' in quote && quote.quote === null) {
106
+ console.log(` Error: ${quote.error}`);
107
+ } else if (quote.quote !== null) {
108
+ console.log(` Converted quote result: ${quote.quote}`);
109
+ } else {
110
+ console.log(' Quote result unavailable');
111
+ }
112
+
113
+ console.log(` Route:`);
114
+ console.log(` DEX Path: ${quote.route.dex_path.join(' -> ')}`);
115
+ console.log(` Token Path: ${quote.route.token_path.join(' -> ')}`);
116
+ console.log(` Quote Data:`);
117
+ console.log(` Contract: ${quote.route.quoteData.contract}`);
118
+ console.log(` Function: ${quote.route.quoteData.function}`);
119
+ console.log(` Parameters:`);
120
+ console.log(stringifyWithBigInt(quote.route.quoteData.parameters));
121
+ console.log(` Swap Data:`);
122
+ console.log(` Contract: ${quote.route.swapData.contract}`);
123
+ console.log(` Function: ${quote.route.swapData.function}`);
124
+ console.log(` Parameters:`);
125
+ console.log(stringifyWithBigInt(quote.route.swapData.parameters));
126
+ }
127
+
128
+ private logQuoteResult(quoteResult: QuoteResult): void {
129
+ console.log('Quote Result:');
130
+ console.log(`Input Data:`, stringifyWithBigInt(quoteResult.inputData));
131
+ console.log(`Best Route:`);
132
+ this.logQuote(quoteResult.bestRoute);
133
+ console.log(`All Routes:`);
134
+ quoteResult.allRoutes.forEach((route, index) => {
135
+ console.log(`Route ${index + 1}:`);
136
+ this.logQuote(route);
137
+ });
138
+ }
139
+
140
+ private logToken(token: Token): void {
141
+ console.log(`- ${token.name} (${token.symbol})`);
142
+ console.log(` Status: ${token.status}`);
143
+ console.log(` Decimals: ${token.tokenDecimals}`);
144
+ console.log(` Contract: ${token.tokenContract || 'N/A'}`);
145
+ console.log(` TokenId: ${token.tokenId || 'N/A'}`);
146
+ console.log('');
147
+ }
148
+
149
+ private logRoutes(routes: SelectedSwapRoute[]): void {
150
+ if (routes.length > 0) {
151
+ console.log(`Found ${routes.length} swap route(s):`);
152
+ routes.forEach((route, index) => {
153
+ console.log(`Route ${index + 1}:`);
154
+ console.log(` DEX Path: ${route.dex_path.join(' -> ')}`);
155
+ console.log(` Token Path: ${route.token_path.join(' -> ')}`);
156
+ console.log(` Quote Data:`);
157
+ console.log(` Contract: ${route.quoteData.contract}`);
158
+ console.log(` Function: ${route.quoteData.function}`);
159
+ console.log(` Parameters:`);
160
+ console.log(stringifyWithBigInt(route.quoteData.parameters));
161
+ console.log(` Swap Data:`);
162
+ console.log(` Contract: ${route.swapData.contract}`);
163
+ console.log(` Function: ${route.swapData.function}`);
164
+ console.log(` Parameters:`);
165
+ console.log(stringifyWithBigInt(route.swapData.parameters));
166
+ console.log(`PostConditions:`);
167
+ console.log(stringifyWithBigInt(route.postConditions));
168
+ });
169
+ } else {
170
+ console.log('No swap routes found');
171
+ }
172
+ }
173
+ }
174
+
175
+ const runTest = async (testName: string, ...args: string[]) => {
176
+ const sdkTest = new BitflowSDKTest();
177
+ switch (testName) {
178
+ case 'getAvailableTokens':
179
+ await sdkTest.testGetAvailableTokens();
180
+ break;
181
+ case 'getPossibleSwaps':
182
+ if (args.length < 1) {
183
+ console.error('Please provide tokenX for getPossibleSwaps');
184
+ process.exit(1);
185
+ }
186
+ await sdkTest.testGetPossibleSwaps(args[0]);
187
+ break;
188
+ case 'getAllPossibleTokenY':
189
+ if (args.length < 1) {
190
+ console.error('Please provide tokenX for getAllPossibleTokenY');
191
+ process.exit(1);
192
+ }
193
+ await sdkTest.testGetAllPossibleTokenY(args[0]);
194
+ break;
195
+ case 'getAllPossibleTokenYRoutes':
196
+ if (args.length < 2) {
197
+ console.error(
198
+ 'Please provide tokenX and tokenY for getAllPossibleTokenYRoutes'
199
+ );
200
+ process.exit(1);
201
+ }
202
+ await sdkTest.testGetAllPossibleTokenYRoutes(args[0], args[1]);
203
+ break;
204
+ case 'getQuoteForRoute':
205
+ if (args.length < 3) {
206
+ console.error(
207
+ 'Please provide tokenX, tokenY, and amount (integer or decimal) for getQuoteForRoute'
208
+ );
209
+ process.exit(1);
210
+ }
211
+ await sdkTest.testGetQuoteForRoute(args[0], args[1], args[2]);
212
+ break;
213
+ default:
214
+ console.error(`Unknown test: ${testName}`);
215
+ break;
216
+ }
217
+ };
218
+
219
+ const [testName, ...args] = process.argv.slice(2);
220
+ if (!testName) {
221
+ console.error('Please provide a test name to run.');
222
+ process.exit(1);
223
+ }
224
+
225
+ runTest(testName, ...args).catch((error) => {
226
+ console.error('Test failed:', error);
227
+ });
package/src/types.ts ADDED
@@ -0,0 +1,137 @@
1
+ import { ClarityValue, PostCondition } from '@stacks/transactions';
2
+ import { StacksNetwork } from '@stacks/network';
3
+
4
+ export type BitflowSDKConfig = {
5
+ BITFLOW_API_HOST: string;
6
+ BITFLOW_API_KEY: string;
7
+ READONLY_CALL_API_HOST: string;
8
+ };
9
+
10
+ export type Token = {
11
+ icon: string;
12
+ name: string;
13
+ status: string;
14
+ symbol: string;
15
+ tokenId: string;
16
+ tokenContract: string | null;
17
+ tokenDecimals: number;
18
+ tokenName: string | null;
19
+ wrapTokens: {
20
+ [key: string]: {
21
+ tokenContract: string;
22
+ tokenDecimals: number;
23
+ tokenName: string | null;
24
+ };
25
+ } | null;
26
+ };
27
+
28
+ export type SwapOptions = {
29
+ [tokenY: string]: SelectedSwapRoute[];
30
+ };
31
+
32
+ export type PostConditionType = {
33
+ senderAddress: string;
34
+ shareFeeContract: string | null;
35
+ dikoStx: string | null;
36
+ tokenContract: string;
37
+ tokenName: string;
38
+ tokenDecimals: number | string;
39
+ };
40
+
41
+ export type SelectedSwapRoute = {
42
+ dex_path: string[];
43
+ postConditions: {
44
+ [key: string]: PostConditionType;
45
+ };
46
+ quoteData: {
47
+ contract: string;
48
+ function: string;
49
+ parameters: {
50
+ [key: string]: any;
51
+ };
52
+ };
53
+ swapData: {
54
+ contract: string;
55
+ function: string;
56
+ parameters: {
57
+ [key: string]: any;
58
+ };
59
+ };
60
+ token_path: string[];
61
+ tokenXDecimals: number;
62
+ tokenYDecimals: number;
63
+ };
64
+
65
+ export type SwapContext = {
66
+ availableTokens: Token[];
67
+ contractInterfaces: {
68
+ [key: string]: any;
69
+ };
70
+ functionArgs: {
71
+ [key: string]: {
72
+ [functionName: string]: any[];
73
+ };
74
+ };
75
+ network: StacksNetwork;
76
+ swapOptions: {
77
+ [tokenX: string]: SwapOptions;
78
+ };
79
+ };
80
+
81
+ export type RouteQuote = {
82
+ route: SelectedSwapRoute;
83
+ quote: number | null;
84
+ params: any;
85
+ quoteData: {
86
+ contract: string;
87
+ function: string;
88
+ parameters: {
89
+ [key: string]: any;
90
+ };
91
+ };
92
+ swapData: {
93
+ contract: string;
94
+ function: string;
95
+ parameters: {
96
+ [key: string]: any;
97
+ };
98
+ };
99
+ dexPath: string[];
100
+ tokenPath: string[];
101
+ tokenXDecimals: number;
102
+ tokenYDecimals: number;
103
+ error?: string;
104
+ };
105
+
106
+ export type QuoteResult = {
107
+ bestRoute: RouteQuote | null;
108
+ allRoutes: RouteQuote[];
109
+ inputData: {
110
+ tokenX: string;
111
+ tokenY: string;
112
+ amountInput: number;
113
+ };
114
+ };
115
+
116
+ export type TxToBroadcast = {
117
+ contractAddress: string;
118
+ contractName: string;
119
+ functionName: string;
120
+ functionArgs: ClarityValue[];
121
+ postConditions: PostCondition[];
122
+ };
123
+
124
+ export type SwapExecutionData = {
125
+ route: SelectedSwapRoute;
126
+ amount: number;
127
+ tokenXDecimals: number;
128
+ tokenYDecimals: number;
129
+ };
130
+
131
+ export type SwapDataParamsAndPostConditions = {
132
+ functionArgs: any[];
133
+ postConditions: any[];
134
+ contractAddress: string;
135
+ contractName: string;
136
+ functionName: string;
137
+ };