| 2 | "code": "\\\\n function convertNanosecondsToTimestamp(nanosecondsStr) {\\\\n const nanoseconds = BigInt(nanosecondsStr);\\\\n const milliseconds = nanoseconds / 1000000n;\\\\n const date = new Date(Number(milliseconds));\\\\n return date;\\\\n }\\\\n\\\\n // Handle \\\\\\\"register_indexer_function\\\\\\\" methods\\\\n async function handleIndexerEditTx(\\\\n accountId,\\\\n functionName,\\\\n receiptId,\\\\n code,\\\\n schema,\\\\n blockHeight,\\\\n blockTimestamp,\\\\n startFromBlockHeight,\\\\n contractFilter\\\\n ) {\\\\n const edit_indexer = {\\\\n author_account_id: accountId,\\\\n indexer_name: functionName,\\\\n deployment_receipt_id: receiptId,\\\\n deployment_timestamp: convertNanosecondsToTimestamp(blockTimestamp),\\\\n deployment_block_height: blockHeight,\\\\n deployment_start_from_block_height: startFromBlockHeight,\\\\n deployment_contract_filter: contractFilter,\\\\n last_change_receipt_id: receiptId,\\\\n last_change_timestamp: convertNanosecondsToTimestamp(blockTimestamp),\\\\n last_change_block_height: blockHeight,\\\\n last_change_start_from_block_height: startFromBlockHeight,\\\\n last_change_contract_filter: contractFilter,\\\\n is_removed: false,\\\\n js_code: code,\\\\n sql_code: schema,\\\\n };\\\\n\\\\n const edit_version = {\\\\n author_account_id: accountId,\\\\n indexer_name: functionName,\\\\n version_receipt_id: receiptId,\\\\n version_timestamp: convertNanosecondsToTimestamp(blockTimestamp),\\\\n version_block_height: blockHeight,\\\\n version_start_from_block_height: startFromBlockHeight,\\\\n version_contract_filter: contractFilter,\\\\n js_code: code,\\\\n sql_code: schema,\\\\n };\\\\n\\\\n try {\\\\n const insertResult = await context.db.Indexers.upsert(\\\\n edit_indexer,\\\\n [\\\\\\\"author_account_id\\\\\\\", \\\\\\\"indexer_name\\\\\\\"],\\\\n [\\\\n \\\\\\\"last_change_receipt_id\\\\\\\",\\\\n \\\\\\\"last_change_timestamp\\\\\\\",\\\\n \\\\\\\"last_change_block_height\\\\\\\",\\\\n \\\\\\\"last_change_start_from_block_height\\\\\\\",\\\\n \\\\\\\"last_change_contract_filter\\\\\\\",\\\\n \\\\\\\"js_code\\\\\\\",\\\\n \\\\\\\"sql_code\\\\\\\",\\\\n ]\\\\n );\\\\n\\\\n const insertVersionResult = await context.db.IndexerVersions.insert(\\\\n edit_version\\\\n );\\\\n } catch (err) {\\\\n console.log(\\\\n `Failed to add indexer edit to the db at blockHeight: ${blockHeight}, ${err}`\\\\n );\\\\n }\\\\n }\\\\n\\\\n const QUERY_API_MAINNET = \\\\\\\"queryapi.dataplatform.near\\\\\\\";\\\\n const queryApiContractTxs = block\\\\n .functionCalls(QUERY_API_MAINNET, \\\\\\\"onlySuccessful\\\\\\\")\\\\n .filter(\\\\n (functionCall) => functionCall.methodName === \\\\\\\"register_indexer_function\\\\\\\"\\\\n )\\\\n .map((functionCall) => {\\\\n const argsAsJSON = functionCall.argsAsJSON();\\\\n const functionName = argsAsJSON.function_name;\\\\n const code = argsAsJSON.code;\\\\n const schema = argsAsJSON.schema;\\\\n const startFromBlockHeight = argsAsJSON.start_block_height;\\\\n const contractFilter = argsAsJSON.filter_json\\\\n ? JSON.parse(argsAsJSON.filter_json).matching_rule.affected_account_id\\\\n : \\\\\\\"\\\\\\\";\\\\n const accountId = functionCall.signerId;\\\\n const receiptId = functionCall.receiptId;\\\\n return {\\\\n accountId,\\\\n functionName,\\\\n receiptId,\\\\n code,\\\\n schema,\\\\n startFromBlockHeight,\\\\n contractFilter,\\\\n };\\\\n });\\\\n\\\\n if (queryApiContractTxs.length > 0) {\\\\n console.log(\\\\\\\"Found QueryAPI Development Activity...\\\\\\\");\\\\n const blockHeight = block.header().height;\\\\n const blockTimestamp = block.header().timestampNanosec;\\\\n await Promise.all(\\\\n queryApiContractTxs.map(\\\\n async ({\\\\n accountId,\\\\n functionName,\\\\n receiptId,\\\\n code,\\\\n schema,\\\\n startFromBlockHeight,\\\\n contractFilter,\\\\n }) => {\\\\n console.log(`Handling ${accountId}/${functionName} indexer...`);\\\\n try {\\\\n await handleIndexerEditTx(\\\\n accountId,\\\\n functionName,\\\\n receiptId,\\\\n code,\\\\n schema,\\\\n blockHeight,\\\\n blockTimestamp,\\\\n startFromBlockHeight,\\\\n contractFilter\\\\n );\\\\n } catch (err) {\\\\n console.log(\\\\n `Error processing receipt at blockHeight: ${blockHeight}: ${err}`\\\\n );\\\\n return err;\\\\n }\\\\n }\\\\n )\\\\n );\\\\n }\\\\n\\\\n // Handle \\\\\\\"remove_indexer_function\\\\\\\" methods\\\\n async function handleIndexerRemoveTx(\\\\n accountId,\\\\n functionName,\\\\n receiptId,\\\\n blockHeight,\\\\n blockTimestamp\\\\n ) {\\\\n const remove_indexer = {\\\\n author_account_id: accountId,\\\\n indexer_name: functionName,\\\\n removed_receipt_id: receiptId,\\\\n removed_timestamp: convertNanosecondsToTimestamp(blockTimestamp),\\\\n removed_block_height: blockHeight,\\\\n is_removed: true,\\\\n };\\\\n\\\\n try {\\\\n const removeResult = await context.db.Indexers.upsert(\\\\n remove_indexer,\\\\n [\\\\\\\"author_account_id\\\\\\\", \\\\\\\"indexer_name\\\\\\\"],\\\\n [\\\\n \\\\\\\"removed_receipt_id\\\\\\\",\\\\n \\\\\\\"removed_timestamp\\\\\\\",\\\\n \\\\\\\"removed_block_height\\\\\\\",\\\\n \\\\\\\"is_removed\\\\\\\",\\\\n ]\\\\n );\\\\n } catch (err) {\\\\n console.log(\\\\n `Failed to remove indexer to the db at blockHeight: ${blockHeight}, ${err}`\\\\n );\\\\n }\\\\n }\\\\n const queryApiRemoveContractTxs = block\\\\n .functionCalls(QUERY_API_MAINNET, \\\\\\\"onlySuccessful\\\\\\\")\\\\n .filter(\\\\n (functionCall) => functionCall.methodName === \\\\\\\"remove_indexer_function\\\\\\\"\\\\n )\\\\n .map((functionCall) => {\\\\n const argsAsJSON = functionCall.argsAsJSON();\\\\n const functionName = argsAsJSON.function_name;\\\\n const accountId = functionCall.action.signerId;\\\\n const receiptId = functionCall.action.receiptId;\\\\n return { accountId, functionName, receiptId };\\\\n });\\\\n\\\\n if (queryApiRemoveContractTxs.length > 0) {\\\\n const blockHeight = block.header().height;\\\\n const blockTimestamp = block.header().timestampNanosec;\\\\n await Promise.all(\\\\n queryApiRemoveContractTxs.map(\\\\n async ({ accountId, functionName, receiptId }) => {\\\\n console.log(`Removing ${accountId}/${functionName} indexer...`);\\\\n try {\\\\n await handleIndexerRemoveTx(\\\\n accountId,\\\\n functionName,\\\\n receiptId,\\\\n blockHeight,\\\\n blockTimestamp\\\\n );\\\\n } catch (err) {\\\\n console.log(\\\\n `Error processing receipt at blockHeight: ${blockHeight}: ${err}`\\\\n );\\\\n return err;\\\\n }\\\\n }\\\\n )\\\\n );\\\\n }\\\\n", |
| 8 | "schema": "CREATE TABLE\\\\n indexers (\\\\n author_account_id TEXT NOT NULL,\\\\n indexer_name TEXT NOT NULL,\\\\n deployment_receipt_id TEXT NOT NULL,\\\\n deployment_timestamp TIMESTAMP NOT NULL,\\\\n deployment_block_height NUMERIC(20) NOT NULL,\\\\n deployment_start_from_block_height NUMERIC(20),\\\\n deployment_contract_filter TEXT,\\\\n last_change_receipt_id TEXT NOT NULL,\\\\n last_change_timestamp TIMESTAMP NOT NULL,\\\\n last_change_block_height NUMERIC(20) NOT NULL,\\\\n last_change_start_from_block_height NUMERIC(20),\\\\n last_change_contract_filter TEXT,\\\\n js_code TEXT NOT NULL,\\\\n sql_code TEXT NOT NULL,\\\\n is_removed BOOLEAN NOT NULL DEFAULT FALSE,\\\\n removed_timestamp TIMESTAMP NOT NULL,\\\\n removed_block_height NUMERIC(20),\\\\n removed_receipt_id TEXT,\\\\n PRIMARY KEY (author_account_id, indexer_name)\\\\n );\\\\n\\\\nCREATE TABLE\\\\n indexer_versions (\\\\n author_account_id TEXT NOT NULL,\\\\n indexer_name TEXT NOT NULL,\\\\n version_receipt_id TEXT NOT NULL,\\\\n version_timestamp TIMESTAMP NOT NULL,\\\\n version_block_height NUMERIC(20) NOT NULL,\\\\n version_start_from_block_height NUMERIC(20),\\\\n version_contract_filter TEXT,\\\\n js_code TEXT NOT NULL,\\\\n sql_code TEXT NOT NULL,\\\\n FOREIGN KEY (author_account_id, indexer_name) REFERENCES indexers (author_account_id, indexer_name),\\\\n PRIMARY KEY (\\\\n author_account_id,\\\\n indexer_name,\\\\n version_receipt_id\\\\n )\\\\n );\\\\n", |