UNPKG

envio

Version:

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

260 lines (226 loc) • 8 kB
// Generated by ReScript, PLEASE EDIT WITH CARE import * as Rest from "../vendored/Rest.res.mjs"; import * as Viem from "viem"; import * as Utils from "../Utils.res.mjs"; import * as Address from "../Address.res.mjs"; import * as Stdlib_BigInt from "@rescript/runtime/lib/es6/Stdlib_BigInt.js"; import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js"; import * as S$RescriptSchema from "rescript-schema/src/S.res.mjs"; import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js"; let JsonRpcError = /* @__PURE__ */Primitive_exceptions.create("Rpc.JsonRpcError"); function makeRpcRoute(method, paramsSchema, resultSchema) { let idSchema = S$RescriptSchema.literal(1); let versionSchema = S$RescriptSchema.literal("2.0"); return () => ({ method: "POST", path: "", input: s => { s.field("method", S$RescriptSchema.literal(method)); s.field("id", idSchema); s.field("jsonrpc", versionSchema); return s.field("params", paramsSchema); }, responses: [s => { s.field("jsonrpc", versionSchema); s.field("id", idSchema); return s.field("result", resultSchema); }] }); } async function jsonRpcFetcher(args) { let response = await Rest.ApiFetcher.$$default(args); let data = response.data; let error = data.error; if (!(error == null)) { throw { RE_EXN_ID: JsonRpcError, _1: { code: error.code, message: error.message }, Error: new Error() }; } return response; } function makeClient(url) { return Rest.client(url, jsonRpcFetcher); } function makeHexSchema(fromStr) { return S$RescriptSchema.transform(S$RescriptSchema.string, s => ({ p: str => { let v = fromStr(str); if (v !== undefined) { return Primitive_option.valFromOption(v); } else { return s.fail("The string is not valid hex", undefined); } }, s: value => Viem.toHex(value) })); } let hexBigintSchema = makeHexSchema(Stdlib_BigInt.fromString); let hexIntSchema = makeHexSchema(v => Number(v)); let decimalFloatSchema = S$RescriptSchema.transform(S$RescriptSchema.string, s => ({ p: str => { let v = Number(str); if (Number.isNaN(v)) { return s.fail("The string is not a valid decimal number", undefined); } else { return v; } } })); let topicFilterSchema = S$RescriptSchema.union([ S$RescriptSchema.literal(null), S$RescriptSchema.schema(s => (s.m(S$RescriptSchema.array(S$RescriptSchema.string)))), S$RescriptSchema.schema(s => (s.m(S$RescriptSchema.string))) ]); let topicQuerySchema = S$RescriptSchema.array(topicFilterSchema); function makeTopicQuery(topic0Opt, topic1Opt, topic2Opt, topic3Opt) { let topic0 = topic0Opt !== undefined ? topic0Opt : []; let topic1 = topic1Opt !== undefined ? topic1Opt : []; let topic2 = topic2Opt !== undefined ? topic2Opt : []; let topic3 = topic3Opt !== undefined ? topic3Opt : []; let topics = [ topic0, topic1, topic2, topic3 ]; let isLastTopicEmpty = () => { let match = Utils.$$Array.last(topics); if (match !== undefined) { return match.length === 0; } else { return false; } }; while (isLastTopicEmpty()) { topics.pop(); }; let toTopicFilter = topic => { let len = topic.length; if (len !== 1) { if (len !== 0) { return topic; } else { return null; } } let single = topic[0]; return single; }; return topics.map(toTopicFilter); } function mapTopicQuery(param) { return makeTopicQuery(param.topic0, param.topic1, param.topic2, param.topic3); } let paramsSchema = S$RescriptSchema.object(s => ({ fromBlock: s.f("fromBlock", hexIntSchema), toBlock: s.f("toBlock", hexIntSchema), address: s.f("address", S$RescriptSchema.option(S$RescriptSchema.array(Address.schema))), topics: s.f("topics", topicQuerySchema) })); let fullParamsSchema = S$RescriptSchema.tuple1(paramsSchema); let logSchema = S$RescriptSchema.object(s => ({ 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) })); let resultSchema = S$RescriptSchema.array(logSchema); let route = makeRpcRoute("eth_getLogs", fullParamsSchema, resultSchema); let GetLogs = { topicFilterSchema: topicFilterSchema, topicQuerySchema: topicQuerySchema, makeTopicQuery: makeTopicQuery, mapTopicQuery: mapTopicQuery, paramsSchema: paramsSchema, fullParamsSchema: fullParamsSchema, logSchema: logSchema, resultSchema: resultSchema, route: route }; let blockSchema = S$RescriptSchema.object(s => ({ 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))) })); let paramsSchema$1 = S$RescriptSchema.tuple(s => ({ blockNumber: s.item(0, hexIntSchema), includeTransactions: s.item(1, S$RescriptSchema.bool) })); let resultSchema$1 = S$RescriptSchema.$$null(blockSchema); let route$1 = makeRpcRoute("eth_getBlockByNumber", paramsSchema$1, resultSchema$1); let rawRoute = makeRpcRoute("eth_getBlockByNumber", paramsSchema$1, S$RescriptSchema.$$null(S$RescriptSchema.json(false))); let GetBlockByNumber = { blockSchema: blockSchema, paramsSchema: paramsSchema$1, resultSchema: resultSchema$1, route: route$1, rawRoute: rawRoute }; let rawRoute$1 = makeRpcRoute("eth_getTransactionByHash", S$RescriptSchema.tuple1(S$RescriptSchema.string), S$RescriptSchema.$$null(S$RescriptSchema.json(false))); let GetTransactionByHash = { rawRoute: rawRoute$1 }; let rawRoute$2 = makeRpcRoute("eth_getTransactionReceipt", S$RescriptSchema.tuple1(S$RescriptSchema.string), S$RescriptSchema.$$null(S$RescriptSchema.json(false))); let GetTransactionReceipt = { rawRoute: rawRoute$2 }; async function getLogs(client, param) { return await Rest.fetch(route, param, client); } async function getBlock(client, blockNumber) { return await Rest.fetch(route$1, { blockNumber: blockNumber, includeTransactions: false }, client); } async function getRawBlock(client, blockNumber) { return await Rest.fetch(rawRoute, { blockNumber: blockNumber, includeTransactions: false }, client); } export { JsonRpcError, makeRpcRoute, jsonRpcFetcher, makeClient, makeHexSchema, hexBigintSchema, hexIntSchema, decimalFloatSchema, GetLogs, GetBlockByNumber, GetTransactionByHash, GetTransactionReceipt, getLogs, getBlock, getRawBlock, } /* hexBigintSchema Not a pure module */