"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());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNativeAsset = exports.getExplorerLink = exports.getExplorerUrl = exports.networkSetup = exports.NETWORK_SETTINGS = void 0;
var constants_1 = require("../utils/constants");
var address_1 = require("./address");
function idToHex(id) {
    return "0x".concat(parseInt(id, 10).toString(16));
}
exports.NETWORK_SETTINGS = {
    1337: {
        chainId: idToHex('1337'),
        chainName: 'Localhost 8545',
        rpcUrls: ['https://127.0.0.1:8545'],
        nativeCurrency: {
            name: 'Ether',
            symbol: 'ETH',
            decimals: 18,
        },
    },
    10: {
        chainId: idToHex('10'),
        chainName: 'Optimism',
        rpcUrls: ['https://mainnet.optimism.io'],
        blockExplorerUrls: ['https://optimistic.etherscan.io/'],
        nativeCurrency: {
            name: 'Ether',
            symbol: 'ETH',
            decimals: 18,
        },
    },
    1: {
        chainId: idToHex('1'),
        chainName: 'Ethereum Mainnet',
        rpcUrls: ['https://mainnet.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161'],
        blockExplorerUrls: ['https://etherscan.io/'],
        nativeCurrency: {
            name: 'Ether',
            symbol: 'ETH',
            decimals: 18,
        },
    },
    56: {
        chainId: idToHex('56'),
        chainName: 'BSC Mainnet',
        nativeCurrency: {
            name: 'Binance Coin',
            symbol: 'BNB',
            decimals: 18,
        },
        rpcUrls: ['https://bsc-dataseed.binance.org'],
        blockExplorerUrls: ['https://bscscan.com/'],
    },
    128: {
        chainId: idToHex('128'),
        chainName: 'HECO Mainnet',
        nativeCurrency: {
            name: 'Huobi Token',
            symbol: 'HT',
            decimals: 18,
        },
        rpcUrls: ['https://http-mainnet.hecochain.com'],
        blockExplorerUrls: ['https://hecoinfo.com/'],
    },
    43114: {
        chainId: idToHex('43114'),
        chainName: 'Avalanche C-Chain',
        nativeCurrency: {
            name: 'AVAX',
            symbol: 'AVAX',
            decimals: 18,
        },
        rpcUrls: ['https://api.avax.network/ext/bc/C/rpc'],
        blockExplorerUrls: ['https://snowtrace.io/'],
    },
    137: {
        chainId: idToHex('137'),
        chainName: 'Polygon Mainnet',
        nativeCurrency: {
            name: 'MATIC',
            symbol: 'MATIC',
            decimals: 18,
        },
        rpcUrls: ['https://polygon-rpc.com'],
        blockExplorerUrls: ['https://polygonscan.com/'],
    },
    250: {
        chainId: idToHex('250'),
        chainName: 'Fantom Opera',
        nativeCurrency: {
            name: 'FTM',
            symbol: 'FTM',
            decimals: 18,
        },
        rpcUrls: ['https://rpc.ftm.tools'],
        blockExplorerUrls: ['https://ftmscan.com/'],
    },
    1666600000: {
        chainId: idToHex('1666600000'),
        chainName: 'Harmony One',
        nativeCurrency: {
            name: 'ONE',
            symbol: 'ONE',
            decimals: 18,
        },
        rpcUrls: ['https://api.s0.t.hmny.io/'],
        blockExplorerUrls: ['https://explorer.harmony.one/'],
    },
    42161: {
        chainId: idToHex('42161'),
        chainName: 'Arbitrum One',
        nativeCurrency: {
            name: 'ETH',
            symbol: 'ETH',
            decimals: 18,
        },
        rpcUrls: [
            'https://arb1.arbitrum.io/rpc',
            'https://arbitrum.public-rpc.com',
            'https://rpc.ankr.com/arbitrum',
        ],
        blockExplorerUrls: ['https://arbiscan.io/'],
    },
    42220: {
        chainId: idToHex('42220'),
        chainName: 'Celo',
        nativeCurrency: {
            name: 'CELO',
            symbol: 'CELO',
            decimals: 18,
        },
        rpcUrls: ['https://forno.celo.org'],
        blockExplorerUrls: ['https://explorer.celo.org/'],
    },
    1284: {
        chainId: idToHex('1284'),
        chainName: 'Moonbeam',
        nativeCurrency: {
            name: 'GLMR',
            symbol: 'GLMR',
            decimals: 18,
        },
        rpcUrls: ['https://rpc.api.moonbeam.network'],
        blockExplorerUrls: ['https://moonbeam.moonscan.io/'],
    },
    1285: {
        chainId: idToHex('1285'),
        chainName: 'Moonriver',
        nativeCurrency: {
            name: 'Moonriver',
            symbol: 'MOVR',
            decimals: 18,
        },
        rpcUrls: ['https://rpc.moonriver.moonbeam.network'],
        blockExplorerUrls: ['https://moonriver.moonscan.io/'],
    },
    25: {
        chainId: idToHex('25'),
        chainName: 'Cronos',
        nativeCurrency: {
            name: 'CRO',
            symbol: 'CRO',
            decimals: 18,
        },
        rpcUrls: ['https://evm-cronos.crypto.org'],
        blockExplorerUrls: ['https://cronos.crypto.org/explorer/'],
    },
};
var networkSetup = function (chainName, library) { return __awaiter(void 0, void 0, void 0, function () {
    var chainId, settings, _1, err_1, _2, err_2;
    var _a, _b, _c, _d;
    return __generator(this, function (_e) {
        switch (_e.label) {
            case 0:
                chainId = constants_1.CHAIN_IDS[chainName];
                settings = chainId && (exports.NETWORK_SETTINGS === null || exports.NETWORK_SETTINGS === void 0 ? void 0 : exports.NETWORK_SETTINGS[chainId]) && exports.NETWORK_SETTINGS[chainId];
                if (!settings)
                    return [2 /*return*/, "No network settings configured for chainId: '".concat(chainName, "'")];
                if (!(chainId === 1)) return [3 /*break*/, 9];
                _e.label = 1;
            case 1:
                _e.trys.push([1, 3, , 8]);
                return [4 /*yield*/, ((_a = library === null || library === void 0 ? void 0 : library.provider) === null || _a === void 0 ? void 0 : _a.request({
                        method: 'wallet_switchEthereumChain',
                        params: [{ chainId: exports.NETWORK_SETTINGS[chainId].chainId }],
                    }))];
            case 2:
                _e.sent();
                return [2 /*return*/, {}];
            case 3:
                _1 = _e.sent();
                _e.label = 4;
            case 4:
                _e.trys.push([4, 6, , 7]);
                return [4 /*yield*/, ((_b = library === null || library === void 0 ? void 0 : library.provider) === null || _b === void 0 ? void 0 : _b.request({
                        method: 'wallet_switchEthereumChain',
                        params: [{ chainId: exports.NETWORK_SETTINGS[chainId].chainId }],
                    }))];
            case 5:
                _e.sent();
                return [2 /*return*/, {}];
            case 6:
                err_1 = _e.sent();
                console.error(err_1);
                if (err_1 === null || err_1 === void 0 ? void 0 : err_1.message) {
                    return [2 /*return*/, { error: err_1.message }];
                }
                else {
                    return [2 /*return*/, { error: 'An unknown error has occurred.' }];
                }
                return [3 /*break*/, 7];
            case 7: return [3 /*break*/, 8];
            case 8: return [3 /*break*/, 16];
            case 9:
                _e.trys.push([9, 11, , 16]);
                return [4 /*yield*/, ((_c = library === null || library === void 0 ? void 0 : library.provider) === null || _c === void 0 ? void 0 : _c.request({
                        method: 'wallet_switchEthereumChain',
                        params: [{ chainId: exports.NETWORK_SETTINGS[chainId].chainId }],
                    }))];
            case 10:
                _e.sent();
                return [2 /*return*/, { success: "Network succesfully changed to ".concat(chainName) }];
            case 11:
                _2 = _e.sent();
                _e.label = 12;
            case 12:
                _e.trys.push([12, 14, , 15]);
                return [4 /*yield*/, ((_d = library === null || library === void 0 ? void 0 : library.provider) === null || _d === void 0 ? void 0 : _d.request({
                        method: 'wallet_addEthereumChain',
                        params: [exports.NETWORK_SETTINGS[chainId]],
                    }))];
            case 13:
                _e.sent();
                return [2 /*return*/, { success: "Network succesfully changed to ".concat(chainName) }];
            case 14:
                err_2 = _e.sent();
                console.error(err_2);
                if (err_2 === null || err_2 === void 0 ? void 0 : err_2.message) {
                    return [2 /*return*/, { error: err_2.message }];
                }
                else {
                    return [2 /*return*/, { error: 'An unknown error has occurred.' }];
                }
                return [3 /*break*/, 15];
            case 15: return [3 /*break*/, 16];
            case 16: return [2 /*return*/];
        }
    });
}); };
exports.networkSetup = networkSetup;
function getExplorerUrl(chain) {
    var _a, _b;
    if (!chain)
        return;
    var chainId = constants_1.CHAIN_IDS[chain];
    if (!chainId)
        return;
    return (_b = (_a = exports.NETWORK_SETTINGS[chainId]) === null || _a === void 0 ? void 0 : _a.blockExplorerUrls) === null || _b === void 0 ? void 0 : _b[0];
}
exports.getExplorerUrl = getExplorerUrl;
function getExplorerLink(chain, address) {
    if (!chain || !address)
        return;
    var explorer = getExplorerUrl(chain);
    if (!explorer)
        return;
    var isAddr = (0, address_1.isAddress)(address);
    return "".concat(explorer).concat(isAddr ? 'address' : 'tx', "/").concat(address);
}
exports.getExplorerLink = getExplorerLink;
var DEFAULT_NATIVE_ASSET = {
    symbol: 'ETH',
    name: 'Native Token',
    decimals: 18,
};
function getNativeAsset(chain) {
    var _a;
    if (!chain)
        return DEFAULT_NATIVE_ASSET;
    var chainId = constants_1.CHAIN_IDS[chain];
    if (!chainId)
        return DEFAULT_NATIVE_ASSET;
    return ((_a = exports.NETWORK_SETTINGS[chainId]) === null || _a === void 0 ? void 0 : _a.nativeCurrency) || DEFAULT_NATIVE_ASSET;
}
exports.getNativeAsset = getNativeAsset;
//# sourceMappingURL=network.js.map