@make-software/cspr-trade-mcp-sdk 0.1.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.
Files changed (55) hide show
  1. package/README.md +257 -0
  2. package/dist/assets/proxy_caller.wasm +0 -0
  3. package/dist/index.d.ts +362 -0
  4. package/dist/index.js +1003 -0
  5. package/package.json +32 -0
  6. package/src/api/currencies.ts +11 -0
  7. package/src/api/http.ts +57 -0
  8. package/src/api/index.ts +9 -0
  9. package/src/api/liquidity.ts +22 -0
  10. package/src/api/pairs.ts +77 -0
  11. package/src/api/quotes.ts +23 -0
  12. package/src/api/rates.ts +33 -0
  13. package/src/api/submission.ts +42 -0
  14. package/src/api/swaps.ts +24 -0
  15. package/src/api/tokens.ts +57 -0
  16. package/src/assets/index.ts +21 -0
  17. package/src/assets/proxy_caller.wasm +0 -0
  18. package/src/client.ts +587 -0
  19. package/src/config.ts +60 -0
  20. package/src/index.ts +4 -0
  21. package/src/resolver/currency-resolver.ts +19 -0
  22. package/src/resolver/index.ts +2 -0
  23. package/src/resolver/token-resolver.ts +43 -0
  24. package/src/transactions/approve.ts +14 -0
  25. package/src/transactions/index.ts +5 -0
  26. package/src/transactions/liquidity.ts +92 -0
  27. package/src/transactions/proxy-wasm.ts +33 -0
  28. package/src/transactions/swap.ts +76 -0
  29. package/src/transactions/transaction-builder.ts +44 -0
  30. package/src/types/api.ts +32 -0
  31. package/src/types/index.ts +6 -0
  32. package/src/types/liquidity.ts +72 -0
  33. package/src/types/pair.ts +29 -0
  34. package/src/types/quote.ts +41 -0
  35. package/src/types/token.ts +48 -0
  36. package/src/types/transaction.ts +72 -0
  37. package/src/utils/amounts.ts +30 -0
  38. package/tests/integration/api.integration.test.ts +64 -0
  39. package/tests/unit/api/http.test.ts +68 -0
  40. package/tests/unit/api/liquidity.test.ts +40 -0
  41. package/tests/unit/api/pairs.test.ts +53 -0
  42. package/tests/unit/api/quotes.test.ts +59 -0
  43. package/tests/unit/api/rates.test.ts +27 -0
  44. package/tests/unit/api/tokens.test.ts +100 -0
  45. package/tests/unit/assets/proxy-caller.test.ts +21 -0
  46. package/tests/unit/client.test.ts +73 -0
  47. package/tests/unit/config.test.ts +23 -0
  48. package/tests/unit/resolver/currency-resolver.test.ts +32 -0
  49. package/tests/unit/resolver/token-resolver.test.ts +51 -0
  50. package/tests/unit/transactions/approve.test.ts +13 -0
  51. package/tests/unit/transactions/liquidity.test.ts +59 -0
  52. package/tests/unit/transactions/proxy-wasm.test.ts +50 -0
  53. package/tests/unit/transactions/swap.test.ts +77 -0
  54. package/tests/unit/utils/amounts.test.ts +44 -0
  55. package/tsconfig.json +9 -0
