"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SVMRouter = void 0;
const express_1 = require("express");
const logger_1 = __importDefault(require("../../../../logger"));
class SVMRouter {
    constructor(csp, chain, _params) {
        this.csp = csp;
        this.chain = chain?.toUpperCase();
        this.router = (0, express_1.Router)();
        this.setDefaultRoutes(this.router);
    }
    ;
    getRouter() {
        return this.router;
    }
    ;
    setDefaultRoutes(router) {
        this.estimateTxFee(router);
        this.getPriorityFee(router);
        this.getRentMinimum(router);
        this.getTokenAccountAddresses(router);
        this.getTokenInfo(router);
        this.decodeRawTransaction(router);
    }
    ;
    estimateTxFee(router) {
        router.post(`/api/${this.chain}/:network/txFee`, async (req, res) => {
            const { rawTx } = req.body;
            const { network } = req.params;
            try {
                res.json(await this.csp.getFee({ network, rawTx }));
            }
            catch (err) {
                if (err?.code != null) {
                    res.status(400).send(err.message);
                }
                else {
                    logger_1.default.error('Fee Error: %o', err.stack || err.message || err);
                    res.status(500).send(err.message || err);
                }
            }
        });
    }
    ;
    getPriorityFee(router) {
        router.get(`/api/${this.chain}/:network/priorityFee/:percentile`, async (req, res) => {
            let { network } = req.params;
            const { percentile } = req.params;
            const priorityFeePercentile = Number(percentile) || 15;
            network = network.toLowerCase();
            try {
                const fee = await this.csp.getPriorityFee({ chain: this.chain, network, percentile: priorityFeePercentile });
                if (!fee) {
                    return res.status(404).send('not available right now');
                }
                return res.json(fee);
            }
            catch (err) {
                logger_1.default.error('Fee Error: %o', err.stack || err.message || err);
                return res.status(500).send('Error getting priority fee from RPC');
            }
        });
    }
    ;
    getRentMinimum(router) {
        router.get(`/api/${this.chain}/:network/rent/:space`, async (req, res) => {
            const { network, space } = req.params;
            try {
                const minimum = await this.csp.getRentExemptionAmount({ network, space });
                res.json(Number(minimum));
            }
            catch (err) {
                if (err?.code != null) {
                    res.status(400).send(err.message);
                }
                else {
                    logger_1.default.error('Rent Error: %o', err.stack || err.message || err);
                    res.status(500).send(err.message || err);
                }
            }
        });
    }
    ;
    getTokenAccountAddresses(router) {
        router.get(`/api/${this.chain}/:network/ata/:address`, async (req, res) => {
            const { network, address } = req.params;
            try {
                const addresses = await this.csp.getTokenAccountAddresses({ network, address });
                res.json(addresses);
            }
            catch (err) {
                if (err?.code != null) {
                    res.status(400).send(err.message);
                }
                else {
                    logger_1.default.error('Token Account Error: %o', err.stack || err.message || err);
                    res.status(500).send(err.message || err);
                }
            }
        });
    }
    ;
    getTokenInfo(router) {
        router.get(`/api/${this.chain}/:network/token/:tokenAddress`, async (req, res) => {
            const { network, tokenAddress } = req.params;
            try {
                const tokenInfo = await this.csp.getSPLTokenInfo(network, tokenAddress);
                res.json(tokenInfo);
            }
            catch (err) {
                logger_1.default.error('Token Info Error: %o', err.stack || err.message || err);
                res.status(500).send(err.message || err);
            }
        });
    }
    ;
    decodeRawTransaction(router) {
        router.post(`/api/${this.chain}/:network/decode`, async (req, res) => {
            const { rawTx } = req.body;
            const { network } = req.params;
            try {
                this.csp.validateRawTx(rawTx);
            }
            catch (err) {
                logger_1.default.error('Transaction Decode Error: %o', err.stack || err.message || err);
                res.status(400).send(err.message || err);
                return;
            }
            try {
                const decodedTx = await this.csp.decodeRawTransaction({ network, rawTx });
                res.json(decodedTx);
            }
            catch (err) {
                logger_1.default.error('Transaction Decode Error: %o', err.stack || err.message || err);
                res.status(500).send(err.message || err);
            }
        });
    }
    ;
}
exports.SVMRouter = SVMRouter;
//# sourceMappingURL=routes.js.map