datai-sdk 1.1.2 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/@graphprotocol/graph-ts/README.md +13 -0
- package/@graphprotocol/graph-ts/chain/arweave.ts +82 -0
- package/@graphprotocol/graph-ts/chain/cosmos.ts +426 -0
- package/@graphprotocol/graph-ts/chain/ethereum.ts +727 -0
- package/@graphprotocol/graph-ts/chain/near.ts +420 -0
- package/@graphprotocol/graph-ts/chain/starknet.ts +39 -0
- package/@graphprotocol/graph-ts/common/collections.ts +495 -0
- package/@graphprotocol/graph-ts/common/conversion.ts +3 -0
- package/@graphprotocol/graph-ts/common/datasource.ts +41 -0
- package/@graphprotocol/graph-ts/common/eager_offset.ts +42 -0
- package/@graphprotocol/graph-ts/common/json.ts +28 -0
- package/@graphprotocol/graph-ts/common/numbers.ts +407 -0
- package/@graphprotocol/graph-ts/common/value.ts +585 -0
- package/@graphprotocol/graph-ts/global/global.ts +4 -0
- package/@graphprotocol/graph-ts/helper-functions.ts +79 -0
- package/@graphprotocol/graph-ts/index.ts +156 -0
- package/@graphprotocol/graph-ts/package.json +3 -0
- package/@graphprotocol/graph-ts/tsconfig.json +4 -0
- package/@graphprotocol/graph-ts/types/tsconfig.base.json +3 -0
- package/package.json +1 -1
- package/patch-graph-ts.js +4 -2
- package/postinstall-script.js +27 -70
|
@@ -0,0 +1,585 @@
|
|
|
1
|
+
import { Bytes, TypedMap } from './collections';
|
|
2
|
+
import { json } from './json';
|
|
3
|
+
import { Address, BigDecimal, BigInt } from './numbers';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Enum for supported value types.
|
|
7
|
+
*/
|
|
8
|
+
export enum ValueKind {
|
|
9
|
+
STRING = 0,
|
|
10
|
+
INT = 1,
|
|
11
|
+
BIGDECIMAL = 2,
|
|
12
|
+
BOOL = 3,
|
|
13
|
+
ARRAY = 4,
|
|
14
|
+
NULL = 5,
|
|
15
|
+
BYTES = 6,
|
|
16
|
+
BIGINT = 7,
|
|
17
|
+
INT8 = 8,
|
|
18
|
+
TIMESTAMP = 9,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const VALUE_KIND_NAMES = [
|
|
22
|
+
'String',
|
|
23
|
+
'Int',
|
|
24
|
+
'BigDecimal',
|
|
25
|
+
'bool',
|
|
26
|
+
'Array',
|
|
27
|
+
'null',
|
|
28
|
+
'Bytes',
|
|
29
|
+
'BigInt',
|
|
30
|
+
'Int8',
|
|
31
|
+
'Timestamp',
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Pointer type for Value data.
|
|
36
|
+
*
|
|
37
|
+
* Big enough to fit any pointer or native `this.data`.
|
|
38
|
+
*/
|
|
39
|
+
export type ValuePayload = u64;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* A dynamically typed value.
|
|
43
|
+
*/
|
|
44
|
+
export class Value {
|
|
45
|
+
constructor(
|
|
46
|
+
public kind: ValueKind,
|
|
47
|
+
public data: ValuePayload,
|
|
48
|
+
) {}
|
|
49
|
+
|
|
50
|
+
toAddress(): Address {
|
|
51
|
+
assert(this.kind == ValueKind.BYTES, 'Value is not an address.');
|
|
52
|
+
return changetype<Address>(this.data as u32);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
toBoolean(): boolean {
|
|
56
|
+
if (this.kind == ValueKind.NULL) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
assert(this.kind == ValueKind.BOOL, 'Value is not a boolean.');
|
|
60
|
+
return this.data != 0;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
toBytes(): Bytes {
|
|
64
|
+
assert(this.kind == ValueKind.BYTES, 'Value is not a byte array.');
|
|
65
|
+
return changetype<Bytes>(this.data as u32);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
toI32(): i32 {
|
|
69
|
+
if (this.kind == ValueKind.NULL) {
|
|
70
|
+
return 0;
|
|
71
|
+
}
|
|
72
|
+
assert(this.kind == ValueKind.INT, 'Value is not an i32.');
|
|
73
|
+
return this.data as i32;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
toI64(): i64 {
|
|
77
|
+
if (this.kind == ValueKind.NULL) {
|
|
78
|
+
return 0;
|
|
79
|
+
}
|
|
80
|
+
assert(this.kind == ValueKind.INT8, 'Value is not an i64.');
|
|
81
|
+
return this.data as i64;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
toTimestamp(): i64 {
|
|
85
|
+
if (this.kind == ValueKind.NULL) {
|
|
86
|
+
return 0;
|
|
87
|
+
}
|
|
88
|
+
assert(this.kind == ValueKind.TIMESTAMP, 'Value is not an i64.');
|
|
89
|
+
return this.data as i64;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
toString(): string {
|
|
93
|
+
assert(this.kind == ValueKind.STRING, 'Value is not a string.');
|
|
94
|
+
return changetype<string>(this.data as u32);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
toBigInt(): BigInt {
|
|
98
|
+
assert(this.kind == ValueKind.BIGINT, 'Value is not a BigInt.');
|
|
99
|
+
return changetype<BigInt>(this.data as u32);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
toBigDecimal(): BigDecimal {
|
|
103
|
+
assert(this.kind == ValueKind.BIGDECIMAL, 'Value is not a BigDecimal.');
|
|
104
|
+
return changetype<BigDecimal>(this.data as u32);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
toArray(): Array<Value> {
|
|
108
|
+
assert(this.kind == ValueKind.ARRAY, 'Value is not an array.');
|
|
109
|
+
return changetype<Array<Value>>(this.data as u32);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
toMatrix(): Array<Array<Value>> {
|
|
113
|
+
const valueArray = this.toArray();
|
|
114
|
+
const out = new Array<Array<Value>>(valueArray.length);
|
|
115
|
+
for (let i: i32 = 0; i < valueArray.length; i++) {
|
|
116
|
+
out[i] = valueArray[i].toArray();
|
|
117
|
+
}
|
|
118
|
+
return out;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
toBooleanArray(): Array<boolean> {
|
|
122
|
+
const values = this.toArray();
|
|
123
|
+
const output = new Array<boolean>(values.length);
|
|
124
|
+
for (let i: i32 = 0; i < values.length; i++) {
|
|
125
|
+
output[i] = values[i].toBoolean();
|
|
126
|
+
}
|
|
127
|
+
return output;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
toBytesArray(): Array<Bytes> {
|
|
131
|
+
const values = this.toArray();
|
|
132
|
+
const output = new Array<Bytes>(values.length);
|
|
133
|
+
for (let i: i32 = 0; i < values.length; i++) {
|
|
134
|
+
output[i] = values[i].toBytes();
|
|
135
|
+
}
|
|
136
|
+
return output;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
toStringArray(): Array<string> {
|
|
140
|
+
const values = this.toArray();
|
|
141
|
+
const output = new Array<string>(values.length);
|
|
142
|
+
for (let i: i32 = 0; i < values.length; i++) {
|
|
143
|
+
output[i] = values[i].toString();
|
|
144
|
+
}
|
|
145
|
+
return output;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
toI32Array(): Array<i32> {
|
|
149
|
+
const values = this.toArray();
|
|
150
|
+
const output = new Array<i32>(values.length);
|
|
151
|
+
for (let i: i32 = 0; i < values.length; i++) {
|
|
152
|
+
output[i] = values[i].toI32();
|
|
153
|
+
}
|
|
154
|
+
return output;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
toI64Array(): Array<i64> {
|
|
158
|
+
const values = this.toArray();
|
|
159
|
+
const output = new Array<i64>(values.length);
|
|
160
|
+
for (let i: i32 = 0; i < values.length; i++) {
|
|
161
|
+
output[i] = values[i].toI32();
|
|
162
|
+
}
|
|
163
|
+
return output;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
toTimestampArray(): Array<i64> {
|
|
167
|
+
const values = this.toArray();
|
|
168
|
+
const output = new Array<i64>(values.length);
|
|
169
|
+
for (let i: i32 = 0; i < values.length; i++) {
|
|
170
|
+
output[i] = values[i].toTimestamp();
|
|
171
|
+
}
|
|
172
|
+
return output;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
toBigIntArray(): Array<BigInt> {
|
|
176
|
+
const values = this.toArray();
|
|
177
|
+
const output = new Array<BigInt>(values.length);
|
|
178
|
+
for (let i: i32 = 0; i < values.length; i++) {
|
|
179
|
+
output[i] = values[i].toBigInt();
|
|
180
|
+
}
|
|
181
|
+
return output;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
toBigDecimalArray(): Array<BigDecimal> {
|
|
185
|
+
const values = this.toArray();
|
|
186
|
+
const output = new Array<BigDecimal>(values.length);
|
|
187
|
+
for (let i: i32 = 0; i < values.length; i++) {
|
|
188
|
+
output[i] = values[i].toBigDecimal();
|
|
189
|
+
}
|
|
190
|
+
return output;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
toBooleanMatrix(): Array<Array<boolean>> {
|
|
194
|
+
const valueMatrix = this.toMatrix();
|
|
195
|
+
const out = new Array<Array<boolean>>(valueMatrix.length);
|
|
196
|
+
for (let i: i32 = 0; i < valueMatrix.length; i++) {
|
|
197
|
+
out[i] = new Array<boolean>(valueMatrix[i].length);
|
|
198
|
+
for (let j: i32 = 0; j < valueMatrix[i].length; j++) {
|
|
199
|
+
out[i][j] = valueMatrix[i][j].toBoolean();
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return out;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
toBytesMatrix(): Array<Array<Bytes>> {
|
|
206
|
+
const valueMatrix = this.toMatrix();
|
|
207
|
+
const out = new Array<Array<Bytes>>(valueMatrix.length);
|
|
208
|
+
for (let i: i32 = 0; i < valueMatrix.length; i++) {
|
|
209
|
+
out[i] = new Array<Bytes>(valueMatrix[i].length);
|
|
210
|
+
for (let j: i32 = 0; j < valueMatrix[i].length; j++) {
|
|
211
|
+
out[i][j] = valueMatrix[i][j].toBytes();
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return out;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
toAddressMatrix(): Array<Array<Address>> {
|
|
218
|
+
const valueMatrix = this.toMatrix();
|
|
219
|
+
const out = new Array<Array<Address>>(valueMatrix.length);
|
|
220
|
+
for (let i: i32 = 0; i < valueMatrix.length; i++) {
|
|
221
|
+
out[i] = new Array<Address>(valueMatrix[i].length);
|
|
222
|
+
for (let j: i32 = 0; j < valueMatrix[i].length; j++) {
|
|
223
|
+
out[i][j] = valueMatrix[i][j].toAddress();
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return out;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
toStringMatrix(): Array<Array<string>> {
|
|
230
|
+
const valueMatrix = this.toMatrix();
|
|
231
|
+
const out = new Array<Array<string>>(valueMatrix.length);
|
|
232
|
+
for (let i: i32 = 0; i < valueMatrix.length; i++) {
|
|
233
|
+
out[i] = new Array<string>(valueMatrix[i].length);
|
|
234
|
+
for (let j: i32 = 0; j < valueMatrix[i].length; j++) {
|
|
235
|
+
out[i][j] = valueMatrix[i][j].toString();
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return out;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
toI32Matrix(): Array<Array<i32>> {
|
|
242
|
+
const valueMatrix = this.toMatrix();
|
|
243
|
+
const out = new Array<Array<i32>>(valueMatrix.length);
|
|
244
|
+
for (let i: i32 = 0; i < valueMatrix.length; i++) {
|
|
245
|
+
out[i] = new Array<i32>(valueMatrix[i].length);
|
|
246
|
+
for (let j: i32 = 0; j < valueMatrix[i].length; j++) {
|
|
247
|
+
out[i][j] = valueMatrix[i][j].toI32();
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return out;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
toI64Matrix(): Array<Array<i64>> {
|
|
254
|
+
const valueMatrix = this.toMatrix();
|
|
255
|
+
const out = new Array<Array<i64>>(valueMatrix.length);
|
|
256
|
+
|
|
257
|
+
for (let i: i32 = 0; i < valueMatrix.length; i++) {
|
|
258
|
+
out[i] = new Array<i64>(valueMatrix[i].length);
|
|
259
|
+
for (let j: i32 = 0; j < valueMatrix[i].length; j++) {
|
|
260
|
+
out[i][j] = valueMatrix[i][j].toI64();
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return out;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
toBigIntMatrix(): Array<Array<BigInt>> {
|
|
267
|
+
const valueMatrix = this.toMatrix();
|
|
268
|
+
const out = new Array<Array<BigInt>>(valueMatrix.length);
|
|
269
|
+
for (let i: i32 = 0; i < valueMatrix.length; i++) {
|
|
270
|
+
out[i] = new Array<BigInt>(valueMatrix[i].length);
|
|
271
|
+
for (let j: i32 = 0; j < valueMatrix[i].length; j++) {
|
|
272
|
+
out[i][j] = valueMatrix[i][j].toBigInt();
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return out;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** Return a string that indicates the kind of value `this` contains for
|
|
279
|
+
* logging and error messages */
|
|
280
|
+
displayKind(): string {
|
|
281
|
+
if (this.kind >= VALUE_KIND_NAMES.length) {
|
|
282
|
+
return `Unknown (${this.kind})`;
|
|
283
|
+
}
|
|
284
|
+
return VALUE_KIND_NAMES[this.kind];
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** Return a string representation of the value of `this` for logging and
|
|
288
|
+
* error messages */
|
|
289
|
+
displayData(): string {
|
|
290
|
+
switch (this.kind) {
|
|
291
|
+
case ValueKind.STRING:
|
|
292
|
+
return this.toString();
|
|
293
|
+
case ValueKind.INT:
|
|
294
|
+
return this.toI32().toString();
|
|
295
|
+
case ValueKind.INT8:
|
|
296
|
+
case ValueKind.TIMESTAMP:
|
|
297
|
+
return this.toI64().toString();
|
|
298
|
+
case ValueKind.BIGDECIMAL:
|
|
299
|
+
return this.toBigDecimal().toString();
|
|
300
|
+
case ValueKind.BOOL:
|
|
301
|
+
return this.toBoolean().toString();
|
|
302
|
+
case ValueKind.ARRAY:
|
|
303
|
+
// TODO: we need to clean it up. Not sure how `this` works in AssemblyScript so leaving as it is for now
|
|
304
|
+
// eslint-disable-next-line no-case-declarations
|
|
305
|
+
const arr = this.toArray();
|
|
306
|
+
return '[' + arr.map<string>(elt => elt.displayData()).join(', ') + ']';
|
|
307
|
+
case ValueKind.NULL:
|
|
308
|
+
return 'null';
|
|
309
|
+
case ValueKind.BYTES:
|
|
310
|
+
return this.toBytes().toHexString();
|
|
311
|
+
case ValueKind.BIGINT:
|
|
312
|
+
return this.toBigInt().toString();
|
|
313
|
+
default:
|
|
314
|
+
return `Unknown data (kind = ${this.kind})`;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
static fromBooleanArray(input: Array<boolean>): Value {
|
|
319
|
+
const output = new Array<Value>(input.length);
|
|
320
|
+
for (let i: i32 = 0; i < input.length; i++) {
|
|
321
|
+
output[i] = Value.fromBoolean(input[i]);
|
|
322
|
+
}
|
|
323
|
+
return Value.fromArray(output);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
static fromBytesArray(input: Array<Bytes>): Value {
|
|
327
|
+
const output = new Array<Value>(input.length);
|
|
328
|
+
for (let i: i32 = 0; i < input.length; i++) {
|
|
329
|
+
output[i] = Value.fromBytes(input[i]);
|
|
330
|
+
}
|
|
331
|
+
return Value.fromArray(output);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
static fromI32Array(input: Array<i32>): Value {
|
|
335
|
+
const output = new Array<Value>(input.length);
|
|
336
|
+
for (let i: i32 = 0; i < input.length; i++) {
|
|
337
|
+
output[i] = Value.fromI32(input[i]);
|
|
338
|
+
}
|
|
339
|
+
return Value.fromArray(output);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
static fromBigIntArray(input: Array<BigInt>): Value {
|
|
343
|
+
const output = new Array<Value>(input.length);
|
|
344
|
+
for (let i: i32 = 0; i < input.length; i++) {
|
|
345
|
+
output[i] = Value.fromBigInt(input[i]);
|
|
346
|
+
}
|
|
347
|
+
return Value.fromArray(output);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
static fromBigDecimalArray(input: Array<BigDecimal>): Value {
|
|
351
|
+
const output = new Array<Value>(input.length);
|
|
352
|
+
for (let i: i32 = 0; i < input.length; i++) {
|
|
353
|
+
output[i] = Value.fromBigDecimal(input[i]);
|
|
354
|
+
}
|
|
355
|
+
return Value.fromArray(output);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
static fromStringArray(input: Array<string>): Value {
|
|
359
|
+
const output = new Array<Value>(input.length);
|
|
360
|
+
for (let i: i32 = 0; i < input.length; i++) {
|
|
361
|
+
output[i] = Value.fromString(input[i]);
|
|
362
|
+
}
|
|
363
|
+
return Value.fromArray(output);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
static fromAddressArray(input: Array<Address>): Value {
|
|
367
|
+
const output = new Array<Value>(input.length);
|
|
368
|
+
for (let i: i32 = 0; i < input.length; i++) {
|
|
369
|
+
output[i] = Value.fromAddress(input[i]);
|
|
370
|
+
}
|
|
371
|
+
return Value.fromArray(output);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
static fromArray(input: Array<Value>): Value {
|
|
375
|
+
return new Value(ValueKind.ARRAY, changetype<u32>(input));
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
static fromBigInt(n: BigInt): Value {
|
|
379
|
+
return new Value(ValueKind.BIGINT, changetype<u32>(n));
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
static fromBoolean(b: bool): Value {
|
|
383
|
+
return new Value(ValueKind.BOOL, b ? 1 : 0);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
static fromBytes(bytes: Bytes): Value {
|
|
387
|
+
return new Value(ValueKind.BYTES, changetype<u32>(bytes));
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
static fromNull(): Value {
|
|
391
|
+
return new Value(ValueKind.NULL, 0);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
static fromI32(n: i32): Value {
|
|
395
|
+
return new Value(ValueKind.INT, n as u64);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
static fromI64(n: i64): Value {
|
|
399
|
+
return new Value(ValueKind.INT8, n as i64);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
static fromTimestamp(n: i64): Value {
|
|
403
|
+
return new Value(ValueKind.TIMESTAMP, n as i64);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
static fromString(s: string): Value {
|
|
407
|
+
return new Value(ValueKind.STRING, changetype<u32>(s));
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
static fromAddress(s: Address): Value {
|
|
411
|
+
return new Value(ValueKind.BYTES, changetype<u32>(s));
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
static fromBigDecimal(n: BigDecimal): Value {
|
|
415
|
+
return new Value(ValueKind.BIGDECIMAL, changetype<u32>(n));
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
static fromMatrix(values: Array<Array<Value>>): Value {
|
|
419
|
+
const innerOut = new Array<Value>(values.length);
|
|
420
|
+
for (let i: i32 = 0; i < innerOut.length; i++) {
|
|
421
|
+
innerOut[i] = Value.fromArray(values[i]);
|
|
422
|
+
}
|
|
423
|
+
return Value.fromArray(innerOut);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
static fromBooleanMatrix(values: Array<Array<boolean>>): Value {
|
|
427
|
+
const out = new Array<Array<Value>>(values.length);
|
|
428
|
+
for (let i: i32 = 0; i < values.length; i++) {
|
|
429
|
+
out[i] = new Array<Value>(values[i].length);
|
|
430
|
+
for (let j: i32 = 0; j < values[i].length; j++) {
|
|
431
|
+
out[i][j] = Value.fromBoolean(values[i][j]);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
return Value.fromMatrix(out);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
static fromBytesMatrix(values: Array<Array<Bytes>>): Value {
|
|
438
|
+
const out = new Array<Array<Value>>(values.length);
|
|
439
|
+
for (let i: i32 = 0; i < values.length; i++) {
|
|
440
|
+
out[i] = new Array<Value>(values[i].length);
|
|
441
|
+
for (let j: i32 = 0; j < values[i].length; j++) {
|
|
442
|
+
out[i][j] = Value.fromBytes(values[i][j]);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
return Value.fromMatrix(out);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
static fromAddressMatrix(values: Array<Array<Address>>): Value {
|
|
449
|
+
const out = new Array<Array<Value>>(values.length);
|
|
450
|
+
for (let i: i32 = 0; i < values.length; i++) {
|
|
451
|
+
out[i] = new Array<Value>(values[i].length);
|
|
452
|
+
for (let j: i32 = 0; j < values[i].length; j++) {
|
|
453
|
+
out[i][j] = Value.fromAddress(values[i][j]);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return Value.fromMatrix(out);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
static fromStringMatrix(values: Array<Array<string>>): Value {
|
|
460
|
+
const out = new Array<Array<Value>>(values.length);
|
|
461
|
+
for (let i: i32 = 0; i < values.length; i++) {
|
|
462
|
+
out[i] = new Array<Value>(values[i].length);
|
|
463
|
+
for (let j: i32 = 0; j < values[i].length; j++) {
|
|
464
|
+
out[i][j] = Value.fromString(values[i][j]);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
return Value.fromMatrix(out);
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
static fromI32Matrix(values: Array<Array<i32>>): Value {
|
|
471
|
+
const out = new Array<Array<Value>>(values.length);
|
|
472
|
+
for (let i: i32 = 0; i < values.length; i++) {
|
|
473
|
+
out[i] = new Array<Value>(values[i].length);
|
|
474
|
+
for (let j: i32 = 0; j < values[i].length; j++) {
|
|
475
|
+
out[i][j] = Value.fromI32(values[i][j]);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
return Value.fromMatrix(out);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
static fromI64Matrix(values: Array<Array<i64>>): Value {
|
|
482
|
+
const out = new Array<Array<Value>>(values.length);
|
|
483
|
+
for (let i: i32 = 0; i < values.length; i++) {
|
|
484
|
+
out[i] = new Array<Value>(values[i].length);
|
|
485
|
+
for (let j: i32 = 0; j < values[i].length; j++) {
|
|
486
|
+
out[i][j] = Value.fromI64(values[i][j]);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
return Value.fromMatrix(out);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
static fromTimestampMatrix(values: Array<Array<i64>>): Value {
|
|
493
|
+
const out = new Array<Array<Value>>(values.length);
|
|
494
|
+
for (let i: i32 = 0; i < values.length; i++) {
|
|
495
|
+
out[i] = new Array<Value>(values[i].length);
|
|
496
|
+
for (let j: i32 = 0; j < values[i].length; j++) {
|
|
497
|
+
out[i][j] = Value.fromTimestamp(values[i][j]);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
return Value.fromMatrix(out);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
static fromBigIntMatrix(values: Array<Array<BigInt>>): Value {
|
|
504
|
+
const out = new Array<Array<Value>>(values.length);
|
|
505
|
+
for (let i: i32 = 0; i < values.length; i++) {
|
|
506
|
+
out[i] = new Array<Value>(values[i].length);
|
|
507
|
+
for (let j: i32 = 0; j < values[i].length; j++) {
|
|
508
|
+
out[i][j] = Value.fromBigInt(values[i][j]);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
return Value.fromMatrix(out);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/** Type hint for JSON values. */
|
|
516
|
+
export enum JSONValueKind {
|
|
517
|
+
NULL = 0,
|
|
518
|
+
BOOL = 1,
|
|
519
|
+
NUMBER = 2,
|
|
520
|
+
STRING = 3,
|
|
521
|
+
ARRAY = 4,
|
|
522
|
+
OBJECT = 5,
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* Pointer type for JSONValue data.
|
|
527
|
+
*
|
|
528
|
+
* Big enough to fit any pointer or native `this.data`.
|
|
529
|
+
*/
|
|
530
|
+
export type JSONValuePayload = u64;
|
|
531
|
+
|
|
532
|
+
export class JSONValue {
|
|
533
|
+
constructor(
|
|
534
|
+
public kind: JSONValueKind,
|
|
535
|
+
public data: JSONValuePayload,
|
|
536
|
+
) {}
|
|
537
|
+
|
|
538
|
+
isNull(): boolean {
|
|
539
|
+
return this.kind == JSONValueKind.NULL;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
toBool(): boolean {
|
|
543
|
+
assert(this.kind == JSONValueKind.BOOL, 'JSON value is not a boolean.');
|
|
544
|
+
return this.data != 0;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
toI64(): i64 {
|
|
548
|
+
assert(this.kind == JSONValueKind.NUMBER, 'JSON value is not a number.');
|
|
549
|
+
const decimalString = changetype<string>(this.data as u32);
|
|
550
|
+
return json.toI64(decimalString);
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
toU64(): u64 {
|
|
554
|
+
assert(this.kind == JSONValueKind.NUMBER, 'JSON value is not a number.');
|
|
555
|
+
const decimalString = changetype<string>(this.data as u32);
|
|
556
|
+
return json.toU64(decimalString);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
toF64(): f64 {
|
|
560
|
+
assert(this.kind == JSONValueKind.NUMBER, 'JSON value is not a number.');
|
|
561
|
+
const decimalString = changetype<string>(this.data as u32);
|
|
562
|
+
return json.toF64(decimalString);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
toBigInt(): BigInt {
|
|
566
|
+
assert(this.kind == JSONValueKind.NUMBER, 'JSON value is not a number.');
|
|
567
|
+
const decimalString = changetype<string>(this.data as u32);
|
|
568
|
+
return json.toBigInt(decimalString);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
toString(): string {
|
|
572
|
+
assert(this.kind == JSONValueKind.STRING, 'JSON value is not a string.');
|
|
573
|
+
return changetype<string>(this.data as u32);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
toArray(): Array<JSONValue> {
|
|
577
|
+
assert(this.kind == JSONValueKind.ARRAY, 'JSON value is not an array.');
|
|
578
|
+
return changetype<Array<JSONValue>>(this.data as u32);
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
toObject(): TypedMap<string, JSONValue> {
|
|
582
|
+
assert(this.kind == JSONValueKind.OBJECT, 'JSON value is not an object.');
|
|
583
|
+
return changetype<TypedMap<string, JSONValue>>(this.data as u32);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { ByteArray } from './index';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Takes 2 ByteArrays and concatenates them
|
|
5
|
+
* @param a - 1st ByteArray
|
|
6
|
+
* @param b - 2nd ByteArray
|
|
7
|
+
* @returns A concatenated ByteArray
|
|
8
|
+
*/
|
|
9
|
+
export function concat(a: ByteArray, b: ByteArray): ByteArray {
|
|
10
|
+
const out = new Uint8Array(a.length + b.length);
|
|
11
|
+
for (let i = 0; i < a.length; i++) {
|
|
12
|
+
out[i] = a[i];
|
|
13
|
+
}
|
|
14
|
+
for (let j = 0; j < b.length; j++) {
|
|
15
|
+
out[a.length + j] = b[j];
|
|
16
|
+
}
|
|
17
|
+
return changetype<ByteArray>(out);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Used for parseCSV() below
|
|
22
|
+
*/
|
|
23
|
+
enum CSVState {
|
|
24
|
+
BETWEEN = 0,
|
|
25
|
+
UNQUOTED_VALUE = 1,
|
|
26
|
+
QUOTED_VALUE = 2,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Parses a CSV string into an array of strings.
|
|
31
|
+
* @param csv CSV string.
|
|
32
|
+
* @returns Array of strings.
|
|
33
|
+
*/
|
|
34
|
+
export function parseCSV(csv: string): Array<string> {
|
|
35
|
+
const values = new Array<string>();
|
|
36
|
+
let valueStart = 0;
|
|
37
|
+
let state = CSVState.BETWEEN;
|
|
38
|
+
|
|
39
|
+
for (let i: i32 = 0; i < csv.length; i++) {
|
|
40
|
+
if (state == CSVState.BETWEEN) {
|
|
41
|
+
if (csv.charAt(i) != ',') {
|
|
42
|
+
if (csv.charAt(i) == '"') {
|
|
43
|
+
state = CSVState.QUOTED_VALUE;
|
|
44
|
+
valueStart = i + 1;
|
|
45
|
+
} else {
|
|
46
|
+
state = CSVState.UNQUOTED_VALUE;
|
|
47
|
+
valueStart = i;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
} else if (state == CSVState.UNQUOTED_VALUE) {
|
|
51
|
+
if (csv.charAt(i) == ',') {
|
|
52
|
+
values.push(csv.substr(valueStart, i - valueStart));
|
|
53
|
+
state = CSVState.BETWEEN;
|
|
54
|
+
}
|
|
55
|
+
} else if (state == CSVState.QUOTED_VALUE && csv.charAt(i) == '"') {
|
|
56
|
+
values.push(csv.substr(valueStart, i - valueStart));
|
|
57
|
+
state = CSVState.BETWEEN;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return values;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Adds 0x1220 to the front of a ByteArray. This can be used when an IPFS hash is stored in an Ethereum Bytes32 type.
|
|
66
|
+
* The IPFS hash will fit in a Bytes32 when 0x1220 is removed. Since 0x1220 is currently in front of every single IPFS
|
|
67
|
+
* hash, this works. But it is possible in the future that IPFS will update their spec.
|
|
68
|
+
* @param a - The ByteArray without 0x1220 prefixed
|
|
69
|
+
* @returns - The ByteArray with 0x1220 prefixed
|
|
70
|
+
*/
|
|
71
|
+
export function addQm(a: ByteArray): ByteArray {
|
|
72
|
+
const out = new Uint8Array(34);
|
|
73
|
+
out[0] = 0x12;
|
|
74
|
+
out[1] = 0x20;
|
|
75
|
+
for (let i = 0; i < 32; i++) {
|
|
76
|
+
out[i + 2] = a[i];
|
|
77
|
+
}
|
|
78
|
+
return changetype<ByteArray>(out);
|
|
79
|
+
}
|