riskmarket-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.
Potentially problematic release.
This version of riskmarket-sdk might be problematic. Click here for more details.
- package/README.md +418 -0
- package/dist/ProtocolSDK.d.ts +82 -0
- package/dist/ProtocolSDK.d.ts.map +1 -0
- package/dist/ProtocolSDK.js +129 -0
- package/dist/abi/ExampleABI.d.ts +160 -0
- package/dist/abi/ExampleABI.d.ts.map +1 -0
- package/dist/abi/ExampleABI.js +92 -0
- package/dist/api/APIClient.d.ts +106 -0
- package/dist/api/APIClient.d.ts.map +1 -0
- package/dist/api/APIClient.js +201 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.esm.js +729 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +746 -0
- package/dist/index.js.map +1 -0
- package/dist/transactions/TransactionBuilder.d.ts +89 -0
- package/dist/transactions/TransactionBuilder.d.ts.map +1 -0
- package/dist/transactions/TransactionBuilder.js +111 -0
- package/dist/types/index.d.ts +109 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +21 -0
- package/dist/utils/constants.d.ts +83 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/constants.js +82 -0
- package/dist/utils/helpers.d.ts +75 -0
- package/dist/utils/helpers.d.ts.map +1 -0
- package/dist/utils/helpers.js +185 -0
- package/package.json +51 -0
package/README.md
ADDED
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
# Risk market SDK
|
|
2
|
+
|
|
3
|
+
A TypeScript SDK for interacting with Risk market smart contracts and APIs.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- ๐ฅ **Type-safe** - Full TypeScript support with comprehensive type definitions
|
|
8
|
+
- ๐ฏ **Three-tier Architecture** - Organized into API, Transactions, and Utilities
|
|
9
|
+
- ๐ **Provider Agnostic** - Works with any ethers.js provider
|
|
10
|
+
- โก **Transaction Preparation** - Returns unsigned transactions for frontend signing
|
|
11
|
+
- ๐ก๏ธ **Error Handling** - Robust error handling with custom error types
|
|
12
|
+
- ๐ฆ **Lightweight** - Minimal dependencies
|
|
13
|
+
- ๐งช **Well Tested** - Comprehensive test coverage
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @yourorg/protocol-sdk ethers
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Or with yarn:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
yarn add @yourorg/protocol-sdk ethers
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Quick Start
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { ProtocolSDK } from "@yourorg/protocol-sdk";
|
|
31
|
+
import { ethers } from "ethers";
|
|
32
|
+
import YourContractABI from "./abis/YourContract.json";
|
|
33
|
+
|
|
34
|
+
// Initialize the SDK
|
|
35
|
+
const provider = new ethers.JsonRpcProvider("YOUR_RPC_URL");
|
|
36
|
+
|
|
37
|
+
const sdk = new ProtocolSDK({
|
|
38
|
+
apiBaseURL: "https://api.yourprotocol.com",
|
|
39
|
+
provider: provider,
|
|
40
|
+
contractAddress: "0x...", // Your contract address
|
|
41
|
+
abi: YourContractABI,
|
|
42
|
+
apiKey: "your-api-key", // Optional
|
|
43
|
+
chainId: 1,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Use the SDK
|
|
47
|
+
const markets = await sdk.api.getAllMarkets();
|
|
48
|
+
console.log("Available markets:", markets);
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Architecture
|
|
52
|
+
|
|
53
|
+
The SDK is organized into three main components:
|
|
54
|
+
|
|
55
|
+
### 1. **APIClient** - Fetching API Data
|
|
56
|
+
|
|
57
|
+
Handles all API requests to your backend server.
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
// Get market data
|
|
61
|
+
const marketData = await sdk.api.getMarketData("market-id");
|
|
62
|
+
|
|
63
|
+
// Get user balance
|
|
64
|
+
const balance = await sdk.api.getUserBalance(userAddress, tokenAddress);
|
|
65
|
+
|
|
66
|
+
// Get transaction history
|
|
67
|
+
const txHistory = await sdk.api.getTransactionHistory(userAddress);
|
|
68
|
+
|
|
69
|
+
// Get token prices
|
|
70
|
+
const prices = await sdk.api.getTokenPrices([token1, token2]);
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### 2. **TransactionBuilder** - Creating Transactions
|
|
74
|
+
|
|
75
|
+
Prepares unsigned transactions that can be signed by the user's wallet.
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
// Prepare a swap transaction
|
|
79
|
+
const swapTx = await sdk.tx.prepareSwapTransaction({
|
|
80
|
+
tokenIn: "0x...",
|
|
81
|
+
tokenOut: "0x...",
|
|
82
|
+
amount: sdk.utils.parseAmount("1.0", 18),
|
|
83
|
+
minAmountOut: sdk.utils.parseAmount("0.95", 18),
|
|
84
|
+
deadline: sdk.utils.getDeadline(20),
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// Sign and send (in your frontend)
|
|
88
|
+
const signer = provider.getSigner();
|
|
89
|
+
const txResponse = await signer.sendTransaction(swapTx);
|
|
90
|
+
const receipt = await txResponse.wait();
|
|
91
|
+
console.log("Transaction confirmed:", receipt.hash);
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### 3. **Utils** - Helper Functions
|
|
95
|
+
|
|
96
|
+
Utility functions for common operations.
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
// Validate addresses
|
|
100
|
+
const isValid = sdk.utils.isValidAddress("0x...");
|
|
101
|
+
|
|
102
|
+
// Format amounts
|
|
103
|
+
const formatted = sdk.utils.formatAmount("1000000000000000000", 18); // "1.0"
|
|
104
|
+
|
|
105
|
+
// Parse amounts
|
|
106
|
+
const parsed = sdk.utils.parseAmount("1.5", 18); // "1500000000000000000"
|
|
107
|
+
|
|
108
|
+
// Get deadline
|
|
109
|
+
const deadline = sdk.utils.getDeadline(20); // 20 minutes from now
|
|
110
|
+
|
|
111
|
+
// Shorten hash for display
|
|
112
|
+
const short = sdk.utils.shortenHash(txHash); // "0x1234...abcd"
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Core Classes
|
|
116
|
+
|
|
117
|
+
### ProtocolSDK (Main Class)
|
|
118
|
+
|
|
119
|
+
The main entry point that combines all functionality.
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
const sdk = new ProtocolSDK(config);
|
|
123
|
+
|
|
124
|
+
// Access components
|
|
125
|
+
sdk.api; // APIClient instance
|
|
126
|
+
sdk.tx; // TransactionBuilder instance
|
|
127
|
+
sdk.utils; // Utils class
|
|
128
|
+
|
|
129
|
+
// Helper methods
|
|
130
|
+
await sdk.getNetworkInfo();
|
|
131
|
+
await sdk.getBlockNumber();
|
|
132
|
+
await sdk.isConnected();
|
|
133
|
+
await sdk.waitForTransaction(txHash);
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### APIClient
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
class APIClient {
|
|
140
|
+
async getMarketData(marketId: string): Promise<MarketData>;
|
|
141
|
+
async getAllMarkets(): Promise<MarketData[]>;
|
|
142
|
+
async getUserBalance(address: string, token: string): Promise<UserBalance>;
|
|
143
|
+
async getAllBalances(address: string): Promise<UserBalance[]>;
|
|
144
|
+
async getTransactionHistory(
|
|
145
|
+
address: string,
|
|
146
|
+
limit?: number
|
|
147
|
+
): Promise<Transaction[]>;
|
|
148
|
+
async getTokenPrice(tokenAddress: string): Promise<string>;
|
|
149
|
+
async getTokenPrices(
|
|
150
|
+
tokenAddresses: string[]
|
|
151
|
+
): Promise<Record<string, string>>;
|
|
152
|
+
async get<T>(endpoint: string, params?: any): Promise<T>;
|
|
153
|
+
async post<T>(endpoint: string, data: any): Promise<T>;
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### TransactionBuilder
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
class TransactionBuilder {
|
|
161
|
+
async prepareSwapTransaction(params: SwapParams): Promise<TransactionRequest>;
|
|
162
|
+
async prepareDepositTransaction(
|
|
163
|
+
params: DepositParams
|
|
164
|
+
): Promise<TransactionRequest>;
|
|
165
|
+
async prepareWithdrawTransaction(
|
|
166
|
+
params: WithdrawParams
|
|
167
|
+
): Promise<TransactionRequest>;
|
|
168
|
+
async prepareApprovalTransaction(
|
|
169
|
+
token: string,
|
|
170
|
+
spender?: string,
|
|
171
|
+
amount?: string
|
|
172
|
+
): Promise<TransactionRequest>;
|
|
173
|
+
async checkAllowance(
|
|
174
|
+
token: string,
|
|
175
|
+
owner: string,
|
|
176
|
+
spender?: string
|
|
177
|
+
): Promise<string>;
|
|
178
|
+
async needsApproval(
|
|
179
|
+
token: string,
|
|
180
|
+
owner: string,
|
|
181
|
+
amount: string
|
|
182
|
+
): Promise<boolean>;
|
|
183
|
+
async getTokenBalance(token: string, address: string): Promise<string>;
|
|
184
|
+
async estimateGas(tx: TransactionRequest): Promise<string>;
|
|
185
|
+
async getGasPrices(): Promise<GasPrices>;
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Utils
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
class Utils {
|
|
193
|
+
static isValidAddress(address: string): boolean;
|
|
194
|
+
static validateAddress(address: string): string;
|
|
195
|
+
static formatAmount(amount: string | bigint, decimals?: number): string;
|
|
196
|
+
static parseAmount(amount: string, decimals?: number): string;
|
|
197
|
+
static validateAmount(amount: string): void;
|
|
198
|
+
static getDeadline(minutes?: number): number;
|
|
199
|
+
static calculatePercentageChange(oldValue: string, newValue: string): string;
|
|
200
|
+
static addGasBuffer(
|
|
201
|
+
gasEstimate: bigint | string,
|
|
202
|
+
bufferPercent?: number
|
|
203
|
+
): string;
|
|
204
|
+
static parseContractError(error: any): string;
|
|
205
|
+
static waitForTransaction(
|
|
206
|
+
txHash: string,
|
|
207
|
+
provider: Provider,
|
|
208
|
+
confirmations?: number
|
|
209
|
+
): Promise<TransactionReceipt>;
|
|
210
|
+
static getGasPrices(provider: Provider): Promise<GasPrices>;
|
|
211
|
+
static getNetworkName(chainId: number): string;
|
|
212
|
+
static sleep(ms: number): Promise<void>;
|
|
213
|
+
static retry<T>(
|
|
214
|
+
fn: () => Promise<T>,
|
|
215
|
+
maxRetries?: number,
|
|
216
|
+
delayMs?: number
|
|
217
|
+
): Promise<T>;
|
|
218
|
+
static shortenHash(
|
|
219
|
+
hash: string,
|
|
220
|
+
startLength?: number,
|
|
221
|
+
endLength?: number
|
|
222
|
+
): string;
|
|
223
|
+
static isTransactionSuccessful(receipt: TransactionReceipt): boolean;
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## Complete Workflow Example
|
|
228
|
+
|
|
229
|
+
Here's a complete example of using the SDK in a DApp:
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
import { ProtocolSDK } from "@yourorg/protocol-sdk";
|
|
233
|
+
import { ethers } from "ethers";
|
|
234
|
+
|
|
235
|
+
async function performSwap() {
|
|
236
|
+
// 1. Initialize SDK
|
|
237
|
+
const provider = new ethers.BrowserProvider(window.ethereum);
|
|
238
|
+
const sdk = new ProtocolSDK({
|
|
239
|
+
apiBaseURL: "https://api.yourprotocol.com",
|
|
240
|
+
provider: await provider.provider,
|
|
241
|
+
contractAddress: "0x...",
|
|
242
|
+
abi: ContractABI,
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
// 2. Get user address
|
|
246
|
+
const signer = await provider.getSigner();
|
|
247
|
+
const userAddress = await signer.getAddress();
|
|
248
|
+
|
|
249
|
+
// 3. Fetch market data
|
|
250
|
+
const markets = await sdk.api.getAllMarkets();
|
|
251
|
+
console.log("Available markets:", markets);
|
|
252
|
+
|
|
253
|
+
// 4. Check token balance
|
|
254
|
+
const tokenAddress = "0xTokenAddress";
|
|
255
|
+
const balance = await sdk.tx.getTokenBalance(tokenAddress, userAddress);
|
|
256
|
+
console.log("Balance:", sdk.utils.formatAmount(balance, 18));
|
|
257
|
+
|
|
258
|
+
// 5. Check if approval is needed
|
|
259
|
+
const amount = sdk.utils.parseAmount("1.0", 18);
|
|
260
|
+
const needsApproval = await sdk.tx.needsApproval(
|
|
261
|
+
tokenAddress,
|
|
262
|
+
userAddress,
|
|
263
|
+
amount
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
// 6. If needed, approve
|
|
267
|
+
if (needsApproval) {
|
|
268
|
+
const approvalTx = await sdk.tx.prepareApprovalTransaction(tokenAddress);
|
|
269
|
+
const approveTxResponse = await signer.sendTransaction(approvalTx);
|
|
270
|
+
await approveTxResponse.wait();
|
|
271
|
+
console.log("Approval confirmed");
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// 7. Prepare swap transaction
|
|
275
|
+
const swapTx = await sdk.tx.prepareSwapTransaction({
|
|
276
|
+
tokenIn: tokenAddress,
|
|
277
|
+
tokenOut: "0xTokenOutAddress",
|
|
278
|
+
amount: amount,
|
|
279
|
+
minAmountOut: sdk.utils.parseAmount("0.95", 18),
|
|
280
|
+
deadline: sdk.utils.getDeadline(20),
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
// 8. Sign and send
|
|
284
|
+
const txResponse = await signer.sendTransaction(swapTx);
|
|
285
|
+
console.log("Transaction sent:", txResponse.hash);
|
|
286
|
+
|
|
287
|
+
// 9. Wait for confirmation
|
|
288
|
+
const receipt = await sdk.waitForTransaction(txResponse.hash);
|
|
289
|
+
|
|
290
|
+
if (sdk.utils.isTransactionSuccessful(receipt)) {
|
|
291
|
+
console.log("โ
Swap successful!");
|
|
292
|
+
|
|
293
|
+
// 10. Fetch updated balance
|
|
294
|
+
const newBalance = await sdk.tx.getTokenBalance(tokenAddress, userAddress);
|
|
295
|
+
console.log("New balance:", sdk.utils.formatAmount(newBalance, 18));
|
|
296
|
+
} else {
|
|
297
|
+
console.error("โ Transaction failed");
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
## Error Handling
|
|
303
|
+
|
|
304
|
+
The SDK uses custom error types for better error handling:
|
|
305
|
+
|
|
306
|
+
```typescript
|
|
307
|
+
import { SDKError, ErrorType } from "@yourorg/protocol-sdk";
|
|
308
|
+
|
|
309
|
+
try {
|
|
310
|
+
await sdk.api.getMarketData("invalid-id");
|
|
311
|
+
} catch (error) {
|
|
312
|
+
if (error instanceof SDKError) {
|
|
313
|
+
console.log("Error type:", error.type); // API_ERROR, TRANSACTION_ERROR, etc.
|
|
314
|
+
console.log("Message:", error.message);
|
|
315
|
+
console.log("Details:", error.details);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
Error types:
|
|
321
|
+
|
|
322
|
+
- `API_ERROR` - API request failed
|
|
323
|
+
- `TRANSACTION_ERROR` - Transaction preparation or execution failed
|
|
324
|
+
- `VALIDATION_ERROR` - Invalid input parameters
|
|
325
|
+
- `NETWORK_ERROR` - Network connectivity issues
|
|
326
|
+
|
|
327
|
+
## TypeScript Support
|
|
328
|
+
|
|
329
|
+
The SDK is fully typed. Import types as needed:
|
|
330
|
+
|
|
331
|
+
```typescript
|
|
332
|
+
import type {
|
|
333
|
+
SDKConfig,
|
|
334
|
+
SwapParams,
|
|
335
|
+
DepositParams,
|
|
336
|
+
MarketData,
|
|
337
|
+
UserBalance,
|
|
338
|
+
Transaction,
|
|
339
|
+
} from "@yourorg/protocol-sdk";
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
## Constants
|
|
343
|
+
|
|
344
|
+
The SDK exports useful constants:
|
|
345
|
+
|
|
346
|
+
```typescript
|
|
347
|
+
import { NETWORKS, GAS_LIMITS, MAX_UINT256 } from "@yourorg/protocol-sdk";
|
|
348
|
+
|
|
349
|
+
console.log(NETWORKS.MAINNET); // { chainId: 1, name: 'Ethereum Mainnet', ... }
|
|
350
|
+
console.log(GAS_LIMITS.SWAP); // 200000
|
|
351
|
+
console.log(MAX_UINT256); // "0xfff..."
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
## Development
|
|
355
|
+
|
|
356
|
+
```bash
|
|
357
|
+
# Install dependencies
|
|
358
|
+
npm install
|
|
359
|
+
|
|
360
|
+
# Build the SDK
|
|
361
|
+
npm run build
|
|
362
|
+
|
|
363
|
+
# Run in development mode
|
|
364
|
+
npm run dev
|
|
365
|
+
|
|
366
|
+
# Run tests
|
|
367
|
+
npm test
|
|
368
|
+
|
|
369
|
+
# Lint code
|
|
370
|
+
npm run lint
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
## Project Structure
|
|
374
|
+
|
|
375
|
+
```
|
|
376
|
+
my-protocol-sdk/
|
|
377
|
+
โโโ src/
|
|
378
|
+
โ โโโ api/
|
|
379
|
+
โ โ โโโ APIClient.ts
|
|
380
|
+
โ โโโ transactions/
|
|
381
|
+
โ โ โโโ TransactionBuilder.ts
|
|
382
|
+
โ โโโ utils/
|
|
383
|
+
โ โ โโโ helpers.ts
|
|
384
|
+
โ โ โโโ constants.ts
|
|
385
|
+
โ โโโ types/
|
|
386
|
+
โ โ โโโ index.ts
|
|
387
|
+
โ โโโ abi/
|
|
388
|
+
โ โ โโโ ExampleABI.ts
|
|
389
|
+
โ โโโ ProtocolSDK.ts
|
|
390
|
+
โ โโโ index.ts
|
|
391
|
+
โโโ examples/
|
|
392
|
+
โ โโโ usage.ts
|
|
393
|
+
โโโ dist/ # Built files
|
|
394
|
+
โโโ package.json
|
|
395
|
+
โโโ tsconfig.json
|
|
396
|
+
โโโ rollup.config.js
|
|
397
|
+
โโโ README.md
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
## Contributing
|
|
401
|
+
|
|
402
|
+
Contributions are welcome! Please read our contributing guidelines first.
|
|
403
|
+
|
|
404
|
+
## License
|
|
405
|
+
|
|
406
|
+
MIT
|
|
407
|
+
|
|
408
|
+
## Support
|
|
409
|
+
|
|
410
|
+
- Documentation: [https://docs.yourprotocol.com](https://docs.yourprotocol.com)
|
|
411
|
+
- Issues: [GitHub Issues](https://github.com/yourorg/protocol-sdk/issues)
|
|
412
|
+
- Discord: [Join our community](https://discord.gg/yourprotocol)
|
|
413
|
+
|
|
414
|
+
## Changelog
|
|
415
|
+
|
|
416
|
+
See [CHANGELOG.md](CHANGELOG.md) for version history.
|
|
417
|
+
|
|
418
|
+
# raingames-sdk
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { Provider } from 'ethers';
|
|
2
|
+
import { APIClient } from './api/APIClient';
|
|
3
|
+
import { TransactionBuilder } from './transactions/TransactionBuilder';
|
|
4
|
+
import { Utils } from './utils/helpers';
|
|
5
|
+
import { SDKConfig } from './types';
|
|
6
|
+
/**
|
|
7
|
+
* Main SDK class that combines all functionality
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* import { ProtocolSDK } from '@yourorg/protocol-sdk';
|
|
12
|
+
* import { ethers } from 'ethers';
|
|
13
|
+
*
|
|
14
|
+
* const provider = new ethers.JsonRpcProvider('YOUR_RPC_URL');
|
|
15
|
+
*
|
|
16
|
+
* const sdk = new ProtocolSDK({
|
|
17
|
+
* apiBaseURL: 'https://api.yourprotocol.com',
|
|
18
|
+
* provider: provider,
|
|
19
|
+
* contractAddress: '0x...',
|
|
20
|
+
* abi: YourContractABI,
|
|
21
|
+
* apiKey: 'your-api-key', // optional
|
|
22
|
+
* });
|
|
23
|
+
*
|
|
24
|
+
* // Use the SDK
|
|
25
|
+
* const markets = await sdk.api.getAllMarkets();
|
|
26
|
+
* const tx = await sdk.tx.prepareSwapTransaction({ ... });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare class ProtocolSDK {
|
|
30
|
+
/** API client for fetching data */
|
|
31
|
+
api: APIClient;
|
|
32
|
+
/** Transaction builder for creating blockchain transactions */
|
|
33
|
+
tx: TransactionBuilder;
|
|
34
|
+
/** Utility functions */
|
|
35
|
+
utils: typeof Utils;
|
|
36
|
+
/** Provider instance */
|
|
37
|
+
provider: Provider;
|
|
38
|
+
/** Main contract address */
|
|
39
|
+
contractAddress: string;
|
|
40
|
+
/** Chain ID */
|
|
41
|
+
chainId?: number;
|
|
42
|
+
/**
|
|
43
|
+
* Initialize the SDK
|
|
44
|
+
* @param config - SDK configuration
|
|
45
|
+
*/
|
|
46
|
+
constructor(config: SDKConfig);
|
|
47
|
+
/**
|
|
48
|
+
* Validate SDK configuration
|
|
49
|
+
*/
|
|
50
|
+
private validateConfig;
|
|
51
|
+
/**
|
|
52
|
+
* Get network information
|
|
53
|
+
*/
|
|
54
|
+
getNetworkInfo(): Promise<{
|
|
55
|
+
name: string;
|
|
56
|
+
chainId: number;
|
|
57
|
+
}>;
|
|
58
|
+
/**
|
|
59
|
+
* Get current block number
|
|
60
|
+
*/
|
|
61
|
+
getBlockNumber(): Promise<number>;
|
|
62
|
+
/**
|
|
63
|
+
* Check if provider is connected
|
|
64
|
+
*/
|
|
65
|
+
isConnected(): Promise<boolean>;
|
|
66
|
+
/**
|
|
67
|
+
* Get transaction receipt
|
|
68
|
+
* @param txHash - Transaction hash
|
|
69
|
+
*/
|
|
70
|
+
getTransactionReceipt(txHash: string): Promise<import("ethers").TransactionReceipt | null>;
|
|
71
|
+
/**
|
|
72
|
+
* Wait for transaction confirmation
|
|
73
|
+
* @param txHash - Transaction hash
|
|
74
|
+
* @param confirmations - Number of confirmations to wait for
|
|
75
|
+
*/
|
|
76
|
+
waitForTransaction(txHash: string, confirmations?: number): Promise<import("ethers").TransactionReceipt | null>;
|
|
77
|
+
/**
|
|
78
|
+
* Get SDK version
|
|
79
|
+
*/
|
|
80
|
+
static getVersion(): string;
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=ProtocolSDK.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ProtocolSDK.d.ts","sourceRoot":"","sources":["../src/ProtocolSDK.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAClC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,mCAAmC,CAAC;AACvE,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACxC,OAAO,EAAE,SAAS,EAAuB,MAAM,SAAS,CAAC;AAEzD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBAAa,WAAW;IACtB,mCAAmC;IAC5B,GAAG,EAAE,SAAS,CAAC;IAEtB,+DAA+D;IACxD,EAAE,EAAE,kBAAkB,CAAC;IAE9B,wBAAwB;IACjB,KAAK,EAAE,OAAO,KAAK,CAAC;IAE3B,wBAAwB;IACjB,QAAQ,EAAE,QAAQ,CAAC;IAE1B,4BAA4B;IACrB,eAAe,EAAE,MAAM,CAAC;IAE/B,eAAe;IACR,OAAO,CAAC,EAAE,MAAM,CAAC;IAExB;;;OAGG;gBACS,MAAM,EAAE,SAAS;IAkB7B;;OAEG;IACH,OAAO,CAAC,cAAc;IAsCtB;;OAEG;IACG,cAAc,IAAI,OAAO,CAAC;QAC9B,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE,MAAM,CAAC;KACjB,CAAC;IAeF;;OAEG;IACG,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC;IAWvC;;OAEG;IACG,WAAW,IAAI,OAAO,CAAC,OAAO,CAAC;IASrC;;;OAGG;IACG,qBAAqB,CAAC,MAAM,EAAE,MAAM;IAY1C;;;;OAIG;IACG,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,aAAa,GAAE,MAAU;IAIlE;;OAEG;IACH,MAAM,CAAC,UAAU,IAAI,MAAM;CAG5B"}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { APIClient } from './api/APIClient';
|
|
2
|
+
import { TransactionBuilder } from './transactions/TransactionBuilder';
|
|
3
|
+
import { Utils } from './utils/helpers';
|
|
4
|
+
import { SDKError, ErrorType } from './types';
|
|
5
|
+
/**
|
|
6
|
+
* Main SDK class that combines all functionality
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { ProtocolSDK } from '@yourorg/protocol-sdk';
|
|
11
|
+
* import { ethers } from 'ethers';
|
|
12
|
+
*
|
|
13
|
+
* const provider = new ethers.JsonRpcProvider('YOUR_RPC_URL');
|
|
14
|
+
*
|
|
15
|
+
* const sdk = new ProtocolSDK({
|
|
16
|
+
* apiBaseURL: 'https://api.yourprotocol.com',
|
|
17
|
+
* provider: provider,
|
|
18
|
+
* contractAddress: '0x...',
|
|
19
|
+
* abi: YourContractABI,
|
|
20
|
+
* apiKey: 'your-api-key', // optional
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // Use the SDK
|
|
24
|
+
* const markets = await sdk.api.getAllMarkets();
|
|
25
|
+
* const tx = await sdk.tx.prepareSwapTransaction({ ... });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export class ProtocolSDK {
|
|
29
|
+
/**
|
|
30
|
+
* Initialize the SDK
|
|
31
|
+
* @param config - SDK configuration
|
|
32
|
+
*/
|
|
33
|
+
constructor(config) {
|
|
34
|
+
// Validate configuration
|
|
35
|
+
this.validateConfig(config);
|
|
36
|
+
// Initialize components
|
|
37
|
+
this.provider = config.provider;
|
|
38
|
+
this.contractAddress = config.contractAddress;
|
|
39
|
+
this.chainId = config.chainId;
|
|
40
|
+
this.api = new APIClient(config.apiBaseURL, config.apiKey);
|
|
41
|
+
this.tx = new TransactionBuilder(config.provider, config.contractAddress, config.abi);
|
|
42
|
+
this.utils = Utils;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Validate SDK configuration
|
|
46
|
+
*/
|
|
47
|
+
validateConfig(config) {
|
|
48
|
+
if (!config.apiBaseURL) {
|
|
49
|
+
throw new SDKError('apiBaseURL is required', ErrorType.VALIDATION_ERROR);
|
|
50
|
+
}
|
|
51
|
+
if (!config.provider) {
|
|
52
|
+
throw new SDKError('provider is required', ErrorType.VALIDATION_ERROR);
|
|
53
|
+
}
|
|
54
|
+
if (!config.contractAddress) {
|
|
55
|
+
throw new SDKError('contractAddress is required', ErrorType.VALIDATION_ERROR);
|
|
56
|
+
}
|
|
57
|
+
if (!config.abi || !Array.isArray(config.abi)) {
|
|
58
|
+
throw new SDKError('abi must be an array', ErrorType.VALIDATION_ERROR);
|
|
59
|
+
}
|
|
60
|
+
// Validate contract address format
|
|
61
|
+
if (!Utils.isValidAddress(config.contractAddress)) {
|
|
62
|
+
throw new SDKError('Invalid contract address', ErrorType.VALIDATION_ERROR);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Get network information
|
|
67
|
+
*/
|
|
68
|
+
async getNetworkInfo() {
|
|
69
|
+
try {
|
|
70
|
+
const network = await this.provider.getNetwork();
|
|
71
|
+
return {
|
|
72
|
+
name: Utils.getNetworkName(Number(network.chainId)),
|
|
73
|
+
chainId: Number(network.chainId),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
throw new SDKError(`Failed to get network info: ${error}`, ErrorType.NETWORK_ERROR);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Get current block number
|
|
82
|
+
*/
|
|
83
|
+
async getBlockNumber() {
|
|
84
|
+
try {
|
|
85
|
+
return await this.provider.getBlockNumber();
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
throw new SDKError(`Failed to get block number: ${error}`, ErrorType.NETWORK_ERROR);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Check if provider is connected
|
|
93
|
+
*/
|
|
94
|
+
async isConnected() {
|
|
95
|
+
try {
|
|
96
|
+
await this.provider.getBlockNumber();
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Get transaction receipt
|
|
105
|
+
* @param txHash - Transaction hash
|
|
106
|
+
*/
|
|
107
|
+
async getTransactionReceipt(txHash) {
|
|
108
|
+
try {
|
|
109
|
+
return await this.provider.getTransactionReceipt(txHash);
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
throw new SDKError(`Failed to get transaction receipt: ${error}`, ErrorType.NETWORK_ERROR, { txHash });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Wait for transaction confirmation
|
|
117
|
+
* @param txHash - Transaction hash
|
|
118
|
+
* @param confirmations - Number of confirmations to wait for
|
|
119
|
+
*/
|
|
120
|
+
async waitForTransaction(txHash, confirmations = 1) {
|
|
121
|
+
return Utils.waitForTransaction(txHash, this.provider, confirmations);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Get SDK version
|
|
125
|
+
*/
|
|
126
|
+
static getVersion() {
|
|
127
|
+
return '1.0.0'; // This should match package.json version
|
|
128
|
+
}
|
|
129
|
+
}
|