"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.ExactlyDepositFetcher = void 0;
const ethers_1 = require("ethers");
const exactly_token_fetcher_1 = require("../common/exactly.token-fetcher");
class ExactlyDepositFetcher extends exactly_token_fetcher_1.ExactlyTokenFetcher {
    constructor() {
        super(...arguments);
        this.groupLabel = 'Variable Deposit';
    }
    getSupply({ definition }) {
        return Promise.resolve(definition.totalFloatingDepositShares);
    }
    getTotalAssets({ definition }) {
        return definition.totalFloatingDepositAssets;
    }
    getApr({ definition: { blockNumber, timestamp, totalFloatingDepositShares, totalFloatingDepositAssets }, multicall: { contract: multicall }, contract: market, }) {
        return __awaiter(this, void 0, void 0, function* () {
            const [, [[, totalSupplyData], [, totalAssetsData], [, tsData]]] = yield multicall.callStatic.aggregate([
                { target: market.address, callData: market.interface.encodeFunctionData('totalSupply') },
                { target: market.address, callData: market.interface.encodeFunctionData('totalAssets') },
                { target: multicall.address, callData: multicall.interface.encodeFunctionData('getCurrentBlockTimestamp') },
            ], true, { blockTag: blockNumber - 123 });
            const [prevTotalSupply] = market.interface.decodeFunctionResult('totalSupply', totalSupplyData);
            const [prevTotalAssets] = market.interface.decodeFunctionResult('totalAssets', totalAssetsData);
            const [prevTimestamp] = multicall.interface.decodeFunctionResult('getCurrentBlockTimestamp', tsData);
            const shareValue = totalFloatingDepositAssets.mul(ethers_1.constants.WeiPerEther).div(totalFloatingDepositShares);
            const prevShareValue = prevTotalAssets.mul(ethers_1.constants.WeiPerEther).div(prevTotalSupply);
            const proportion = shareValue.mul(ethers_1.constants.WeiPerEther).div(prevShareValue);
            return (Number(proportion) / 1e16 - 100) * (31536000 / (timestamp - prevTimestamp.toNumber()));
        });
    }
}
exports.ExactlyDepositFetcher = ExactlyDepositFetcher;
//# sourceMappingURL=exactly.deposit.token-fetcher.js.map