@silentswap/sdk 0.0.52 → 0.0.53
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/dist/client.test.d.ts +1 -0
- package/dist/client.test.js +77 -0
- package/dist/errors.d.ts +83 -0
- package/dist/errors.js +144 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +8 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { createSilentSwapClient } from './client.js';
|
|
3
|
+
import { ENVIRONMENT } from './constants.js';
|
|
4
|
+
// Mock fetch globally
|
|
5
|
+
global.fetch = vi.fn();
|
|
6
|
+
describe('SilentSwapClient', () => {
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
vi.clearAllMocks();
|
|
9
|
+
});
|
|
10
|
+
describe('createSilentSwapClient', () => {
|
|
11
|
+
it('should create a client with MAINNET environment', () => {
|
|
12
|
+
const client = createSilentSwapClient({
|
|
13
|
+
environment: ENVIRONMENT.MAINNET,
|
|
14
|
+
});
|
|
15
|
+
expect(client).toBeDefined();
|
|
16
|
+
expect(client.gatewayAddress).toBeDefined();
|
|
17
|
+
expect(client.baseUrl).toBeDefined();
|
|
18
|
+
});
|
|
19
|
+
it('should create a client with STAGING environment', () => {
|
|
20
|
+
const client = createSilentSwapClient({
|
|
21
|
+
environment: ENVIRONMENT.STAGING,
|
|
22
|
+
});
|
|
23
|
+
expect(client).toBeDefined();
|
|
24
|
+
expect(client.gatewayAddress).toBeDefined();
|
|
25
|
+
});
|
|
26
|
+
it('should throw error for unsupported environment', () => {
|
|
27
|
+
expect(() => {
|
|
28
|
+
createSilentSwapClient({
|
|
29
|
+
environment: 'INVALID',
|
|
30
|
+
});
|
|
31
|
+
}).toThrow('Unsupported environment');
|
|
32
|
+
});
|
|
33
|
+
it('should use custom baseUrl when provided', () => {
|
|
34
|
+
const customUrl = 'https://custom-api.example.com';
|
|
35
|
+
const client = createSilentSwapClient({
|
|
36
|
+
environment: ENVIRONMENT.MAINNET,
|
|
37
|
+
baseUrl: customUrl,
|
|
38
|
+
});
|
|
39
|
+
expect(client.baseUrl).toBe(customUrl);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
describe('nonce', () => {
|
|
43
|
+
it('should fetch nonce successfully', async () => {
|
|
44
|
+
const client = createSilentSwapClient({
|
|
45
|
+
environment: ENVIRONMENT.STAGING,
|
|
46
|
+
});
|
|
47
|
+
const mockResponse = {
|
|
48
|
+
nonce: 'test-nonce-123',
|
|
49
|
+
};
|
|
50
|
+
global.fetch.mockResolvedValueOnce({
|
|
51
|
+
ok: true,
|
|
52
|
+
text: async () => JSON.stringify(mockResponse),
|
|
53
|
+
});
|
|
54
|
+
const [error, result] = await client.nonce('0x1234567890123456789012345678901234567890');
|
|
55
|
+
expect(error).toBeUndefined();
|
|
56
|
+
expect(result).toBeDefined();
|
|
57
|
+
});
|
|
58
|
+
it('should handle API errors', async () => {
|
|
59
|
+
const client = createSilentSwapClient({
|
|
60
|
+
environment: ENVIRONMENT.STAGING,
|
|
61
|
+
});
|
|
62
|
+
const mockError = {
|
|
63
|
+
type: 'AUTH_ERROR',
|
|
64
|
+
error: 'Invalid address',
|
|
65
|
+
};
|
|
66
|
+
global.fetch.mockResolvedValueOnce({
|
|
67
|
+
ok: false,
|
|
68
|
+
status: 400,
|
|
69
|
+
text: async () => JSON.stringify(mockError),
|
|
70
|
+
});
|
|
71
|
+
const [error, result] = await client.nonce('invalid-address');
|
|
72
|
+
expect(error).toBeDefined();
|
|
73
|
+
expect(result).toBeUndefined();
|
|
74
|
+
expect(error?.type).toBe('AUTH_ERROR');
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
});
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom error classes for SilentSwap SDK
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Base error class for all SDK errors
|
|
6
|
+
*/
|
|
7
|
+
export declare class SilentSwapError extends Error {
|
|
8
|
+
readonly code?: string | undefined;
|
|
9
|
+
readonly cause?: Error | undefined;
|
|
10
|
+
constructor(message: string, code?: string | undefined, cause?: Error | undefined);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Error thrown when API request fails
|
|
14
|
+
*/
|
|
15
|
+
export declare class ApiError extends SilentSwapError {
|
|
16
|
+
readonly statusCode?: number | undefined;
|
|
17
|
+
readonly response?: unknown | undefined;
|
|
18
|
+
constructor(message: string, statusCode?: number | undefined, response?: unknown | undefined, cause?: Error);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Error thrown when network request fails
|
|
22
|
+
*/
|
|
23
|
+
export declare class NetworkError extends SilentSwapError {
|
|
24
|
+
readonly originalError?: Error | undefined;
|
|
25
|
+
constructor(message: string, originalError?: Error | undefined);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Error thrown when authentication fails
|
|
29
|
+
*/
|
|
30
|
+
export declare class AuthenticationError extends SilentSwapError {
|
|
31
|
+
readonly cause?: Error | undefined;
|
|
32
|
+
constructor(message: string, cause?: Error | undefined);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Error thrown when quote request fails
|
|
36
|
+
*/
|
|
37
|
+
export declare class QuoteError extends SilentSwapError {
|
|
38
|
+
readonly cause?: Error | undefined;
|
|
39
|
+
constructor(message: string, cause?: Error | undefined);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Error thrown when order creation fails
|
|
43
|
+
*/
|
|
44
|
+
export declare class OrderError extends SilentSwapError {
|
|
45
|
+
readonly cause?: Error | undefined;
|
|
46
|
+
constructor(message: string, cause?: Error | undefined);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Error thrown when validation fails
|
|
50
|
+
*/
|
|
51
|
+
export declare class ValidationError extends SilentSwapError {
|
|
52
|
+
readonly field?: string | undefined;
|
|
53
|
+
constructor(message: string, field?: string | undefined);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Error thrown when configuration is invalid
|
|
57
|
+
*/
|
|
58
|
+
export declare class ConfigurationError extends SilentSwapError {
|
|
59
|
+
readonly config?: unknown | undefined;
|
|
60
|
+
constructor(message: string, config?: unknown | undefined);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Error thrown when wallet operation fails
|
|
64
|
+
*/
|
|
65
|
+
export declare class WalletError extends SilentSwapError {
|
|
66
|
+
readonly cause?: Error | undefined;
|
|
67
|
+
constructor(message: string, cause?: Error | undefined);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Error thrown when transaction fails
|
|
71
|
+
*/
|
|
72
|
+
export declare class TransactionError extends SilentSwapError {
|
|
73
|
+
readonly transactionHash?: string | undefined;
|
|
74
|
+
readonly cause?: Error | undefined;
|
|
75
|
+
constructor(message: string, transactionHash?: string | undefined, cause?: Error | undefined);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Helper function to convert ErrorResponse to appropriate error class
|
|
79
|
+
*/
|
|
80
|
+
export declare function createErrorFromResponse(errorResponse: {
|
|
81
|
+
type: string;
|
|
82
|
+
error: string;
|
|
83
|
+
}, statusCode?: number): SilentSwapError;
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom error classes for SilentSwap SDK
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Base error class for all SDK errors
|
|
6
|
+
*/
|
|
7
|
+
export class SilentSwapError extends Error {
|
|
8
|
+
code;
|
|
9
|
+
cause;
|
|
10
|
+
constructor(message, code, cause) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.code = code;
|
|
13
|
+
this.cause = cause;
|
|
14
|
+
this.name = 'SilentSwapError';
|
|
15
|
+
// Maintains proper stack trace for where our error was thrown (only available on V8)
|
|
16
|
+
if (Error.captureStackTrace) {
|
|
17
|
+
Error.captureStackTrace(this, SilentSwapError);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Error thrown when API request fails
|
|
23
|
+
*/
|
|
24
|
+
export class ApiError extends SilentSwapError {
|
|
25
|
+
statusCode;
|
|
26
|
+
response;
|
|
27
|
+
constructor(message, statusCode, response, cause) {
|
|
28
|
+
super(message, 'API_ERROR', cause);
|
|
29
|
+
this.statusCode = statusCode;
|
|
30
|
+
this.response = response;
|
|
31
|
+
this.name = 'ApiError';
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Error thrown when network request fails
|
|
36
|
+
*/
|
|
37
|
+
export class NetworkError extends SilentSwapError {
|
|
38
|
+
originalError;
|
|
39
|
+
constructor(message, originalError) {
|
|
40
|
+
super(message, 'NETWORK_ERROR', originalError);
|
|
41
|
+
this.originalError = originalError;
|
|
42
|
+
this.name = 'NetworkError';
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Error thrown when authentication fails
|
|
47
|
+
*/
|
|
48
|
+
export class AuthenticationError extends SilentSwapError {
|
|
49
|
+
cause;
|
|
50
|
+
constructor(message, cause) {
|
|
51
|
+
super(message, 'AUTHENTICATION_ERROR', cause);
|
|
52
|
+
this.cause = cause;
|
|
53
|
+
this.name = 'AuthenticationError';
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Error thrown when quote request fails
|
|
58
|
+
*/
|
|
59
|
+
export class QuoteError extends SilentSwapError {
|
|
60
|
+
cause;
|
|
61
|
+
constructor(message, cause) {
|
|
62
|
+
super(message, 'QUOTE_ERROR', cause);
|
|
63
|
+
this.cause = cause;
|
|
64
|
+
this.name = 'QuoteError';
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Error thrown when order creation fails
|
|
69
|
+
*/
|
|
70
|
+
export class OrderError extends SilentSwapError {
|
|
71
|
+
cause;
|
|
72
|
+
constructor(message, cause) {
|
|
73
|
+
super(message, 'ORDER_ERROR', cause);
|
|
74
|
+
this.cause = cause;
|
|
75
|
+
this.name = 'OrderError';
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Error thrown when validation fails
|
|
80
|
+
*/
|
|
81
|
+
export class ValidationError extends SilentSwapError {
|
|
82
|
+
field;
|
|
83
|
+
constructor(message, field) {
|
|
84
|
+
super(message, 'VALIDATION_ERROR');
|
|
85
|
+
this.field = field;
|
|
86
|
+
this.name = 'ValidationError';
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Error thrown when configuration is invalid
|
|
91
|
+
*/
|
|
92
|
+
export class ConfigurationError extends SilentSwapError {
|
|
93
|
+
config;
|
|
94
|
+
constructor(message, config) {
|
|
95
|
+
super(message, 'CONFIGURATION_ERROR');
|
|
96
|
+
this.config = config;
|
|
97
|
+
this.name = 'ConfigurationError';
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Error thrown when wallet operation fails
|
|
102
|
+
*/
|
|
103
|
+
export class WalletError extends SilentSwapError {
|
|
104
|
+
cause;
|
|
105
|
+
constructor(message, cause) {
|
|
106
|
+
super(message, 'WALLET_ERROR', cause);
|
|
107
|
+
this.cause = cause;
|
|
108
|
+
this.name = 'WalletError';
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Error thrown when transaction fails
|
|
113
|
+
*/
|
|
114
|
+
export class TransactionError extends SilentSwapError {
|
|
115
|
+
transactionHash;
|
|
116
|
+
cause;
|
|
117
|
+
constructor(message, transactionHash, cause) {
|
|
118
|
+
super(message, 'TRANSACTION_ERROR', cause);
|
|
119
|
+
this.transactionHash = transactionHash;
|
|
120
|
+
this.cause = cause;
|
|
121
|
+
this.name = 'TransactionError';
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Helper function to convert ErrorResponse to appropriate error class
|
|
126
|
+
*/
|
|
127
|
+
export function createErrorFromResponse(errorResponse, statusCode) {
|
|
128
|
+
const { type, error } = errorResponse;
|
|
129
|
+
switch (type) {
|
|
130
|
+
case 'AUTHENTICATION_ERROR':
|
|
131
|
+
case 'AUTH_ERROR':
|
|
132
|
+
return new AuthenticationError(error);
|
|
133
|
+
case 'QUOTE_ERROR':
|
|
134
|
+
return new QuoteError(error);
|
|
135
|
+
case 'ORDER_ERROR':
|
|
136
|
+
return new OrderError(error);
|
|
137
|
+
case 'VALIDATION_ERROR':
|
|
138
|
+
return new ValidationError(error);
|
|
139
|
+
case 'NETWORK_ERROR':
|
|
140
|
+
return new NetworkError(error);
|
|
141
|
+
default:
|
|
142
|
+
return new ApiError(error, statusCode, errorResponse);
|
|
143
|
+
}
|
|
144
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@silentswap/sdk",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.53",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"files": [
|
|
@@ -13,7 +13,10 @@
|
|
|
13
13
|
"clean": "rm -rf dist",
|
|
14
14
|
"dev": "tsc --watch",
|
|
15
15
|
"lint": "eslint src --ext .ts",
|
|
16
|
-
"lint:fix": "eslint src --ext .ts --fix"
|
|
16
|
+
"lint:fix": "eslint src --ext .ts --fix",
|
|
17
|
+
"test": "vitest",
|
|
18
|
+
"test:watch": "vitest --watch",
|
|
19
|
+
"test:coverage": "vitest --coverage"
|
|
17
20
|
},
|
|
18
21
|
"dependencies": {
|
|
19
22
|
"@cosmjs/amino": "0.36.2",
|
|
@@ -32,11 +35,13 @@
|
|
|
32
35
|
"@types/node": "24.9.1",
|
|
33
36
|
"@types/web": "0.0.283",
|
|
34
37
|
"@typescript-eslint/parser": "8.46.2",
|
|
38
|
+
"@vitest/coverage-v8": "^2.1.0",
|
|
35
39
|
"abitype": "1.1.1",
|
|
36
40
|
"eslint": "9.38.0",
|
|
37
41
|
"eslint-import-resolver-typescript": "4.4.4",
|
|
38
42
|
"eslint-plugin-import-x": "4.16.1",
|
|
39
43
|
"typescript": "5.9.3",
|
|
40
|
-
"typescript-eslint": "8.46.2"
|
|
44
|
+
"typescript-eslint": "8.46.2",
|
|
45
|
+
"vitest": "^2.1.0"
|
|
41
46
|
}
|
|
42
47
|
}
|