@kaspacom/swap-sdk 1.1.3 → 1.1.4

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.
@@ -0,0 +1,239 @@
1
+ import { Trade, Pair } from '@uniswap/v2-sdk';
2
+ import { Currency, TradeType, Token } from '@uniswap/sdk-core';
3
+ import { Eip1193Provider, BrowserProvider, JsonRpcProvider, Signer, Contract, ContractTransactionResponse, TransactionResponse } from 'ethers';
4
+
5
+ interface Erc20Token {
6
+ address: string;
7
+ symbol: string;
8
+ name: string;
9
+ decimals: number;
10
+ balance?: number;
11
+ rawBalance?: string;
12
+ logoURI?: string;
13
+ }
14
+ interface SwapSettings {
15
+ maxSlippage: string;
16
+ swapDeadline: number;
17
+ }
18
+ interface SwapState {
19
+ needsApproval: boolean;
20
+ isApproved: boolean;
21
+ isApproving: boolean;
22
+ isSwapping: boolean;
23
+ error: string | null;
24
+ success: boolean;
25
+ transactionHash: string | null;
26
+ status: string | null;
27
+ expectedAmountOut: string | null;
28
+ isCalculating: boolean;
29
+ }
30
+ interface SwapSectionData {
31
+ title: string;
32
+ amount: string;
33
+ usdValue: string;
34
+ }
35
+ interface SwapSdkOptions {
36
+ networkConfig: SwapSdkNetworkConfig | string;
37
+ walletProvider?: Eip1193Provider;
38
+ partnerKey?: string;
39
+ onChange?: (state: SwapControllerOutput, patch: Partial<SwapControllerOutput>) => Promise<void>;
40
+ }
41
+ interface SwapControllerInput {
42
+ fromToken?: Erc20Token | null;
43
+ toToken?: Erc20Token | null;
44
+ amount?: number;
45
+ isOutputAmount?: boolean;
46
+ settings?: Partial<SwapSettings>;
47
+ }
48
+ declare enum LoaderStatuses {
49
+ CALCULATING_QUOTE = 1,
50
+ APPROVING = 2,
51
+ SWAPPING = 3
52
+ }
53
+ interface ComputedAmounts {
54
+ maxAmountIn?: string;
55
+ minAmountOut?: string;
56
+ amountIn: string;
57
+ amountOut: string;
58
+ amountInRaw: string;
59
+ amountOutRaw: string;
60
+ maxAmountInRaw?: string;
61
+ minAmountOutRaw?: string;
62
+ }
63
+ interface SwapControllerOutput {
64
+ error?: string;
65
+ txHash?: string;
66
+ approveTxHash?: string;
67
+ tradeInfo?: Trade<Currency, Currency, TradeType.EXACT_INPUT> | Trade<Currency, Currency, TradeType.EXACT_OUTPUT>;
68
+ computed?: ComputedAmounts;
69
+ loader: LoaderStatuses | null;
70
+ }
71
+ interface SwapSdkNetworkConfig {
72
+ name: string;
73
+ chainId: number;
74
+ rpcUrl: string;
75
+ routerAddress: string;
76
+ factoryAddress: string;
77
+ proxyAddress?: string;
78
+ graphEndpoint: string;
79
+ blockExplorerUrl?: string;
80
+ additionalJsonRpcApiProviderOptionsOptions?: any;
81
+ nativeToken: Erc20Token;
82
+ wrappedToken: Erc20Token;
83
+ }
84
+
85
+ declare class WalletService {
86
+ private networkProvider;
87
+ private signer;
88
+ private address;
89
+ private config;
90
+ private injectedProvider?;
91
+ private walletProvider?;
92
+ constructor(config: SwapSdkNetworkConfig, injectedProvider?: Eip1193Provider);
93
+ connect(injectedProvider?: Eip1193Provider): Promise<string>;
94
+ disconnect(): void;
95
+ isConnected(): boolean;
96
+ getAddress(): string | null;
97
+ getProvider(): BrowserProvider | JsonRpcProvider | null;
98
+ getSigner(): Signer | null;
99
+ getCurrentChainId(): Promise<number>;
100
+ connectWallet(): Promise<string>;
101
+ disconnectWallet(): void;
102
+ }
103
+
104
+ declare class SwapService {
105
+ private config;
106
+ private swapOptions;
107
+ private provider;
108
+ private signer;
109
+ private routerContract;
110
+ private factoryContract;
111
+ private proxyContract?;
112
+ private wethAddress;
113
+ private chainId;
114
+ private pairs;
115
+ private pairsLoadedPromise;
116
+ private resolvePairsLoaded;
117
+ private partnerFeeLoadedPromise;
118
+ private resolvePartnerFeeLoaded;
119
+ private partnerFee;
120
+ private wethToken;
121
+ constructor(provider: BrowserProvider | JsonRpcProvider, config: SwapSdkNetworkConfig, swapOptions: SwapSdkOptions);
122
+ loadPartnerFee(): Promise<bigint>;
123
+ setSigner(signer: Signer): void;
124
+ /**
125
+ * Rounds a number string to the specified number of decimal places
126
+ * to avoid parseUnits errors with too many decimals
127
+ */
128
+ private roundToDecimals;
129
+ /**
130
+ * Loads all pairs from The Graph and caches them as Uniswap SDK Pair instances.
131
+ * @param graphEndpoint The GraphQL endpoint URL
132
+ */
133
+ loadAllPairsFromGraph(): Promise<void>;
134
+ waitForPairsLoaded(): Promise<void>;
135
+ waitForPartnerFeeLoaded(): Promise<void>;
136
+ createSDKPair(pair: {
137
+ id: string;
138
+ token0: {
139
+ id: string;
140
+ symbol: string;
141
+ name: string;
142
+ decimals: string | number;
143
+ };
144
+ token1: {
145
+ id: string;
146
+ symbol: string;
147
+ name: string;
148
+ decimals: string | number;
149
+ };
150
+ reserve0?: string;
151
+ reserve1?: string;
152
+ positionValueUsd?: number;
153
+ totalTokens?: number;
154
+ }): Pair;
155
+ /**
156
+ * Returns the cached pairs for use in routing.
157
+ */
158
+ getPairs(): Pair[];
159
+ /**
160
+ * Finds the best trade path using Uniswap SDK for a given input amount.
161
+ * Returns the best path as an array of addresses, or null if no trade found.
162
+ */
163
+ private getBestTrade;
164
+ private trimTrailingZeros;
165
+ /**
166
+ *
167
+ * @param sellToken
168
+ * @param buyToken
169
+ * @param targetAmount
170
+ * @param isOutputAmount true if user input output (How much tokens to receive) and not input (how much tokens to sell)
171
+ * @param slippage
172
+ * @returns
173
+ */
174
+ calculateTrade(sellToken: Erc20Token, buyToken: Erc20Token, targetAmount: string, isOutputAmount: boolean, slippage: string): Promise<{
175
+ trade: Trade<Currency, Currency, TradeType.EXACT_INPUT> | Trade<Currency, Currency, TradeType.EXACT_OUTPUT>;
176
+ computed: ComputedAmounts;
177
+ }>;
178
+ checkApproval(tokenAddress: string, amount: string, spenderAddress: string): Promise<boolean>;
179
+ isApprovalNeeded(fromToken: Erc20Token, amountInWei: bigint): Promise<{
180
+ isApprovalNeeded: boolean;
181
+ tokenContract?: Contract;
182
+ signerAddress?: string;
183
+ allowanceTo?: string;
184
+ }>;
185
+ approveIfNeedApproval(fromToken: Erc20Token, amountInWei: bigint): Promise<ContractTransactionResponse | null>;
186
+ swapTokens(fromToken: Erc20Token, toToken: Erc20Token, amountInWei: string, amountOutWei: string, path: string[], isOutputAmount: boolean, deadline: number): Promise<TransactionResponse>;
187
+ getPairAddress(tokenA: string, tokenB: string): Promise<string>;
188
+ checkLiquidityExists(tokenA: string, tokenB: string): Promise<boolean>;
189
+ createTrade(fromToken: Erc20Token, toToken: Erc20Token, amountIn: string, slippageTolerance?: number): Promise<Trade<Token, Token, TradeType>>;
190
+ /**
191
+ * Fetch tokens from the graph endpoint (subgraph)
192
+ * @param graphEndpoint The GraphQL endpoint URL
193
+ * @param search Optional search string for symbol or name
194
+ */
195
+ getTokensFromGraph(limit?: number, search?: string): Promise<Erc20Token[]>;
196
+ /**
197
+ * Concatenates bytes: selector, array of bytes (each element is Uint8Array), array length (uint8, 1 byte), marker (bytes16(keccak256(markerString)))
198
+ * @param selectorBytes Uint8Array — function selector (usually 4 bytes)
199
+ * @param arrayOfBytes Uint8Array[] — array of bytes (each element is Uint8Array)
200
+ * @param markerString string — string from which bytes16(keccak256(...)) will be derived
201
+ * @returns Uint8Array — concatenated result
202
+ */
203
+ private concatSelectorAndParams;
204
+ }
205
+
206
+ declare const DEFAULT_SETTINGS: SwapSettings;
207
+ declare class SwapSdkController {
208
+ private walletService;
209
+ private swapService;
210
+ private state;
211
+ private input;
212
+ private options;
213
+ constructor(options: SwapSdkOptions);
214
+ private initServices;
215
+ private setChange;
216
+ connectWallet(injectedProvider?: Eip1193Provider): Promise<string>;
217
+ disconnectWallet(): void;
218
+ getState(): SwapControllerOutput;
219
+ private get settings();
220
+ calculateQuoteIfNeeded(): Promise<void>;
221
+ setData(input: Partial<SwapControllerInput>): Promise<SwapControllerOutput>;
222
+ isNeedApproval(): Promise<boolean>;
223
+ approveIfNeeded(): Promise<string | undefined>;
224
+ swap(): Promise<string>;
225
+ getPartnerFee(): Promise<number>;
226
+ getTokensFromGraph(limit?: number, search?: string): Promise<Erc20Token[]>;
227
+ get currentNetworkConfig(): SwapSdkNetworkConfig;
228
+ }
229
+
230
+ declare const NETWORKS: Record<string, SwapSdkNetworkConfig>;
231
+
232
+ /**
233
+ * Create a new headless swap controller
234
+ * @param options Configuration options for the controller
235
+ * @returns SwapSdkController instance
236
+ */
237
+ declare function createKaspaComSwapController(options: SwapSdkOptions): SwapSdkController;
238
+
239
+ export { type ComputedAmounts, DEFAULT_SETTINGS as DEFAULT_SWAP_SETTINGS, type Erc20Token, LoaderStatuses, NETWORKS, type SwapControllerInput, type SwapControllerOutput, SwapSdkController, type SwapSdkNetworkConfig, type SwapSdkOptions, type SwapSectionData, SwapService, type SwapSettings, type SwapState, WalletService, createKaspaComSwapController };