@bitflowlabs/core-sdk 2.3.0 → 2.3.2-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.
@@ -1,172 +0,0 @@
1
- import { Pc, PostCondition } from "@stacks/transactions";
2
-
3
- const isWSTXv2Token = (value: any): boolean =>
4
- typeof value === "string" &&
5
- value.includes("SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.token-wstx-v2");
6
-
7
- const checkForWSTXv2 = (obj: any): boolean => {
8
- if (typeof obj !== "object" || obj === null) {
9
- return false;
10
- }
11
-
12
- for (const key in obj) {
13
- if (isWSTXv2Token(obj[key])) {
14
- return true;
15
- }
16
- if (typeof obj[key] === "object" && checkForWSTXv2(obj[key])) {
17
- return true;
18
- }
19
- }
20
-
21
- return false;
22
- };
23
-
24
- export const createSwapPostConditions = async (
25
- functionArgs: any,
26
- postConditionsData: any,
27
- senderAddress: string,
28
- tokenXDecimals: number,
29
- tokenYDecimals: number
30
- ): Promise<PostCondition[]> => {
31
- const postConditions: PostCondition[] = [];
32
- const postConditionKeys = Object.keys(postConditionsData);
33
-
34
- const isWSTXv2Involved = checkForWSTXv2(functionArgs);
35
-
36
- for (let i = 0; i < postConditionKeys.length; i++) {
37
- const key = postConditionKeys[i];
38
- const pcData = postConditionsData[key];
39
- const isFirst = i === 0;
40
- const isLast = i === postConditionKeys.length - 1;
41
- const isTxSender = pcData.senderAddress === "tx-sender";
42
- const isSTX =
43
- pcData.tokenContract === "token-stx" || pcData.tokenName === "token-stx";
44
- const isAutoAlexV3OrLqstx =
45
- pcData.tokenName === "auto-alex-v3" || pcData.tokenName === "lqstx";
46
-
47
- let postConditionAmount: bigint;
48
- let conditionCode: string;
49
-
50
- if (isAutoAlexV3OrLqstx && (isFirst || isLast)) {
51
- postConditionAmount = BigInt(0);
52
- conditionCode = "willSendGte";
53
- } else {
54
- if (isFirst) {
55
- postConditionAmount = functionArgs.dx;
56
- conditionCode = "willSendEq";
57
- } else if (isLast) {
58
- postConditionAmount = functionArgs["min-dy"];
59
- conditionCode = "willSendGte";
60
- } else {
61
- postConditionAmount = BigInt(0);
62
- conditionCode = "willSendGte";
63
- }
64
- }
65
-
66
- if (isSTX && isWSTXv2Involved) {
67
- postConditionAmount = postConditionAmount / BigInt(100);
68
- } else if (!isSTX && (isFirst || isLast)) {
69
- let decimalDifference: number;
70
- if (isFirst) {
71
- decimalDifference = tokenXDecimals - pcData.tokenDecimals;
72
- } else {
73
- decimalDifference = tokenYDecimals - pcData.tokenDecimals;
74
- }
75
-
76
- if (decimalDifference > 0) {
77
- postConditionAmount =
78
- postConditionAmount / BigInt(10 ** decimalDifference);
79
- } else if (decimalDifference < 0) {
80
- postConditionAmount =
81
- postConditionAmount * BigInt(10 ** Math.abs(decimalDifference));
82
- }
83
- }
84
-
85
- try {
86
- if (isTxSender) {
87
- if (isSTX) {
88
- postConditions.push(
89
- conditionCode === "willSendEq"
90
- ? Pc.principal(senderAddress)
91
- .willSendEq(postConditionAmount)
92
- .ustx()
93
- : Pc.principal(senderAddress)
94
- .willSendGte(postConditionAmount)
95
- .ustx()
96
- );
97
- } else {
98
- postConditions.push(
99
- conditionCode === "willSendEq"
100
- ? Pc.principal(senderAddress)
101
- .willSendEq(postConditionAmount)
102
- .ft(pcData.tokenContract, pcData.tokenName)
103
- : Pc.principal(senderAddress)
104
- .willSendGte(postConditionAmount)
105
- .ft(pcData.tokenContract, pcData.tokenName)
106
- );
107
- }
108
- } else {
109
- if (isSTX) {
110
- postConditions.push(
111
- conditionCode === "willSendEq"
112
- ? Pc.principal(pcData.senderAddress)
113
- .willSendEq(postConditionAmount)
114
- .ustx()
115
- : Pc.principal(pcData.senderAddress)
116
- .willSendGte(postConditionAmount)
117
- .ustx()
118
- );
119
- } else {
120
- postConditions.push(
121
- conditionCode === "willSendEq"
122
- ? Pc.principal(pcData.senderAddress)
123
- .willSendEq(postConditionAmount)
124
- .ft(pcData.tokenContract, pcData.tokenName)
125
- : Pc.principal(pcData.senderAddress)
126
- .willSendGte(postConditionAmount)
127
- .ft(pcData.tokenContract, pcData.tokenName)
128
- );
129
- }
130
- }
131
-
132
- if (pcData.shareFeeContract !== null) {
133
- if (isSTX) {
134
- postConditions.push(
135
- Pc.principal(pcData.shareFeeContract).willSendGte(0).ustx()
136
- );
137
- } else {
138
- postConditions.push(
139
- Pc.principal(pcData.shareFeeContract)
140
- .willSendGte(0)
141
- .ft(pcData.tokenContract, pcData.tokenName)
142
- );
143
- }
144
- }
145
-
146
- if (pcData.dikoStx !== null) {
147
- if (isTxSender) {
148
- postConditions.push(
149
- Pc.principal(senderAddress)
150
- .willSendGte(0)
151
- .ft(pcData.dikoStx, "wstx")
152
- );
153
- } else {
154
- postConditions.push(
155
- Pc.principal(pcData.senderAddress)
156
- .willSendGte(0)
157
- .ft(pcData.dikoStx, "wstx")
158
- );
159
- }
160
- }
161
- } catch (error) {
162
- console.error(`Error creating post condition for ${key}:`, error);
163
- throw new Error(
164
- `Failed to create post condition for ${key}: ${
165
- (error as Error).message
166
- }`
167
- );
168
- }
169
- }
170
-
171
- return postConditions;
172
- };
@@ -1,298 +0,0 @@
1
- import { FungibleConditionCode, Pc, PostCondition } from "@stacks/transactions";
2
-
3
- const isWSTXv2Token = (value: any): boolean =>
4
- typeof value === "string" &&
5
- value.includes("SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.token-wstx-v2");
6
-
7
- const checkForWSTXv2 = (obj: any): boolean => {
8
- if (typeof obj !== "object" || obj === null) {
9
- return false;
10
- }
11
-
12
- for (const key in obj) {
13
- if (isWSTXv2Token(obj[key])) {
14
- return true;
15
- }
16
- if (typeof obj[key] === "object" && checkForWSTXv2(obj[key])) {
17
- return true;
18
- }
19
- }
20
-
21
- return false;
22
- };
23
-
24
- const createSafeAssetInfo = (
25
- contractAddress: string,
26
- contractName: string,
27
- assetName: string
28
- ) => {
29
- return {
30
- contract: `${contractAddress}.${contractName}` as `${string}.${string}`,
31
- tokenName: `${assetName}`,
32
- };
33
- };
34
-
35
- const makeStandardSTXPostCondition = (
36
- senderAddress: string,
37
- conditionCode:
38
- | FungibleConditionCode.Equal
39
- | FungibleConditionCode.GreaterEqual,
40
- postConditionAmount: bigint
41
- ) => {
42
- let initialPostCondition = Pc.principal(senderAddress);
43
- let postCondition;
44
- if (conditionCode === FungibleConditionCode.Equal) {
45
- postCondition = initialPostCondition.willSendEq(postConditionAmount);
46
- } else {
47
- postCondition = initialPostCondition.willSendGte(postConditionAmount);
48
- }
49
- return postCondition.ustx();
50
- };
51
-
52
- const makeStandardFungiblePostCondition = (
53
- senderAddress: string,
54
- conditionCode:
55
- | FungibleConditionCode.Equal
56
- | FungibleConditionCode.GreaterEqual,
57
- postConditionAmount: bigint,
58
- asset: { contract: `${string}.${string}`; tokenName: string }
59
- ) => {
60
- let initialPostCondition = Pc.principal(senderAddress);
61
- let postCondition;
62
- if (conditionCode === FungibleConditionCode.Equal) {
63
- postCondition = initialPostCondition.willSendEq(postConditionAmount);
64
- } else {
65
- postCondition = initialPostCondition.willSendGte(postConditionAmount);
66
- }
67
- return postCondition.ft(asset.contract, asset.tokenName);
68
- };
69
-
70
- const makeContractSTXPostCondition = (
71
- contractAddress: string,
72
- contractName: string,
73
- conditionCode:
74
- | FungibleConditionCode.Equal
75
- | FungibleConditionCode.GreaterEqual,
76
- postConditionAmount: bigint
77
- ) => {
78
- return makeStandardSTXPostCondition(
79
- `${contractAddress}.${contractName}`,
80
- conditionCode,
81
- postConditionAmount
82
- );
83
- };
84
-
85
- const makeContractFungiblePostCondition = (
86
- contractAddress: string,
87
- contractName: string,
88
- conditionCode:
89
- | FungibleConditionCode.Equal
90
- | FungibleConditionCode.GreaterEqual,
91
- postConditionAmount: bigint,
92
- asset: { contract: `${string}.${string}`; tokenName: string }
93
- ) => {
94
- return makeStandardFungiblePostCondition(
95
- `${contractAddress}.${contractName}`,
96
- conditionCode,
97
- postConditionAmount,
98
- asset
99
- );
100
- };
101
-
102
- export const createSwapPostConditions = async (
103
- functionArgs: any,
104
- postConditionsData: any,
105
- senderAddress: string,
106
- tokenXDecimals: number,
107
- tokenYDecimals: number
108
- ): Promise<PostCondition[]> => {
109
- const postConditions: PostCondition[] = [];
110
- const postConditionKeys = Object.keys(postConditionsData);
111
-
112
- const isWSTXv2Involved = checkForWSTXv2(functionArgs);
113
-
114
- for (let i = 0; i < postConditionKeys.length; i++) {
115
- const key = postConditionKeys[i];
116
- const pcData = postConditionsData[key];
117
- const isFirst = i === 0;
118
- const isLast = i === postConditionKeys.length - 1;
119
- const isTxSender = pcData.senderAddress === "tx-sender";
120
- const isSTX =
121
- pcData.tokenContract === "token-stx" || pcData.tokenName === "token-stx";
122
- const isAutoAlexV3OrLqstx =
123
- pcData.tokenName === "auto-alex-v3" || pcData.tokenName === "lqstx";
124
-
125
- let postConditionAmount: bigint;
126
- let conditionCode: FungibleConditionCode;
127
-
128
- if (isAutoAlexV3OrLqstx && (isFirst || isLast)) {
129
- postConditionAmount = BigInt(0);
130
- conditionCode = FungibleConditionCode.GreaterEqual;
131
- } else {
132
- if (isFirst) {
133
- postConditionAmount = functionArgs.dx;
134
- conditionCode = FungibleConditionCode.Equal;
135
- } else if (isLast) {
136
- postConditionAmount = functionArgs["min-dy"];
137
- conditionCode = FungibleConditionCode.GreaterEqual;
138
- } else {
139
- postConditionAmount = BigInt(0);
140
- conditionCode = FungibleConditionCode.GreaterEqual;
141
- }
142
- }
143
-
144
- if (isSTX && isWSTXv2Involved) {
145
- postConditionAmount = postConditionAmount / BigInt(100);
146
- } else if (!isSTX && (isFirst || isLast)) {
147
- let decimalDifference: number;
148
- if (isFirst) {
149
- decimalDifference = tokenXDecimals - pcData.tokenDecimals;
150
- } else {
151
- decimalDifference = tokenYDecimals - pcData.tokenDecimals;
152
- }
153
-
154
- if (decimalDifference > 0) {
155
- postConditionAmount =
156
- postConditionAmount / BigInt(10 ** decimalDifference);
157
- } else if (decimalDifference < 0) {
158
- postConditionAmount =
159
- postConditionAmount * BigInt(10 ** Math.abs(decimalDifference));
160
- }
161
- }
162
-
163
- try {
164
- if (isTxSender) {
165
- if (isSTX) {
166
- postConditions.push(
167
- makeStandardSTXPostCondition(
168
- senderAddress,
169
- conditionCode,
170
- postConditionAmount
171
- )
172
- );
173
- } else {
174
- const [contractAddress, contractName] =
175
- pcData.tokenContract.split(".");
176
- const assetInfo = createSafeAssetInfo(
177
- contractAddress,
178
- contractName,
179
- pcData.tokenName
180
- );
181
- postConditions.push(
182
- makeStandardFungiblePostCondition(
183
- senderAddress,
184
- conditionCode,
185
- postConditionAmount,
186
- assetInfo
187
- )
188
- );
189
- }
190
- } else {
191
- const [contractAddress, contractName] = pcData.senderAddress.split(".");
192
- if (isSTX) {
193
- postConditions.push(
194
- makeContractSTXPostCondition(
195
- contractAddress,
196
- contractName,
197
- conditionCode,
198
- postConditionAmount
199
- )
200
- );
201
- } else {
202
- const [tokenContractAddress, tokenContractName] =
203
- pcData.tokenContract.split(".");
204
- const assetInfo = createSafeAssetInfo(
205
- tokenContractAddress,
206
- tokenContractName,
207
- pcData.tokenName
208
- );
209
- postConditions.push(
210
- makeContractFungiblePostCondition(
211
- contractAddress,
212
- contractName,
213
- conditionCode,
214
- postConditionAmount,
215
- assetInfo
216
- )
217
- );
218
- }
219
- }
220
-
221
- if (pcData.shareFeeContract !== null) {
222
- const [shareFeeContractAddress, shareFeeContractName] =
223
- pcData.shareFeeContract.split(".");
224
-
225
- if (isSTX) {
226
- postConditions.push(
227
- makeContractSTXPostCondition(
228
- shareFeeContractAddress,
229
- shareFeeContractName,
230
- FungibleConditionCode.GreaterEqual,
231
- BigInt(0)
232
- )
233
- );
234
- } else {
235
- const [tokenContractAddress, tokenContractName] =
236
- pcData.tokenContract.split(".");
237
- const assetInfo = createSafeAssetInfo(
238
- tokenContractAddress,
239
- tokenContractName,
240
- pcData.tokenName
241
- );
242
-
243
- postConditions.push(
244
- makeContractFungiblePostCondition(
245
- shareFeeContractAddress,
246
- shareFeeContractName,
247
- FungibleConditionCode.GreaterEqual,
248
- BigInt(0),
249
- assetInfo
250
- )
251
- );
252
- }
253
- }
254
-
255
- if (pcData.dikoStx !== null) {
256
- const [dikoContractAddress, dikoContractName] =
257
- pcData.dikoStx.split(".");
258
- const dikoAssetInfo = createSafeAssetInfo(
259
- dikoContractAddress,
260
- dikoContractName,
261
- "wstx"
262
- );
263
-
264
- if (isTxSender) {
265
- postConditions.push(
266
- makeStandardFungiblePostCondition(
267
- senderAddress,
268
- FungibleConditionCode.GreaterEqual,
269
- BigInt(0),
270
- dikoAssetInfo
271
- )
272
- );
273
- } else {
274
- const [senderContractAddress, senderContractName] =
275
- pcData.senderAddress.split(".");
276
- postConditions.push(
277
- makeContractFungiblePostCondition(
278
- senderContractAddress,
279
- senderContractName,
280
- FungibleConditionCode.GreaterEqual,
281
- BigInt(0),
282
- dikoAssetInfo
283
- )
284
- );
285
- }
286
- }
287
- } catch (error) {
288
- console.error(`Error creating post condition for ${key}:`, error);
289
- throw new Error(
290
- `Failed to create post condition for ${key}: ${
291
- (error as Error).message
292
- }`
293
- );
294
- }
295
- }
296
-
297
- return postConditions;
298
- };
package/src/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export { BitflowSDK } from "./BitflowSDK";
2
- export * from "./types";
3
- export * from "./keeper/types";