@gardenfi/swap 0.0.2-beta.2 → 0.0.2-beta.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.
Files changed (230) hide show
  1. package/dist/index104.cjs +1 -1
  2. package/dist/index104.js +4 -4
  3. package/dist/index106.cjs +1 -1
  4. package/dist/index106.js +3 -3
  5. package/dist/index107.cjs +1 -1
  6. package/dist/index107.js +3 -3
  7. package/dist/index116.cjs +1 -1
  8. package/dist/index116.js +1 -1
  9. package/dist/index117.cjs +1 -1
  10. package/dist/index117.js +1 -1
  11. package/dist/index12.cjs +1 -1
  12. package/dist/index12.js +12 -12
  13. package/dist/index120.cjs +1 -1
  14. package/dist/index120.js +2 -2
  15. package/dist/index121.cjs +1 -1
  16. package/dist/index121.js +7 -7
  17. package/dist/index132.cjs +1 -1
  18. package/dist/index132.js +1 -1
  19. package/dist/index133.cjs +1 -1
  20. package/dist/index133.js +1 -1
  21. package/dist/index134.cjs +1 -1
  22. package/dist/index134.js +1 -1
  23. package/dist/index135.cjs +1 -1
  24. package/dist/index135.js +1 -1
  25. package/dist/index138.cjs +1 -1
  26. package/dist/index138.js +2 -2
  27. package/dist/index161.cjs +1 -1
  28. package/dist/index161.js +2 -2
  29. package/dist/index167.cjs +1 -1
  30. package/dist/index167.js +1 -1
  31. package/dist/index168.cjs +1 -1
  32. package/dist/index168.js +2 -2
  33. package/dist/index169.cjs +1 -1
  34. package/dist/index169.js +2 -2
  35. package/dist/index170.cjs +1 -1
  36. package/dist/index170.js +2 -2
  37. package/dist/index171.cjs +1 -1
  38. package/dist/index171.js +2 -2
  39. package/dist/index172.cjs +1 -1
  40. package/dist/index172.js +2 -2
  41. package/dist/index272.cjs +1 -1
  42. package/dist/index272.js +9 -174
  43. package/dist/index273.cjs +1 -2
  44. package/dist/index273.js +30 -91
  45. package/dist/index274.cjs +1 -1
  46. package/dist/index274.js +2 -41
  47. package/dist/index275.cjs +1 -1
  48. package/dist/index275.js +47 -4
  49. package/dist/index276.cjs +1 -1
  50. package/dist/index276.js +11 -9
  51. package/dist/index277.cjs +1 -1
  52. package/dist/index277.js +174 -30
  53. package/dist/index278.cjs +2 -1
  54. package/dist/index278.js +92 -2
  55. package/dist/index279.cjs +1 -1
  56. package/dist/index279.js +38 -45
  57. package/dist/index280.cjs +1 -1
  58. package/dist/index280.js +4 -11
  59. package/dist/index281.cjs +1 -1
  60. package/dist/index281.js +3 -3
  61. package/dist/index283.cjs +1 -2
  62. package/dist/index283.js +42 -41
  63. package/dist/index284.cjs +1 -1
  64. package/dist/index284.js +321 -2
  65. package/dist/index285.cjs +1 -1
  66. package/dist/index285.js +605 -5
  67. package/dist/index286.cjs +1 -1
  68. package/dist/index286.js +109 -10
  69. package/dist/index287.cjs +1 -1
  70. package/dist/index287.js +167 -19
  71. package/dist/index288.cjs +1 -1
  72. package/dist/index288.js +301 -8
  73. package/dist/index289.cjs +1 -1
  74. package/dist/index289.js +93 -72
  75. package/dist/index290.cjs +1 -1
  76. package/dist/index290.js +54 -63
  77. package/dist/index291.cjs +1 -1
  78. package/dist/index291.js +51 -186
  79. package/dist/index292.cjs +1 -1
  80. package/dist/index292.js +42 -20
  81. package/dist/index293.cjs +1 -1
  82. package/dist/index293.js +14 -41
  83. package/dist/index294.cjs +1 -1
  84. package/dist/index294.js +315 -23
  85. package/dist/index295.cjs +1 -1
  86. package/dist/index295.js +57 -88
  87. package/dist/index296.cjs +1 -1
  88. package/dist/index296.js +19 -40
  89. package/dist/index297.cjs +1 -1
  90. package/dist/index297.js +28 -15
  91. package/dist/index298.cjs +1 -1
  92. package/dist/index298.js +3 -13
  93. package/dist/index299.cjs +7 -1
  94. package/dist/index299.js +237 -7
  95. package/dist/index300.cjs +1 -1
  96. package/dist/index300.js +48 -38
  97. package/dist/index301.cjs +1 -1
  98. package/dist/index301.js +211 -14
  99. package/dist/index302.cjs +1 -1
  100. package/dist/index302.js +77 -38
  101. package/dist/index303.cjs +1 -1
  102. package/dist/index303.js +25 -316
  103. package/dist/index304.cjs +1 -1
  104. package/dist/index304.js +73 -602
  105. package/dist/index305.cjs +2 -1
  106. package/dist/index305.js +41 -109
  107. package/dist/index306.cjs +1 -1
  108. package/dist/index306.js +2 -168
  109. package/dist/index307.cjs +1 -1
  110. package/dist/index307.js +5 -301
  111. package/dist/index308.cjs +1 -1
  112. package/dist/index308.js +10 -93
  113. package/dist/index309.cjs +1 -1
  114. package/dist/index309.js +18 -55
  115. package/dist/index310.cjs +1 -1
  116. package/dist/index310.js +8 -53
  117. package/dist/index311.cjs +1 -1
  118. package/dist/index311.js +72 -44
  119. package/dist/index312.cjs +1 -1
  120. package/dist/index312.js +64 -14
  121. package/dist/index313.cjs +1 -1
  122. package/dist/index313.js +184 -313
  123. package/dist/index314.cjs +1 -1
  124. package/dist/index314.js +19 -56
  125. package/dist/index315.cjs +1 -1
  126. package/dist/index315.js +42 -19
  127. package/dist/index316.cjs +1 -1
  128. package/dist/index316.js +23 -27
  129. package/dist/index317.cjs +1 -1
  130. package/dist/index317.js +90 -3
  131. package/dist/index318.cjs +1 -7
  132. package/dist/index318.js +33 -230
  133. package/dist/index319.cjs +1 -1
  134. package/dist/index319.js +17 -48
  135. package/dist/index320.cjs +1 -1
  136. package/dist/index320.js +11 -210
  137. package/dist/index321.cjs +1 -1
  138. package/dist/index321.js +7 -81
  139. package/dist/index322.cjs +1 -1
  140. package/dist/index322.js +36 -28
  141. package/dist/index323.cjs +1 -1
  142. package/dist/index323.js +15 -76
  143. package/dist/index326.cjs +1 -1
  144. package/dist/index326.js +1 -1
  145. package/dist/index327.cjs +1 -1
  146. package/dist/index327.js +1 -1
  147. package/dist/index333.cjs +1 -1
  148. package/dist/index333.js +2 -2
  149. package/dist/index334.cjs +1 -1
  150. package/dist/index334.js +2 -2
  151. package/dist/index340.cjs +1 -1
  152. package/dist/index340.js +1 -1
  153. package/dist/index341.cjs +1 -1
  154. package/dist/index341.js +1 -1
  155. package/dist/index343.cjs +1 -1
  156. package/dist/index343.js +1 -1
  157. package/dist/index346.cjs +1 -1
  158. package/dist/index346.js +1 -1
  159. package/dist/index347.cjs +1 -1
  160. package/dist/index347.js +1 -1
  161. package/dist/index348.cjs +1 -1
  162. package/dist/index348.js +3 -3
  163. package/dist/index349.cjs +1 -2
  164. package/dist/index349.js +4 -193
  165. package/dist/index350.cjs +2 -1
  166. package/dist/index350.js +168 -52
  167. package/dist/index351.cjs +1 -1
  168. package/dist/index351.js +65 -331
  169. package/dist/index352.cjs +1 -1
  170. package/dist/index352.js +332 -149
  171. package/dist/index353.cjs +1 -1
  172. package/dist/index353.js +161 -5
  173. package/dist/index354.cjs +1 -1
  174. package/dist/index354.js +5 -5
  175. package/dist/index357.cjs +1 -1
  176. package/dist/index357.js +8 -205
  177. package/dist/index359.cjs +1 -1
  178. package/dist/index359.js +205 -8
  179. package/dist/index360.cjs +1 -1
  180. package/dist/index360.js +94 -68
  181. package/dist/index361.cjs +1 -1
  182. package/dist/index361.js +28 -94
  183. package/dist/index362.cjs +1 -1
  184. package/dist/index362.js +147 -27
  185. package/dist/index363.cjs +1 -1
  186. package/dist/index363.js +324 -118
  187. package/dist/index364.cjs +1 -1
  188. package/dist/index364.js +72 -353
  189. package/dist/index365.cjs +1 -1
  190. package/dist/index365.js +29 -71
  191. package/dist/index366.cjs +1 -1
  192. package/dist/index366.js +5 -31
  193. package/dist/index367.cjs +1 -1
  194. package/dist/index367.js +275 -5
  195. package/dist/index368.cjs +1 -1
  196. package/dist/index368.js +171 -4
  197. package/dist/index369.cjs +1 -1
  198. package/dist/index369.js +10 -141
  199. package/dist/index370.cjs +1 -1
  200. package/dist/index370.js +105 -273
  201. package/dist/index371.cjs +1 -1
  202. package/dist/index371.js +10 -171
  203. package/dist/index372.cjs +1 -1
  204. package/dist/index372.js +69 -10
  205. package/dist/index373.cjs +1 -1
  206. package/dist/index373.js +3 -107
  207. package/dist/index374.cjs +1 -1
  208. package/dist/index374.js +140 -9
  209. package/dist/index375.cjs +1 -1
  210. package/dist/index375.js +4 -4
  211. package/dist/index376.cjs +1 -1
  212. package/dist/index376.js +2 -79
  213. package/dist/index377.cjs +1 -1
  214. package/dist/index377.js +79 -2
  215. package/dist/index379.cjs +1 -1
  216. package/dist/index379.js +1 -1
  217. package/dist/index380.cjs +1 -1
  218. package/dist/index380.js +1 -1
  219. package/dist/index381.cjs +1 -1
  220. package/dist/index381.js +2 -22
  221. package/dist/index382.cjs +1 -1
  222. package/dist/index382.js +22 -2
  223. package/dist/index385.cjs +1 -1
  224. package/dist/index385.js +2 -2
  225. package/dist/index77.cjs +1 -1
  226. package/dist/index77.js +13 -13
  227. package/dist/index99.cjs +1 -1
  228. package/dist/index99.js +1 -1
  229. package/dist/src/lib/store/swapStore.d.ts +1 -1
  230. package/package.json +5 -5
