"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SubgraphMixin = void 0;
const graphql_request_1 = require("graphql-request");
const base_core_sdk_1 = require("./../mixins/base-core-sdk");
const DELAYS_PER_CHAINID = {
    1: {
        BLOCK_DELAY: 12000,
        MAX_SAME_BLOCK: 20,
        MAX_SAME_BLOCK_DELAY: 30000,
        DEFAULT_TIMEOUT: 60000
    },
    137: {
        BLOCK_DELAY: 1000,
        MAX_SAME_BLOCK: 20,
        MAX_SAME_BLOCK_DELAY: 5000,
        DEFAULT_TIMEOUT: 30000
    },
    80002: {
        BLOCK_DELAY: 1000,
        MAX_SAME_BLOCK: 20,
        MAX_SAME_BLOCK_DELAY: 5000,
        DEFAULT_TIMEOUT: 30000
    },
    8453: {
        BLOCK_DELAY: 1000,
        MAX_SAME_BLOCK: 20,
        MAX_SAME_BLOCK_DELAY: 5000,
        DEFAULT_TIMEOUT: 30000
    },
    10: {
        BLOCK_DELAY: 1000,
        MAX_SAME_BLOCK: 20,
        MAX_SAME_BLOCK_DELAY: 5000,
        DEFAULT_TIMEOUT: 30000
    },
    84532: {
        BLOCK_DELAY: 1000,
        MAX_SAME_BLOCK: 20,
        MAX_SAME_BLOCK_DELAY: 5000,
        DEFAULT_TIMEOUT: 30000
    },
    42161: {
        BLOCK_DELAY: 1000,
        MAX_SAME_BLOCK: 20,
        MAX_SAME_BLOCK_DELAY: 5000,
        DEFAULT_TIMEOUT: 30000
    },
    421614: {
        BLOCK_DELAY: 1000,
        MAX_SAME_BLOCK: 20,
        MAX_SAME_BLOCK_DELAY: 5000,
        DEFAULT_TIMEOUT: 30000
    },
    11155111: {
        BLOCK_DELAY: 12000,
        MAX_SAME_BLOCK: 20,
        MAX_SAME_BLOCK_DELAY: 30000,
        DEFAULT_TIMEOUT: 60000
    },
    11155420: {
        BLOCK_DELAY: 12000,
        MAX_SAME_BLOCK: 20,
        MAX_SAME_BLOCK_DELAY: 30000,
        DEFAULT_TIMEOUT: 60000
    },
    31337: {
        BLOCK_DELAY: 2000,
        MAX_SAME_BLOCK: 20,
        MAX_SAME_BLOCK_DELAY: 1000,
        DEFAULT_TIMEOUT: 3000
    }
};
class SubgraphMixin extends base_core_sdk_1.BaseCoreSDK {
    waitForGraphNodeIndexing(blockNumberOrTransaction) {
        return __awaiter(this, void 0, void 0, function* () {
            const delays = DELAYS_PER_CHAINID[this._chainId];
            let blockToWaitFor = 0;
            if (typeof blockNumberOrTransaction === "number") {
                blockToWaitFor = blockNumberOrTransaction;
            }
            else if (blockNumberOrTransaction === null || blockNumberOrTransaction === void 0 ? void 0 : blockNumberOrTransaction["blockNumber"]) {
                blockToWaitFor = blockNumberOrTransaction
                    .blockNumber;
            }
            else if (blockNumberOrTransaction === null || blockNumberOrTransaction === void 0 ? void 0 : blockNumberOrTransaction["wait"]) {
                const txReceipt = yield blockNumberOrTransaction.wait();
                blockToWaitFor = txReceipt.blockNumber;
            }
            if (blockToWaitFor > 0) {
                let currentBlock = yield this.getSubgraphBlockNumber();
                let oldCurrentBlock = currentBlock;
                let sameBlockNumber = 0;
                while (currentBlock < blockToWaitFor) {
                    yield this.wait(delays.BLOCK_DELAY);
                    currentBlock = yield this.getSubgraphBlockNumber();
                    if (currentBlock === oldCurrentBlock) {
                        console.log("sameBlockNumber", sameBlockNumber);
                        if (sameBlockNumber++ >= delays.MAX_SAME_BLOCK) {
                            // Seems that the subgraph does not update its current block
                            console.error(`Seems that the subgraph does not update its current block after ${currentBlock}`);
                            yield this.wait(delays.MAX_SAME_BLOCK_DELAY);
                            return;
                        }
                    }
                    else {
                        oldCurrentBlock = currentBlock;
                        sameBlockNumber = 0;
                    }
                }
                return;
            }
            yield this.wait(delays.DEFAULT_TIMEOUT);
        });
    }
    getSubgraphBlockNumber() {
        return __awaiter(this, void 0, void 0, function* () {
            var _a, _b;
            const client = new graphql_request_1.GraphQLClient(this._subgraphUrl);
            const response = yield client.request((0, graphql_request_1.gql) `
      query MyQuery {
        _meta {
          block {
            number
          }
        }
      }
    `);
            return ((_b = (_a = response === null || response === void 0 ? void 0 : response._meta) === null || _a === void 0 ? void 0 : _a.block) === null || _b === void 0 ? void 0 : _b.number) || 0;
        });
    }
    wait(ms) {
        return __awaiter(this, void 0, void 0, function* () {
            return new Promise((resolve) => {
                setTimeout(resolve, ms);
            });
        });
    }
}
exports.SubgraphMixin = SubgraphMixin;
//# sourceMappingURL=mixin.js.map