koilib 9.2.0 → 9.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/Provider.ts CHANGED
@@ -15,6 +15,66 @@ import { koinos } from "./protoModules/protocol-proto.js";
15
15
  import { decodeBase64url, encodeBase64url } from "./utils";
16
16
  import { Serializer } from "./Serializer";
17
17
 
18
+ const getObjectTypes = {
19
+ nested: {
20
+ get_object_arguments: {
21
+ fields: {
22
+ space: {
23
+ type: "object_space",
24
+ id: 1,
25
+ },
26
+ key: {
27
+ type: "bytes",
28
+ id: 2,
29
+ },
30
+ },
31
+ },
32
+ get_object_result: {
33
+ fields: {
34
+ value: {
35
+ type: "database_object",
36
+ id: 1,
37
+ },
38
+ },
39
+ },
40
+ object_space: {
41
+ fields: {
42
+ system: {
43
+ type: "bool",
44
+ id: 1,
45
+ },
46
+ zone: {
47
+ type: "bytes",
48
+ id: 2,
49
+ options: {
50
+ "(koinos.btype)": "ADDRESS",
51
+ },
52
+ },
53
+ id: {
54
+ type: "uint32",
55
+ id: 3,
56
+ },
57
+ },
58
+ },
59
+ database_object: {
60
+ fields: {
61
+ exists: {
62
+ type: "bool",
63
+ id: 1,
64
+ },
65
+ value: {
66
+ type: "bytes",
67
+ id: 2,
68
+ },
69
+ key: {
70
+ type: "bytes",
71
+ id: 3,
72
+ },
73
+ },
74
+ },
75
+ },
76
+ };
77
+
18
78
  export interface ProviderInterface {
19
79
  call: <T = unknown>(method: string, params: unknown) => Promise<T>;
20
80
  getNonce: (
@@ -129,6 +189,56 @@ export interface ProviderInterface {
129
189
  }
130
190
  | undefined
131
191
  >;
192
+ invokeGetObject?: (args: {
193
+ address: string;
194
+ id: number;
195
+ key?: string;
196
+ system?: boolean;
197
+ zone?: string;
198
+ }) => Promise<string | undefined>;
199
+ invokeGetNextObject?: (args: {
200
+ address: string;
201
+ id: number;
202
+ key?: string;
203
+ system?: boolean;
204
+ zone?: string;
205
+ }) => Promise<
206
+ | {
207
+ exists: boolean;
208
+ value: string;
209
+ key: string;
210
+ }
211
+ | undefined
212
+ >;
213
+ invokeGetPrevObject?: (args: {
214
+ address: string;
215
+ id: number;
216
+ key?: string;
217
+ system?: boolean;
218
+ zone?: string;
219
+ }) => Promise<
220
+ | {
221
+ exists: boolean;
222
+ value: string;
223
+ key: string;
224
+ }
225
+ | undefined
226
+ >;
227
+ invokeGetManyObjects?: (args: {
228
+ address: string;
229
+ id: number;
230
+ key?: string;
231
+ limit?: number;
232
+ direction?: "ascending" | "descending";
233
+ system?: boolean;
234
+ zone?: string;
235
+ }) => Promise<
236
+ {
237
+ exists: boolean;
238
+ value: string;
239
+ key: string;
240
+ }[]
241
+ >;
132
242
  }
133
243
 
134
244
  /* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access */
@@ -1109,6 +1219,285 @@ export class Provider implements ProviderInterface {
1109
1219
  value: { address: string };
1110
1220
  }>(serializer, "get_contract_address", { name });
1111
1221
  }