@@ -0,0 +1,59 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { buildAddLiquidityInnerArgs, buildRemoveLiquidityInnerArgs } from '../../../src/transactions/liquidity.js';
3
+
4
+ const ACCOUNT_HASH = 'account-hash-0000000000000000000000000000000000000000000000000000000000000000';
5
+ const DEADLINE = Date.now() + 20 * 60 * 1000;
6
+ const TOKEN_A_HASH = 'hash-1111111111111111111111111111111111111111111111111111111111111111';
7
+ const TOKEN_B_HASH = 'hash-2222222222222222222222222222222222222222222222222222222222222222';
8
+ const USDT_HASH = 'hash-3333333333333333333333333333333333333333333333333333333333333333';
9
+
10
+ describe('Liquidity transaction builders', () => {
11
+ describe('buildAddLiquidityInnerArgs', () => {
12
+ it('should build args for token-token add_liquidity', () => {
13
+ const args = buildAddLiquidityInnerArgs({
14
+ isCSPRPair: false,
15
+ tokenAHash: TOKEN_A_HASH,
16
+ tokenBHash: TOKEN_B_HASH,
17
+ amountADesired: '1000000',
18
+ amountBDesired: '2000000',
19
+ amountAMin: '970000',
20
+ amountBMin: '1940000',
21
+ accountHash: ACCOUNT_HASH,
22
+ deadline: DEADLINE,
23
+ });
24
+ const bytes = args.toBytes();
25
+ expect(bytes.length).toBeGreaterThan(0);
26
+ });
27
+
28
+ it('should build args for CSPR pair add_liquidity_cspr', () => {
29
+ const args = buildAddLiquidityInnerArgs({
30
+ isCSPRPair: true,
31
+ tokenHash: USDT_HASH,
32
+ amountTokenDesired: '50000000',
33
+ amountTokenMin: '48500000',
34
+ amountCSPRMin: '97000000000',
35
+ accountHash: ACCOUNT_HASH,
36
+ deadline: DEADLINE,
37
+ });
38
+ const bytes = args.toBytes();
39
+ expect(bytes.length).toBeGreaterThan(0);
40
+ });
41
+ });
42
+
43
+ describe('buildRemoveLiquidityInnerArgs', () => {
44
+ it('should build args for token-token remove_liquidity', () => {
45
+ const args = buildRemoveLiquidityInnerArgs({
46
+ isCSPRPair: false,
47
+ tokenAHash: TOKEN_A_HASH,
48
+ tokenBHash: TOKEN_B_HASH,
49
+ liquidity: '500000',
50
+ amountAMin: '485000',
51
+ amountBMin: '970000',
52
+ accountHash: ACCOUNT_HASH,
53
+ deadline: DEADLINE,
54
+ });
55
+ const bytes = args.toBytes();
56
+ expect(bytes.length).toBeGreaterThan(0);
57
+ });
58
+ });
59
+ });
@@ -0,0 +1,50 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { buildProxyWasmArgs, serializeInnerArgs } from '../../../src/transactions/proxy-wasm.js';
3
+ import { Args, CLValue, CLTypeKey, Key } from 'casper-js-sdk';
4
+
5
+ describe('Proxy WASM encoding', () => {
6
+ it('should serialize inner args to byte array', () => {
7
+ const innerArgs = Args.fromMap({
8
+ amount_in: CLValue.newCLUInt256('1000'),
9
+ to: CLValue.newCLKey(Key.newKey('account-hash-0000000000000000000000000000000000000000000000000000000000000000')),
10
+ });
11
+
12
+ const bytes = serializeInnerArgs(innerArgs);
13
+ expect(bytes).toBeInstanceOf(Uint8Array);
14
+ expect(bytes.length).toBeGreaterThan(0);
15
+ });
16
+
17
+ it('should build outer proxy WASM args with correct structure', () => {
18
+ const routerHash = '04a11a367e708c52557930c4e9c1301f4465100d1b1b6d0a62b48d3e32402867';
19
+ const innerArgs = Args.fromMap({
20
+ amount_in: CLValue.newCLUInt256('1000'),
21
+ });
22
+
23
+ const outerArgs = buildProxyWasmArgs({
24
+ routerPackageHash: routerHash,
25
+ entryPoint: 'swap_exact_tokens_for_tokens',
26
+ innerArgs,
27
+ attachedValue: '0',
28
+ });
29
+
30
+ expect(outerArgs).toBeDefined();
31
+ const bytes = outerArgs.toBytes();
32
+ expect(bytes.length).toBeGreaterThan(0);
33
+ });
34
+
35
+ it('should set attached_value and amount for CSPR operations', () => {
36
+ const routerHash = '04a11a367e708c52557930c4e9c1301f4465100d1b1b6d0a62b48d3e32402867';
37
+ const innerArgs = Args.fromMap({
38
+ amount_out_min: CLValue.newCLUInt256('500'),
39
+ });
40
+
41
+ const outerArgs = buildProxyWasmArgs({
42
+ routerPackageHash: routerHash,
43
+ entryPoint: 'swap_exact_cspr_for_tokens',
44
+ innerArgs,
45
+ attachedValue: '100000000000',
46
+ });
47
+
48
+ expect(outerArgs).toBeDefined();
49
+ });
50
+ });
@@ -0,0 +1,77 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import {
3
+ getSwapEntryPoint,
4
+ buildSwapInnerArgs,
5
+ } from '../../../src/transactions/swap.js';
6
+
7
+ describe('Swap transaction builder', () => {
8
+ describe('getSwapEntryPoint', () => {
9
+ it('should return swap_exact_cspr_for_tokens for CSPR->Token exact_in', () => {
10
+ expect(getSwapEntryPoint(true, false, 'exact_in')).toBe('swap_exact_cspr_for_tokens');
11
+ });
12
+
13
+ it('should return swap_cspr_for_exact_tokens for CSPR->Token exact_out', () => {
14
+ expect(getSwapEntryPoint(true, false, 'exact_out')).toBe('swap_cspr_for_exact_tokens');
15
+ });
16
+
17
+ it('should return swap_exact_tokens_for_cspr for Token->CSPR exact_in', () => {
18
+ expect(getSwapEntryPoint(false, true, 'exact_in')).toBe('swap_exact_tokens_for_cspr');
19
+ });
20
+
21
+ it('should return swap_tokens_for_exact_cspr for Token->CSPR exact_out', () => {
22
+ expect(getSwapEntryPoint(false, true, 'exact_out')).toBe('swap_tokens_for_exact_cspr');
23
+ });
24
+
25
+ it('should return swap_exact_tokens_for_tokens for Token->Token exact_in', () => {
26
+ expect(getSwapEntryPoint(false, false, 'exact_in')).toBe('swap_exact_tokens_for_tokens');
27
+ });
28
+
29
+ it('should return swap_tokens_for_exact_tokens for Token->Token exact_out', () => {
30
+ expect(getSwapEntryPoint(false, false, 'exact_out')).toBe('swap_tokens_for_exact_tokens');
31
+ });
32
+ });
33
+
34
+ describe('buildSwapInnerArgs', () => {
35
+ const ACCOUNT_HASH = 'account-hash-0000000000000000000000000000000000000000000000000000000000000000';
36
+ const WCSPR_HASH = 'hash-3d80df21ba4ee4d66a2a1f60c32570dd5685e4b279f6538162a5fd1314847c1e';
37
+ const USDT_HASH = 'hash-1111111111111111111111111111111111111111111111111111111111111111';
38
+ const TOKEN_A_HASH = 'hash-2222222222222222222222222222222222222222222222222222222222222222';
39
+ const TOKEN_B_HASH = 'hash-3333333333333333333333333333333333333333333333333333333333333333';
40
+
41
+ it('should build inner args for CSPR->Token exact_in', () => {
42
+ const args = buildSwapInnerArgs({
43
+ isFirstTokenNative: true,
44
+ isSecondTokenNative: false,
45
+ quoteType: 'exact_in',
46
+ path: [WCSPR_HASH, USDT_HASH],
47
+ accountHash: ACCOUNT_HASH,
48
+ deadline: Date.now() + 20 * 60 * 1000,
49
+ amountIn: '100000000000',
50
+ amountOut: '50000000',
51
+ amountInMax: '103000000000',
52
+ amountOutMin: '48500000',
53
+ });
54
+
55
+ const bytes = args.toBytes();
56
+ expect(bytes.length).toBeGreaterThan(0);
57
+ });
58
+
59
+ it('should build inner args for Token->Token exact_in', () => {
60
+ const args = buildSwapInnerArgs({
61
+ isFirstTokenNative: false,
62
+ isSecondTokenNative: false,
63
+ quoteType: 'exact_in',
64
+ path: [TOKEN_A_HASH, TOKEN_B_HASH],
65
+ accountHash: ACCOUNT_HASH,
66
+ deadline: Date.now() + 20 * 60 * 1000,
67
+ amountIn: '1000000',
68
+ amountOut: '2000000',
69
+ amountInMax: '1030000',
70
+ amountOutMin: '1940000',
71
+ });
72
+
73
+ const bytes = args.toBytes();
74
+ expect(bytes.length).toBeGreaterThan(0);
75
+ });
76
+ });
77
+ });
@@ -0,0 +1,44 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { toRawAmount, toFormattedAmount, calculateMinWithSlippage, calculateMaxWithSlippage } from '../../../src/utils/amounts.js';
3
+
4
+ describe('Amount conversion', () => {
5
+ it('should convert human amount to raw (CSPR, 9 decimals)', () => {
6
+ expect(toRawAmount('100', 9)).toBe('100000000000');
7
+ });
8
+
9
+ it('should convert human amount to raw (USDT, 6 decimals)', () => {
10
+ expect(toRawAmount('50.5', 6)).toBe('50500000');
11
+ });
12
+
13
+ it('should convert human amount to raw (1.23456789, 9 decimals)', () => {
14
+ expect(toRawAmount('1.23456789', 9)).toBe('1234567890');
15
+ });
16
+
17
+ it('should convert raw to human (CSPR)', () => {
18
+ expect(toFormattedAmount('100000000000', 9)).toBe('100');
19
+ });
20
+
21
+ it('should convert raw to human (USDT)', () => {
22
+ expect(toFormattedAmount('50500000', 6)).toBe('50.5');
23
+ });
24
+
25
+ it('should handle zero', () => {
26
+ expect(toRawAmount('0', 9)).toBe('0');
27
+ expect(toFormattedAmount('0', 9)).toBe('0');
28
+ });
29
+ });
30
+
31
+ describe('Slippage calculations', () => {
32
+ it('should calculate min amount with 3% slippage', () => {
33
+ expect(calculateMinWithSlippage('100000000000', 300)).toBe('97000000000');
34
+ });
35
+
36
+ it('should calculate max amount with 3% slippage', () => {
37
+ expect(calculateMaxWithSlippage('100000000000', 300)).toBe('103000000000');
38
+ });
39
+
40
+ it('should round min down and max up', () => {
41
+ expect(calculateMinWithSlippage('333', 300)).toBe('323');
42
+ expect(calculateMaxWithSlippage('333', 300)).toBe('343');
43
+ });
44
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist",
5
+ "rootDir": "src"
6
+ },
7
+ "include": ["src/**/*"],
8
+ "exclude": ["tests/**/*", "dist/**/*"]
9
+ }