package/dist/index363.js CHANGED
@@ -1,151 +1,357 @@
1
- import { ulebEncode as n } from "./index362.js";
2
- import { encodeStr as o } from "./index316.js";
3
- class u {
4
- constructor({
5
- initialSize: t = 1024,
6
- maxSize: i = 1 / 0,
7
- allocateSize: e = 1024
8
- } = {}) {
9
- this.bytePosition = 0, this.size = t, this.maxSize = i, this.allocateSize = e, this.dataView = new DataView(new ArrayBuffer(t));
10
- }
11
- ensureSizeOrGrow(t) {
12
- const i = this.bytePosition + t;
13
- if (i > this.size) {
14
- const e = Math.min(
15
- this.maxSize,
16
- Math.max(this.size + i, this.size + this.allocateSize)
17
- );
18
- if (i > e)
19
- throw new Error(
20
- `Attempting to serialize to BCS, but buffer does not have enough size. Allocated size: ${this.size}, Max size: ${this.maxSize}, Required size: ${i}`
21
- );
22
- this.size = e;
23
- const s = new ArrayBuffer(this.size);
24
- new Uint8Array(s).set(new Uint8Array(this.dataView.buffer)), this.dataView = new DataView(s);
1
+ import { lazyBcsType as l, BcsEnum as h, BcsStruct as g, BcsTuple as E, stringLikeBcsType as B, BcsType as w, fixedSizeBcsType as f, dynamicSizeBcsType as x, bigUIntBcsType as u, uIntBcsType as m } from "./index284.js";
2
+ import { ulebEncode as y } from "./index361.js";
3
+ function $(e, r, a) {
4
+ return new w({
5
+ read: (t) => {
6
+ const d = new Array(e);
7
+ for (let n = 0; n < e; n++)
8
+ d[n] = r.read(t);
9
+ return d;
10
+ },
11
+ write: (t, d) => {
12
+ for (const n of t)
13
+ r.write(n, d);
14
+ },
15
+ ...a,
16
+ name: (a == null ? void 0 : a.name) ?? `${r.name}[${e}]`,
17
+ validate: (t) => {
18
+ var d;
19
+ if ((d = a == null ? void 0 : a.validate) == null || d.call(a, t), !t || typeof t != "object" || !("length" in t))
20
+ throw new TypeError(`Expected array, found ${typeof t}`);
21
+ if (t.length !== e)
22
+ throw new TypeError(`Expected array of length ${e}, found ${t.length}`);
25
23
  }
26
- }
24
+ });
25
+ }
26
+ function b(e) {
27
+ return c.enum(`Option<${e.name}>`, {
28
+ None: null,
29
+ Some: e
30
+ }).transform({
31
+ input: (r) => r == null ? { None: !0 } : { Some: r },
32
+ output: (r) => r.$kind === "Some" ? r.Some : null
33
+ });
34
+ }
35
+ function T(e, r) {
36
+ return new w({
37
+ read: (a) => {
38
+ const t = a.readULEB(), d = new Array(t);
39
+ for (let n = 0; n < t; n++)
40
+ d[n] = e.read(a);
41
+ return d;
42
+ },
43
+ write: (a, t) => {
44
+ t.writeULEB(a.length);
45
+ for (const d of a)
46
+ e.write(d, t);
47
+ },
48
+ ...r,
49
+ name: (r == null ? void 0 : r.name) ?? `vector<${e.name}>`,
50
+ validate: (a) => {
51
+ var t;
52
+ if ((t = r == null ? void 0 : r.validate) == null || t.call(r, a), !a || typeof a != "object" || !("length" in a))
53
+ throw new TypeError(`Expected array, found ${typeof a}`);
54
+ }
55
+ });
56
+ }
57
+ function M(e, r) {
58
+ return c.vector(c.tuple([e, r])).transform({
59
+ name: `Map<${e.name}, ${r.name}>`,
60
+ input: (a) => [...a.entries()],
61
+ output: (a) => {
62
+ const t = /* @__PURE__ */ new Map();
63
+ for (const [d, n] of a)
64
+ t.set(d, n);
65
+ return t;
66
+ }
67
+ });
68
+ }
69
+ const c = {
27
70
  /**
28
- * Shift current cursor position by `bytes`.
29
- *
30
- * @param {Number} bytes Number of bytes to
31
- * @returns {this} Self for possible chaining.
71
+ * Creates a BcsType that can be used to read and write an 8-bit unsigned integer.
72
+ * @example
73
+ * bcs.u8().serialize(255).toBytes() // Uint8Array [ 255 ]
32
74
  */
33
- shift(t) {
34
- return this.bytePosition += t, this;
35
- }
75
+ u8(e) {
76
+ return m({
77
+ readMethod: "read8",
78
+ writeMethod: "write8",
79
+ size: 1,
80
+ maxValue: 2 ** 8 - 1,
81
+ ...e,
82
+ name: (e == null ? void 0 : e.name) ?? "u8"
83
+ });
84
+ },
36
85
  /**
37
- * Write a U8 value into a buffer and shift cursor position by 1.
38
- * @param {Number} value Value to write.
39
- * @returns {this}
86
+ * Creates a BcsType that can be used to read and write a 16-bit unsigned integer.
87
+ * @example
88
+ * bcs.u16().serialize(65535).toBytes() // Uint8Array [ 255, 255 ]
40
89
  */
41
- write8(t) {
42
- return this.ensureSizeOrGrow(1), this.dataView.setUint8(this.bytePosition, Number(t)), this.shift(1);
43
- }
90
+ u16(e) {
91
+ return m({
92
+ readMethod: "read16",
93
+ writeMethod: "write16",
94
+ size: 2,
95
+ maxValue: 2 ** 16 - 1,
96
+ ...e,
97
+ name: (e == null ? void 0 : e.name) ?? "u16"
98
+ });
99
+ },
44
100
  /**
45
- * Write a U8 value into a buffer and shift cursor position by 1.
46
- * @param {Number} value Value to write.
47
- * @returns {this}
101
+ * Creates a BcsType that can be used to read and write a 32-bit unsigned integer.
102
+ * @example
103
+ * bcs.u32().serialize(4294967295).toBytes() // Uint8Array [ 255, 255, 255, 255 ]
48
104
  */
49
- writeBytes(t) {
50
- this.ensureSizeOrGrow(t.length);
51
- for (let i = 0; i < t.length; i++)
52
- this.dataView.setUint8(this.bytePosition + i, t[i]);
53
- return this.shift(t.length);
54
- }
105
+ u32(e) {
106
+ return m({
107
+ readMethod: "read32",
108
+ writeMethod: "write32",
109
+ size: 4,
110
+ maxValue: 2 ** 32 - 1,
111
+ ...e,
112
+ name: (e == null ? void 0 : e.name) ?? "u32"
113
+ });
114
+ },
55
115
  /**
56
- * Write a U16 value into a buffer and shift cursor position by 2.
57
- * @param {Number} value Value to write.
58
- * @returns {this}
116
+ * Creates a BcsType that can be used to read and write a 64-bit unsigned integer.
117
+ * @example
118
+ * bcs.u64().serialize(1).toBytes() // Uint8Array [ 1, 0, 0, 0, 0, 0, 0, 0 ]
59
119
  */
60
- write16(t) {
61
- return this.ensureSizeOrGrow(2), this.dataView.setUint16(this.bytePosition, Number(t), !0), this.shift(2);
62
- }
120
+ u64(e) {
121
+ return u({
122
+ readMethod: "read64",
123
+ writeMethod: "write64",
124
+ size: 8,
125
+ maxValue: 2n ** 64n - 1n,
126
+ ...e,
127
+ name: (e == null ? void 0 : e.name) ?? "u64"
128
+ });
129
+ },
63
130
  /**
64
- * Write a U32 value into a buffer and shift cursor position by 4.
65
- * @param {Number} value Value to write.
66
- * @returns {this}
131
+ * Creates a BcsType that can be used to read and write a 128-bit unsigned integer.
132
+ * @example
133
+ * bcs.u128().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]
67
134
  */
68
- write32(t) {
69
- return this.ensureSizeOrGrow(4), this.dataView.setUint32(this.bytePosition, Number(t), !0), this.shift(4);
70
- }
135
+ u128(e) {
136
+ return u({
137
+ readMethod: "read128",
138
+ writeMethod: "write128",
139
+ size: 16,
140
+ maxValue: 2n ** 128n - 1n,
141
+ ...e,
142
+ name: (e == null ? void 0 : e.name) ?? "u128"
143
+ });
144
+ },
71
145
  /**
72
- * Write a U64 value into a buffer and shift cursor position by 8.
73
- * @param {bigint} value Value to write.
74
- * @returns {this}
146
+ * Creates a BcsType that can be used to read and write a 256-bit unsigned integer.
147
+ * @example
148
+ * bcs.u256().serialize(1).toBytes() // Uint8Array [ 1, ..., 0 ]
75
149
  */
76
- write64(t) {
77
- return h(BigInt(t), 8).forEach((i) => this.write8(i)), this;
78
- }
150
+ u256(e) {
151
+ return u({
152
+ readMethod: "read256",
153
+ writeMethod: "write256",
154
+ size: 32,
155
+ maxValue: 2n ** 256n - 1n,
156
+ ...e,
157
+ name: (e == null ? void 0 : e.name) ?? "u256"
158
+ });
159
+ },
79
160
  /**
80
- * Write a U128 value into a buffer and shift cursor position by 16.
161
+ * Creates a BcsType that can be used to read and write boolean values.
162
+ * @example
163
+ * bcs.bool().serialize(true).toBytes() // Uint8Array [ 1 ]
164
+ */
165
+ bool(e) {
166
+ return f({
167
+ size: 1,
168
+ read: (r) => r.read8() === 1,
169
+ write: (r, a) => a.write8(r ? 1 : 0),
170
+ ...e,
171
+ name: (e == null ? void 0 : e.name) ?? "bool",
172
+ validate: (r) => {
173
+ var a;
174
+ if ((a = e == null ? void 0 : e.validate) == null || a.call(e, r), typeof r != "boolean")
175
+ throw new TypeError(`Expected boolean, found ${typeof r}`);
176
+ }
177
+ });
178
+ },
179
+ /**
180
+ * Creates a BcsType that can be used to read and write unsigned LEB encoded integers
181
+ * @example
81
182
  *
82
- * @param {bigint} value Value to write.
83
- * @returns {this}
84
183
  */
85
- write128(t) {
86
- return h(BigInt(t), 16).forEach((i) => this.write8(i)), this;
87
- }
184
+ uleb128(e) {
185
+ return x({
186
+ read: (r) => r.readULEB(),
187
+ serialize: (r) => Uint8Array.from(y(r)),
188
+ ...e,
189
+ name: (e == null ? void 0 : e.name) ?? "uleb128"
190
+ });
191
+ },
192
+ /**
193
+ * Creates a BcsType representing a fixed length byte array
194
+ * @param size The number of bytes this types represents
195
+ * @example
196
+ * bcs.bytes(3).serialize(new Uint8Array([1, 2, 3])).toBytes() // Uint8Array [1, 2, 3]
197
+ */
198
+ bytes(e, r) {
199
+ return f({
200
+ size: e,
201
+ read: (a) => a.readBytes(e),
202
+ write: (a, t) => {
203
+ t.writeBytes(new Uint8Array(a));
204
+ },
205
+ ...r,
206
+ name: (r == null ? void 0 : r.name) ?? `bytes[${e}]`,
207
+ validate: (a) => {
208
+ var t;
209
+ if ((t = r == null ? void 0 : r.validate) == null || t.call(r, a), !a || typeof a != "object" || !("length" in a))
210
+ throw new TypeError(`Expected array, found ${typeof a}`);
211
+ if (a.length !== e)
212
+ throw new TypeError(`Expected array of length ${e}, found ${a.length}`);
213
+ }
214
+ });
215
+ },
88
216
  /**
89
- * Write a U256 value into a buffer and shift cursor position by 16.
217
+ * Creates a BcsType representing a variable length byte array
90
218
  *
91
- * @param {bigint} value Value to write.
92
- * @returns {this}
219
+ * @example
220
+ * bcs.byteVector().serialize([1, 2, 3]).toBytes() // Uint8Array [3, 1, 2, 3]
93
221
  */
94
- write256(t) {
95
- return h(BigInt(t), 32).forEach((i) => this.write8(i)), this;
96
- }
222
+ byteVector(e) {
223
+ return new w({
224
+ read: (r) => {
225
+ const a = r.readULEB();
226
+ return r.readBytes(a);
227
+ },
228
+ write: (r, a) => {
229
+ const t = new Uint8Array(r);
230
+ a.writeULEB(t.length), a.writeBytes(t);
231
+ },
232
+ ...e,
233
+ name: (e == null ? void 0 : e.name) ?? "vector<u8>",
234
+ serializedSize: (r) => {
235
+ const a = "length" in r ? r.length : null;
236
+ return a == null ? null : y(a).length + a;
237
+ },
238
+ validate: (r) => {
239
+ var a;
240
+ if ((a = e == null ? void 0 : e.validate) == null || a.call(e, r), !r || typeof r != "object" || !("length" in r))
241
+ throw new TypeError(`Expected array, found ${typeof r}`);
242
+ }
243
+ });
244
+ },
97
245
  /**
98
- * Write a ULEB value into a buffer and shift cursor position by number of bytes
99
- * written.
100
- * @param {Number} value Value to write.
101
- * @returns {this}
246
+ * Creates a BcsType that can ser/de string values. Strings will be UTF-8 encoded
247
+ * @example
248
+ * bcs.string().serialize('a').toBytes() // Uint8Array [ 1, 97 ]
102
249
  */
103
- writeULEB(t) {
104
- return n(t).forEach((i) => this.write8(i)), this;
105
- }
250
+ string(e) {
251
+ return B({
252
+ toBytes: (r) => new TextEncoder().encode(r),
253
+ fromBytes: (r) => new TextDecoder().decode(r),
254
+ ...e,
255
+ name: (e == null ? void 0 : e.name) ?? "string"
256
+ });
257
+ },
258
+ /**
259
+ * Creates a BcsType that represents a fixed length array of a given type
260
+ * @param size The number of elements in the array
261
+ * @param type The BcsType of each element in the array
262
+ * @example
263
+ * bcs.fixedArray(3, bcs.u8()).serialize([1, 2, 3]).toBytes() // Uint8Array [ 1, 2, 3 ]
264
+ */
265
+ fixedArray: $,
266
+ /**
267
+ * Creates a BcsType representing an optional value
268
+ * @param type The BcsType of the optional value
269
+ * @example
270
+ * bcs.option(bcs.u8()).serialize(null).toBytes() // Uint8Array [ 0 ]
271
+ * bcs.option(bcs.u8()).serialize(1).toBytes() // Uint8Array [ 1, 1 ]
272
+ */
273
+ option: b,
106
274
  /**
107
- * Write a vector into a buffer by first writing the vector length and then calling
108
- * a callback on each passed value.
275
+ * Creates a BcsType representing a variable length vector of a given type
276
+ * @param type The BcsType of each element in the vector
109
277
  *
110
- * @param {Array<Any>} vector Array of elements to write.
111
- * @param {WriteVecCb} cb Callback to call on each element of the vector.
112
- * @returns {this}
278
+ * @example
279
+ * bcs.vector(bcs.u8()).toBytes([1, 2, 3]) // Uint8Array [ 3, 1, 2, 3 ]
113
280
  */
114
- writeVec(t, i) {
115
- return this.writeULEB(t.length), Array.from(t).forEach((e, s) => i(this, e, s, t.length)), this;
116
- }
281
+ vector: T,
117
282
  /**
118
- * Adds support for iterations over the object.
119
- * @returns {Uint8Array}
283
+ * Creates a BcsType representing a tuple of a given set of types
284
+ * @param types The BcsTypes for each element in the tuple
285
+ *
286
+ * @example
287
+ * const tuple = bcs.tuple([bcs.u8(), bcs.string(), bcs.bool()])
288
+ * tuple.serialize([1, 'a', true]).toBytes() // Uint8Array [ 1, 1, 97, 1 ]
120
289
  */
121
- // oxlint-disable-next-line require-yields
122
- *[Symbol.iterator]() {
123
- for (let t = 0; t < this.bytePosition; t++)
124
- yield this.dataView.getUint8(t);
125
- return this.toBytes();
126
- }
290
+ tuple(e, r) {
291
+ return new E({
292
+ fields: e,
293
+ ...r
294
+ });
295
+ },
127
296
  /**
128
- * Get underlying buffer taking only value bytes (in case initial buffer size was bigger).
129
- * @returns {Uint8Array} Resulting bcs.
297
+ * Creates a BcsType representing a struct of a given set of fields
298
+ * @param name The name of the struct
299
+ * @param fields The fields of the struct. The order of the fields affects how data is serialized and deserialized
300
+ *
301
+ * @example
302
+ * const struct = bcs.struct('MyStruct', {
303
+ * a: bcs.u8(),
304
+ * b: bcs.string(),
305
+ * })
306
+ * struct.serialize({ a: 1, b: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]
130
307
  */
131
- toBytes() {
132
- return new Uint8Array(this.dataView.buffer.slice(0, this.bytePosition));
133
- }
308
+ struct(e, r, a) {
309
+ return new g({
310
+ name: e,
311
+ fields: r,
312
+ ...a
313
+ });
314
+ },
134
315
  /**
135
- * Represent data as 'hex' or 'base64'
136
- * @param encoding Encoding to use: 'base64' or 'hex'
316
+ * Creates a BcsType representing an enum of a given set of options
317
+ * @param name The name of the enum
318
+ * @param values The values of the enum. The order of the values affects how data is serialized and deserialized.
319
+ * null can be used to represent a variant with no data.
320
+ *
321
+ * @example
322
+ * const enum = bcs.enum('MyEnum', {
323
+ * A: bcs.u8(),
324
+ * B: bcs.string(),
325
+ * C: null,
326
+ * })
327
+ * enum.serialize({ A: 1 }).toBytes() // Uint8Array [ 0, 1 ]
328
+ * enum.serialize({ B: 'a' }).toBytes() // Uint8Array [ 1, 1, 97 ]
329
+ * enum.serialize({ C: true }).toBytes() // Uint8Array [ 2 ]
137
330
  */
138
- toString(t) {
139
- return o(this.toBytes(), t);
331
+ enum(e, r, a) {
332
+ return new h({
333
+ name: e,
334
+ fields: r,
335
+ ...a
336
+ });
337
+ },
338
+ /**
339
+ * Creates a BcsType representing a map of a given key and value type
340
+ * @param keyType The BcsType of the key
341
+ * @param valueType The BcsType of the value
342
+ * @example
343
+ * const map = bcs.map(bcs.u8(), bcs.string())
344
+ * map.serialize(new Map([[2, 'a']])).toBytes() // Uint8Array [ 1, 2, 1, 97 ]
345
+ */
346
+ map: M,
347
+ /**
348
+ * Creates a BcsType that wraps another BcsType which is lazily evaluated. This is useful for creating recursive types.
349
+ * @param cb A callback that returns the BcsType
350
+ */
351
+ lazy(e) {
352
+ return l(e);
140
353
  }
141
- }
142
- function h(r, t) {
143
- const i = new Uint8Array(t);
144
- let e = 0;
145
- for (; r > 0; )
146
- i[e] = Number(r % BigInt(256)), r = r / BigInt(256), e += 1;
147
- return i;
148
- }
354
+ };
149
355
  export {
150
- u as BcsWriter
356
+ c as bcs
151
357
  };
package/dist/index364.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("./index303.cjs"),m=require("./index362.cjs");function y(e,r,a){return new d.BcsType({read:t=>{const n=new Array(e);for(let u=0;u<e;u++)n[u]=r.read(t);return n},write:(t,n)=>{for(const u of t)r.write(u,n)},...a,name:(a==null?void 0:a.name)??`${r.name}[${e}]`,validate:t=>{var n;if((n=a==null?void 0:a.validate)==null||n.call(a,t),!t||typeof t!="object"||!("length"in t))throw new TypeError(`Expected array, found ${typeof t}`);if(t.length!==e)throw new TypeError(`Expected array of length ${e}, found ${t.length}`)}})}function w(e){return c.enum(`Option<${e.name}>`,{None:null,Some:e}).transform({input:r=>r==null?{None:!0}:{Some:r},output:r=>r.$kind==="Some"?r.Some:null})}function f(e,r){return new d.BcsType({read:a=>{const t=a.readULEB(),n=new Array(t);for(let u=0;u<t;u++)n[u]=e.read(a);return n},write:(a,t)=>{t.writeULEB(a.length);for(const n of a)e.write(n,t)},...r,name:(r==null?void 0:r.name)??`vector<${e.name}>`,validate:a=>{var t;if((t=r==null?void 0:r.validate)==null||t.call(r,a),!a||typeof a!="object"||!("length"in a))throw new TypeError(`Expected array, found ${typeof a}`)}})}function l(e,r){return c.vector(c.tuple([e,r])).transform({name:`Map<${e.name}, ${r.name}>`,input:a=>[...a.entries()],output:a=>{const t=new Map;for(const[n,u]of a)t.set(n,u);return t}})}const c={u8(e){return d.uIntBcsType({readMethod:"read8",writeMethod:"write8",size:1,maxValue:2**8-1,...e,name:(e==null?void 0:e.name)??"u8"})},u16(e){return d.uIntBcsType({readMethod:"read16",writeMethod:"write16",size:2,maxValue:2**16-1,...e,name:(e==null?void 0:e.name)??"u16"})},u32(e){return d.uIntBcsType({readMethod:"read32",writeMethod:"write32",size:4,maxValue:2**32-1,...e,name:(e==null?void 0:e.name)??"u32"})},u64(e){return d.bigUIntBcsType({readMethod:"read64",writeMethod:"write64",size:8,maxValue:2n**64n-1n,...e,name:(e==null?void 0:e.name)??"u64"})},u128(e){return d.bigUIntBcsType({readMethod:"read128",writeMethod:"write128",size:16,maxValue:2n**128n-1n,...e,name:(e==null?void 0:e.name)??"u128"})},u256(e){return d.bigUIntBcsType({readMethod:"read256",writeMethod:"write256",size:32,maxValue:2n**256n-1n,...e,name:(e==null?void 0:e.name)??"u256"})},bool(e){return d.fixedSizeBcsType({size:1,read:r=>r.read8()===1,write:(r,a)=>a.write8(r?1:0),...e,name:(e==null?void 0:e.name)??"bool",validate:r=>{var a;if((a=e==null?void 0:e.validate)==null||a.call(e,r),typeof r!="boolean")throw new TypeError(`Expected boolean, found ${typeof r}`)}})},uleb128(e){return d.dynamicSizeBcsType({read:r=>r.readULEB(),serialize:r=>Uint8Array.from(m.ulebEncode(r)),...e,name:(e==null?void 0:e.name)??"uleb128"})},bytes(e,r){return d.fixedSizeBcsType({size:e,read:a=>a.readBytes(e),write:(a,t)=>{t.writeBytes(new Uint8Array(a))},...r,name:(r==null?void 0:r.name)??`bytes[${e}]`,validate:a=>{var t;if((t=r==null?void 0:r.validate)==null||t.call(r,a),!a||typeof a!="object"||!("length"in a))throw new TypeError(`Expected array, found ${typeof a}`);if(a.length!==e)throw new TypeError(`Expected array of length ${e}, found ${a.length}`)}})},byteVector(e){return new d.BcsType({read:r=>{const a=r.readULEB();return r.readBytes(a)},write:(r,a)=>{const t=new Uint8Array(r);a.writeULEB(t.length),a.writeBytes(t)},...e,name:(e==null?void 0:e.name)??"vector<u8>",serializedSize:r=>{const a="length"in r?r.length:null;return a==null?null:m.ulebEncode(a).length+a},validate:r=>{var a;if((a=e==null?void 0:e.validate)==null||a.call(e,r),!r||typeof r!="object"||!("length"in r))throw new TypeError(`Expected array, found ${typeof r}`)}})},string(e){return d.stringLikeBcsType({toBytes:r=>new TextEncoder().encode(r),fromBytes:r=>new TextDecoder().decode(r),...e,name:(e==null?void 0:e.name)??"string"})},fixedArray:y,option:w,vector:f,tuple(e,r){return new d.BcsTuple({fields:e,...r})},struct(e,r,a){return new d.BcsStruct({name:e,fields:r,...a})},enum(e,r,a){return new d.BcsEnum({name:e,fields:r,...a})},map:l,lazy(e){return d.lazyBcsType(e)}};exports.bcs=c;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./index297.cjs"),i=require("./index117.cjs"),o=/^vector<(.+)>$/,l=/^([^:]+)::([^:]+)::([^<]+)(<(.+)>)?/;class t{static parseFromStr(r,e=!1){if(r==="address")return{address:null};if(r==="bool")return{bool:null};if(r==="u8")return{u8:null};if(r==="u16")return{u16:null};if(r==="u32")return{u32:null};if(r==="u64")return{u64:null};if(r==="u128")return{u128:null};if(r==="u256")return{u256:null};if(r==="signer")return{signer:null};const u=r.match(o);if(u)return{vector:t.parseFromStr(u[1],e)};const n=r.match(l);if(n)return{struct:{address:e?i.normalizeSuiAddress(n[1]):n[1],module:n[2],name:n[3],typeParams:n[5]===void 0?[]:t.parseStructTypeArgs(n[5],e)}};throw new Error(`Encountered unexpected token when parsing type args for ${r}`)}static parseStructTypeArgs(r,e=!1){return s.splitGenericParameters(r).map(u=>t.parseFromStr(u,e))}static tagToString(r){if("bool"in r)return"bool";if("u8"in r)return"u8";if("u16"in r)return"u16";if("u32"in r)return"u32";if("u64"in r)return"u64";if("u128"in r)return"u128";if("u256"in r)return"u256";if("address"in r)return"address";if("signer"in r)return"signer";if("vector"in r)return`vector<${t.tagToString(r.vector)}>`;if("struct"in r){const e=r.struct,u=e.typeParams.map(t.tagToString).join(", ");return`${e.address}::${e.module}::${e.name}${u?`<${u}>`:""}`}throw new Error("Invalid TypeTag")}}exports.TypeTagSerializer=t;