UNPKG

envio

Version:

A latency and sync speed optimized, developer friendly blockchain data indexer.

199 lines (178 loc) • 7.21 kB
// Generated by ReScript, PLEASE EDIT WITH CARE 'use strict'; var Viem = require("viem"); var Utils = require("../Utils.res.js"); var $$BigInt = require("../bindings/BigInt.res.js"); var Address = require("../Address.res.js"); var Belt_Array = require("rescript/lib/js/belt_Array.js"); var Caml_option = require("rescript/lib/js/caml_option.js"); var S$RescriptSchema = require("rescript-schema/src/S.res.js"); function makeRpcRoute(method, paramsSchema, resultSchema) { var idSchema = S$RescriptSchema.literal(1); var versionSchema = S$RescriptSchema.literal("2.0"); return function () { return { method: "POST", path: "", input: (function (s) { s.field("method", S$RescriptSchema.literal(method)); s.field("id", idSchema); s.field("jsonrpc", versionSchema); return s.field("params", paramsSchema); }), responses: [(function (s) { s.field("jsonrpc", versionSchema); s.field("id", idSchema); return s.field("result", resultSchema); })] }; }; } function makeHexSchema(fromStr) { return S$RescriptSchema.transform(S$RescriptSchema.string, (function (s) { return { p: (function (str) { var v = fromStr(str); if (v !== undefined) { return Caml_option.valFromOption(v); } else { return s.fail("The string is not valid hex", undefined); } }), s: (function (value) { return Viem.toHex(value); }) }; })); } var hexBigintSchema = makeHexSchema($$BigInt.fromString); var hexIntSchema = makeHexSchema(function (v) { return Number(v); }); var topicFilterSchema = S$RescriptSchema.union([ S$RescriptSchema.literal(null), S$RescriptSchema.schema(function (s) { return s.m(S$RescriptSchema.array(S$RescriptSchema.string)); }), S$RescriptSchema.schema(function (s) { return s.m(S$RescriptSchema.string); }) ]); var topicQuerySchema = S$RescriptSchema.array(topicFilterSchema); function makeTopicQuery(topic0Opt, topic1Opt, topic2Opt, topic3Opt) { var topic0 = topic0Opt !== undefined ? topic0Opt : []; var topic1 = topic1Opt !== undefined ? topic1Opt : []; var topic2 = topic2Opt !== undefined ? topic2Opt : []; var topic3 = topic3Opt !== undefined ? topic3Opt : []; var topics = [ topic0, topic1, topic2, topic3 ]; var isLastTopicEmpty = function () { var match = Utils.$$Array.last(topics); if (match !== undefined) { return match.length === 0; } else { return false; } }; while(isLastTopicEmpty()) { topics.pop(); }; var toTopicFilter = function (topic) { var len = topic.length; if (len !== 1) { if (len !== 0) { return topic; } else { return null; } } var single = topic[0]; return single; }; return Belt_Array.map(topics, toTopicFilter); } function mapTopicQuery(param) { return makeTopicQuery(param.topic0, param.topic1, param.topic2, param.topic3); } var paramsSchema = S$RescriptSchema.object(function (s) { return { fromBlock: s.f("fromBlock", hexIntSchema), toBlock: s.f("toBlock", hexIntSchema), address: s.f("address", S$RescriptSchema.array(Address.schema)), topics: s.f("topics", topicQuerySchema) }; }); var logSchema = S$RescriptSchema.object(function (s) { return { address: s.f("address", Address.schema), topics: s.f("topics", S$RescriptSchema.array(S$RescriptSchema.string)), data: s.f("data", S$RescriptSchema.string), blockNumber: s.f("blockNumber", hexIntSchema), transactionHash: s.f("transactionHash", S$RescriptSchema.string), transactionIndex: s.f("transactionIndex", hexIntSchema), blockHash: s.f("blockHash", S$RescriptSchema.string), logIndex: s.f("logIndex", hexIntSchema), removed: s.f("removed", S$RescriptSchema.bool) }; }); var route = makeRpcRoute("eth_getLogs", S$RescriptSchema.tuple1(paramsSchema), S$RescriptSchema.array(logSchema)); var GetLogs = { topicFilterSchema: topicFilterSchema, topicQuerySchema: topicQuerySchema, makeTopicQuery: makeTopicQuery, mapTopicQuery: mapTopicQuery, paramsSchema: paramsSchema, logSchema: logSchema, route: route }; var blockSchema = S$RescriptSchema.object(function (s) { return { difficulty: s.f("difficulty", S$RescriptSchema.$$null(hexBigintSchema)), extraData: s.f("extraData", S$RescriptSchema.string), gasLimit: s.f("gasLimit", hexBigintSchema), gasUsed: s.f("gasUsed", hexBigintSchema), hash: s.f("hash", S$RescriptSchema.string), logsBloom: s.f("logsBloom", S$RescriptSchema.string), miner: s.f("miner", Address.schema), mixHash: s.f("mixHash", S$RescriptSchema.$$null(S$RescriptSchema.string)), nonce: s.f("nonce", S$RescriptSchema.$$null(hexBigintSchema)), number: s.f("number", hexIntSchema), parentHash: s.f("parentHash", S$RescriptSchema.string), receiptsRoot: s.f("receiptsRoot", S$RescriptSchema.string), sha3Uncles: s.f("sha3Uncles", S$RescriptSchema.string), size: s.f("size", hexBigintSchema), stateRoot: s.f("stateRoot", S$RescriptSchema.string), timestamp: s.f("timestamp", hexIntSchema), totalDifficulty: s.f("totalDifficulty", S$RescriptSchema.$$null(hexBigintSchema)), transactions: s.f("transactions", S$RescriptSchema.array(S$RescriptSchema.json(false))), transactionsRoot: s.f("transactionsRoot", S$RescriptSchema.string), uncles: s.f("uncles", S$RescriptSchema.$$null(S$RescriptSchema.array(S$RescriptSchema.string))) }; }); var route$1 = makeRpcRoute("eth_getBlockByNumber", S$RescriptSchema.tuple(function (s) { return { blockNumber: s.item(0, hexIntSchema), includeTransactions: s.item(1, S$RescriptSchema.bool) }; }), S$RescriptSchema.$$null(blockSchema)); var GetBlockByNumber = { blockSchema: blockSchema, route: route$1 }; var route$2 = makeRpcRoute("eth_blockNumber", S$RescriptSchema.tuple(function (param) { }), hexIntSchema); var GetBlockHeight = { route: route$2 }; exports.makeRpcRoute = makeRpcRoute; exports.makeHexSchema = makeHexSchema; exports.hexBigintSchema = hexBigintSchema; exports.hexIntSchema = hexIntSchema; exports.GetLogs = GetLogs; exports.GetBlockByNumber = GetBlockByNumber; exports.GetBlockHeight = GetBlockHeight; /* hexBigintSchema Not a pure module */