"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 __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.RedactedEarningsResolver = void 0;
const common_1 = require("@nestjs/common");
const axios_1 = __importDefault(require("axios"));
const ethers_1 = require("ethers");
const app_toolkit_interface_1 = require("../../../app-toolkit/app-toolkit.interface");
const cache_decorator_1 = require("../../../cache/cache.decorator");
const ethers_2 = require("../contracts/ethers");
const BTRFLY_REWARDS = 'https://raw.githubusercontent.com/redacted-cartel/distributions/master/protocol-v2/latest/btrfly.json';
const ETH_REWARDS = 'https://raw.githubusercontent.com/redacted-cartel/distributions/master/protocol-v2/latest/eth.json';
let RedactedEarningsResolver = class RedactedEarningsResolver {
    getClaimableAmount(address, network) {
        return __awaiter(this, void 0, void 0, function* () {
            let btrflyClaimable = ethers_1.BigNumber.from(0);
            let ethClaimable = ethers_1.BigNumber.from(0);
            try {
                const [totalBtrfly, totalEth] = yield this.getTotalRewards(address);
                const [claimedBtrfly, claimedEth] = yield this.getClaimedRewards(address, network);
                btrflyClaimable = totalBtrfly.gt(0) ? totalBtrfly.sub(claimedBtrfly) : ethers_1.BigNumber.from(0);
                ethClaimable = totalEth.gt(0) ? totalEth.sub(claimedEth) : ethers_1.BigNumber.from(0);
            }
            catch (err) {
                console.error(err);
            }
            return [btrflyClaimable, ethClaimable];
        });
    }
    getTotalRewards(address) {
        var _a, _b, _c, _d;
        return __awaiter(this, void 0, void 0, function* () {
            const [bData, eData] = yield this.getRawData();
            const bTotal = (_b = (_a = bData.find(r => r.account.toLowerCase() === address)) === null || _a === void 0 ? void 0 : _a.amount) !== null && _b !== void 0 ? _b : '0';
            const eTotal = (_d = (_c = eData.find(r => r.account.toLowerCase() === address)) === null || _c === void 0 ? void 0 : _c.amount) !== null && _d !== void 0 ? _d : '0';
            const btrfly = ethers_1.BigNumber.from(bTotal);
            const eth = ethers_1.BigNumber.from(eTotal);
            return [btrfly, eth];
        });
    }
    getClaimedRewards(address, network) {
        return __awaiter(this, void 0, void 0, function* () {
            const contract = ethers_2.RedactedRewardDistributor__factory.connect('0xd7807e5752b368a6a64b76828aaff0750522a76e', this.appToolkit.getNetworkProvider(network));
            const btrflyClaimed = yield contract.claimed('0xc55126051b22ebb829d00368f4b12bde432de5da', address);
            const ethClaimed = yield contract.claimed('0xa52fd396891e7a74b641a2cb1a6999fcf56b077e', address);
            return [btrflyClaimed, ethClaimed];
        });
    }
    getRawData() {
        var _a, _b;
        return __awaiter(this, void 0, void 0, function* () {
            const [btrflyResult, ethResult] = yield Promise.allSettled([
                axios_1.default.get(BTRFLY_REWARDS),
                axios_1.default.get(ETH_REWARDS),
            ]);
            const btrflyData = btrflyResult.status === 'fulfilled' ? (_a = btrflyResult.value) === null || _a === void 0 ? void 0 : _a.data : [];
            const ethData = ethResult.status === 'fulfilled' ? (_b = ethResult.value) === null || _b === void 0 ? void 0 : _b.data : [];
            return [btrflyData, ethData];
        });
    }
};
__decorate([
    (0, common_1.Inject)(app_toolkit_interface_1.APP_TOOLKIT),
    __metadata("design:type", Object)
], RedactedEarningsResolver.prototype, "appToolkit", void 0);
__decorate([
    (0, cache_decorator_1.Cache)({
        key: `studio:redacted-cartel:claimable-rewards-raw-data`,
        ttl: 60 * 60 * 24,
    }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], RedactedEarningsResolver.prototype, "getRawData", null);
RedactedEarningsResolver = __decorate([
    (0, common_1.Injectable)()
], RedactedEarningsResolver);
exports.RedactedEarningsResolver = RedactedEarningsResolver;
//# sourceMappingURL=redacted.earnings-resolver.js.map