"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 __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.BeanstalkBalanceResolver = exports.GET_DEPOSIT = void 0;
const common_1 = require("@nestjs/common");
const graphql_request_1 = require("graphql-request");
const lodash_1 = require("lodash");
const the_graph_helper_1 = require("../../../app-toolkit/helpers/the-graph.helper");
exports.GET_DEPOSIT = (0, graphql_request_1.gql) `
  query getSiloBalance($token: String, $account: ID!) {
    farmer(id: $account) {
      deposits(where: { token: $token, amount_gt: 0 }) {
        amount
      }
      withdraws(where: { token: $token, claimed: false }) {
        amount
      }
    }
  }
`;
let BeanstalkBalanceResolver = class BeanstalkBalanceResolver {
    getSiloBalancesData(address, tokenAddress) {
        return __awaiter(this, void 0, void 0, function* () {
            const endpoint = `https://graph.node.bean.money/subgraphs/name/beanstalk`;
            const data = yield (0, the_graph_helper_1.gqlFetch)({
                endpoint,
                query: exports.GET_DEPOSIT,
                variables: {
                    token: tokenAddress,
                    account: address,
                },
            });
            return data.farmer;
        });
    }
    getSiloBalances(address, token) {
        return __awaiter(this, void 0, void 0, function* () {
            const siloBalancesData = yield this.getSiloBalancesData(address, token);
            const depositBalances = (0, lodash_1.sumBy)(siloBalancesData === null || siloBalancesData === void 0 ? void 0 : siloBalancesData.deposits, t => Number(t.amount));
            const withdrawnBalances = (0, lodash_1.sumBy)(siloBalancesData === null || siloBalancesData === void 0 ? void 0 : siloBalancesData.withdraws, t => Number(t.amount));
            return depositBalances + withdrawnBalances;
        });
    }
};
BeanstalkBalanceResolver = __decorate([
    (0, common_1.Injectable)()
], BeanstalkBalanceResolver);
exports.BeanstalkBalanceResolver = BeanstalkBalanceResolver;
//# sourceMappingURL=beanstalk.balance-resolver.js.map