UNPKG

@martins-finance/venus-js

Version:

A JavaScript SDK for Ethereum and the Venus Protocol.

274 lines 8.62 kB
"use strict"; exports.__esModule = true; exports.getDecimals = exports.toChecksumAddress = exports.getNetNameWithChainId = exports.getAbi = exports.getAssetNameByAddress = exports.getAddress = exports.request = void 0; /** * @file Utility * @desc These methods are helpers for the Venus class. */ var ethers_1 = require("ethers"); var constants_1 = require("./constants"); /* eslint-disable */ var _request; var http; var https; function _nodeJsRequest(options) { return new Promise(function (resolve, reject) { var url = options.url || options.hostname; // Use 'https' if the protocol is not specified in 'options.hostname' if (url.indexOf("http://") !== 0 && url.indexOf("https://") !== 0) { url = "https://" + url; } // Choose the right module based on the protocol in 'options.hostname' var httpOrHttps = url.indexOf("http://") === 0 ? http : https; // Remove the 'http://' so the native node.js module will understand options.hostname = url.split('://')[1]; var body = ''; var req = httpOrHttps.request(options, function (res) { res.on("data", function (bodyBuffer) { body += bodyBuffer.toString(); }); res.on("end", function () { resolve({ status: res.statusCode, statusText: res.statusMessage, body: body }); }); }); req.on('timeout', function () { req.abort(); return reject({ status: 408, statusText: 'Client HTTP request timeout limit reached.' }); }); req.on('error', function (err) { if (req.aborted) return; if (err !== null && err.toString() === '[object Object]') { console.error(JSON.stringify(err)); } else { console.error(err); } return reject(); }); if (options.body) { req.write(JSON.stringify(options.body)); } req.end(); }); } function _webBrowserRequest(options) { return new Promise(function (resolve, reject) { var xhr = new XMLHttpRequest(); var contentTypeIsSet = false; options = options || {}; var method = options.method || "GET"; var url = options.url || options.hostname; url += typeof options.path === "string" ? options.path : ""; if (typeof url !== "string") { return reject("HTTP Request: Invalid URL."); } // Use 'https' if the protocol is not specified in 'options.hostname' if (url.indexOf("http://") !== 0 && url.indexOf("https://") !== 0) { url = "https://" + url; } xhr.open(method, url); for (var header in options.headers) { if ({}.hasOwnProperty.call(options.headers, header)) { var lcHeader = header.toLowerCase(); contentTypeIsSet = lcHeader === "content-type" ? true : contentTypeIsSet; xhr.setRequestHeader(header, options.headers[header]); } } if (!contentTypeIsSet) { xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); } xhr.onload = function () { var body; if (xhr.status >= 100 && xhr.status < 400) { try { JSON.parse(xhr.response); body = xhr.response; } catch (e) { body = xhr.statusText; } return resolve({ status: xhr.status, statusText: xhr.statusText, body: body }); } else { return reject({ status: xhr.status, statusText: xhr.statusText }); } }; if (method !== "GET") { xhr.send(JSON.stringify(options.body)); } else { xhr.send(); } }); } try { window; _request = _webBrowserRequest; } catch (e) { http = require('http'); https = require('https'); _request = _nodeJsRequest; } /** * A generic HTTP request method that works in Node.js and the Web Browser. * * @param {object} options HTTP request options. See Node.js http.request * documentation for details. * * @hidden * * @returns {Promise<object>} Returns a promise and eventually an HTTP response * (JavaScript object). */ function request(options) { return _request.apply(null, [options]); } exports.request = request; /* eslint-enable */ /** * Gets the contract address of the named contract. This method supports * contracts used by the Venus Protocol. * * @param {string} contract The name of the contract. * @param {string} [network] Optional name of the Ethereum network. Main net and * all the popular public test nets are supported. * * @returns {string} Returns the address of the contract. * * @example * ``` * console.log('vBNB Address: ', Venus.util.getAddress(Venus.vBNB)); * ``` */ function getAddress(contract, network) { if (network === void 0) { network = 'mainnet'; } return constants_1.address[network][contract]; } exports.getAddress = getAddress; /** * Gets the contract name using the contract address. This method supports * contracts used by the Venus Protocol. * * @param {string} contractAddress The address of the contract. * @param {string} [network] Optional name of the Ethereum network. Main net and * all the popular public test nets are supported. * * @returns {string} Returns the name of the contract. * * @example * ``` * console.log('Contract at this address: ', Venus.util.getNameByAddress(contractAddress)); * ``` */ function getAssetNameByAddress(contractAddress, network) { if (network === void 0) { network = 'mainnet'; } var name; var assets = constants_1.address[network]; for (var _i = 0, _a = Object.entries(assets); _i < _a.length; _i++) { var _b = _a[_i], assetName = _b[0], assetAddress = _b[1]; if (contractAddress === assetAddress) { name = assetName; } } return name; } exports.getAssetNameByAddress = getAssetNameByAddress; /** * Gets a contract ABI as a JavaScript array. This method supports * contracts used by the Venus Protocol. * * @param {string} contract The name of the contract. * * @returns {Array} Returns the ABI of the contract as a JavaScript array. * * @example * ``` * console.log('vBNB ABI: ', Venus.util.getAbi(Venus.vBNB)); * ``` */ function getAbi(contract) { return constants_1.abi[contract]; } exports.getAbi = getAbi; /** * Gets the name of an Ethereum network based on its chain ID. * * @param {string} chainId The chain ID of the network. * * @returns {string} Returns the name of the Ethereum network. * * @example * ``` * console.log('bnbt : ', Venus.util.getNetNameWithChainId(97)); * ``` */ function getNetNameWithChainId(chainId) { var networks = { 1: 'mainnet', 3: 'ropsten', 4: 'rinkeby', 5: 'goerli', 42: 'kovan', 56: 'mainnet', 97: 'bnbt' }; return networks[chainId]; } exports.getNetNameWithChainId = getNetNameWithChainId; /** * Applies the EIP-55 checksum to an Ethereum address. * * @param {string} _address The Ethereum address to apply the checksum. * * @returns {string} Returns a string of the Ethereum address. */ function toChecksumAddress(address) { var chars = address.toLowerCase().substring(2).split(''); var expanded = new Uint8Array(40); for (var i = 0; i < 40; i++) { expanded[i] = chars[i].charCodeAt(0); } var hash = ethers_1.ethers.utils.keccak256; (expanded); var ret = ''; for (var i = 0; i < address.length; i++) { if (parseInt(hash[i], 16) >= 8) { ret += address[i].toUpperCase(); } else { ret += address[i]; } } return ret; } exports.toChecksumAddress = toChecksumAddress; /** * Gets the number of decimals of a token * * @param {string} token The name of the token * * @returns {number} Returns the number of decimals used in the token */ function getDecimals(token) { return constants_1.decimals[token]; } exports.getDecimals = getDecimals; //# sourceMappingURL=util.js.map