@mimicprotocol/lib-ts 0.0.1-rc.9

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.
@@ -0,0 +1,316 @@
1
+ // eslint-disable-next-line no-secrets/no-secrets
2
+ // This file is based on code from "The Graph Tooling" (https://github.com/graphprotocol/graph-tooling/tree/7faa3098b2e6c61f09fc81b8b2d333e66b0080d1).
3
+ // Licensed under the MIT License.
4
+ // Copyright (c) 2018 Graph Protocol, Inc. and contributors.
5
+ // Modified by Mimic Protocol, 2025.
6
+
7
+ import { bytesToHexString, bytesToString } from '../helpers'
8
+ import { Serializable } from '../helpers'
9
+
10
+ import { BigInt } from './BigInt'
11
+ import { Bytes } from './Bytes'
12
+
13
+ /**
14
+ * Represents a byte array (Uint8Array) with utility methods
15
+ * for conversions between different numeric and string formats.
16
+ */
17
+ export class ByteArray extends Uint8Array implements Serializable {
18
+ /**
19
+ * Creates an empty ByteArray.
20
+ * The resulting byte array is in little-endian order.
21
+ */
22
+ static empty(): ByteArray {
23
+ return ByteArray.fromI32(0)
24
+ }
25
+
26
+ /**
27
+ * Creates a ByteArray from a signed 8-bit integer (i8).
28
+ * The resulting byte array is in little-endian order.
29
+ */
30
+ static fromI8(x: i8): ByteArray {
31
+ return ByteArray.fromInteger(x, 1)
32
+ }
33
+
34
+ /**
35
+ * Creates a ByteArray from an unsigned 8-bit integer (u8).
36
+ * The resulting byte array is in little-endian order.
37
+ */
38
+ static fromU8(x: u8): ByteArray {
39
+ return ByteArray.fromInteger(x, 1)
40
+ }
41
+
42
+ /**
43
+ * Creates a ByteArray from a signed 16-bit integer (i16).
44
+ * The resulting byte array is in little-endian order.
45
+ */
46
+ static fromI16(x: i16): ByteArray {
47
+ return ByteArray.fromInteger(x, 2)
48
+ }
49
+
50
+ /**
51
+ * Creates a ByteArray from an unsigned 16-bit integer (u16).
52
+ * The resulting byte array is in little-endian order.
53
+ */
54
+ static fromU16(x: u16): ByteArray {
55
+ return ByteArray.fromInteger(x, 2)
56
+ }
57
+
58
+ /**
59
+ * Creates a ByteArray from a signed 32-bit integer (i32).
60
+ * The resulting byte array is in little-endian order.
61
+ */
62
+ static fromI32(x: i32): ByteArray {
63
+ return ByteArray.fromInteger(x, 4)
64
+ }
65
+
66
+ /**
67
+ * Creates a ByteArray from an unsigned 32-bit integer (u32).
68
+ * The resulting byte array is in little-endian order.
69
+ */
70
+ static fromU32(x: u32): ByteArray {
71
+ return ByteArray.fromInteger(x, 4)
72
+ }
73
+
74
+ /**
75
+ * Creates a ByteArray from a signed 64-bit integer (i64).
76
+ * The resulting byte array is in little-endian order.
77
+ */
78
+ static fromI64(x: i64): ByteArray {
79
+ return ByteArray.fromInteger(x, 8)
80
+ }
81
+
82
+ /**
83
+ * Creates a ByteArray from an unsigned 64-bit integer (u64).
84
+ * The resulting byte array is in little-endian order.
85
+ */
86
+ static fromU64(x: u64): ByteArray {
87
+ return ByteArray.fromInteger(x, 8)
88
+ }
89
+
90
+ /**
91
+ * Creates a ByteArray from a boolean value.
92
+ * The resulting byte array is in little-endian order.
93
+ */
94
+ static fromBool(x: bool): ByteArray {
95
+ return ByteArray.fromInteger((x ? 1 : 0) as u8, 1)
96
+ }
97
+
98
+ /**
99
+ * Converts a hexadecimal string to a ByteArray.
100
+ * The input must contain an even number of characters.
101
+ * It may optionally start with '0x'.
102
+ */
103
+ static fromHexString(hex: string): ByteArray {
104
+ assert(hex.length % 2 == 0, 'input ' + hex + ' has odd length')
105
+ if (hex.length >= 2 && hex.charAt(0) == '0' && hex.charAt(1) == 'x') hex = hex.substring(2)
106
+ const output = new Bytes(hex.length / 2)
107
+ for (let i = 0; i < hex.length; i += 2) output[i / 2] = I8.parseInt(hex.substring(i, i + 2), 16)
108
+ return output
109
+ }
110
+
111
+ /**
112
+ * Converts a UTF-8 string to a ByteArray.
113
+ */
114
+ static fromUTF8(str: string): ByteArray {
115
+ const utf8 = String.UTF8.encode(str)
116
+ return changetype<ByteArray>(ByteArray.wrap(utf8))
117
+ }
118
+
119
+ /**
120
+ * Converts a BigInt to a ByteArray.
121
+ */
122
+ static fromBigInt(bigInt: BigInt): ByteArray {
123
+ return changetype<ByteArray>(bigInt)
124
+ }
125
+
126
+ /**
127
+ * Creates a ByteArray from an unsigned arbitrary-length integer.
128
+ * The resulting byte array is in little-endian order.
129
+ */
130
+ private static fromInteger<T extends number>(value: T, length: u8): ByteArray {
131
+ const self = new ByteArray(length)
132
+ for (let i: u8 = 0; i < length; i++) self[i] = (value >> (i * 8)) as u8
133
+ return self
134
+ }
135
+
136
+ /**
137
+ * Concatenates this ByteArray with another ByteArray.
138
+ */
139
+ concat(other: ByteArray): ByteArray {
140
+ const newArray = new ByteArray(this.length + other.length)
141
+ newArray.set(this, 0)
142
+ newArray.set(other, this.length)
143
+ return newArray
144
+ }
145
+
146
+ /**
147
+ * Concatenates this ByteArray with a signed 32-bit integer.
148
+ */
149
+ concatI32(other: i32): ByteArray {
150
+ return this.concat(ByteArray.fromI32(other))
151
+ }
152
+
153
+ /**
154
+ * Compares this ByteArray to another for equality.
155
+ */
156
+ @operator('==')
157
+ equals(other: ByteArray): boolean {
158
+ if (this.length != other.length) return false
159
+ for (let i = 0; i < this.length; i++) if (this[i] != other[i]) return false
160
+ return true
161
+ }
162
+
163
+ /**
164
+ * Compares this ByteArray to another for inequality.
165
+ */
166
+ @operator('!=')
167
+ notEqual(other: ByteArray): boolean {
168
+ return !(this == other)
169
+ }
170
+
171
+ /**
172
+ * Interprets the byte array as a little-endian U32.
173
+ * Throws in case of overflow.
174
+ */
175
+ toU32(): u32 {
176
+ for (let i = 4; i < this.length; i++) {
177
+ if (this[i] != 0) {
178
+ assert(false, 'overflow converting ' + this.toHexString() + ' to u32')
179
+ }
180
+ }
181
+ const paddedBytes = new Bytes(4)
182
+ paddedBytes[0] = 0
183
+ paddedBytes[1] = 0
184
+ paddedBytes[2] = 0
185
+ paddedBytes[3] = 0
186
+ const minLen = paddedBytes.length < this.length ? paddedBytes.length : this.length
187
+ for (let i = 0; i < minLen; i++) {
188
+ paddedBytes[i] = this[i]
189
+ }
190
+ let x: u32 = 0
191
+ x = (x | paddedBytes[3]) << 8
192
+ x = (x | paddedBytes[2]) << 8
193
+ x = (x | paddedBytes[1]) << 8
194
+ x = x | paddedBytes[0]
195
+ return x
196
+ }
197
+
198
+ /**
199
+ * Interprets the byte array as a little-endian I32.
200
+ * Throws in case of overflow.
201
+ */
202
+ toI32(): i32 {
203
+ const isNeg = this.length > 0 && this[this.length - 1] >> 7 == 1
204
+ const padding = isNeg ? 255 : 0
205
+ for (let i = 4; i < this.length; i++) {
206
+ if (this[i] != padding) {
207
+ assert(false, 'overflow converting ' + this.toHexString() + ' to i32')
208
+ }
209
+ }
210
+
211
+ const paddedBytes = new Bytes(4)
212
+ paddedBytes[0] = padding
213
+ paddedBytes[1] = padding
214
+ paddedBytes[2] = padding
215
+ paddedBytes[3] = padding
216
+ const minLen = paddedBytes.length < this.length ? paddedBytes.length : this.length
217
+ for (let i = 0; i < minLen; i++) paddedBytes[i] = this[i]
218
+
219
+ let x: i32 = 0
220
+ x = (x | paddedBytes[3]) << 8
221
+ x = (x | paddedBytes[2]) << 8
222
+ x = (x | paddedBytes[1]) << 8
223
+ x = x | paddedBytes[0]
224
+ return x
225
+ }
226
+
227
+ /**
228
+ * Interprets the byte array as a little-endian I64.
229
+ * Throws in case of overflow.
230
+ */
231
+ toI64(): i64 {
232
+ const isNeg = this.length > 0 && this[this.length - 1] >> 7 == 1
233
+ const padding = isNeg ? 255 : 0
234
+ for (let i = 8; i < this.length; i++) {
235
+ if (this[i] != padding) {
236
+ assert(false, 'overflow converting ' + this.toHexString() + ' to i64')
237
+ }
238
+ }
239
+
240
+ const paddedBytes = new Bytes(8)
241
+ paddedBytes[0] = padding
242
+ paddedBytes[1] = padding
243
+ paddedBytes[2] = padding
244
+ paddedBytes[3] = padding
245
+ paddedBytes[4] = padding
246
+ paddedBytes[5] = padding
247
+ paddedBytes[6] = padding
248
+ paddedBytes[7] = padding
249
+ const minLen = paddedBytes.length < this.length ? paddedBytes.length : this.length
250
+ for (let i = 0; i < minLen; i++) paddedBytes[i] = this[i]
251
+
252
+ let x: i64 = 0
253
+ x = (x | paddedBytes[7]) << 8
254
+ x = (x | paddedBytes[6]) << 8
255
+ x = (x | paddedBytes[5]) << 8
256
+ x = (x | paddedBytes[4]) << 8
257
+ x = (x | paddedBytes[3]) << 8
258
+ x = (x | paddedBytes[2]) << 8
259
+ x = (x | paddedBytes[1]) << 8
260
+ x = x | paddedBytes[0]
261
+ return x
262
+ }
263
+
264
+ /**
265
+ * Interprets the byte array as a little-endian U64.
266
+ * Throws in case of overflow.
267
+ */
268
+ toU64(): u64 {
269
+ for (let i = 8; i < this.length; i++) {
270
+ if (this[i] != 0) {
271
+ assert(false, 'overflow converting ' + this.toHexString() + ' to u64')
272
+ }
273
+ }
274
+
275
+ const paddedBytes = new Bytes(8)
276
+ paddedBytes[0] = 0
277
+ paddedBytes[1] = 0
278
+ paddedBytes[2] = 0
279
+ paddedBytes[3] = 0
280
+ paddedBytes[4] = 0
281
+ paddedBytes[5] = 0
282
+ paddedBytes[6] = 0
283
+ paddedBytes[7] = 0
284
+ const minLen = paddedBytes.length < this.length ? paddedBytes.length : this.length
285
+ for (let i = 0; i < minLen; i++) paddedBytes[i] = this[i]
286
+
287
+ let x: u64 = 0
288
+ x = (x | paddedBytes[7]) << 8
289
+ x = (x | paddedBytes[6]) << 8
290
+ x = (x | paddedBytes[5]) << 8
291
+ x = (x | paddedBytes[4]) << 8
292
+ x = (x | paddedBytes[3]) << 8
293
+ x = (x | paddedBytes[2]) << 8
294
+ x = (x | paddedBytes[1]) << 8
295
+ x = x | paddedBytes[0]
296
+ return x
297
+ }
298
+
299
+ /**
300
+ * Converts this ByteArray to a hexadecimal string.
301
+ */
302
+ toHexString(): string {
303
+ return bytesToHexString(this)
304
+ }
305
+
306
+ /**
307
+ * Converts this ByteArray to a string representation.
308
+ */
309
+ toString(): string {
310
+ return bytesToString(this)
311
+ }
312
+
313
+ serialize(): string {
314
+ return this.toHexString()
315
+ }
316
+ }
@@ -0,0 +1,137 @@
1
+ // eslint-disable-next-line no-secrets/no-secrets
2
+ // This file is based on code from "The Graph Tooling" (https://github.com/graphprotocol/graph-tooling/tree/7faa3098b2e6c61f09fc81b8b2d333e66b0080d1).
3
+ // Licensed under the MIT License.
4
+ // Copyright (c) 2018 Graph Protocol, Inc. and contributors.
5
+ // Modified by Mimic Protocol, 2025.
6
+
7
+ import { ByteArray } from './ByteArray'
8
+
9
+ /**
10
+ * A dynamically-sized byte array with utility methods
11
+ * for conversions between different formats.
12
+ */
13
+ export class Bytes extends ByteArray {
14
+ /**
15
+ * Creates a Bytes instance from a ByteArray.
16
+ */
17
+ static fromByteArray(byteArray: ByteArray): Bytes {
18
+ return changetype<Bytes>(byteArray)
19
+ }
20
+
21
+ /**
22
+ * Creates a Bytes instance from a Uint8Array.
23
+ */
24
+ static fromUint8Array(uint8Array: Uint8Array): Bytes {
25
+ return changetype<Bytes>(uint8Array)
26
+ }
27
+
28
+ /**
29
+ * Converts a hexadecimal string to a Bytes instance.
30
+ * The input must contain an even number of characters.
31
+ * It may optionally start with '0x'.
32
+ */
33
+ static fromHexString(hex: string): Bytes {
34
+ return changetype<Bytes>(ByteArray.fromHexString(hex))
35
+ }
36
+
37
+ /**
38
+ * Converts a UTF-8 string to a Bytes instance.
39
+ */
40
+ static fromUTF8(str: string): Bytes {
41
+ return Bytes.fromByteArray(ByteArray.fromUTF8(str))
42
+ }
43
+
44
+ /**
45
+ * Returns an empty Bytes instance initialized to zero.
46
+ */
47
+ static empty(): Bytes {
48
+ return changetype<Bytes>(ByteArray.empty())
49
+ }
50
+
51
+ /**
52
+ * Creates a Bytes instance from a signed 8-bit integer (i8).
53
+ * The resulting byte array is in little-endian order.
54
+ */
55
+ static fromI8(x: i8): Bytes {
56
+ return changetype<Bytes>(ByteArray.fromI8(x))
57
+ }
58
+
59
+ /**
60
+ * Creates a Bytes instance from an unsigned 8-bit integer (u8).
61
+ * The resulting byte array is in little-endian order.
62
+ */
63
+ static fromU8(x: u8): Bytes {
64
+ return changetype<Bytes>(ByteArray.fromU8(x))
65
+ }
66
+
67
+ /**
68
+ * Creates a Bytes instance from a signed 16-bit integer (i16).
69
+ * The resulting byte array is in little-endian order.
70
+ */
71
+ static fromI16(x: i16): Bytes {
72
+ return changetype<Bytes>(ByteArray.fromI16(x))
73
+ }
74
+
75
+ /**
76
+ * Creates a Bytes instance from an unsigned 16-bit integer (u16).
77
+ * The resulting byte array is in little-endian order.
78
+ */
79
+ static fromU16(x: u16): Bytes {
80
+ return changetype<Bytes>(ByteArray.fromU16(x))
81
+ }
82
+
83
+ /**
84
+ * Creates a Bytes instance from a signed 32-bit integer (i32).
85
+ * The resulting byte array is in little-endian order.
86
+ */
87
+ static fromI32(i: i32): Bytes {
88
+ return changetype<Bytes>(ByteArray.fromI32(i))
89
+ }
90
+
91
+ /**
92
+ * Creates a Bytes instance from an unsigned 32-bit integer (u32).
93
+ * The resulting byte array is in little-endian order.
94
+ */
95
+ static fromU32(x: u32): Bytes {
96
+ return changetype<Bytes>(ByteArray.fromU32(x))
97
+ }
98
+
99
+ /**
100
+ * Creates a Bytes instance from a signed 64-bit integer (i64).
101
+ * The resulting byte array is in little-endian order.
102
+ */
103
+ static fromI64(x: i64): Bytes {
104
+ return changetype<Bytes>(ByteArray.fromI64(x))
105
+ }
106
+
107
+ /**
108
+ * Creates a Bytes instance from an unsigned 64-bit integer (u64).
109
+ * The resulting byte array is in little-endian order.
110
+ */
111
+ static fromU64(x: u64): Bytes {
112
+ return changetype<Bytes>(ByteArray.fromU64(x))
113
+ }
114
+
115
+ /**
116
+ * Creates a Bytes instance from a boolean value.
117
+ * The resulting byte array is in little-endian order.
118
+ */
119
+ static fromBool(x: bool): Bytes {
120
+ return changetype<Bytes>(ByteArray.fromBool(x))
121
+ }
122
+
123
+ /**
124
+ * Concatenates this Bytes instance with another ByteArray.
125
+ * The argument must be of type Bytes.
126
+ */
127
+ concat(other: ByteArray): Bytes {
128
+ return changetype<Bytes>(super.concat(other))
129
+ }
130
+
131
+ /**
132
+ * Concatenates this Bytes instance with a signed 32-bit integer (i32).
133
+ */
134
+ concatI32(other: i32): Bytes {
135
+ return changetype<Bytes>(super.concat(ByteArray.fromI32(other)))
136
+ }
137
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Represents supported blockchain network identifiers.
3
+ * Each value corresponds to the chain ID used by the respective network.
4
+ */
5
+ export enum ChainId {
6
+ ETHEREUM = 1,
7
+ POLYGON = 137,
8
+ OPTIMISM = 10,
9
+ }
@@ -0,0 +1,30 @@
1
+ import { join, Serializable, serialize } from '../helpers'
2
+
3
+ /**
4
+ * Represents a parameter for EVM decoding, used to process data returned from smart contract calls.
5
+ * This class specifies the expected ABI type and the encoded data to be decoded.
6
+ */
7
+ export class EvmDecodeParam implements Serializable {
8
+ private static readonly SERIALIZED_PREFIX: string = 'EvmDecodeParam'
9
+
10
+ private _abi_type: string
11
+ private _value: string
12
+
13
+ /**
14
+ * Creates a new EvmDecodeParam instance.
15
+ * @param abi_type - The ABI type signature for decoding (e.g., "uint256", "address", "string", "(uint256,string)")
16
+ * @param value - The encoded hex string data to be decoded
17
+ */
18
+ constructor(abi_type: string, value: string) {
19
+ this._abi_type = abi_type
20
+ this._value = value
21
+ }
22
+
23
+ toString(): string {
24
+ return `${EvmDecodeParam.SERIALIZED_PREFIX}(${join([serialize(this._abi_type), serialize(this._value)])})`
25
+ }
26
+
27
+ serialize(): string {
28
+ return this.toString()
29
+ }
30
+ }
@@ -0,0 +1,54 @@
1
+ import { join, Serializable, serialize, serializeArray, Stringable } from '../helpers'
2
+
3
+ /**
4
+ * Represents a parameter for EVM encoding, used to prepare data for smart contract calls.
5
+ * This class encapsulates both primitive values and complex structures (arrays, tuples)
6
+ * in a format suitable for ABI encoding.
7
+ */
8
+ export class EvmEncodeParam implements Serializable {
9
+ private static readonly SERIALIZED_PREFIX: string = 'EvmEncodeParam'
10
+
11
+ private _type: string
12
+ private _value: string
13
+ private _values: EvmEncodeParam[]
14
+
15
+ /**
16
+ * Creates a new EvmEncodeParam instance.
17
+ * @param type - The ABI type signature (e.g., "uint256", "address", "string", "()")
18
+ * @param value - The serialized value for primitive types
19
+ * @param values - Array of nested EvmEncodeParam instances for complex types
20
+ */
21
+ constructor(type: string, value: string, values: EvmEncodeParam[]) {
22
+ this._type = type
23
+ this._value = value
24
+ this._values = values
25
+ }
26
+
27
+ /**
28
+ * Creates an EvmEncodeParam for a primitive value.
29
+ * @param type - The ABI type signature (e.g., "uint256", "address", "string")
30
+ * @param value - The value to encode, must implement Stringable interface
31
+ * @returns A new EvmEncodeParam instance representing the primitive value
32
+ */
33
+ static fromValue<T extends Stringable>(type: string, value: T): EvmEncodeParam {
34
+ return new EvmEncodeParam(type, serialize(value), [])
35
+ }
36
+
37
+ /**
38
+ * Creates an EvmEncodeParam for complex types like arrays or tuples.
39
+ * @param type - The ABI type signature (e.g., "address[]", "()", "()[]")
40
+ * @param values - Array of EvmEncodeParam instances representing the nested elements
41
+ * @returns A new EvmEncodeParam instance representing the complex type
42
+ */
43
+ static fromValues(type: string, values: EvmEncodeParam[]): EvmEncodeParam {
44
+ return new EvmEncodeParam(type, '', values)
45
+ }
46
+
47
+ toString(): string {
48
+ return `${EvmEncodeParam.SERIALIZED_PREFIX}(${join([serialize(this._type), serialize(this._value), serializeArray(this._values)])})`
49
+ }
50
+
51
+ serialize(): string {
52
+ return this.toString()
53
+ }
54
+ }
@@ -0,0 +1,7 @@
1
+ export * from './Address'
2
+ export * from './BigInt'
3
+ export * from './ByteArray'
4
+ export * from './Bytes'
5
+ export * from './ChainId'
6
+ export * from './EvmDecodeParam'
7
+ export * from './EvmEncodeParam'