@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.
- package/README.md +205 -0
- package/dist/src/BitflowSDK.d.ts +14 -0
- package/dist/src/BitflowSDK.js +275 -0
- package/dist/src/BitflowSDK.js.map +1 -0
- package/dist/src/config.d.ts +3 -0
- package/dist/src/config.js +20 -0
- package/dist/src/config.js.map +1 -0
- package/dist/src/helpers/callGetSwapParams.d.ts +2 -0
- package/dist/src/helpers/callGetSwapParams.js +85 -0
- package/dist/src/helpers/callGetSwapParams.js.map +1 -0
- package/dist/src/helpers/callReadOnlyHelper.d.ts +8 -0
- package/dist/src/helpers/callReadOnlyHelper.js +191 -0
- package/dist/src/helpers/callReadOnlyHelper.js.map +1 -0
- package/dist/src/helpers/callSwapHelper.d.ts +9 -0
- package/dist/src/helpers/callSwapHelper.js +41 -0
- package/dist/src/helpers/callSwapHelper.js.map +1 -0
- package/dist/src/helpers/constructFunctionArgs.d.ts +1 -0
- package/dist/src/helpers/constructFunctionArgs.js +22 -0
- package/dist/src/helpers/constructFunctionArgs.js.map +1 -0
- package/dist/src/helpers/convertValuesHelper.d.ts +2 -0
- package/dist/src/helpers/convertValuesHelper.js +181 -0
- package/dist/src/helpers/convertValuesHelper.js.map +1 -0
- package/dist/src/helpers/fetchContractInterfaceHelper.d.ts +1 -0
- package/dist/src/helpers/fetchContractInterfaceHelper.js +20 -0
- package/dist/src/helpers/fetchContractInterfaceHelper.js.map +1 -0
- package/dist/src/helpers/fetchDataHelper.d.ts +2 -0
- package/dist/src/helpers/fetchDataHelper.js +44 -0
- package/dist/src/helpers/fetchDataHelper.js.map +1 -0
- package/dist/src/helpers/fetchPossibleSwap.d.ts +2 -0
- package/dist/src/helpers/fetchPossibleSwap.js +20 -0
- package/dist/src/helpers/fetchPossibleSwap.js.map +1 -0
- package/dist/src/helpers/getContractInterfaceAndFunction.d.ts +4 -0
- package/dist/src/helpers/getContractInterfaceAndFunction.js +15 -0
- package/dist/src/helpers/getContractInterfaceAndFunction.js.map +1 -0
- package/dist/src/helpers/getFunctionArgs.d.ts +1 -0
- package/dist/src/helpers/getFunctionArgs.js +12 -0
- package/dist/src/helpers/getFunctionArgs.js.map +1 -0
- package/dist/src/helpers/getTokenDecimalsHelper.d.ts +5 -0
- package/dist/src/helpers/getTokenDecimalsHelper.js +28 -0
- package/dist/src/helpers/getTokenDecimalsHelper.js.map +1 -0
- package/dist/src/helpers/getTokenNameHelper.d.ts +2 -0
- package/dist/src/helpers/getTokenNameHelper.js +22 -0
- package/dist/src/helpers/getTokenNameHelper.js.map +1 -0
- package/dist/src/helpers/handleResultHelper.d.ts +4 -0
- package/dist/src/helpers/handleResultHelper.js +80 -0
- package/dist/src/helpers/handleResultHelper.js.map +1 -0
- package/dist/src/helpers/postConditionsHelper.d.ts +2 -0
- package/dist/src/helpers/postConditionsHelper.js +135 -0
- package/dist/src/helpers/postConditionsHelper.js.map +1 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +8 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/test/testMethods.d.ts +1 -0
- package/dist/src/test/testMethods.js +178 -0
- package/dist/src/test/testMethods.js.map +1 -0
- package/dist/src/types.d.ts +126 -0
- package/dist/src/types.js +3 -0
- package/dist/src/types.js.map +1 -0
- package/package.json +57 -0
- package/src/BitflowSDK.ts +385 -0
- package/src/config.ts +22 -0
- package/src/helpers/callGetSwapParams.ts +122 -0
- package/src/helpers/callReadOnlyHelper.ts +243 -0
- package/src/helpers/callSwapHelper.ts +53 -0
- package/src/helpers/constructFunctionArgs.ts +24 -0
- package/src/helpers/convertValuesHelper.ts +214 -0
- package/src/helpers/fetchContractInterfaceHelper.ts +19 -0
- package/src/helpers/fetchDataHelper.ts +41 -0
- package/src/helpers/fetchPossibleSwap.ts +18 -0
- package/src/helpers/getContractInterfaceAndFunction.ts +20 -0
- package/src/helpers/getFunctionArgs.ts +12 -0
- package/src/helpers/getTokenDecimalsHelper.ts +33 -0
- package/src/helpers/getTokenNameHelper.ts +26 -0
- package/src/helpers/handleResultHelper.ts +85 -0
- package/src/helpers/postConditionsHelper.ts +246 -0
- package/src/index.ts +2 -0
- package/src/test/testMethods.ts +227 -0
- package/src/types.ts +137 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { ClarityValue, PostCondition } from '@stacks/transactions';
|
|
2
|
+
import { StacksNetwork } from '@stacks/network';
|
|
3
|
+
export type BitflowSDKConfig = {
|
|
4
|
+
BITFLOW_API_HOST: string;
|
|
5
|
+
BITFLOW_API_KEY: string;
|
|
6
|
+
READONLY_CALL_API_HOST: string;
|
|
7
|
+
};
|
|
8
|
+
export type Token = {
|
|
9
|
+
icon: string;
|
|
10
|
+
name: string;
|
|
11
|
+
status: string;
|
|
12
|
+
symbol: string;
|
|
13
|
+
tokenId: string;
|
|
14
|
+
tokenContract: string | null;
|
|
15
|
+
tokenDecimals: number;
|
|
16
|
+
tokenName: string | null;
|
|
17
|
+
wrapTokens: {
|
|
18
|
+
[key: string]: {
|
|
19
|
+
tokenContract: string;
|
|
20
|
+
tokenDecimals: number;
|
|
21
|
+
tokenName: string | null;
|
|
22
|
+
};
|
|
23
|
+
} | null;
|
|
24
|
+
};
|
|
25
|
+
export type SwapOptions = {
|
|
26
|
+
[tokenY: string]: SelectedSwapRoute[];
|
|
27
|
+
};
|
|
28
|
+
export type PostConditionType = {
|
|
29
|
+
senderAddress: string;
|
|
30
|
+
shareFeeContract: string | null;
|
|
31
|
+
dikoStx: string | null;
|
|
32
|
+
tokenContract: string;
|
|
33
|
+
tokenName: string;
|
|
34
|
+
tokenDecimals: number | string;
|
|
35
|
+
};
|
|
36
|
+
export type SelectedSwapRoute = {
|
|
37
|
+
dex_path: string[];
|
|
38
|
+
postConditions: {
|
|
39
|
+
[key: string]: PostConditionType;
|
|
40
|
+
};
|
|
41
|
+
quoteData: {
|
|
42
|
+
contract: string;
|
|
43
|
+
function: string;
|
|
44
|
+
parameters: {
|
|
45
|
+
[key: string]: any;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
swapData: {
|
|
49
|
+
contract: string;
|
|
50
|
+
function: string;
|
|
51
|
+
parameters: {
|
|
52
|
+
[key: string]: any;
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
token_path: string[];
|
|
56
|
+
tokenXDecimals: number;
|
|
57
|
+
tokenYDecimals: number;
|
|
58
|
+
};
|
|
59
|
+
export type SwapContext = {
|
|
60
|
+
availableTokens: Token[];
|
|
61
|
+
contractInterfaces: {
|
|
62
|
+
[key: string]: any;
|
|
63
|
+
};
|
|
64
|
+
functionArgs: {
|
|
65
|
+
[key: string]: {
|
|
66
|
+
[functionName: string]: any[];
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
network: StacksNetwork;
|
|
70
|
+
swapOptions: {
|
|
71
|
+
[tokenX: string]: SwapOptions;
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
export type RouteQuote = {
|
|
75
|
+
route: SelectedSwapRoute;
|
|
76
|
+
quote: number | null;
|
|
77
|
+
params: any;
|
|
78
|
+
quoteData: {
|
|
79
|
+
contract: string;
|
|
80
|
+
function: string;
|
|
81
|
+
parameters: {
|
|
82
|
+
[key: string]: any;
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
swapData: {
|
|
86
|
+
contract: string;
|
|
87
|
+
function: string;
|
|
88
|
+
parameters: {
|
|
89
|
+
[key: string]: any;
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
dexPath: string[];
|
|
93
|
+
tokenPath: string[];
|
|
94
|
+
tokenXDecimals: number;
|
|
95
|
+
tokenYDecimals: number;
|
|
96
|
+
error?: string;
|
|
97
|
+
};
|
|
98
|
+
export type QuoteResult = {
|
|
99
|
+
bestRoute: RouteQuote | null;
|
|
100
|
+
allRoutes: RouteQuote[];
|
|
101
|
+
inputData: {
|
|
102
|
+
tokenX: string;
|
|
103
|
+
tokenY: string;
|
|
104
|
+
amountInput: number;
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
export type TxToBroadcast = {
|
|
108
|
+
contractAddress: string;
|
|
109
|
+
contractName: string;
|
|
110
|
+
functionName: string;
|
|
111
|
+
functionArgs: ClarityValue[];
|
|
112
|
+
postConditions: PostCondition[];
|
|
113
|
+
};
|
|
114
|
+
export type SwapExecutionData = {
|
|
115
|
+
route: SelectedSwapRoute;
|
|
116
|
+
amount: number;
|
|
117
|
+
tokenXDecimals: number;
|
|
118
|
+
tokenYDecimals: number;
|
|
119
|
+
};
|
|
120
|
+
export type SwapDataParamsAndPostConditions = {
|
|
121
|
+
functionArgs: any[];
|
|
122
|
+
postConditions: any[];
|
|
123
|
+
contractAddress: string;
|
|
124
|
+
contractName: string;
|
|
125
|
+
functionName: string;
|
|
126
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bitflowlabs/core-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "SDK for interacting with Bitflow Protocol",
|
|
5
|
+
"main": "dist/src/index.js",
|
|
6
|
+
"types": "dist/src/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"src"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"test": "jest",
|
|
14
|
+
"lint": "eslint . --ext .ts",
|
|
15
|
+
"prepare": "npm run build",
|
|
16
|
+
"test:watch": "jest --watch",
|
|
17
|
+
"prepublishOnly": "npm run build"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/BitflowFinance/bitflow-sdk.git"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"bitflow",
|
|
25
|
+
"blockchain",
|
|
26
|
+
"defi",
|
|
27
|
+
"sdk"
|
|
28
|
+
],
|
|
29
|
+
"author": "Bitflow Protocol",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/jest": "^29.5.13",
|
|
33
|
+
"@types/node": "^20.16.10",
|
|
34
|
+
"@typescript-eslint/eslint-plugin": "^5.0.0",
|
|
35
|
+
"@typescript-eslint/parser": "^5.0.0",
|
|
36
|
+
"eslint": "^8.0.1",
|
|
37
|
+
"jest": "^29.7.0",
|
|
38
|
+
"ts-jest": "^29.2.5",
|
|
39
|
+
"ts-node": "^10.9.2",
|
|
40
|
+
"typescript": "^5.6.2"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@stacks/connect": "^7.7.1",
|
|
44
|
+
"@stacks/network": "^6.16.0",
|
|
45
|
+
"@stacks/transactions": "^6.16.1"
|
|
46
|
+
},
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=20.0.0"
|
|
49
|
+
},
|
|
50
|
+
"bugs": {
|
|
51
|
+
"url": "https://github.com/BitflowFinance/bitflow-sdk/issues"
|
|
52
|
+
},
|
|
53
|
+
"homepage": "https://github.com/BitflowFinance/bitflow-sdk#readme",
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public"
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
import { StacksMainnet } from '@stacks/network';
|
|
2
|
+
import { StacksProvider } from '@stacks/connect';
|
|
3
|
+
import {
|
|
4
|
+
SwapContext,
|
|
5
|
+
Token,
|
|
6
|
+
SwapOptions,
|
|
7
|
+
SelectedSwapRoute,
|
|
8
|
+
QuoteResult,
|
|
9
|
+
SwapExecutionData,
|
|
10
|
+
RouteQuote,
|
|
11
|
+
SwapDataParamsAndPostConditions,
|
|
12
|
+
BitflowSDKConfig,
|
|
13
|
+
} from './types';
|
|
14
|
+
import { fetchAllTokensFromAPI } from './helpers/fetchDataHelper';
|
|
15
|
+
import { executeSwapHelper } from './helpers/callSwapHelper';
|
|
16
|
+
import { callReadOnlyFunctionHelper } from './helpers/callReadOnlyHelper';
|
|
17
|
+
import { fetchPossibleSwapsFromAPI } from './helpers/fetchPossibleSwap';
|
|
18
|
+
import { getContractInterfaceAndFunction } from './helpers/getContractInterfaceAndFunction';
|
|
19
|
+
import { configs, validateConfig } from './config';
|
|
20
|
+
import { executeGetParams } from './helpers/callGetSwapParams';
|
|
21
|
+
|
|
22
|
+
export class BitflowSDK {
|
|
23
|
+
private context: SwapContext;
|
|
24
|
+
|
|
25
|
+
constructor(config?: Partial<BitflowSDKConfig>) {
|
|
26
|
+
if (config) Object.assign(configs, config);
|
|
27
|
+
|
|
28
|
+
validateConfig();
|
|
29
|
+
|
|
30
|
+
this.context = {
|
|
31
|
+
availableTokens: [],
|
|
32
|
+
contractInterfaces: {},
|
|
33
|
+
functionArgs: {},
|
|
34
|
+
network: new StacksMainnet(),
|
|
35
|
+
swapOptions: {},
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
this.initializeContext();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
private async initializeContext(): Promise<void> {
|
|
42
|
+
this.context.availableTokens = await fetchAllTokensFromAPI();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public async getAvailableTokens(): Promise<Token[]> {
|
|
46
|
+
if (this.context.availableTokens.length === 0) {
|
|
47
|
+
await this.initializeContext();
|
|
48
|
+
}
|
|
49
|
+
return this.context.availableTokens;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public async getPossibleSwaps(tokenX: string): Promise<SwapOptions> {
|
|
53
|
+
if (!this.context.swapOptions[tokenX]) {
|
|
54
|
+
this.context.swapOptions[tokenX] = await fetchPossibleSwapsFromAPI(
|
|
55
|
+
tokenX
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
return this.context.swapOptions[tokenX];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public async getAllPossibleTokenY(tokenX: string): Promise<string[]> {
|
|
62
|
+
const swapOptions = await this.getPossibleSwaps(tokenX);
|
|
63
|
+
return Object.keys(swapOptions);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
public async getAllPossibleTokenYRoutes(
|
|
67
|
+
tokenX: string,
|
|
68
|
+
tokenY: string
|
|
69
|
+
): Promise<SelectedSwapRoute[]> {
|
|
70
|
+
const swapOptions = await this.getPossibleSwaps(tokenX);
|
|
71
|
+
return swapOptions[tokenY] || [];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
public async getQuoteForRoute(
|
|
75
|
+
tokenX: string,
|
|
76
|
+
tokenY: string,
|
|
77
|
+
amountInput: number
|
|
78
|
+
): Promise<QuoteResult> {
|
|
79
|
+
const routes = await this.getAllPossibleTokenYRoutes(tokenX, tokenY);
|
|
80
|
+
const allRoutes: RouteQuote[] = [];
|
|
81
|
+
|
|
82
|
+
for (let routeIndex = 0; routeIndex < routes.length; routeIndex++) {
|
|
83
|
+
const route = routes[routeIndex];
|
|
84
|
+
try {
|
|
85
|
+
// Check if quoteData is null or undefined
|
|
86
|
+
if (!route.quoteData) {
|
|
87
|
+
console.warn(
|
|
88
|
+
`Skipping route ${routeIndex + 1} due to null quoteData:`,
|
|
89
|
+
route
|
|
90
|
+
);
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const {
|
|
95
|
+
contract,
|
|
96
|
+
function: functionName,
|
|
97
|
+
parameters,
|
|
98
|
+
} = route.quoteData;
|
|
99
|
+
|
|
100
|
+
// Check if any required property is missing
|
|
101
|
+
if (!contract || !functionName || !parameters) {
|
|
102
|
+
console.warn(
|
|
103
|
+
`Skipping route ${
|
|
104
|
+
routeIndex + 1
|
|
105
|
+
} due to missing required properties:`,
|
|
106
|
+
route.quoteData
|
|
107
|
+
);
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const [contractDeployer, contractName] = contract.split('.');
|
|
112
|
+
|
|
113
|
+
if (!this.context.contractInterfaces[contract]) {
|
|
114
|
+
this.context.contractInterfaces[contract] =
|
|
115
|
+
await getContractInterfaceAndFunction(
|
|
116
|
+
contractDeployer,
|
|
117
|
+
contractName,
|
|
118
|
+
functionName
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
const { interface: contractInterface, functionArgs } =
|
|
122
|
+
this.context.contractInterfaces[contract];
|
|
123
|
+
|
|
124
|
+
const params = { ...parameters };
|
|
125
|
+
|
|
126
|
+
if ('dx' in params && params.dx === null) {
|
|
127
|
+
params.dx = amountInput;
|
|
128
|
+
} else if ('dy' in params && params.dy === null) {
|
|
129
|
+
params.dy = amountInput;
|
|
130
|
+
} else if ('amount' in params && params.amount === null) {
|
|
131
|
+
params.amount = amountInput;
|
|
132
|
+
} else if ('amt-in' in params && params['amt-in'] === null) {
|
|
133
|
+
params['amt-in'] = amountInput;
|
|
134
|
+
} else if ('amt-in-max' in params && params['amt-in-max'] === null) {
|
|
135
|
+
params['amt-in-max'] = amountInput;
|
|
136
|
+
} else if ('y-amount' in params && params['y-amount'] === null) {
|
|
137
|
+
params['y-amount'] = amountInput;
|
|
138
|
+
params['x-amount'] = amountInput;
|
|
139
|
+
} else if ('x-amount' in params && params['x-amount'] === null) {
|
|
140
|
+
params['x-amount'] = amountInput;
|
|
141
|
+
} else {
|
|
142
|
+
params.dx = amountInput;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const { convertedResult, rawResult, tokenXDecimals, tokenYDecimals } =
|
|
146
|
+
await callReadOnlyFunctionHelper(
|
|
147
|
+
contractDeployer,
|
|
148
|
+
contractName,
|
|
149
|
+
functionName,
|
|
150
|
+
params,
|
|
151
|
+
contractDeployer,
|
|
152
|
+
tokenX,
|
|
153
|
+
tokenY,
|
|
154
|
+
route.swapData,
|
|
155
|
+
this.context
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
if (typeof convertedResult === 'number' && convertedResult > 0) {
|
|
159
|
+
const updatedQuoteData = {
|
|
160
|
+
...route.quoteData,
|
|
161
|
+
parameters: { ...params },
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
const updatedSwapData = {
|
|
165
|
+
...route.swapData,
|
|
166
|
+
parameters: {
|
|
167
|
+
...route.swapData.parameters,
|
|
168
|
+
amount:
|
|
169
|
+
params.amount ||
|
|
170
|
+
params.dx ||
|
|
171
|
+
params['amt-in'] ||
|
|
172
|
+
params['amt-in-max'] ||
|
|
173
|
+
params['y-amount'] ||
|
|
174
|
+
params['x-amount'] ||
|
|
175
|
+
params.dy,
|
|
176
|
+
dx:
|
|
177
|
+
params.amount ||
|
|
178
|
+
params.dx ||
|
|
179
|
+
params['amt-in'] ||
|
|
180
|
+
params['amt-in-max'] ||
|
|
181
|
+
params['y-amount'] ||
|
|
182
|
+
params['x-amount'] ||
|
|
183
|
+
params.dy,
|
|
184
|
+
dy:
|
|
185
|
+
params.amount ||
|
|
186
|
+
params.dx ||
|
|
187
|
+
params['amt-in'] ||
|
|
188
|
+
params['amt-in-max'] ||
|
|
189
|
+
params['y-amount'] ||
|
|
190
|
+
params['x-amount'] ||
|
|
191
|
+
params.dy,
|
|
192
|
+
'amt-in':
|
|
193
|
+
params.amount ||
|
|
194
|
+
params.dx ||
|
|
195
|
+
params['amt-in'] ||
|
|
196
|
+
params['amt-in-max'] ||
|
|
197
|
+
params['y-amount'] ||
|
|
198
|
+
params['x-amount'] ||
|
|
199
|
+
params.dy,
|
|
200
|
+
'amt-in-max':
|
|
201
|
+
params.amount ||
|
|
202
|
+
params.dx ||
|
|
203
|
+
params['amt-in'] ||
|
|
204
|
+
params['amt-in-max'] ||
|
|
205
|
+
params['y-amount'] ||
|
|
206
|
+
params['x-amount'] ||
|
|
207
|
+
params.dy,
|
|
208
|
+
'y-amount':
|
|
209
|
+
params.amount ||
|
|
210
|
+
params.dx ||
|
|
211
|
+
params['amt-in'] ||
|
|
212
|
+
params['amt-in-max'] ||
|
|
213
|
+
params['y-amount'] ||
|
|
214
|
+
params['x-amount'] ||
|
|
215
|
+
params.dy,
|
|
216
|
+
'x-amount':
|
|
217
|
+
params.amount ||
|
|
218
|
+
params.dx ||
|
|
219
|
+
params['amt-in'] ||
|
|
220
|
+
params['amt-in-max'] ||
|
|
221
|
+
params['y-amount'] ||
|
|
222
|
+
params['x-amount'] ||
|
|
223
|
+
params.dy,
|
|
224
|
+
|
|
225
|
+
'min-received': rawResult,
|
|
226
|
+
'min-dy': rawResult,
|
|
227
|
+
'min-dz': rawResult,
|
|
228
|
+
'min-dw': rawResult,
|
|
229
|
+
'amt-out': rawResult,
|
|
230
|
+
'amt-out-min': rawResult,
|
|
231
|
+
'min-x-amount': rawResult,
|
|
232
|
+
'min-dv': rawResult,
|
|
233
|
+
'min-y-amount': rawResult,
|
|
234
|
+
'min-dx': rawResult,
|
|
235
|
+
},
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
const quoteResult: RouteQuote = {
|
|
239
|
+
route: {
|
|
240
|
+
...route,
|
|
241
|
+
quoteData: updatedQuoteData,
|
|
242
|
+
swapData: updatedSwapData,
|
|
243
|
+
},
|
|
244
|
+
quote: convertedResult,
|
|
245
|
+
params: params,
|
|
246
|
+
quoteData: updatedQuoteData,
|
|
247
|
+
swapData: updatedSwapData,
|
|
248
|
+
dexPath: route.dex_path,
|
|
249
|
+
tokenPath: route.token_path,
|
|
250
|
+
tokenXDecimals: tokenXDecimals,
|
|
251
|
+
tokenYDecimals: tokenYDecimals,
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
allRoutes.push(quoteResult);
|
|
255
|
+
} else {
|
|
256
|
+
throw new Error('Invalid quote result');
|
|
257
|
+
}
|
|
258
|
+
} catch (error) {
|
|
259
|
+
console.warn(
|
|
260
|
+
`Failed to get quote for route ${routeIndex + 1}:`,
|
|
261
|
+
route,
|
|
262
|
+
error
|
|
263
|
+
);
|
|
264
|
+
allRoutes.push({
|
|
265
|
+
route,
|
|
266
|
+
quote: null,
|
|
267
|
+
params: route.quoteData
|
|
268
|
+
? { ...route.quoteData.parameters, amountInput }
|
|
269
|
+
: { amountInput },
|
|
270
|
+
quoteData: route.quoteData,
|
|
271
|
+
swapData: route.swapData,
|
|
272
|
+
dexPath: route.dex_path,
|
|
273
|
+
tokenPath: route.token_path,
|
|
274
|
+
tokenXDecimals: route.tokenXDecimals,
|
|
275
|
+
tokenYDecimals: route.tokenYDecimals,
|
|
276
|
+
error: (error as Error).message,
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
allRoutes.sort((a, b) => (b.quote || 0) - (a.quote || 0));
|
|
282
|
+
|
|
283
|
+
const result = {
|
|
284
|
+
bestRoute: allRoutes[0]?.quote !== null ? allRoutes[0] : null,
|
|
285
|
+
allRoutes,
|
|
286
|
+
inputData: { tokenX, tokenY, amountInput },
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
return result;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
public async getSwapParams(
|
|
293
|
+
swapExecutionData: SwapExecutionData,
|
|
294
|
+
senderAddress: string,
|
|
295
|
+
slippageTolerance: number = 0.015
|
|
296
|
+
): Promise<SwapDataParamsAndPostConditions> {
|
|
297
|
+
const { route, amount, tokenXDecimals, tokenYDecimals } = swapExecutionData;
|
|
298
|
+
|
|
299
|
+
const executionData: SwapExecutionData = {
|
|
300
|
+
route: {
|
|
301
|
+
...route,
|
|
302
|
+
swapData: {
|
|
303
|
+
...route.swapData,
|
|
304
|
+
parameters: {
|
|
305
|
+
...route.swapData.parameters,
|
|
306
|
+
amount:
|
|
307
|
+
route.swapData.parameters.amount ||
|
|
308
|
+
amount ||
|
|
309
|
+
route.swapData.parameters['amt-in'] ||
|
|
310
|
+
route.swapData.parameters['amt-in-max'] ||
|
|
311
|
+
route.swapData.parameters['y-amount'],
|
|
312
|
+
dx:
|
|
313
|
+
route.swapData.parameters.dx ||
|
|
314
|
+
amount ||
|
|
315
|
+
route.swapData.parameters['amt-in'] ||
|
|
316
|
+
route.swapData.parameters['amt-in-max'] ||
|
|
317
|
+
route.swapData.parameters['y-amount'],
|
|
318
|
+
'amt-in':
|
|
319
|
+
route.swapData.parameters.dx ||
|
|
320
|
+
amount ||
|
|
321
|
+
route.swapData.parameters['amt-in'] ||
|
|
322
|
+
route.swapData.parameters['amt-in-max'] ||
|
|
323
|
+
route.swapData.parameters['y-amount'] ||
|
|
324
|
+
route.swapData.parameters.dy,
|
|
325
|
+
'amt-in-max':
|
|
326
|
+
route.swapData.parameters.dx ||
|
|
327
|
+
amount ||
|
|
328
|
+
route.swapData.parameters['amt-in'] ||
|
|
329
|
+
route.swapData.parameters['amt-in-max'] ||
|
|
330
|
+
route.swapData.parameters['y-amount'] ||
|
|
331
|
+
route.swapData.parameters.dy,
|
|
332
|
+
'y-amount':
|
|
333
|
+
route.swapData.parameters.dx ||
|
|
334
|
+
amount ||
|
|
335
|
+
route.swapData.parameters['amt-in'] ||
|
|
336
|
+
route.swapData.parameters['amt-in-max'] ||
|
|
337
|
+
route.swapData.parameters['y-amount'] ||
|
|
338
|
+
route.swapData.parameters.dy,
|
|
339
|
+
dy:
|
|
340
|
+
route.swapData.parameters.dy ||
|
|
341
|
+
amount ||
|
|
342
|
+
route.swapData.parameters['amt-in'] ||
|
|
343
|
+
route.swapData.parameters['amt-in-max'] ||
|
|
344
|
+
route.swapData.parameters['y-amount'] ||
|
|
345
|
+
route.swapData.parameters.dy,
|
|
346
|
+
},
|
|
347
|
+
},
|
|
348
|
+
},
|
|
349
|
+
amount,
|
|
350
|
+
tokenXDecimals: tokenXDecimals,
|
|
351
|
+
tokenYDecimals: tokenYDecimals,
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
return await executeGetParams(
|
|
355
|
+
executionData,
|
|
356
|
+
senderAddress,
|
|
357
|
+
slippageTolerance,
|
|
358
|
+
this.context
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
public async executeSwap(
|
|
363
|
+
swapExecutionData: SwapExecutionData,
|
|
364
|
+
senderAddress: string,
|
|
365
|
+
slippageTolerance: number = 0.015,
|
|
366
|
+
stacksProvider?: StacksProvider,
|
|
367
|
+
onFinish?: (data: any) => void,
|
|
368
|
+
onCancel?: () => void
|
|
369
|
+
): Promise<void> {
|
|
370
|
+
const swapParams = await this.getSwapParams(
|
|
371
|
+
swapExecutionData,
|
|
372
|
+
senderAddress,
|
|
373
|
+
slippageTolerance
|
|
374
|
+
);
|
|
375
|
+
|
|
376
|
+
await executeSwapHelper(
|
|
377
|
+
swapParams,
|
|
378
|
+
senderAddress,
|
|
379
|
+
this.context,
|
|
380
|
+
stacksProvider,
|
|
381
|
+
onFinish,
|
|
382
|
+
onCancel
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { BitflowSDKConfig } from './types';
|
|
2
|
+
|
|
3
|
+
export const configs: BitflowSDKConfig = {
|
|
4
|
+
BITFLOW_API_HOST:
|
|
5
|
+
process.env.NEXT_PUBLIC_BITFLOW_API_HOST ?? process.env.BITFLOW_API_HOST,
|
|
6
|
+
BITFLOW_API_KEY:
|
|
7
|
+
process.env.NEXT_PUBLIC_BITFLOW_API_KEY ?? process.env.BITFLOW_API_KEY,
|
|
8
|
+
READONLY_CALL_API_HOST:
|
|
9
|
+
process.env.NEXT_PUBLIC_READONLY_CALL_API_HOST ??
|
|
10
|
+
process.env.READONLY_CALL_API_HOST,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export function validateConfig() {
|
|
14
|
+
const requiredEnvVars = Object.keys(configs) as (keyof BitflowSDKConfig)[];
|
|
15
|
+
|
|
16
|
+
for (const envVar of requiredEnvVars) {
|
|
17
|
+
if (!configs[envVar]) {
|
|
18
|
+
console.error(`Missing required configuration: ${envVar}`);
|
|
19
|
+
throw new Error(`Missing required configuration: ${envVar}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|