koilib 9.4.0 → 10.0.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/dist/koinos.js +314 -23
- package/dist/koinos.min.js +1 -1
- package/lib/Serializer.js +30 -4
- package/lib/Serializer.js.map +1 -1
- package/lib/browser/Serializer.js +30 -4
- package/lib/browser/Serializer.js.map +1 -1
- package/lib/browser/utils.d.ts +15 -1
- package/lib/browser/utils.js +32 -18
- package/lib/browser/utils.js.map +1 -1
- package/lib/utils.d.ts +15 -1
- package/lib/utils.js +32 -18
- package/lib/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/Serializer.ts +28 -4
- package/src/utils.ts +36 -17
package/dist/koinos.js
CHANGED
|
@@ -23021,6 +23021,8 @@ class Contract {
|
|
|
23021
23021
|
...this.options,
|
|
23022
23022
|
...options,
|
|
23023
23023
|
};
|
|
23024
|
+
if (!opts.provider)
|
|
23025
|
+
opts.provider = this.provider;
|
|
23024
23026
|
const { read_only: readOnly, return: output, default_output: defaultOutput, preformat_argument: preformatArgument, preformat_return: preformatReturn, } = this.abi.methods[name];
|
|
23025
23027
|
let args;
|
|
23026
23028
|
if (typeof preformatArgument === "function") {
|
|
@@ -23457,6 +23459,65 @@ exports.Provider = void 0;
|
|
|
23457
23459
|
const protocol_proto_js_1 = __webpack_require__(9104);
|
|
23458
23460
|
const utils_1 = __webpack_require__(8593);
|
|
23459
23461
|
const Serializer_1 = __webpack_require__(7187);
|
|
23462
|
+
const getObjectTypes = {
|
|
23463
|
+
nested: {
|
|
23464
|
+
get_object_arguments: {
|
|
23465
|
+
fields: {
|
|
23466
|
+
space: {
|
|
23467
|
+
type: "object_space",
|
|
23468
|
+
id: 1,
|
|
23469
|
+
},
|
|
23470
|
+
key: {
|
|
23471
|
+
type: "bytes",
|
|
23472
|
+
id: 2,
|
|
23473
|
+
},
|
|
23474
|
+
},
|
|
23475
|
+
},
|
|
23476
|
+
get_object_result: {
|
|
23477
|
+
fields: {
|
|
23478
|
+
value: {
|
|
23479
|
+
type: "database_object",
|
|
23480
|
+
id: 1,
|
|
23481
|
+
},
|
|
23482
|
+
},
|
|
23483
|
+
},
|
|
23484
|
+
object_space: {
|
|
23485
|
+
fields: {
|
|
23486
|
+
system: {
|
|
23487
|
+
type: "bool",
|
|
23488
|
+
id: 1,
|
|
23489
|
+
},
|
|
23490
|
+
zone: {
|
|
23491
|
+
type: "bytes",
|
|
23492
|
+
id: 2,
|
|
23493
|
+
options: {
|
|
23494
|
+
"(koinos.btype)": "ADDRESS",
|
|
23495
|
+
},
|
|
23496
|
+
},
|
|
23497
|
+
id: {
|
|
23498
|
+
type: "uint32",
|
|
23499
|
+
id: 3,
|
|
23500
|
+
},
|
|
23501
|
+
},
|
|
23502
|
+
},
|
|
23503
|
+
database_object: {
|
|
23504
|
+
fields: {
|
|
23505
|
+
exists: {
|
|
23506
|
+
type: "bool",
|
|
23507
|
+
id: 1,
|
|
23508
|
+
},
|
|
23509
|
+
value: {
|
|
23510
|
+
type: "bytes",
|
|
23511
|
+
id: 2,
|
|
23512
|
+
},
|
|
23513
|
+
key: {
|
|
23514
|
+
type: "bytes",
|
|
23515
|
+
id: 3,
|
|
23516
|
+
},
|
|
23517
|
+
},
|
|
23518
|
+
},
|
|
23519
|
+
},
|
|
23520
|
+
};
|
|
23460
23521
|
/* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access */
|
|
23461
23522
|
async function sleep(ms) {
|
|
23462
23523
|
return new Promise((r) => setTimeout(r, ms));
|
|
@@ -24210,6 +24271,194 @@ class Provider {
|
|
|
24210
24271
|
});
|
|
24211
24272
|
return this.invokeSystemCall(serializer, "get_contract_address", { name });
|
|
24212
24273
|
}
|
|
24274
|
+
async invokeGetObjectGeneric({ systemCallIdOrName, address, id, key = "", system = false, zone = address, }) {
|
|
24275
|
+
const serializer = new Serializer_1.Serializer(getObjectTypes, {
|
|
24276
|
+
argumentsTypeName: "get_object_arguments",
|
|
24277
|
+
returnTypeName: "get_object_result",
|
|
24278
|
+
});
|
|
24279
|
+
const result = await this.invokeSystemCall(serializer, systemCallIdOrName, {
|
|
24280
|
+
space: {
|
|
24281
|
+
system,
|
|
24282
|
+
zone,
|
|
24283
|
+
id,
|
|
24284
|
+
},
|
|
24285
|
+
key,
|
|
24286
|
+
}, {
|
|
24287
|
+
caller: address,
|
|
24288
|
+
caller_privilege: system ? 0 : 1,
|
|
24289
|
+
});
|
|
24290
|
+
return result === null || result === void 0 ? void 0 : result.value;
|
|
24291
|
+
}
|
|
24292
|
+
/**
|
|
24293
|
+
* Function to get an object from the database via the get_object system call.
|
|
24294
|
+
* @param args - object space and key
|
|
24295
|
+
* @param args.address - caller address (also used as zone when zone is omitted)
|
|
24296
|
+
* @param args.id - object space id
|
|
24297
|
+
* @param args.key - object key (defaults to empty)
|
|
24298
|
+
* @param args.zone - object space zone (defaults to address)
|
|
24299
|
+
*
|
|
24300
|
+
* @example
|
|
24301
|
+
* ```ts
|
|
24302
|
+
* const provider = new Provider("https://api.koinos.io");
|
|
24303
|
+
* const result = await provider.invokeGetObject({
|
|
24304
|
+
* address: "1KD9Es7LBBjA1FY3ViCgQJ7e6WH1ipKbhz",
|
|
24305
|
+
* id: 0,
|
|
24306
|
+
* });
|
|
24307
|
+
* console.log(result);
|
|
24308
|
+
*
|
|
24309
|
+
* // {
|
|
24310
|
+
* // exists: true,
|
|
24311
|
+
* // value: '...',
|
|
24312
|
+
* // key: ''
|
|
24313
|
+
* // }
|
|
24314
|
+
* ```
|
|
24315
|
+
*/
|
|
24316
|
+
async invokeGetObject({ address, id, key = "", system = false, zone = address, }) {
|
|
24317
|
+
const result = await this.invokeGetObjectGeneric({
|
|
24318
|
+
systemCallIdOrName: "get_object",
|
|
24319
|
+
address,
|
|
24320
|
+
id,
|
|
24321
|
+
key,
|
|
24322
|
+
system,
|
|
24323
|
+
zone,
|
|
24324
|
+
});
|
|
24325
|
+
if (!result || !result.exists)
|
|
24326
|
+
return undefined;
|
|
24327
|
+
return result.value;
|
|
24328
|
+
}
|
|
24329
|
+
/**
|
|
24330
|
+
* Function to get the next object from the database via the
|
|
24331
|
+
* get_next_object system call.
|
|
24332
|
+
* @param args - object space and key
|
|
24333
|
+
* @param args.address - caller address (also used as zone when zone is omitted)
|
|
24334
|
+
* @param args.id - object space id
|
|
24335
|
+
* @param args.key - object key to seek from (defaults to empty)
|
|
24336
|
+
* @param args.system - whether the space is a system space
|
|
24337
|
+
* @param args.zone - object space zone (defaults to address)
|
|
24338
|
+
*
|
|
24339
|
+
* @example
|
|
24340
|
+
* ```ts
|
|
24341
|
+
* const provider = new Provider("https://api.koinos.io");
|
|
24342
|
+
* const result = await provider.invokeGetNextObject({
|
|
24343
|
+
* address: "1KD9Es7LBBjA1FY3ViCgQJ7e6WH1ipKbhz",
|
|
24344
|
+
* id: 0,
|
|
24345
|
+
* });
|
|
24346
|
+
* console.log(result);
|
|
24347
|
+
*
|
|
24348
|
+
* // {
|
|
24349
|
+
* // exists: true,
|
|
24350
|
+
* // value: '...',
|
|
24351
|
+
* // key: '...'
|
|
24352
|
+
* // }
|
|
24353
|
+
* ```
|
|
24354
|
+
*/
|
|
24355
|
+
async invokeGetNextObject({ address, id, key = "", system = false, zone = address, }) {
|
|
24356
|
+
return this.invokeGetObjectGeneric({
|
|
24357
|
+
systemCallIdOrName: "get_next_object",
|
|
24358
|
+
address,
|
|
24359
|
+
id,
|
|
24360
|
+
key,
|
|
24361
|
+
system,
|
|
24362
|
+
zone,
|
|
24363
|
+
});
|
|
24364
|
+
}
|
|
24365
|
+
/**
|
|
24366
|
+
* Function to get the previous object from the database via the
|
|
24367
|
+
* get_prev_object system call.
|
|
24368
|
+
* @param args - object space and key
|
|
24369
|
+
* @param args.address - caller address (also used as zone when zone is omitted)
|
|
24370
|
+
* @param args.id - object space id
|
|
24371
|
+
* @param args.key - object key to seek from (defaults to empty)
|
|
24372
|
+
* @param args.system - whether the space is a system space
|
|
24373
|
+
* @param args.zone - object space zone (defaults to address)
|
|
24374
|
+
*
|
|
24375
|
+
* @example
|
|
24376
|
+
* ```ts
|
|
24377
|
+
* const provider = new Provider("https://api.koinos.io");
|
|
24378
|
+
* const result = await provider.invokeGetPrevObject({
|
|
24379
|
+
* address: "1KD9Es7LBBjA1FY3ViCgQJ7e6WH1ipKbhz",
|
|
24380
|
+
* id: 0,
|
|
24381
|
+
* key: "...",
|
|
24382
|
+
* });
|
|
24383
|
+
* console.log(result);
|
|
24384
|
+
*
|
|
24385
|
+
* // {
|
|
24386
|
+
* // exists: true,
|
|
24387
|
+
* // value: '...',
|
|
24388
|
+
* // key: '...'
|
|
24389
|
+
* // }
|
|
24390
|
+
* ```
|
|
24391
|
+
*/
|
|
24392
|
+
async invokeGetPrevObject({ address, id, key = "", system = false, zone = address, }) {
|
|
24393
|
+
return this.invokeGetObjectGeneric({
|
|
24394
|
+
systemCallIdOrName: "get_prev_object",
|
|
24395
|
+
address,
|
|
24396
|
+
id,
|
|
24397
|
+
key,
|
|
24398
|
+
system,
|
|
24399
|
+
zone,
|
|
24400
|
+
});
|
|
24401
|
+
}
|
|
24402
|
+
/**
|
|
24403
|
+
* Function to get many objects from the database by repeatedly
|
|
24404
|
+
* calling get_next_object or get_prev_object.
|
|
24405
|
+
* @param args - object space and pagination options
|
|
24406
|
+
* @param args.address - caller address (also used as zone when zone is omitted)
|
|
24407
|
+
* @param args.id - object space id
|
|
24408
|
+
* @param args.key - offset key to seek from (defaults to empty)
|
|
24409
|
+
* @param args.limit - maximum number of objects to return
|
|
24410
|
+
* @param args.direction - traversal direction ("ascending" or "descending")
|
|
24411
|
+
* @param args.system - whether the space is a system space
|
|
24412
|
+
* @param args.zone - object space zone (defaults to address)
|
|
24413
|
+
*
|
|
24414
|
+
* @example
|
|
24415
|
+
* ```ts
|
|
24416
|
+
* const provider = new Provider("https://api.koinos.io");
|
|
24417
|
+
* const result = await provider.invokeGetManyObjects({
|
|
24418
|
+
* address: "1KD9Es7LBBjA1FY3ViCgQJ7e6WH1ipKbhz",
|
|
24419
|
+
* id: 0,
|
|
24420
|
+
* limit: 10,
|
|
24421
|
+
* });
|
|
24422
|
+
* console.log(result);
|
|
24423
|
+
*
|
|
24424
|
+
* // [
|
|
24425
|
+
* // {
|
|
24426
|
+
* // exists: true,
|
|
24427
|
+
* // value: '...',
|
|
24428
|
+
* // key: '...'
|
|
24429
|
+
* // },
|
|
24430
|
+
* // ...
|
|
24431
|
+
* // ]
|
|
24432
|
+
* ```
|
|
24433
|
+
*/
|
|
24434
|
+
async invokeGetManyObjects({ address, id, key = "", limit = Number.MAX_SAFE_INTEGER, direction = "ascending", system = false, zone = address, }) {
|
|
24435
|
+
const result = [];
|
|
24436
|
+
let currentKey = key;
|
|
24437
|
+
let done = false;
|
|
24438
|
+
do {
|
|
24439
|
+
const obj = direction === "ascending"
|
|
24440
|
+
? await this.invokeGetNextObject({
|
|
24441
|
+
address,
|
|
24442
|
+
id,
|
|
24443
|
+
key: currentKey,
|
|
24444
|
+
system,
|
|
24445
|
+
zone,
|
|
24446
|
+
})
|
|
24447
|
+
: await this.invokeGetPrevObject({
|
|
24448
|
+
address,
|
|
24449
|
+
id,
|
|
24450
|
+
key: currentKey,
|
|
24451
|
+
system,
|
|
24452
|
+
zone,
|
|
24453
|
+
});
|
|
24454
|
+
if (obj && obj.exists) {
|
|
24455
|
+
result.push(obj);
|
|
24456
|
+
currentKey = obj.key;
|
|
24457
|
+
}
|
|
24458
|
+
done = !obj || !obj.exists || result.length >= limit;
|
|
24459
|
+
} while (!done);
|
|
24460
|
+
return result;
|
|
24461
|
+
}
|
|
24213
24462
|
}
|
|
24214
24463
|
exports.Provider = Provider;
|
|
24215
24464
|
exports["default"] = Provider;
|
|
@@ -24252,6 +24501,29 @@ const protobufjs_1 = __webpack_require__(3281);
|
|
|
24252
24501
|
const koinosPbToProto = __importStar(__webpack_require__(9385));
|
|
24253
24502
|
const utils_1 = __webpack_require__(8593);
|
|
24254
24503
|
const googleProtobufDescriptor_1 = __webpack_require__(2722);
|
|
24504
|
+
/**
|
|
24505
|
+
* Google namespace of the types with koilib's complete copy of
|
|
24506
|
+
* descriptor.proto. Contracts include descriptor.proto only because
|
|
24507
|
+
* koinos/options.proto extends it, and the embedded copy can be
|
|
24508
|
+
* incomplete (missing FieldDescriptorProto.Label, for instance), so
|
|
24509
|
+
* its types are replaced while any other google types are kept.
|
|
24510
|
+
*/
|
|
24511
|
+
function withGoogleDescriptor(google) {
|
|
24512
|
+
var _a;
|
|
24513
|
+
const descriptor = googleProtobufDescriptor_1.googleProtobufDescriptor.google.nested.protobuf;
|
|
24514
|
+
const embedded = (_a = google === null || google === void 0 ? void 0 : google.nested) === null || _a === void 0 ? void 0 : _a.protobuf;
|
|
24515
|
+
return {
|
|
24516
|
+
...google,
|
|
24517
|
+
nested: {
|
|
24518
|
+
...google === null || google === void 0 ? void 0 : google.nested,
|
|
24519
|
+
protobuf: {
|
|
24520
|
+
...embedded,
|
|
24521
|
+
...descriptor,
|
|
24522
|
+
nested: { ...embedded === null || embedded === void 0 ? void 0 : embedded.nested, ...descriptor.nested },
|
|
24523
|
+
},
|
|
24524
|
+
},
|
|
24525
|
+
};
|
|
24526
|
+
}
|
|
24255
24527
|
const OP_BYTES_1 = "(btype)";
|
|
24256
24528
|
const OP_BYTES_2 = "(koinos.btype)";
|
|
24257
24529
|
const nativeTypes = [
|
|
@@ -24329,6 +24601,7 @@ const nativeTypes = [
|
|
|
24329
24601
|
*/
|
|
24330
24602
|
class Serializer {
|
|
24331
24603
|
constructor(types, opts) {
|
|
24604
|
+
var _a;
|
|
24332
24605
|
/**
|
|
24333
24606
|
* Preformat bytes for base64url, base58 or hex string
|
|
24334
24607
|
*/
|
|
@@ -24344,17 +24617,21 @@ class Serializer {
|
|
|
24344
24617
|
this.types = types;
|
|
24345
24618
|
if (typeof types === "string") {
|
|
24346
24619
|
const protos = koinosPbToProto.convert((0, utils_1.decodeBase64)(types));
|
|
24347
|
-
this.root =
|
|
24620
|
+
this.root = protobufjs_1.Root.fromJSON({ nested: googleProtobufDescriptor_1.googleProtobufDescriptor });
|
|
24348
24621
|
for (const proto of protos) {
|
|
24622
|
+
// descriptor.proto is proto2 and loses its labels when converted
|
|
24623
|
+
// to text, so use the copy loaded above instead
|
|
24624
|
+
if (proto.file === "google/protobuf/descriptor.proto")
|
|
24625
|
+
continue;
|
|
24349
24626
|
(0, protobufjs_1.parse)(proto.definition, this.root, { keepCase: true });
|
|
24350
24627
|
}
|
|
24351
24628
|
}
|
|
24352
24629
|
else {
|
|
24353
24630
|
this.root = protobufjs_1.Root.fromJSON({
|
|
24354
|
-
|
|
24355
|
-
|
|
24356
|
-
|
|
24357
|
-
|
|
24631
|
+
nested: {
|
|
24632
|
+
...types.nested,
|
|
24633
|
+
google: withGoogleDescriptor((_a = types.nested) === null || _a === void 0 ? void 0 : _a.google),
|
|
24634
|
+
},
|
|
24358
24635
|
});
|
|
24359
24636
|
}
|
|
24360
24637
|
if (opts === null || opts === void 0 ? void 0 : opts.defaultTypeName)
|
|
@@ -27684,33 +27961,47 @@ function formatUnits(value, decimals) {
|
|
|
27684
27961
|
}
|
|
27685
27962
|
exports.formatUnits = formatUnits;
|
|
27686
27963
|
/**
|
|
27687
|
-
* Function to format a decimal point number in an integer
|
|
27964
|
+
* Function to format a decimal point number in an integer, without
|
|
27965
|
+
* rounding.
|
|
27966
|
+
*
|
|
27967
|
+
* The value must be a string made of ASCII digits, an optional leading
|
|
27968
|
+
* "-" and at most one "." or "," decimal separator, with at least one
|
|
27969
|
+
* digit. Surrounding whitespace is ignored. Fractional digits beyond
|
|
27970
|
+
* `decimals` are accepted only when they are zeros (the value is still
|
|
27971
|
+
* exact); any other excess digit throws, so that the amount shown to
|
|
27972
|
+
* the user is always the amount that gets encoded.
|
|
27973
|
+
* @throws Error if the value is not a string in that format, cannot be
|
|
27974
|
+
* represented exactly with `decimals` decimals, or `decimals` is not a
|
|
27975
|
+
* non-negative integer.
|
|
27688
27976
|
* @example
|
|
27689
27977
|
* ```js
|
|
27690
27978
|
* const amount = parseUnits("0.00123456", 8);
|
|
27691
27979
|
* console.log(amount);
|
|
27692
27980
|
* // '123456'
|
|
27981
|
+
*
|
|
27982
|
+
* parseUnits("1.000000000", 8); // '100000000' (exact)
|
|
27983
|
+
* parseUnits("1.999999995", 8); // throws: not representable
|
|
27693
27984
|
* ```
|
|
27694
27985
|
*/
|
|
27695
27986
|
function parseUnits(value, decimals) {
|
|
27696
|
-
|
|
27697
|
-
|
|
27698
|
-
|
|
27699
|
-
|
|
27700
|
-
|
|
27701
|
-
|
|
27702
|
-
|
|
27703
|
-
|
|
27704
|
-
|
|
27705
|
-
|
|
27706
|
-
|
|
27707
|
-
|
|
27708
|
-
decimalPart
|
|
27709
|
-
|
|
27710
|
-
decimalPart = (BigInt(decimalPart) + BigInt(1)).toString();
|
|
27711
|
-
}
|
|
27987
|
+
if (!Number.isInteger(decimals) || decimals < 0) {
|
|
27988
|
+
throw new Error(`invalid decimals: ${String(decimals)}`);
|
|
27989
|
+
}
|
|
27990
|
+
if (typeof value !== "string") {
|
|
27991
|
+
throw new Error(`invalid number: expected a string, got ${typeof value}`);
|
|
27992
|
+
}
|
|
27993
|
+
const match = /^(-?)(\d*)(?:[.,](\d*))?$/.exec(value.trim());
|
|
27994
|
+
if (!match || (match[2] === "" && !match[3])) {
|
|
27995
|
+
throw new Error(`invalid number: ${value}`);
|
|
27996
|
+
}
|
|
27997
|
+
const [, sign, integerPart, decimalPart = ""] = match;
|
|
27998
|
+
if (decimalPart.length > decimals &&
|
|
27999
|
+
/[1-9]/.test(decimalPart.slice(decimals))) {
|
|
28000
|
+
throw new Error(`invalid number: ${value} cannot be represented with ${decimals} decimals`);
|
|
27712
28001
|
}
|
|
27713
|
-
|
|
28002
|
+
const fraction = decimalPart.slice(0, decimals).padEnd(decimals, "0");
|
|
28003
|
+
const units = `${integerPart || "0"}${fraction}`.replace(/^0+(?=\d)/, "");
|
|
28004
|
+
return `${sign}${units}`;
|
|
27714
28005
|
}
|
|
27715
28006
|
exports.parseUnits = parseUnits;
|
|
27716
28007
|
/**
|