UNPKG

@zebec-network/exchange-card-sdk

Version:
72 lines (71 loc) 2.48 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.hashSHA256 = hashSHA256; exports.parseAlgo = parseAlgo; exports.formatAlgo = formatAlgo; exports.parseAlgorandAsset = parseAlgorandAsset; exports.formatAlgorandAsset = formatAlgorandAsset; exports.getAssetDecimals = getAssetDecimals; const bignumber_js_1 = require("bignumber.js"); const crypto_1 = __importDefault(require("crypto")); function hashSHA256(input) { const hash = crypto_1.default.createHash("sha256"); hash.update(input); const hex = hash.digest("hex"); return hex; } /** * Convert ALGO to microAlgos * @param algos Amount in ALGO * @returns Amount in microAlgos */ function parseAlgo(algos) { return BigInt((0, bignumber_js_1.BigNumber)(algos).times(1_000_000).toFixed(0)); } /** * Convert microAlgos to ALGO * @param microAlgos Amount in microAlgos * @returns Amount in ALGO */ function formatAlgo(microAlgos) { return (0, bignumber_js_1.BigNumber)(microAlgos).div(1_000_000).toFixed(); } /** * Convert Amount to micro-token amount (base units) * @param amount Amount in decimal units * @param decimals Number of decimals for the asset * @returns Amount in micro-token base units */ function parseAlgorandAsset(amount, decimals) { return BigInt((0, bignumber_js_1.BigNumber)(amount).times((0, bignumber_js_1.BigNumber)(10).pow(decimals)).toFixed(0)); } /** * Convert micro-token Amount to Amount * @param microAmount Amount in micro units * @param decimals Number of decimals for the asset * @returns Amount in decimal units */ function formatAlgorandAsset(microAmount, decimals) { return (0, bignumber_js_1.BigNumber)(microAmount).div((0, bignumber_js_1.BigNumber)(10).pow(decimals)).toFixed(); } const ALGORAND_ASSET_DECIMALS_CACHE = new Map(); /** * * @param client Algod Client * @param assetId asset index of Asset * @returns */ async function getAssetDecimals(client, assetId) { // Check if we already have this value cached if (ALGORAND_ASSET_DECIMALS_CACHE.has(assetId)) { return ALGORAND_ASSET_DECIMALS_CACHE.get(assetId); } const assetInfo = await client.getAssetByID(assetId).do(); const decimals = assetInfo.params.decimals; // Cache the result for future use ALGORAND_ASSET_DECIMALS_CACHE.set(assetId, decimals); return decimals; }