envio
Version:
A latency and sync speed optimized, developer friendly blockchain data indexer.
422 lines (399 loc) • 14.7 kB
JavaScript
// Generated by ReScript, PLEASE EDIT WITH CARE
import * as Utils from "./Utils.res.mjs";
import * as Address from "./Address.res.mjs";
import * as FuelSDK from "./sources/FuelSDK.res.mjs";
import * as Internal from "./Internal.res.mjs";
import * as TopicFilter from "./TopicFilter.res.mjs";
import * as LogSelection from "./LogSelection.res.mjs";
import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js";
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
import * as S$RescriptSchema from "rescript-schema/src/S.res.mjs";
let paramMetaSchema = S$RescriptSchema.recursive(self => S$RescriptSchema.object(s => ({
name: s.f("name", S$RescriptSchema.string),
abiType: s.f("abiType", S$RescriptSchema.string),
indexed: s.fieldOr("indexed", S$RescriptSchema.bool, false),
components: s.f("components", S$RescriptSchema.option(S$RescriptSchema.array(self)))
})));
function normalizeOrThrow(value) {
if (Array.isArray(value)) {
return value;
} else {
return [value];
}
}
function splitTupleComponents(inner) {
let components = [];
let depth = 0;
let start = 0;
for (let i = 0, i_finish = inner.length; i < i_finish; ++i) {
let ch = inner.charAt(i);
if (ch === "(") {
depth = depth + 1 | 0;
} else if (ch === ")") {
depth = depth - 1 | 0;
} else if (ch === "," && depth === 0) {
components.push(inner.slice(start, i));
start = i + 1 | 0;
}
}
if (start < inner.length) {
components.push(inner.slice(start));
}
return components;
}
function abiTypeToSchema(abiType) {
if (abiType.endsWith("]")) {
let bracketIdx = abiType.lastIndexOf("[");
let baseType = abiType.slice(0, bracketIdx);
return S$RescriptSchema.array(abiTypeToSchema(baseType));
}
if (abiType.startsWith("(") && abiType.endsWith(")")) {
let inner = abiType.slice(1, abiType.length - 1 | 0);
let components = splitTupleComponents(inner);
let schemas = components.map(c => abiTypeToSchema(c.trim()));
return S$RescriptSchema.tuple(s => schemas.map((schema, i) => s.item(i, schema)));
}
switch (abiType) {
case "address" :
return Address.schema;
case "bool" :
return S$RescriptSchema.bool;
case "bytes" :
case "string" :
return S$RescriptSchema.string;
default:
if (abiType.startsWith("uint") || abiType.startsWith("int")) {
return Utils.$$BigInt.schema;
} else if (abiType.startsWith("bytes")) {
return S$RescriptSchema.string;
} else {
return Stdlib_JsError.throwWithMessage(`Unsupported ABI type: ` + abiType);
}
}
}
function abiTypeToSimulateSchema(abiType) {
if (abiType.endsWith("]")) {
let bracketIdx = abiType.lastIndexOf("[");
let baseType = abiType.slice(0, bracketIdx);
return S$RescriptSchema.array(abiTypeToSimulateSchema(baseType));
}
if (abiType.startsWith("(") && abiType.endsWith(")")) {
let inner = abiType.slice(1, abiType.length - 1 | 0);
let components = splitTupleComponents(inner);
let schemas = components.map(c => abiTypeToSimulateSchema(c.trim()));
return S$RescriptSchema.tuple(s => schemas.map((schema, i) => s.item(i, schema)));
}
switch (abiType) {
case "bool" :
return S$RescriptSchema.bool;
case "address" :
case "bytes" :
case "string" :
return S$RescriptSchema.string;
default:
if (abiType.startsWith("uint") || abiType.startsWith("int")) {
return S$RescriptSchema.bigint;
} else if (abiType.startsWith("bytes")) {
return S$RescriptSchema.string;
} else {
return Stdlib_JsError.throwWithMessage(`Unsupported ABI type: ` + abiType);
}
}
}
function abiTypeToDefaultValue(abiType) {
if (abiType.endsWith("]")) {
return [];
}
if (abiType.startsWith("(") && abiType.endsWith(")")) {
let inner = abiType.slice(1, abiType.length - 1 | 0);
let components = splitTupleComponents(inner);
return components.map(c => abiTypeToDefaultValue(c.trim()));
}
switch (abiType) {
case "address" :
return "0x0000000000000000000000000000000000000000";
case "bool" :
return false;
case "bytes" :
case "string" :
return "";
default:
if (abiType.startsWith("uint") || abiType.startsWith("int")) {
return 0n;
} else if (abiType.startsWith("bytes")) {
return "";
} else {
return undefined;
}
}
}
function componentsToObjectSchema(leafSchema, abiType, components) {
if (!abiType.endsWith("]")) {
return S$RescriptSchema.object(s => {
let dict = {};
components.forEach(c => {
let sub = c.components;
let childSchema = sub !== undefined ? componentsToObjectSchema(leafSchema, c.abiType, sub) : leafSchema(c.abiType);
dict[c.name] = s.f(c.name, childSchema);
});
return dict;
});
}
let bracketIdx = abiType.lastIndexOf("[");
let baseType = abiType.slice(0, bracketIdx);
return S$RescriptSchema.array(componentsToObjectSchema(leafSchema, baseType, components));
}
function componentsToDefaultValue(abiType, components) {
if (abiType.endsWith("]")) {
return [];
}
let dict = {};
components.forEach(c => {
let sub = c.components;
let v = sub !== undefined ? componentsToDefaultValue(c.abiType, sub) : abiTypeToDefaultValue(c.abiType);
dict[c.name] = v;
});
return dict;
}
function buildParamsSchema(params) {
if (params.length === 0) {
return S$RescriptSchema.shape(S$RescriptSchema.literal(null), param => {});
} else {
return S$RescriptSchema.object(s => {
let dict = {};
params.forEach(p => {
let components = p.components;
let paramSchema = components !== undefined && !p.indexed ? componentsToObjectSchema(abiTypeToSchema, p.abiType, components) : abiTypeToSchema(p.abiType);
dict[p.name] = s.f(p.name, paramSchema);
});
return dict;
});
}
}
function buildSimulateParamsSchema(params) {
if (params.length === 0) {
return S$RescriptSchema.shape(S$RescriptSchema.unknown, param => {});
} else {
return S$RescriptSchema.schema(s => {
let dict = {};
params.forEach(p => {
let components = p.components;
let match = components !== undefined ? [
componentsToObjectSchema(abiTypeToSimulateSchema, p.abiType, components),
componentsToDefaultValue(p.abiType, components)
] : [
abiTypeToSimulateSchema(p.abiType),
abiTypeToDefaultValue(p.abiType)
];
dict[p.name] = s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(match[0]), match[1]));
});
return dict;
});
}
}
function getTopicEncoder(abiType) {
if (abiType.endsWith("]") || abiType.startsWith("(")) {
return TopicFilter.castToHexUnsafe;
}
switch (abiType) {
case "address" :
return TopicFilter.fromAddress;
case "bool" :
return TopicFilter.fromBool;
case "bytes" :
return TopicFilter.fromDynamicBytes;
case "string" :
return TopicFilter.fromDynamicString;
default:
if (abiType.startsWith("uint")) {
return TopicFilter.fromBigInt;
} else if (abiType.startsWith("int")) {
return TopicFilter.fromSignedBigInt;
} else if (abiType.startsWith("bytes")) {
return TopicFilter.castToHexUnsafe;
} else {
return Stdlib_JsError.throwWithMessage(`Unsupported topic filter ABI type: ` + abiType);
}
}
}
function buildTopicGetter(p) {
let encoder = getTopicEncoder(p.abiType);
return eventFilter => Stdlib_Option.mapOr(eventFilter[p.name], [], topicFilters => normalizeOrThrow(topicFilters).map(encoder));
}
let alwaysIncludedBlockFields = [
"number",
"timestamp",
"hash"
];
function resolveFieldSelection(blockFields, transactionFields, globalBlockFieldsSet, globalTransactionFieldsSet) {
let selectedBlockFields = blockFields !== undefined ? new Set(alwaysIncludedBlockFields.concat(blockFields)) : globalBlockFieldsSet;
let selectedTransactionFields = transactionFields !== undefined ? new Set(transactionFields) : globalTransactionFieldsSet;
return [
selectedBlockFields,
selectedTransactionFields
];
}
let compileAddressFilter = (function (body) {
return new Function("event", "blockNumber", "indexingAddresses", body);
});
function buildAddressFilterBody(groups) {
if (groups.length === 0) {
return;
}
let leaf = name => `(ic = indexingAddresses[p[` + JSON.stringify(name) + `]]) !== undefined && ic.effectiveStartBlock <= blockNumber`;
let groupExprs = groups.map(group => "(" + group.map(leaf).join(" && ") + ")");
return "var p = event.params, ic; return " + groupExprs.join(" || ") + ";";
}
function buildAddressFilter(groups) {
return Stdlib_Option.map(buildAddressFilterBody(groups), compileAddressFilter);
}
function buildEvmEventConfig(contractName, eventName, sighash, params, isWildcard, handler, contractRegister, eventFilters, probeChainId, onEventBlockFilterSchema, blockFields, transactionFields, startBlock, globalBlockFieldsSetOpt, globalTransactionFieldsSetOpt) {
let globalBlockFieldsSet = globalBlockFieldsSetOpt !== undefined ? Primitive_option.valFromOption(globalBlockFieldsSetOpt) : new Set();
let globalTransactionFieldsSet = globalTransactionFieldsSetOpt !== undefined ? Primitive_option.valFromOption(globalTransactionFieldsSetOpt) : new Set();
let topicCount = Stdlib_Array.reduce(params, 1, (acc, p) => {
if (p.indexed) {
return acc + 1 | 0;
} else {
return acc;
}
});
let indexedParams = params.filter(p => p.indexed);
let match = LogSelection.parseEventFiltersOrThrow(eventFilters, sighash, indexedParams.map(p => p.name), contractName, probeChainId, onEventBlockFilterSchema, Stdlib_Option.map(indexedParams[0], buildTopicGetter), Stdlib_Option.map(indexedParams[1], buildTopicGetter), Stdlib_Option.map(indexedParams[2], buildTopicGetter));
let whereStartBlock = match.startBlock;
let filterByAddresses = match.filterByAddresses;
let resolvedStartBlock = whereStartBlock !== undefined ? whereStartBlock : startBlock;
let match$1 = resolveFieldSelection(blockFields, transactionFields, globalBlockFieldsSet, globalTransactionFieldsSet);
return {
id: sighash + "_" + topicCount.toString(),
name: eventName,
contractName: contractName,
isWildcard: isWildcard,
filterByAddresses: filterByAddresses,
dependsOnAddresses: !isWildcard || filterByAddresses,
clientAddressFilter: Stdlib_Option.map(buildAddressFilterBody(match.addressFilterParamGroups), compileAddressFilter),
handler: handler,
contractRegister: contractRegister,
paramsRawEventSchema: buildParamsSchema(params),
simulateParamsSchema: buildSimulateParamsSchema(params),
startBlock: resolvedStartBlock,
getEventFiltersOrThrow: match.getEventFiltersOrThrow,
selectedBlockFields: match$1[0],
selectedTransactionFields: match$1[1],
sighash: sighash,
topicCount: topicCount,
paramsMetadata: params
};
}
function buildSvmInstructionEventConfig(contractName, instructionName, programId, discriminator, discriminatorByteLen, includeTransaction, includeLogs, includeTokenBalances, accountFilters, isInner, isWildcard, handler, contractRegister, accountsOpt, argsOpt, definedTypesOpt, startBlock) {
let accounts = accountsOpt !== undefined ? accountsOpt : [];
let args = argsOpt !== undefined ? argsOpt : null;
let definedTypes = definedTypesOpt !== undefined ? definedTypesOpt : null;
let paramsSchema = Utils.Schema.coerceToJsonPgType(S$RescriptSchema.json(false));
return {
id: discriminator !== undefined ? discriminator : "none",
name: instructionName,
contractName: contractName,
isWildcard: isWildcard,
filterByAddresses: false,
dependsOnAddresses: !isWildcard,
handler: handler,
contractRegister: contractRegister,
paramsRawEventSchema: paramsSchema,
simulateParamsSchema: paramsSchema,
startBlock: startBlock,
programId: programId,
discriminator: discriminator,
discriminatorByteLen: discriminatorByteLen,
includeTransaction: includeTransaction,
includeLogs: includeLogs,
includeTokenBalances: includeTokenBalances,
accountFilters: accountFilters,
isInner: isInner,
accounts: accounts,
args: args,
definedTypes: definedTypes
};
}
function buildFuelEventConfig(contractName, eventName, kind, sighash, rawAbi, isWildcard, handler, contractRegister, startBlock) {
let fuelKind;
switch (kind) {
case "burn" :
fuelKind = "Burn";
break;
case "call" :
fuelKind = "Call";
break;
case "logData" :
let abi = FuelSDK.transpileAbi(rawAbi);
fuelKind = {
TAG: "LogData",
logId: sighash,
decode: FuelSDK.Receipt.getLogDataDecoder(abi, sighash)
};
break;
case "mint" :
fuelKind = "Mint";
break;
case "transfer" :
fuelKind = "Transfer";
break;
default:
fuelKind = Stdlib_JsError.throwWithMessage(`Unsupported Fuel event kind: ` + kind);
}
let paramsSchema;
switch (kind) {
case "logData" :
paramsSchema = Utils.Schema.coerceToJsonPgType(S$RescriptSchema.json(false));
break;
case "burn" :
case "mint" :
paramsSchema = Internal.fuelSupplyParamsSchema;
break;
case "call" :
case "transfer" :
paramsSchema = Internal.fuelTransferParamsSchema;
break;
default:
paramsSchema = Stdlib_JsError.throwWithMessage(`Unsupported Fuel event kind: ` + kind);
}
let tmp = kind === "logData" ? sighash : kind;
return {
id: tmp,
name: eventName,
contractName: contractName,
isWildcard: isWildcard,
filterByAddresses: false,
dependsOnAddresses: !isWildcard,
handler: handler,
contractRegister: contractRegister,
paramsRawEventSchema: paramsSchema,
simulateParamsSchema: paramsSchema,
startBlock: startBlock,
kind: fuelKind
};
}
export {
paramMetaSchema,
normalizeOrThrow,
splitTupleComponents,
abiTypeToSchema,
abiTypeToSimulateSchema,
abiTypeToDefaultValue,
componentsToObjectSchema,
componentsToDefaultValue,
buildParamsSchema,
buildSimulateParamsSchema,
getTopicEncoder,
buildTopicGetter,
alwaysIncludedBlockFields,
resolveFieldSelection,
compileAddressFilter,
buildAddressFilterBody,
buildAddressFilter,
buildEvmEventConfig,
buildSvmInstructionEventConfig,
buildFuelEventConfig,
}
/* paramMetaSchema Not a pure module */