"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TenderizeTokenDefinitionsResolver = exports.TOKEN_QUERY = void 0;
const common_1 = require("@nestjs/common");
const axios_1 = __importDefault(require("axios"));
const graphql_request_1 = require("graphql-request");
const app_toolkit_interface_1 = require("../../../app-toolkit/app-toolkit.interface");
const the_graph_helper_1 = require("../../../app-toolkit/helpers/the-graph.helper");
const cache_decorator_1 = require("../../../cache/cache.decorator");
const network_interface_1 = require("../../../types/network.interface");
exports.TOKEN_QUERY = (0, graphql_request_1.gql) `
  {
    configs {
      id
      lpToken
      tenderToken
      symbol
      steak
      tenderSwap
      tenderFarm
    }
  }
`;
let TenderizeTokenDefinitionsResolver = class TenderizeTokenDefinitionsResolver {
    constructor(appToolkit) {
        this.appToolkit = appToolkit;
    }
    getVaultDefinitionsData(network) {
        return __awaiter(this, void 0, void 0, function* () {
            const data = yield (0, the_graph_helper_1.gqlFetch)({
                endpoint: `https://api.thegraph.com/subgraphs/name/tenderize/tenderize-${network}`,
                query: exports.TOKEN_QUERY,
            });
            return data;
        });
    }
    getTokenDefinitions(network) {
        return __awaiter(this, void 0, void 0, function* () {
            const definitionsData = yield this.getVaultDefinitionsData(network);
            const tokenDefinitions = definitionsData.configs.map(token => {
                return {
                    id: token.id.toLowerCase(),
                    steak: token.steak.toLowerCase(),
                    lpToken: token.lpToken.toLowerCase(),
                    tenderToken: token.tenderToken.toLowerCase(),
                    tenderSwap: token.tenderSwap.toLowerCase(),
                    tenderFarm: token.tenderFarm.toLowerCase(),
                };
            });
            return tokenDefinitions;
        });
    }
    getTokenApyData() {
        return __awaiter(this, void 0, void 0, function* () {
            const { data } = yield axios_1.default.get('https://www.tenderize.me/api/apy');
            return data;
        });
    }
    getTokenApy(id) {
        var _a, _b;
        return __awaiter(this, void 0, void 0, function* () {
            const apyDataRaw = yield this.getTokenApyData();
            const apyData = Object.values(apyDataRaw);
            const apyRaw = (_b = (_a = apyData.find(x => x.subgraphId.toLowerCase() === id)) === null || _a === void 0 ? void 0 : _a.apy) !== null && _b !== void 0 ? _b : 0;
            return Number(apyRaw);
        });
    }
    getRewardRateData(network, tenderFarm) {
        return __awaiter(this, void 0, void 0, function* () {
            return (0, the_graph_helper_1.gqlFetch)({
                endpoint: `https://api.thegraph.com/subgraphs/name/tenderize/tenderize-${network}`,
                variables: { tenderFarm },
                query: (0, graphql_request_1.gql) `
        query ($tenderFarm: String!) {
          rewardsAddedEvents(where: { tenderFarm: $tenderFarm }, first: 2, orderBy: timestamp, orderDirection: desc) {
            amount
            timestamp
          }
        }
      `,
            });
        });
    }
    getRewardRate(network, tenderFarm) {
        return __awaiter(this, void 0, void 0, function* () {
            const { rewardsAddedEvents } = yield this.getRewardRateData(network, tenderFarm.toLowerCase());
            if (rewardsAddedEvents.length != 2)
                return 0;
            const [{ amount, timestamp }, { timestamp: prevTimestamp }] = rewardsAddedEvents;
            return BigInt(amount) / (BigInt(timestamp) - BigInt(prevTimestamp));
        });
    }
};
__decorate([
    (0, cache_decorator_1.Cache)({
        key: network => `studio:tenderize:${network}:token-data`,
        ttl: 5 * 60,
    }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], TenderizeTokenDefinitionsResolver.prototype, "getVaultDefinitionsData", null);
__decorate([
    (0, cache_decorator_1.Cache)({
        key: `studio:tenderize:token-apy-data`,
        ttl: 5 * 60,
    }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], TenderizeTokenDefinitionsResolver.prototype, "getTokenApyData", null);
__decorate([
    (0, cache_decorator_1.Cache)({
        key: (network, tenderFarm) => `studio:tenderize:${network}:${tenderFarm}-reward-data`,
        ttl: 5 * 60,
    }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, String]),
    __metadata("design:returntype", Promise)
], TenderizeTokenDefinitionsResolver.prototype, "getRewardRateData", null);
TenderizeTokenDefinitionsResolver = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, common_1.Inject)(app_toolkit_interface_1.APP_TOOLKIT)),
    __metadata("design:paramtypes", [Object])
], TenderizeTokenDefinitionsResolver);
exports.TenderizeTokenDefinitionsResolver = TenderizeTokenDefinitionsResolver;
//# sourceMappingURL=tenderize.token-definition-resolver.js.map