1222
+
1223
+ async invokeGetObjectGeneric({
1224
+ systemCallIdOrName,
1225
+ address,
1226
+ id,
1227
+ key = "",
1228
+ system = false,
1229
+ zone = address,
1230
+ }: {
1231
+ systemCallIdOrName: string | number;
1232
+ address: string;
1233
+ id: number;
1234
+ key?: string;
1235
+ system?: boolean;
1236
+ zone?: string;
1237
+ }) {
1238
+ const serializer = new Serializer(getObjectTypes, {
1239
+ argumentsTypeName: "get_object_arguments",
1240
+ returnTypeName: "get_object_result",
1241
+ });
1242
+ const result = await this.invokeSystemCall<{
1243
+ value: {
1244
+ exists: boolean;
1245
+ value: string;
1246
+ key: string;
1247
+ };
1248
+ }>(
1249
+ serializer,
1250
+ systemCallIdOrName,
1251
+ {
1252
+ space: {
1253
+ system,
1254
+ zone,
1255
+ id,
1256
+ },
1257
+ key,
1258
+ },
1259
+ {
1260
+ caller: address,
1261
+ caller_privilege: system ? 0 : 1,
1262
+ }
1263
+ );
1264
+ return result?.value;
1265
+ }
1266
+
1267
+ /**
1268
+ * Function to get an object from the database via the get_object system call.
1269
+ * @param args - object space and key
1270
+ * @param args.address - caller address (also used as zone when zone is omitted)
1271
+ * @param args.id - object space id
1272
+ * @param args.key - object key (defaults to empty)
1273
+ * @param args.zone - object space zone (defaults to address)
1274
+ *
1275
+ * @example
1276
+ * ```ts
1277
+ * const provider = new Provider("https://api.koinos.io");
1278
+ * const result = await provider.invokeGetObject({
1279
+ * address: "1KD9Es7LBBjA1FY3ViCgQJ7e6WH1ipKbhz",
1280
+ * id: 0,
1281
+ * });
1282
+ * console.log(result);
1283
+ *
1284
+ * // {
1285
+ * // exists: true,
1286
+ * // value: '...',
1287
+ * // key: ''
1288
+ * // }
1289
+ * ```
1290
+ */
1291
+ async invokeGetObject({
1292
+ address,
1293
+ id,
1294
+ key = "",
1295
+ system = false,
1296
+ zone = address,
1297
+ }: {
1298
+ address: string;
1299
+ id: number;
1300
+ key?: string;
1301
+ system?: boolean;
1302
+ zone?: string;
1303
+ }) {
1304
+ const result = await this.invokeGetObjectGeneric({
1305
+ systemCallIdOrName: "get_object",
1306
+ address,
1307
+ id,
1308
+ key,
1309
+ system,
1310
+ zone,
1311
+ });
1312
+ if (!result || !result.exists) return undefined;
1313
+ return result.value;
1314
+ }
1315
+
1316
+ /**
1317
+ * Function to get the next object from the database via the
1318
+ * get_next_object system call.
1319
+ * @param args - object space and key
1320
+ * @param args.address - caller address (also used as zone when zone is omitted)
1321
+ * @param args.id - object space id
1322
+ * @param args.key - object key to seek from (defaults to empty)
1323
+ * @param args.system - whether the space is a system space
1324
+ * @param args.zone - object space zone (defaults to address)
1325
+ *
1326
+ * @example
1327
+ * ```ts
1328
+ * const provider = new Provider("https://api.koinos.io");
1329
+ * const result = await provider.invokeGetNextObject({
1330
+ * address: "1KD9Es7LBBjA1FY3ViCgQJ7e6WH1ipKbhz",
1331
+ * id: 0,
1332
+ * });
1333
+ * console.log(result);
1334
+ *
1335
+ * // {
1336
+ * // exists: true,
1337
+ * // value: '...',
1338
+ * // key: '...'
1339
+ * // }
1340
+ * ```
1341
+ */
1342
+ async invokeGetNextObject({
1343
+ address,
1344
+ id,
1345
+ key = "",
1346
+ system = false,
1347
+ zone = address,
1348
+ }: {
1349
+ address: string;
1350
+ id: number;
1351
+ key?: string;
1352
+ system?: boolean;
1353
+ zone?: string;
1354
+ }) {
1355
+ return this.invokeGetObjectGeneric({
1356
+ systemCallIdOrName: "get_next_object",
1357
+ address,
1358
+ id,
1359
+ key,
1360
+ system,
1361
+ zone,
1362
+ });
1363
+ }
1364
+
1365
+ /**
1366
+ * Function to get the previous object from the database via the
1367
+ * get_prev_object system call.
1368
+ * @param args - object space and key
1369
+ * @param args.address - caller address (also used as zone when zone is omitted)
1370
+ * @param args.id - object space id
1371
+ * @param args.key - object key to seek from (defaults to empty)
1372
+ * @param args.system - whether the space is a system space
1373
+ * @param args.zone - object space zone (defaults to address)
1374
+ *
1375
+ * @example
1376
+ * ```ts
1377
+ * const provider = new Provider("https://api.koinos.io");
1378
+ * const result = await provider.invokeGetPrevObject({
1379
+ * address: "1KD9Es7LBBjA1FY3ViCgQJ7e6WH1ipKbhz",
1380
+ * id: 0,
1381
+ * key: "...",
1382
+ * });
1383
+ * console.log(result);
1384
+ *
1385
+ * // {
1386
+ * // exists: true,
1387
+ * // value: '...',
1388
+ * // key: '...'
1389
+ * // }
1390
+ * ```
1391
+ */
1392
+ async invokeGetPrevObject({
1393
+ address,
1394
+ id,
1395
+ key = "",
1396
+ system = false,
1397
+ zone = address,
1398
+ }: {
1399
+ address: string;
1400
+ id: number;
1401
+ key?: string;
1402
+ system?: boolean;
1403
+ zone?: string;
1404
+ }) {
1405
+ return this.invokeGetObjectGeneric({
1406
+ systemCallIdOrName: "get_prev_object",
1407
+ address,
1408
+ id,
1409
+ key,
1410
+ system,
1411
+ zone,
1412
+ });
1413
+ }
1414
+
1415
+ /**
1416
+ * Function to get many objects from the database by repeatedly
1417
+ * calling get_next_object or get_prev_object.
1418
+ * @param args - object space and pagination options
1419
+ * @param args.address - caller address (also used as zone when zone is omitted)
1420
+ * @param args.id - object space id
1421
+ * @param args.key - offset key to seek from (defaults to empty)
1422
+ * @param args.limit - maximum number of objects to return
1423
+ * @param args.direction - traversal direction ("ascending" or "descending")
1424
+ * @param args.system - whether the space is a system space
1425
+ * @param args.zone - object space zone (defaults to address)
1426
+ *
1427
+ * @example
1428
+ * ```ts
1429
+ * const provider = new Provider("https://api.koinos.io");
1430
+ * const result = await provider.invokeGetManyObjects({
1431
+ * address: "1KD9Es7LBBjA1FY3ViCgQJ7e6WH1ipKbhz",
1432
+ * id: 0,
1433
+ * limit: 10,
1434
+ * });
1435
+ * console.log(result);
1436
+ *
1437
+ * // [
1438
+ * // {
1439
+ * // exists: true,
1440
+ * // value: '...',
1441
+ * // key: '...'
1442
+ * // },
1443
+ * // ...
1444
+ * // ]
1445
+ * ```
1446
+ */
1447
+ async invokeGetManyObjects({
1448
+ address,
1449
+ id,
1450
+ key = "",
1451
+ limit = Number.MAX_SAFE_INTEGER,
1452
+ direction = "ascending",
1453
+ system = false,
1454
+ zone = address,
1455
+ }: {
1456
+ address: string;
1457
+ id: number;
1458
+ key?: string;
1459
+ limit?: number;
1460
+ direction?: "ascending" | "descending";
1461
+ system?: boolean;
1462
+ zone?: string;
1463
+ }) {
1464
+ const result: {
1465
+ exists: boolean;
1466
+ value: string;
1467
+ key: string;
1468
+ }[] = [];
1469
+
1470
+ let currentKey = key;
1471
+ let done = false;
1472
+
1473
+ do {
1474
+ const obj =
1475
+ direction === "ascending"
1476
+ ? await this.invokeGetNextObject({
1477
+ address,
1478
+ id,
1479
+ key: currentKey,
1480
+ system,
1481
+ zone,
1482
+ })
1483
+ : await this.invokeGetPrevObject({
1484
+ address,
1485
+ id,
1486
+ key: currentKey,
1487
+ system,
1488
+ zone,
1489
+ });
1490
+
1491
+ if (obj && obj.exists) {
1492
+ result.push(obj);
1493
+ currentKey = obj.key;
1494
+ }
1495
+
1496
+ done = !obj || !obj.exists || result.length >= limit;
1497
+ } while (!done);
1498
+
1499
+ return result;
1500
+ }
1112
1501
  }
1113
1502
 
1114
1503
  export default Provider;