UNPKG

deth

Version:

Ethereum node focused on Developer Experience

53 lines (52 loc) 2.09 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const t = __importStar(require("io-ts")); const Either_1 = require("fp-ts/lib/Either"); const primitives_1 = require("../../primitives"); const mapCodec = (type, mapInput, mapOutput) => new t.Type(type.name, type.is, (i, c) => mapInput(type.validate(i, c)), a => mapOutput(type.encode(a))); // @TODO: without any I can't get compiler to accept this type... exports.quantity = codecFromMake(primitives_1.makeQuantity); exports.hexData = codecFromMake(primitives_1.makeHexData); exports.hash = codecFromMake(primitives_1.makeHash); exports.address = codecFromMake(primitives_1.makeAddress); const toNull = (x) => (x !== null && x !== void 0 ? x : null); const toUndefined = (x) => (x !== null && x !== void 0 ? x : undefined); // automatically deals with null/undefined conversions across the RPC <-> our code boundary // decodes: null and undefined to undefined // encodes undefined to nulls exports.undefinable = (type) => mapCodec(t.union([type, t.null, t.undefined]), Either_1.map(toUndefined), toNull); function codecFromMake(make) { return new t.Type('RPC_QUANTITY', checkFromMake(make), validateFromMake(make), make); } function checkFromMake(make) { return (value) => { if (typeof value !== 'string') { return false; } try { return make(value) === value; } catch (_a) { return false; } }; } function validateFromMake(make) { return (value, c) => { if (typeof value !== 'string') { return t.failure('Value is not a string', c); } try { return t.success(make(value)); } catch (e) { return t.failure(`Can't parse "${value}"`, c); } }; }