ccxt 4.3.70 → 4.3.72

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 (153) hide show
  1. package/README.md +6 -5
  2. package/dist/ccxt.browser.min.js +13 -10
  3. package/dist/cjs/ccxt.js +6 -1
  4. package/dist/cjs/src/abstract/paradex.js +9 -0
  5. package/dist/cjs/src/base/Exchange.js +49 -0
  6. package/dist/cjs/src/paradex.js +2075 -0
  7. package/dist/cjs/src/pro/bequant.js +4 -0
  8. package/dist/cjs/src/pro/paradex.js +365 -0
  9. package/dist/cjs/src/static_dependencies/noble-curves/abstract/poseidon.js +100 -0
  10. package/dist/cjs/src/static_dependencies/noble-curves/abstract/weierstrass.js +1 -0
  11. package/dist/cjs/src/static_dependencies/scure-starknet/index.js +284 -0
  12. package/dist/cjs/src/static_dependencies/starknet/constants.js +60 -0
  13. package/dist/cjs/src/static_dependencies/starknet/types/calldata.js +26 -0
  14. package/dist/cjs/src/static_dependencies/starknet/types/lib/contract/abi.js +8 -0
  15. package/dist/cjs/src/static_dependencies/starknet/types/lib/contract/index.js +13 -0
  16. package/dist/cjs/src/static_dependencies/starknet/types/lib/index.js +56 -0
  17. package/dist/cjs/src/static_dependencies/starknet/types/typedData.js +19 -0
  18. package/dist/cjs/src/static_dependencies/starknet/utils/assert.js +15 -0
  19. package/dist/cjs/src/static_dependencies/starknet/utils/cairoDataTypes/felt.js +44 -0
  20. package/dist/cjs/src/static_dependencies/starknet/utils/cairoDataTypes/uint256.js +122 -0
  21. package/dist/cjs/src/static_dependencies/starknet/utils/cairoDataTypes/uint512.js +137 -0
  22. package/dist/cjs/src/static_dependencies/starknet/utils/calldata/byteArray.js +61 -0
  23. package/dist/cjs/src/static_dependencies/starknet/utils/calldata/cairo.js +218 -0
  24. package/dist/cjs/src/static_dependencies/starknet/utils/calldata/enum/CairoCustomEnum.js +57 -0
  25. package/dist/cjs/src/static_dependencies/starknet/utils/calldata/enum/CairoOption.js +64 -0
  26. package/dist/cjs/src/static_dependencies/starknet/utils/calldata/enum/CairoResult.js +63 -0
  27. package/dist/cjs/src/static_dependencies/starknet/utils/calldata/formatter.js +66 -0
  28. package/dist/cjs/src/static_dependencies/starknet/utils/calldata/index.js +281 -0
  29. package/dist/cjs/src/static_dependencies/starknet/utils/calldata/parser/index.js +33 -0
  30. package/dist/cjs/src/static_dependencies/starknet/utils/calldata/parser/parser-0-1.1.0.js +37 -0
  31. package/dist/cjs/src/static_dependencies/starknet/utils/calldata/parser/parser-2.0.0.js +40 -0
  32. package/dist/cjs/src/static_dependencies/starknet/utils/calldata/propertyOrder.js +156 -0
  33. package/dist/cjs/src/static_dependencies/starknet/utils/calldata/requestParser.js +250 -0
  34. package/dist/cjs/src/static_dependencies/starknet/utils/calldata/responseParser.js +215 -0
  35. package/dist/cjs/src/static_dependencies/starknet/utils/calldata/tuple.js +112 -0
  36. package/dist/cjs/src/static_dependencies/starknet/utils/calldata/validate.js +206 -0
  37. package/dist/cjs/src/static_dependencies/starknet/utils/encode.js +58 -0
  38. package/dist/cjs/src/static_dependencies/starknet/utils/hash/classHash.js +57 -0
  39. package/dist/cjs/src/static_dependencies/starknet/utils/merkle.js +76 -0
  40. package/dist/cjs/src/static_dependencies/starknet/utils/num.js +92 -0
  41. package/dist/cjs/src/static_dependencies/starknet/utils/selector.js +48 -0
  42. package/dist/cjs/src/static_dependencies/starknet/utils/shortString.js +101 -0
  43. package/dist/cjs/src/static_dependencies/starknet/utils/typedData.js +334 -0
  44. package/dist/cjs/src/woo.js +4 -2
  45. package/examples/js/cli.js +8 -4
  46. package/js/ccxt.d.ts +8 -2
  47. package/js/ccxt.js +6 -2
  48. package/js/src/abstract/paradex.d.ts +43 -0
  49. package/js/src/abstract/paradex.js +11 -0
  50. package/js/src/base/Exchange.d.ts +7 -0
  51. package/js/src/base/Exchange.js +45 -0
  52. package/js/src/base/types.d.ts +10 -10
  53. package/js/src/paradex.d.ts +76 -0
  54. package/js/src/paradex.js +2075 -0
  55. package/js/src/pro/bequant.js +4 -0
  56. package/js/src/pro/paradex.d.ts +15 -0
  57. package/js/src/pro/paradex.js +366 -0
  58. package/js/src/static_dependencies/noble-curves/abstract/weierstrass.d.ts +24 -0
  59. package/js/src/static_dependencies/noble-curves/abstract/weierstrass.js +1 -1
  60. package/js/src/static_dependencies/scure-starknet/index.d.ts +79 -0
  61. package/js/src/static_dependencies/scure-starknet/index.js +323 -0
  62. package/js/src/static_dependencies/starknet/constants.d.ts +61 -0
  63. package/js/src/static_dependencies/starknet/constants.js +67 -0
  64. package/js/src/static_dependencies/starknet/index.d.ts +7 -0
  65. package/js/src/static_dependencies/starknet/index.js +50 -0
  66. package/js/src/static_dependencies/starknet/types/cairoEnum.d.ts +2 -0
  67. package/js/src/static_dependencies/starknet/types/cairoEnum.js +7 -0
  68. package/js/src/static_dependencies/starknet/types/calldata.d.ts +19 -0
  69. package/js/src/static_dependencies/starknet/types/calldata.js +28 -0
  70. package/js/src/static_dependencies/starknet/types/index.d.ts +13 -0
  71. package/js/src/static_dependencies/starknet/types/index.js +16 -0
  72. package/js/src/static_dependencies/starknet/types/lib/contract/abi.d.ts +71 -0
  73. package/js/src/static_dependencies/starknet/types/lib/contract/abi.js +13 -0
  74. package/js/src/static_dependencies/starknet/types/lib/contract/index.d.ts +24 -0
  75. package/js/src/static_dependencies/starknet/types/lib/contract/index.js +16 -0
  76. package/js/src/static_dependencies/starknet/types/lib/contract/legacy.d.ts +33 -0
  77. package/js/src/static_dependencies/starknet/types/lib/contract/legacy.js +7 -0
  78. package/js/src/static_dependencies/starknet/types/lib/contract/sierra.d.ts +52 -0
  79. package/js/src/static_dependencies/starknet/types/lib/contract/sierra.js +7 -0
  80. package/js/src/static_dependencies/starknet/types/lib/index.d.ts +248 -0
  81. package/js/src/static_dependencies/starknet/types/lib/index.js +52 -0
  82. package/js/src/static_dependencies/starknet/types/typedData.d.ts +44 -0
  83. package/js/src/static_dependencies/starknet/types/typedData.js +19 -0
  84. package/js/src/static_dependencies/starknet/utils/address.d.ts +53 -0
  85. package/js/src/static_dependencies/starknet/utils/address.js +89 -0
  86. package/js/src/static_dependencies/starknet/utils/assert.d.ts +7 -0
  87. package/js/src/static_dependencies/starknet/utils/assert.js +17 -0
  88. package/js/src/static_dependencies/starknet/utils/cairoDataTypes/felt.d.ts +6 -0
  89. package/js/src/static_dependencies/starknet/utils/cairoDataTypes/felt.js +43 -0
  90. package/js/src/static_dependencies/starknet/utils/cairoDataTypes/uint256.d.ts +72 -0
  91. package/js/src/static_dependencies/starknet/utils/cairoDataTypes/uint256.js +117 -0
  92. package/js/src/static_dependencies/starknet/utils/cairoDataTypes/uint512.d.ts +76 -0
  93. package/js/src/static_dependencies/starknet/utils/cairoDataTypes/uint512.js +136 -0
  94. package/js/src/static_dependencies/starknet/utils/calldata/byteArray.d.ts +32 -0
  95. package/js/src/static_dependencies/starknet/utils/calldata/byteArray.js +59 -0
  96. package/js/src/static_dependencies/starknet/utils/calldata/cairo.d.ts +183 -0
  97. package/js/src/static_dependencies/starknet/utils/calldata/cairo.js +229 -0
  98. package/js/src/static_dependencies/starknet/utils/calldata/enum/CairoCustomEnum.d.ts +38 -0
  99. package/js/src/static_dependencies/starknet/utils/calldata/enum/CairoCustomEnum.js +57 -0
  100. package/js/src/static_dependencies/starknet/utils/calldata/enum/CairoOption.d.ts +35 -0
  101. package/js/src/static_dependencies/starknet/utils/calldata/enum/CairoOption.js +64 -0
  102. package/js/src/static_dependencies/starknet/utils/calldata/enum/CairoResult.d.ts +34 -0
  103. package/js/src/static_dependencies/starknet/utils/calldata/enum/CairoResult.js +63 -0
  104. package/js/src/static_dependencies/starknet/utils/calldata/enum/index.d.ts +3 -0
  105. package/js/src/static_dependencies/starknet/utils/calldata/enum/index.js +9 -0
  106. package/js/src/static_dependencies/starknet/utils/calldata/formatter.d.ts +9 -0
  107. package/js/src/static_dependencies/starknet/utils/calldata/formatter.js +67 -0
  108. package/js/src/static_dependencies/starknet/utils/calldata/index.d.ts +89 -0
  109. package/js/src/static_dependencies/starknet/utils/calldata/index.js +280 -0
  110. package/js/src/static_dependencies/starknet/utils/calldata/parser/index.d.ts +5 -0
  111. package/js/src/static_dependencies/starknet/utils/calldata/parser/index.js +30 -0
  112. package/js/src/static_dependencies/starknet/utils/calldata/parser/interface.d.ts +20 -0
  113. package/js/src/static_dependencies/starknet/utils/calldata/parser/interface.js +8 -0
  114. package/js/src/static_dependencies/starknet/utils/calldata/parser/parser-0-1.1.0.d.ts +24 -0
  115. package/js/src/static_dependencies/starknet/utils/calldata/parser/parser-0-1.1.0.js +36 -0
  116. package/js/src/static_dependencies/starknet/utils/calldata/parser/parser-2.0.0.d.ts +23 -0
  117. package/js/src/static_dependencies/starknet/utils/calldata/parser/parser-2.0.0.js +40 -0
  118. package/js/src/static_dependencies/starknet/utils/calldata/propertyOrder.d.ts +2 -0
  119. package/js/src/static_dependencies/starknet/utils/calldata/propertyOrder.js +155 -0
  120. package/js/src/static_dependencies/starknet/utils/calldata/requestParser.d.ts +11 -0
  121. package/js/src/static_dependencies/starknet/utils/calldata/requestParser.js +248 -0
  122. package/js/src/static_dependencies/starknet/utils/calldata/responseParser.d.ts +11 -0
  123. package/js/src/static_dependencies/starknet/utils/calldata/responseParser.js +214 -0
  124. package/js/src/static_dependencies/starknet/utils/calldata/tuple.d.ts +6 -0
  125. package/js/src/static_dependencies/starknet/utils/calldata/tuple.js +113 -0
  126. package/js/src/static_dependencies/starknet/utils/calldata/validate.d.ts +6 -0
  127. package/js/src/static_dependencies/starknet/utils/calldata/validate.js +208 -0
  128. package/js/src/static_dependencies/starknet/utils/encode.d.ts +207 -0
  129. package/js/src/static_dependencies/starknet/utils/encode.js +282 -0
  130. package/js/src/static_dependencies/starknet/utils/hash/classHash.d.ts +57 -0
  131. package/js/src/static_dependencies/starknet/utils/hash/classHash.js +224 -0
  132. package/js/src/static_dependencies/starknet/utils/hash/index.d.ts +6 -0
  133. package/js/src/static_dependencies/starknet/utils/hash/index.js +13 -0
  134. package/js/src/static_dependencies/starknet/utils/json.d.ts +24 -0
  135. package/js/src/static_dependencies/starknet/utils/json.js +43 -0
  136. package/js/src/static_dependencies/starknet/utils/merkle.d.ts +35 -0
  137. package/js/src/static_dependencies/starknet/utils/merkle.js +84 -0
  138. package/js/src/static_dependencies/starknet/utils/num.d.ts +182 -0
  139. package/js/src/static_dependencies/starknet/utils/num.js +244 -0
  140. package/js/src/static_dependencies/starknet/utils/selector.d.ts +48 -0
  141. package/js/src/static_dependencies/starknet/utils/selector.js +85 -0
  142. package/js/src/static_dependencies/starknet/utils/shortString.d.ts +57 -0
  143. package/js/src/static_dependencies/starknet/utils/shortString.js +96 -0
  144. package/js/src/static_dependencies/starknet/utils/starknetId.d.ts +113 -0
  145. package/js/src/static_dependencies/starknet/utils/starknetId.js +265 -0
  146. package/js/src/static_dependencies/starknet/utils/typedData.d.ts +54 -0
  147. package/js/src/static_dependencies/starknet/utils/typedData.js +321 -0
  148. package/js/src/static_dependencies/starknet/utils/uint256.d.ts +21 -0
  149. package/js/src/static_dependencies/starknet/utils/uint256.js +32 -0
  150. package/js/src/static_dependencies/starknet/utils/url.d.ts +29 -0
  151. package/js/src/static_dependencies/starknet/utils/url.js +70 -0
  152. package/js/src/woo.js +4 -2
  153. package/package.json +1 -1
