envio
Version:
A latency and sync speed optimized, developer friendly blockchain data indexer.
324 lines (310 loc) • 14 kB
JavaScript
// Generated by ReScript, PLEASE EDIT WITH CARE
import * as Address from "./Address.res.mjs";
import * as ChainMap from "./ChainMap.res.mjs";
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
import * as SimulateSource from "./sources/SimulateSource.res.mjs";
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 evmSimulateBlockSchema = S$RescriptSchema.schema(s => ({
number: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.int), 0)),
timestamp: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.int), 0)),
hash: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.string), "")),
parentHash: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.string), "")),
sha3Uncles: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.string), "")),
logsBloom: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.string), "")),
transactionsRoot: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.string), "")),
stateRoot: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.string), "")),
receiptsRoot: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.string), "")),
miner: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(Address.schema), "0x0000000000000000000000000000000000000000")),
extraData: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.string), "")),
size: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.bigint), 0n)),
gasLimit: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.bigint), 0n)),
gasUsed: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.bigint), 0n)),
nonce: s.m(S$RescriptSchema.$$null(S$RescriptSchema.bigint)),
difficulty: s.m(S$RescriptSchema.$$null(S$RescriptSchema.bigint)),
totalDifficulty: s.m(S$RescriptSchema.$$null(S$RescriptSchema.bigint)),
uncles: s.m(S$RescriptSchema.$$null(S$RescriptSchema.array(S$RescriptSchema.string))),
baseFeePerGas: s.m(S$RescriptSchema.$$null(S$RescriptSchema.bigint)),
blobGasUsed: s.m(S$RescriptSchema.$$null(S$RescriptSchema.bigint)),
excessBlobGas: s.m(S$RescriptSchema.$$null(S$RescriptSchema.bigint)),
parentBeaconBlockRoot: s.m(S$RescriptSchema.$$null(S$RescriptSchema.string)),
withdrawalsRoot: s.m(S$RescriptSchema.$$null(S$RescriptSchema.string)),
l1BlockNumber: s.m(S$RescriptSchema.$$null(S$RescriptSchema.int)),
sendCount: s.m(S$RescriptSchema.$$null(S$RescriptSchema.string)),
sendRoot: s.m(S$RescriptSchema.$$null(S$RescriptSchema.string)),
mixHash: s.m(S$RescriptSchema.$$null(S$RescriptSchema.string))
}));
function parseEvmSimulateBlock(defaultBlockNumber, blockJson) {
let block = blockJson !== undefined ? S$RescriptSchema.convertOrThrow(blockJson, evmSimulateBlockSchema) : S$RescriptSchema.convertOrThrow({}, evmSimulateBlockSchema);
if (Stdlib_Option.isNone(blockJson) || block.number === 0) {
block["number"] = defaultBlockNumber;
}
return block;
}
let evmSimulateTransactionSchema = S$RescriptSchema.schema(s => ({
transactionIndex: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.int), 0)),
hash: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.string), "")),
gas: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.bigint), 0n)),
input: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.string), "")),
nonce: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.bigint), 0n)),
value: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.bigint), 0n)),
cumulativeGasUsed: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.bigint), 0n)),
effectiveGasPrice: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.bigint), 0n)),
gasUsed: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.bigint), 0n)),
logsBloom: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.string), "")),
accessList: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.json(false)), null)),
v: s.m(S$RescriptSchema.$$null(S$RescriptSchema.string)),
r: s.m(S$RescriptSchema.$$null(S$RescriptSchema.string)),
s: s.m(S$RescriptSchema.$$null(S$RescriptSchema.string)),
yParity: s.m(S$RescriptSchema.$$null(S$RescriptSchema.string)),
from: s.m(S$RescriptSchema.$$null(Address.schema)),
to: s.m(S$RescriptSchema.$$null(Address.schema)),
contractAddress: s.m(S$RescriptSchema.$$null(S$RescriptSchema.string)),
gasPrice: s.m(S$RescriptSchema.$$null(S$RescriptSchema.bigint)),
maxPriorityFeePerGas: s.m(S$RescriptSchema.$$null(S$RescriptSchema.bigint)),
maxFeePerGas: s.m(S$RescriptSchema.$$null(S$RescriptSchema.bigint)),
maxFeePerBlobGas: s.m(S$RescriptSchema.$$null(S$RescriptSchema.bigint)),
blobVersionedHashes: s.m(S$RescriptSchema.$$null(S$RescriptSchema.array(S$RescriptSchema.string))),
root: s.m(S$RescriptSchema.$$null(S$RescriptSchema.string)),
status: s.m(S$RescriptSchema.$$null(S$RescriptSchema.int)),
type: s.m(S$RescriptSchema.$$null(S$RescriptSchema.int)),
l1Fee: s.m(S$RescriptSchema.$$null(S$RescriptSchema.bigint)),
l1GasPrice: s.m(S$RescriptSchema.$$null(S$RescriptSchema.bigint)),
l1GasUsed: s.m(S$RescriptSchema.$$null(S$RescriptSchema.bigint)),
l1FeeScalar: s.m(S$RescriptSchema.$$null(S$RescriptSchema.float)),
gasUsedForL1: s.m(S$RescriptSchema.$$null(S$RescriptSchema.bigint)),
authorizationList: s.m(S$RescriptSchema.$$null(S$RescriptSchema.json(false)))
}));
function parseEvmSimulateTransaction(transactionJson) {
if (transactionJson !== undefined) {
return S$RescriptSchema.convertOrThrow(transactionJson, evmSimulateTransactionSchema);
} else {
return S$RescriptSchema.convertOrThrow({}, evmSimulateTransactionSchema);
}
}
let fuelSimulateBlockSchema = S$RescriptSchema.schema(s => ({
id: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.string), "")),
height: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.int), 0)),
time: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.int), 0))
}));
function parseFuelSimulateBlock(defaultBlockNumber, blockJson) {
let block = blockJson !== undefined ? S$RescriptSchema.convertOrThrow(blockJson, fuelSimulateBlockSchema) : S$RescriptSchema.convertOrThrow({}, fuelSimulateBlockSchema);
if (Stdlib_Option.isNone(blockJson) || block.height === 0) {
block["height"] = defaultBlockNumber;
}
return block;
}
let fuelSimulateTransactionSchema = S$RescriptSchema.schema(s => ({
id: s.m(S$RescriptSchema.Option.getOr(S$RescriptSchema.$$null(S$RescriptSchema.string), ""))
}));
function parseFuelSimulateTransaction(transactionJson) {
if (transactionJson !== undefined) {
return S$RescriptSchema.convertOrThrow(transactionJson, fuelSimulateTransactionSchema);
} else {
return S$RescriptSchema.convertOrThrow({}, fuelSimulateTransactionSchema);
}
}
function findEventConfig(config, contractName, eventName) {
let found = {
contents: undefined
};
ChainMap.values(config.chainMap).forEach(chainConfig => {
chainConfig.contracts.forEach(contract => {
if (contract.name === contractName) {
contract.events.forEach(eventConfig => {
if (eventConfig.name === eventName) {
found.contents = eventConfig;
return;
}
});
return;
}
});
});
return found.contents;
}
function parse(simulateItems, config, chainConfig) {
let chain = ChainMap.Chain.makeUnsafe(chainConfig.id);
let chainId = chainConfig.id;
let startBlock = chainConfig.startBlock;
let currentBlock = {
contents: startBlock
};
let currentLogIndex = {
contents: 0
};
let items = [];
simulateItems.forEach(rawJson => {
let match = rawJson.contract;
let match$1 = rawJson.event;
if (match === undefined) {
return Stdlib_JsError.throwWithMessage(`simulate: Invalid item. Each item must have "contract" and "event" fields.`);
}
if (match$1 === undefined) {
return Stdlib_JsError.throwWithMessage(`simulate: Invalid item. Each item must have "contract" and "event" fields.`);
}
let ec = findEventConfig(config, match, match$1);
let eventConfig = ec !== undefined ? ec : Stdlib_JsError.throwWithMessage(`simulate: Event "` + match$1 + `" not found on contract "` + match + `". Check that the contract and event names match your config.yaml.`);
let json = rawJson.params;
let paramsJson = json !== undefined ? json : ({});
let params = S$RescriptSchema.convertOrThrow(paramsJson, eventConfig.simulateParamsSchema);
let li = rawJson.logIndex;
let logIndex;
if (li !== undefined) {
logIndex = li;
} else {
let li$1 = currentLogIndex.contents;
currentLogIndex.contents = li$1 + 1 | 0;
logIndex = li$1;
}
let addr = rawJson.srcAddress;
let srcAddress;
if (addr !== undefined) {
srcAddress = Primitive_option.valFromOption(addr);
} else {
let addr$1 = {
contents: "0x0000000000000000000000000000000000000000"
};
chainConfig.contracts.forEach(contract => {
if (contract.name !== match) {
return;
}
let a = contract.addresses[0];
if (a !== undefined) {
addr$1.contents = Primitive_option.valFromOption(a);
return;
}
});
srcAddress = addr$1.contents;
}
let blockJson = rawJson.block;
let blockJson$1 = (blockJson == null) ? undefined : Primitive_option.some(blockJson);
let transactionJson = rawJson.transaction;
let transactionJson$1 = (transactionJson == null) ? undefined : Primitive_option.some(transactionJson);
let match$2 = config.ecosystem.name;
let match$3;
switch (match$2) {
case "evm" :
let block = parseEvmSimulateBlock(currentBlock.contents, blockJson$1);
match$3 = [
block,
block.number,
block.timestamp,
block.hash
];
break;
case "fuel" :
let block$1 = parseFuelSimulateBlock(currentBlock.contents, blockJson$1);
match$3 = [
block$1,
block$1.height,
block$1.time,
block$1.id
];
break;
case "svm" :
match$3 = Stdlib_JsError.throwWithMessage("simulate is not supported for SVM ecosystem");
break;
}
let blockNumber = match$3[1];
let match$4 = config.ecosystem.name;
let transaction;
switch (match$4) {
case "evm" :
transaction = parseEvmSimulateTransaction(transactionJson$1);
break;
case "fuel" :
transaction = parseFuelSimulateTransaction(transactionJson$1);
break;
case "svm" :
transaction = Stdlib_JsError.throwWithMessage("simulate is not supported for SVM ecosystem");
break;
}
currentBlock.contents = blockNumber;
items.push({
kind: 0,
eventConfig: eventConfig,
timestamp: match$3[2],
chain: chain,
blockNumber: blockNumber,
blockHash: match$3[3],
logIndex: logIndex,
event: {
contractName: eventConfig.contractName,
eventName: eventConfig.name,
params: params,
chainId: chainId,
srcAddress: srcAddress,
logIndex: logIndex,
transaction: transaction,
block: match$3[0]
}
});
});
return items;
}
function patchConfig(config, processConfig) {
let processChains = processConfig.chains;
if (processChains == null) {
return config;
}
let newChainMap = ChainMap.mapWithKey(config.chainMap, (chain, chainConfig) => {
let chainIdStr = chain.toString();
let processChainJson = processChains[chainIdStr];
if (processChainJson === undefined) {
return chainConfig;
}
let simulateRaw = processChainJson.simulate;
if (simulateRaw == null) {
return chainConfig;
}
let items = parse(simulateRaw, config, chainConfig);
let startBlock = processChainJson.startBlock;
let endBlock = processChainJson.endBlock;
let source = SimulateSource.make(items, endBlock, chain);
let newrecord = {...chainConfig};
newrecord.sourceConfig = {
TAG: "CustomSources",
_0: [source]
};
newrecord.endBlock = endBlock;
newrecord.startBlock = startBlock;
return newrecord;
});
return {
name: config.name,
description: config.description,
handlers: config.handlers,
contractHandlers: config.contractHandlers,
shouldRollbackOnReorg: config.shouldRollbackOnReorg,
shouldSaveFullHistory: config.shouldSaveFullHistory,
storage: config.storage,
chainMap: newChainMap,
defaultChain: config.defaultChain,
ecosystem: config.ecosystem,
enableRawEvents: config.enableRawEvents,
maxAddrInPartition: config.maxAddrInPartition,
batchSize: config.batchSize,
lowercaseAddresses: config.lowercaseAddresses,
isDev: config.isDev,
userEntitiesByName: config.userEntitiesByName,
userEntities: config.userEntities,
allEntities: config.allEntities,
allEnums: config.allEnums
};
}
export {
evmSimulateBlockSchema,
parseEvmSimulateBlock,
evmSimulateTransactionSchema,
parseEvmSimulateTransaction,
fuelSimulateBlockSchema,
parseFuelSimulateBlock,
fuelSimulateTransactionSchema,
parseFuelSimulateTransaction,
findEventConfig,
parse,
patchConfig,
}
/* evmSimulateBlockSchema Not a pure module */