UNPKG

interchainjs

Version:

InterchainJS is a JavaScript library for interacting with Cosmos SDK based blockchains.

115 lines (114 loc) 3.96 kB
"use strict"; /** * This file and any referenced files were automatically generated by @hyperweb/telescope@1.17.4 * DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain * and run the transpile command or npm scripts command that is used to regenerate this bundle. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.setPaginationParams = exports.base64FromBytes = exports.bytesFromBase64 = void 0; exports.omitDefault = omitDefault; exports.toDuration = toDuration; exports.fromDuration = fromDuration; exports.isSet = isSet; exports.isObject = isObject; exports.isRpc = isRpc; exports.toTimestamp = toTimestamp; exports.fromTimestamp = fromTimestamp; exports.fromJsonTimestamp = fromJsonTimestamp; var encoding_1 = require("@interchainjs/encoding"); Object.defineProperty(exports, "bytesFromBase64", { enumerable: true, get: function () { return encoding_1.fromBase64; } }); Object.defineProperty(exports, "base64FromBytes", { enumerable: true, get: function () { return encoding_1.toBase64; } }); function omitDefault(input) { if (typeof input === 'string') { return input === '' ? undefined : input; } if (typeof input === 'number') { return input === 0 ? undefined : input; } if (typeof input === "boolean") { return input === false ? undefined : input; } if (typeof input === 'bigint') { return input === BigInt(0) ? undefined : input; } throw new Error(`Got unsupported type ${typeof input}`); } function toDuration(duration) { return { seconds: BigInt(Math.floor(parseInt(duration) / 1000000000)), nanos: parseInt(duration) % 1000000000 }; } function fromDuration(duration) { return (parseInt(duration.seconds.toString()) * 1000000000 + duration.nanos).toString(); } function isSet(value) { return value !== null && value !== undefined; } function isObject(value) { return typeof value === 'object' && value !== null; } const setPaginationParams = (options, pagination) => { if (!pagination) { return options; } if (typeof pagination?.countTotal !== 'undefined') { options.params['pagination.count_total'] = pagination.countTotal; } if (typeof pagination?.key !== 'undefined') { // String to Uint8Array // let uint8arr = new Uint8Array(Buffer.from(data,'base64')); // Uint8Array to String options.params['pagination.key'] = Buffer.from(pagination.key).toString('base64'); } if (typeof pagination?.limit !== 'undefined') { options.params['pagination.limit'] = pagination.limit.toString(); } if (typeof pagination?.offset !== 'undefined') { options.params['pagination.offset'] = pagination.offset.toString(); } if (typeof pagination?.reverse !== 'undefined') { options.params['pagination.reverse'] = pagination.reverse; } return options; }; exports.setPaginationParams = setPaginationParams; function isRpc(rpc) { return rpc !== null && rpc !== undefined && typeof rpc.request === 'function'; } function toTimestamp(date) { const seconds = numberToLong(date.getTime() / 1_000); const nanos = (date.getTime() % 1000) * 1000000; return { seconds, nanos }; } function fromTimestamp(t) { let millis = Number(t.seconds) * 1000; millis += t.nanos / 1000000; return new Date(millis); } const timestampFromJSON = (object) => { return { seconds: isSet(object.seconds) ? BigInt(object.seconds.toString()) : BigInt(0), nanos: isSet(object.nanos) ? Number(object.nanos) : 0 }; }; function fromJsonTimestamp(o) { if (o instanceof Date) { return toTimestamp(o); } else if (typeof o === 'string') { return toTimestamp(new Date(o)); } else { return timestampFromJSON(o); } } function numberToLong(number) { return BigInt(Math.trunc(number)); }