@@ -0,0 +1,137 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var encode = require('../encode.js');
6
+ var felt = require('./felt.js');
7
+ var uint256 = require('./uint256.js');
8
+
9
+ /* eslint-disable no-bitwise */
10
+ const UINT_512_MAX = (1n << 512n) - 1n;
11
+ const UINT_512_MIN = 0n;
12
+ const UINT_128_MIN = 0n;
13
+ class CairoUint512 {
14
+ constructor(...arr) {
15
+ if (typeof arr[0] === 'object' &&
16
+ arr.length === 1 &&
17
+ 'limb0' in arr[0] &&
18
+ 'limb1' in arr[0] &&
19
+ 'limb2' in arr[0] &&
20
+ 'limb3' in arr[0]) {
21
+ const props = CairoUint512.validateProps(arr[0].limb0, arr[0].limb1, arr[0].limb2, arr[0].limb3);
22
+ this.limb0 = props.limb0;
23
+ this.limb1 = props.limb1;
24
+ this.limb2 = props.limb2;
25
+ this.limb3 = props.limb3;
26
+ }
27
+ else if (arr.length === 1) {
28
+ const bigInt = CairoUint512.validate(arr[0]);
29
+ this.limb0 = bigInt & uint256.UINT_128_MAX;
30
+ this.limb1 = (bigInt & (uint256.UINT_128_MAX << 128n)) >> 128n;
31
+ this.limb2 = (bigInt & (uint256.UINT_128_MAX << 256n)) >> 256n;
32
+ this.limb3 = bigInt >> 384n;
33
+ }
34
+ else if (arr.length === 4) {
35
+ const props = CairoUint512.validateProps(arr[0], arr[1], arr[2], arr[3]);
36
+ this.limb0 = props.limb0;
37
+ this.limb1 = props.limb1;
38
+ this.limb2 = props.limb2;
39
+ this.limb3 = props.limb3;
40
+ }
41
+ else {
42
+ throw Error('Incorrect Uint512 constructor parameters');
43
+ }
44
+ }
45
+ /**
46
+ * Validate if BigNumberish can be represented as Uint512
47
+ */
48
+ static validate(bigNumberish) {
49
+ const bigInt = BigInt(bigNumberish);
50
+ if (bigInt < UINT_512_MIN)
51
+ throw Error('bigNumberish is smaller than UINT_512_MIN.');
52
+ if (bigInt > UINT_512_MAX)
53
+ throw Error('bigNumberish is bigger than UINT_512_MAX.');
54
+ return bigInt;
55
+ }
56
+ /**
57
+ * Validate if limbs can be represented as Uint512
58
+ */
59
+ static validateProps(limb0, limb1, limb2, limb3) {
60
+ const l0 = BigInt(limb0);
61
+ const l1 = BigInt(limb1);
62
+ const l2 = BigInt(limb2);
63
+ const l3 = BigInt(limb3);
64
+ [l0, l1, l2, l3].forEach((value, index) => {
65
+ if (value < UINT_128_MIN || value > uint256.UINT_128_MAX) {
66
+ throw Error(`limb${index} is not in the range of a u128 number`);
67
+ }
68
+ });
69
+ return { limb0: l0, limb1: l1, limb2: l2, limb3: l3 };
70
+ }
71
+ /**
72
+ * Check if BigNumberish can be represented as Uint512
73
+ */
74
+ static is(bigNumberish) {
75
+ try {
76
+ CairoUint512.validate(bigNumberish);
77
+ }
78
+ catch (error) {
79
+ return false;
80
+ }
81
+ return true;
82
+ }
83
+ /**
84
+ * Check if provided abi type is this data type
85
+ */
86
+ static isAbiType(abiType) {
87
+ return abiType === CairoUint512.abiSelector;
88
+ }
89
+ /**
90
+ * Return bigint representation
91
+ */
92
+ toBigInt() {
93
+ return (this.limb3 << 384n) + (this.limb2 << 256n) + (this.limb1 << 128n) + this.limb0;
94
+ }
95
+ /**
96
+ * Return Uint512 structure with HexString props
97
+ * limbx: HexString
98
+ */
99
+ toUint512HexString() {
100
+ return {
101
+ limb0: encode.addHexPrefix(this.limb0.toString(16)),
102
+ limb1: encode.addHexPrefix(this.limb1.toString(16)),
103
+ limb2: encode.addHexPrefix(this.limb2.toString(16)),
104
+ limb3: encode.addHexPrefix(this.limb3.toString(16)),
105
+ };
106
+ }
107
+ /**
108
+ * Return Uint512 structure with DecimalString props
109
+ * limbx DecString
110
+ */
111
+ toUint512DecimalString() {
112
+ return {
113
+ limb0: this.limb0.toString(10),
114
+ limb1: this.limb1.toString(10),
115
+ limb2: this.limb2.toString(10),
116
+ limb3: this.limb3.toString(10),
117
+ };
118
+ }
119
+ /**
120
+ * Return api requests representation witch is felt array
121
+ */
122
+ toApiRequest() {
123
+ // lower limb first : https://github.com/starkware-libs/cairo/blob/07484c52791b76abcc18fd86265756904557d0d2/corelib/src/test/integer_test.cairo#L767
124
+ return [
125
+ felt.CairoFelt(this.limb0),
126
+ felt.CairoFelt(this.limb1),
127
+ felt.CairoFelt(this.limb2),
128
+ felt.CairoFelt(this.limb3),
129
+ ];
130
+ }
131
+ }
132
+ CairoUint512.abiSelector = 'core::integer::u512';
133
+
134
+ exports.CairoUint512 = CairoUint512;
135
+ exports.UINT_128_MIN = UINT_128_MIN;
136
+ exports.UINT_512_MAX = UINT_512_MAX;
137
+ exports.UINT_512_MIN = UINT_512_MIN;
@@ -0,0 +1,61 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var num = require('../num.js');
6
+ var shortString = require('../shortString.js');
7
+
8
+ /**
9
+ * convert a Cairo ByteArray to a JS string
10
+ * @param myByteArray Cairo representation of a LongString
11
+ * @returns a JS string
12
+ * @example
13
+ * ```typescript
14
+ * const myByteArray = {
15
+ * data: [],
16
+ * pending_word: '0x414243444546474849',
17
+ * pending_word_len: 9
18
+ * }
19
+ * const result: String = stringFromByteArray(myByteArray); // ABCDEFGHI
20
+ * ```
21
+ */
22
+ function stringFromByteArray(myByteArray) {
23
+ const pending_word = BigInt(myByteArray.pending_word) === 0n
24
+ ? ''
25
+ : shortString.decodeShortString(num.toHex(myByteArray.pending_word));
26
+ return (myByteArray.data.reduce((cumuledString, encodedString) => {
27
+ const add = BigInt(encodedString) === 0n ? '' : shortString.decodeShortString(num.toHex(encodedString));
28
+ return cumuledString + add;
29
+ }, '') + pending_word);
30
+ }
31
+ /**
32
+ * convert a JS string to a Cairo ByteArray
33
+ * @param targetString a JS string
34
+ * @returns Cairo representation of a LongString
35
+ * @example
36
+ * ```typescript
37
+ * const myByteArray: ByteArray = byteArrayFromString("ABCDEFGHI");
38
+ * ```
39
+ * Result is :
40
+ * {
41
+ * data: [],
42
+ * pending_word: '0x414243444546474849',
43
+ * pending_word_len: 9
44
+ * }
45
+ */
46
+ function byteArrayFromString(targetString) {
47
+ const shortStrings = shortString.splitLongString(targetString);
48
+ const remainder = shortStrings[shortStrings.length - 1];
49
+ const shortStringsEncoded = shortStrings.map(shortString.encodeShortString);
50
+ const [pendingWord, pendingWordLength] = remainder === undefined || remainder.length === 31
51
+ ? ['0x00', 0]
52
+ : [shortStringsEncoded.pop(), remainder.length];
53
+ return {
54
+ data: shortStringsEncoded.length === 0 ? [] : shortStringsEncoded,
55
+ pending_word: pendingWord,
56
+ pending_word_len: pendingWordLength,
57
+ };
58
+ }
59
+
60
+ exports.byteArrayFromString = byteArrayFromString;
61
+ exports.stringFromByteArray = stringFromByteArray;
@@ -0,0 +1,218 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var calldata = require('../../types/calldata.js');
6
+ require('../../types/lib/index.js');
7
+ var felt$1 = require('../cairoDataTypes/felt.js');
8
+ var uint256$1 = require('../cairoDataTypes/uint256.js');
9
+ require('../cairoDataTypes/uint512.js');
10
+
11
+ // Intended for internal usage, maybe should be exported somewhere else and not exported to utils
12
+ /**
13
+ * Checks if the given name ends with "_len".
14
+ *
15
+ * @param {string} name - The name to be checked.
16
+ * @returns - True if the name ends with "_len", false otherwise.
17
+ */
18
+ const isLen = (name) => /_len$/.test(name);
19
+ /**
20
+ * Checks if a given type is felt.
21
+ *
22
+ * @param {string} type - The type to check.
23
+ * @returns - True if the type is felt, false otherwise.
24
+ */
25
+ const isTypeFelt = (type) => type === 'felt' || type === 'core::felt252';
26
+ /**
27
+ * Checks if the given type is an array type.
28
+ *
29
+ * @param {string} type - The type to check.
30
+ * @returns - `true` if the type is an array type, `false` otherwise.
31
+ */
32
+ const isTypeArray = (type) => /\*/.test(type) ||
33
+ type.startsWith('core::array::Array::') ||
34
+ type.startsWith('core::array::Span::');
35
+ /**
36
+ * Checks if the given type is a tuple type.
37
+ *
38
+ * @param {string} type - The type to be checked.
39
+ * @returns - `true` if the type is a tuple type, otherwise `false`.
40
+ */
41
+ const isTypeTuple = (type) => /^\(.*\)$/i.test(type);
42
+ /**
43
+ * Checks whether a given type is a named tuple.
44
+ *
45
+ * @param {string} type - The type to be checked.
46
+ * @returns - True if the type is a named tuple, false otherwise.
47
+ */
48
+ const isTypeNamedTuple = (type) => /\(.*\)/i.test(type) && type.includes(':');
49
+ /**
50
+ * Checks if a given type is a struct.
51
+ *
52
+ * @param {string} type - The type to check for existence.
53
+ * @param {AbiStructs} structs - The collection of structs to search in.
54
+ * @returns - True if the type exists in the structs, false otherwise.
55
+ */
56
+ const isTypeStruct = (type, structs) => type in structs;
57
+ /**
58
+ * Checks if a given type is an enum.
59
+ *
60
+ * @param {string} type - The type to check.
61
+ * @param {AbiEnums} enums - The enumeration to search in.
62
+ * @returns - True if the type exists in the enumeration, otherwise false.
63
+ */
64
+ const isTypeEnum = (type, enums) => type in enums;
65
+ /**
66
+ * Determines if the given type is an Option type.
67
+ *
68
+ * @param {string} type - The type to check.
69
+ * @returns - True if the type is an Option type, false otherwise.
70
+ */
71
+ const isTypeOption = (type) => type.startsWith('core::option::Option::');
72
+ /**
73
+ * Checks whether a given type starts with 'core::result::Result::'.
74
+ *
75
+ * @param {string} type - The type to check.
76
+ * @returns - True if the type starts with 'core::result::Result::', false otherwise.
77
+ */
78
+ const isTypeResult = (type) => type.startsWith('core::result::Result::');
79
+ /**
80
+ * Checks if the given value is a valid Uint type.
81
+ *
82
+ * @param {string} type - The value to check.
83
+ * @returns - Returns true if the value is a valid Uint type, otherwise false.
84
+ */
85
+ const isTypeUint = (type) => Object.values(calldata.Uint).includes(type);
86
+ /**
87
+ * Checks if the given type is a literal type.
88
+ *
89
+ * @param {string} type - The type to check.
90
+ * @returns - True if the type is a literal type, false otherwise.
91
+ */
92
+ const isTypeLiteral = (type) => Object.values(calldata.Literal).includes(type);
93
+ /**
94
+ * Checks if the given type is a boolean type.
95
+ *
96
+ * @param {string} type - The type to be checked.
97
+ * @returns - Returns true if the type is a boolean type, otherwise false.
98
+ */
99
+ const isTypeBool = (type) => type === 'core::bool';
100
+ /**
101
+ * Determines if the given type is an Ethereum address type.
102
+ *
103
+ * @param {string} type - The type to check.
104
+ * @returns - Returns true if the given type is 'core::starknet::eth_address::EthAddress', otherwise false.
105
+ */
106
+ const isTypeEthAddress = (type) => type === 'core::starknet::eth_address::EthAddress';
107
+ /**
108
+ * Checks if the given type is 'core::bytes_31::bytes31'.
109
+ *
110
+ * @param {string} type - The type to check.
111
+ * @returns - True if the type is 'core::bytes_31::bytes31', false otherwise.
112
+ */
113
+ const isTypeBytes31 = (type) => type === 'core::bytes_31::bytes31';
114
+ /**
115
+ * Checks if the given type is equal to the 'core::byte_array::ByteArray'.
116
+ *
117
+ * @param {string} type - The type to check.
118
+ * @returns - True if the given type is equal to 'core::byte_array::ByteArray', false otherwise.
119
+ */
120
+ const isTypeByteArray = (type) => type === 'core::byte_array::byteArray.js';
121
+ const isTypeSecp256k1Point = (type) => type === 'core::starknet::secp256k1::Secp256k1Point';
122
+ const isCairo1Type = (type) => type.includes('::');
123
+ /**
124
+ * Retrieves the array type from the given type string.
125
+ *
126
+ * @param {string} type - The type string.
127
+ * @returns - The array type.
128
+ */
129
+ const getArrayType = (type) => {
130
+ if (isCairo1Type(type)) {
131
+ return type.substring(type.indexOf('<') + 1, type.lastIndexOf('>'));
132
+ }
133
+ return type.replace('*', '');
134
+ };
135
+ /**
136
+ * Test if an ABI comes from a Cairo 1 contract
137
+ * @param abi representing the interface of a Cairo contract
138
+ * @returns TRUE if it is an ABI from a Cairo1 contract
139
+ * @example
140
+ * ```typescript
141
+ * const isCairo1: boolean = isCairo1Abi(myAbi: Abi);
142
+ * ```
143
+ */
144
+ function isCairo1Abi(abi) {
145
+ const { cairo } = getAbiContractVersion(abi);
146
+ if (cairo === undefined) {
147
+ throw Error('Unable to determine Cairo version');
148
+ }
149
+ return cairo === '1';
150
+ }
151
+ /**
152
+ * Return ContractVersion (Abi version) based on Abi
153
+ * or undefined for unknown version
154
+ * @param abi
155
+ * @returns string
156
+ */
157
+ function getAbiContractVersion(abi) {
158
+ // determine by interface for "Cairo 1.2"
159
+ if (abi.find((it) => it.type === 'interface')) {
160
+ return { cairo: '1', compiler: '2' };
161
+ }
162
+ // determine by function io types "Cairo 1.1" or "Cairo 0.0"
163
+ // find first function with inputs or outputs
164
+ const testFunction = abi.find((it) => it.type === 'function' && (it.inputs.length || it.outputs.length));
165
+ if (!testFunction) {
166
+ return { cairo: undefined, compiler: undefined };
167
+ }
168
+ const io = testFunction.inputs.length ? testFunction.inputs : testFunction.outputs;
169
+ if (isCairo1Type(io[0].type)) {
170
+ return { cairo: '1', compiler: '1' };
171
+ }
172
+ return { cairo: '0', compiler: '0' };
173
+ }
174
+ /**
175
+ * named tuple cairo type is described as js object {}
176
+ * struct cairo type are described as js object {}
177
+ * array cairo type are described as js array []
178
+ */
179
+ /**
180
+ * Create Uint256 Cairo type (helper for common struct type)
181
+ * @example
182
+ * ```typescript
183
+ * uint256('892349863487563453485768723498');
184
+ * ```
185
+ */
186
+ const uint256 = (it) => {
187
+ return new uint256$1.CairoUint256(it).toUint256DecimalString();
188
+ };
189
+ /**
190
+ * Create felt Cairo type (cairo type helper)
191
+ * @returns format: felt-string
192
+ */
193
+ function felt(it) {
194
+ return felt$1.CairoFelt(it);
195
+ }
196
+
197
+ exports.felt = felt;
198
+ exports.getAbiContractVersion = getAbiContractVersion;
199
+ exports.getArrayType = getArrayType;
200
+ exports.isCairo1Abi = isCairo1Abi;
201
+ exports.isCairo1Type = isCairo1Type;
202
+ exports.isLen = isLen;
203
+ exports.isTypeArray = isTypeArray;
204
+ exports.isTypeBool = isTypeBool;
205
+ exports.isTypeByteArray = isTypeByteArray;
206
+ exports.isTypeBytes31 = isTypeBytes31;
207
+ exports.isTypeEnum = isTypeEnum;
208
+ exports.isTypeEthAddress = isTypeEthAddress;
209
+ exports.isTypeFelt = isTypeFelt;
210
+ exports.isTypeLiteral = isTypeLiteral;
211
+ exports.isTypeNamedTuple = isTypeNamedTuple;
212
+ exports.isTypeOption = isTypeOption;
213
+ exports.isTypeResult = isTypeResult;
214
+ exports.isTypeSecp256k1Point = isTypeSecp256k1Point;
215
+ exports.isTypeStruct = isTypeStruct;
216
+ exports.isTypeTuple = isTypeTuple;
217
+ exports.isTypeUint = isTypeUint;
218
+ exports.uint256 = uint256;
@@ -0,0 +1,57 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ /**
6
+ * Class to handle Cairo custom Enum
7
+ * @param enumContent object containing the variants and its content. Example :
8
+ * {Success: 234, Warning: undefined, Error: undefined}.
9
+ * Only one variant with a value, object, array.
10
+ * @returns an instance representing a Cairo custom Enum.
11
+ * @example
12
+ * ```typescript
13
+ * const myCairoEnum = new CairoCustomEnum( {Success: undefined, Warning: "0x7f32ea", Error: undefined})
14
+ * ```
15
+ */
16
+ class CairoCustomEnum {
17
+ /**
18
+ * @param enumContent an object with the variants as keys and the content as value. Only one content shall be defined.
19
+ */
20
+ constructor(enumContent) {
21
+ const variantsList = Object.values(enumContent);
22
+ if (variantsList.length === 0) {
23
+ throw new Error('This Enum must have at least 1 variant');
24
+ }
25
+ const nbActiveVariants = variantsList.filter((content) => typeof content !== 'undefined').length;
26
+ if (nbActiveVariants !== 1) {
27
+ throw new Error('This Enum must have exactly one active variant');
28
+ }
29
+ this.variant = enumContent;
30
+ }
31
+ /**
32
+ *
33
+ * @returns the content of the valid variant of a Cairo custom Enum.
34
+ */
35
+ unwrap() {
36
+ const variants = Object.entries(this.variant);
37
+ const activeVariant = variants.find((item) => typeof item[1] !== 'undefined');
38
+ if (typeof activeVariant === 'undefined') {
39
+ return undefined;
40
+ }
41
+ return activeVariant[1];
42
+ }
43
+ /**
44
+ *
45
+ * @returns the name of the valid variant of a Cairo custom Enum.
46
+ */
47
+ activeVariant() {
48
+ const variants = Object.entries(this.variant);
49
+ const activeVariant = variants.find((item) => typeof item[1] !== 'undefined');
50
+ if (typeof activeVariant === 'undefined') {
51
+ return '';
52
+ }
53
+ return activeVariant[0];
54
+ }
55
+ }
56
+
57
+ exports.CairoCustomEnum = CairoCustomEnum;
@@ -0,0 +1,64 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ exports.CairoOptionVariant = void 0;
6
+ (function (CairoOptionVariant) {
7
+ CairoOptionVariant[CairoOptionVariant["Some"] = 0] = "Some";
8
+ CairoOptionVariant[CairoOptionVariant["None"] = 1] = "None";
9
+ })(exports.CairoOptionVariant || (exports.CairoOptionVariant = {}));
10
+ /**
11
+ * Class to handle Cairo Option
12
+ * @param variant CairoOptionVariant.Some or CairoOptionVariant.None
13
+ * @param someContent value of type T.
14
+ * @returns an instance representing a Cairo Option.
15
+ * @example
16
+ * ```typescript
17
+ * const myOption = new CairoOption<BigNumberish>(CairoOptionVariant.Some, "0x54dda8");
18
+ * ```
19
+ */
20
+ class CairoOption {
21
+ constructor(variant, someContent) {
22
+ if (!(variant in exports.CairoOptionVariant)) {
23
+ throw new Error('Wrong variant : should be CairoOptionVariant.Some or .None.');
24
+ }
25
+ if (variant === exports.CairoOptionVariant.Some) {
26
+ if (typeof someContent === 'undefined') {
27
+ throw new Error('The creation of a Cairo Option with "Some" variant needs a content as input.');
28
+ }
29
+ this.Some = someContent;
30
+ this.None = undefined;
31
+ }
32
+ else {
33
+ this.Some = undefined;
34
+ this.None = true;
35
+ }
36
+ }
37
+ /**
38
+ *
39
+ * @returns the content of the valid variant of a Cairo custom Enum.
40
+ * If None, returns 'undefined'.
41
+ */
42
+ unwrap() {
43
+ if (this.None) {
44
+ return undefined;
45
+ }
46
+ return this.Some;
47
+ }
48
+ /**
49
+ *
50
+ * @returns true if the valid variant is 'isSome'.
51
+ */
52
+ isSome() {
53
+ return !(typeof this.Some === 'undefined');
54
+ }
55
+ /**
56
+ *
57
+ * @returns true if the valid variant is 'isNone'.
58
+ */
59
+ isNone() {
60
+ return this.None === true;
61
+ }
62
+ }
63
+
64
+ exports.CairoOption = CairoOption;
@@ -0,0 +1,63 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ exports.CairoResultVariant = void 0;
6
+ (function (CairoResultVariant) {
7
+ CairoResultVariant[CairoResultVariant["Ok"] = 0] = "Ok";
8
+ CairoResultVariant[CairoResultVariant["Err"] = 1] = "Err";
9
+ })(exports.CairoResultVariant || (exports.CairoResultVariant = {}));
10
+ /**
11
+ * Class to handle Cairo Result
12
+ * @param variant CairoResultVariant.Ok or CairoResultVariant.Err
13
+ * @param resultContent value of type T or U.
14
+ * @returns an instance representing a Cairo Result.
15
+ * @example
16
+ * ```typescript
17
+ * const myOption = new CairoResult<BigNumberish, CustomError>(CairoResultVariant.Ok, "0x54dda8");
18
+ * ```
19
+ */
20
+ class CairoResult {
21
+ constructor(variant, resultContent) {
22
+ if (!(variant in exports.CairoResultVariant)) {
23
+ throw new Error('Wrong variant : should be CairoResultVariant.Ok or .Err.');
24
+ }
25
+ if (variant === exports.CairoResultVariant.Ok) {
26
+ this.Ok = resultContent;
27
+ this.Err = undefined;
28
+ }
29
+ else {
30
+ this.Ok = undefined;
31
+ this.Err = resultContent;
32
+ }
33
+ }
34
+ /**
35
+ *
36
+ * @returns the content of the valid variant of a Cairo Result.
37
+ */
38
+ unwrap() {
39
+ if (typeof this.Ok !== 'undefined') {
40
+ return this.Ok;
41
+ }
42
+ if (typeof this.Err !== 'undefined') {
43
+ return this.Err;
44
+ }
45
+ throw new Error('Both Result.Ok and .Err are undefined. Not authorized.');
46
+ }
47
+ /**
48
+ *
49
+ * @returns true if the valid variant is 'Ok'.
50
+ */
51
+ isOk() {
52
+ return !(typeof this.Ok === 'undefined');
53
+ }
54
+ /**
55
+ *
56
+ * @returns true if the valid variant is 'isErr'.
57
+ */
58
+ isErr() {
59
+ return !(typeof this.Err === 'undefined');
60
+ }
61
+ }
62
+
63
+ exports.CairoResult = CairoResult;
@@ -0,0 +1,66 @@
1
+ 'use strict';
2
+
3
+ var num = require('../num.js');
4
+ var shortString = require('../shortString.js');
5
+
6
+ const guard = {
7
+ isBN: (data, type, key) => {
8
+ if (!num.isBigInt(data[key]))
9
+ throw new Error(`Data and formatter mismatch on ${key}:${type[key]}, expected response data ${key}:${data[key]} to be BN instead it is ${typeof data[key]}`);
10
+ },
11
+ unknown: (data, type, key) => {
12
+ throw new Error(`Unhandled formatter type on ${key}:${type[key]} for data ${key}:${data[key]}`);
13
+ },
14
+ };
15
+ /**
16
+ * Formats the given data based on the provided type definition.
17
+ *
18
+ * @param {any} data - The data to be formatted.
19
+ * @param {any} type - The type definition for the data.
20
+ * @param {any} [sameType] - The same type definition to be used (optional).
21
+ * @returns - The formatted data.
22
+ */
23
+ function formatter(data, type, sameType) {
24
+ // match data element with type element
25
+ return Object.entries(data).reduce((acc, [key, value]) => {
26
+ const elType = sameType ?? type[key];
27
+ if (!(key in type) && !sameType) {
28
+ // no type definition for element return original element
29
+ acc[key] = value;
30
+ return acc;
31
+ }
32
+ if (elType === 'string') {
33
+ if (Array.isArray(data[key])) {
34
+ // long string (felt*)
35
+ const arrayStr = formatter(data[key], data[key].map((_) => elType));
36
+ acc[key] = Object.values(arrayStr).join('');
37
+ return acc;
38
+ }
39
+ guard.isBN(data, type, key);
40
+ acc[key] = shortString.decodeShortString(value);
41
+ return acc;
42
+ }
43
+ if (elType === 'number') {
44
+ guard.isBN(data, type, key);
45
+ acc[key] = Number(value);
46
+ return acc;
47
+ }
48
+ if (typeof elType === 'function') {
49
+ acc[key] = elType(value);
50
+ return acc;
51
+ }
52
+ if (Array.isArray(elType)) {
53
+ const arrayObj = formatter(data[key], elType, elType[0]);
54
+ acc[key] = Object.values(arrayObj);
55
+ return acc;
56
+ }
57
+ if (typeof elType === 'object') {
58
+ acc[key] = formatter(data[key], elType);
59
+ return acc;
60
+ }
61
+ guard.unknown(data, type, key);
62
+ return acc;
63
+ }, {});
64
+ }
65
+
66
+ module.exports = formatter;