UNPKG

zic-tron-sdk

Version:
163 lines (162 loc) 5.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.findFragmentByName = findFragmentByName; exports.findFunctioFragmentByName = findFunctioFragmentByName; exports.findEventFragmentByName = findEventFragmentByName; exports.percentToBps = percentToBps; exports.bpsToPercent = bpsToPercent; exports.toHex = toHex; exports.fromHex = fromHex; exports.isEmailValid = isEmailValid; exports.areDatesOfSameDay = areDatesOfSameDay; exports.getTokenDecimals = getTokenDecimals; exports.fromTronTransaction = fromTronTransaction; exports.toTronSignedTransaction = toTronSignedTransaction; exports.toTronContractType = toTronContractType; exports.broadcastReturnResponseCodeToName = broadcastReturnResponseCodeToName; const tronweb_1 = require("tronweb"); const artifacts_1 = require("./artifacts"); function findFragmentByName(abi, name, throwIfNotFound = true) { const fragment = abi.find((abi) => "name" in abi && abi.name === name); if (!fragment && throwIfNotFound) { throw new Error("Could not find fragment"); } return fragment; } function findFunctioFragmentByName(abi, name, throwIfNotFound = true) { const fragment = abi.find((abi) => abi.type.toLowerCase() === "function" && "name" in abi && abi.name === name); if (!fragment && throwIfNotFound) { throw new Error("Could not find fragment"); } return fragment; } function findEventFragmentByName(abi, name, throwIfNotFound = true) { const fragment = abi.find((abi) => abi.type.toLowerCase() === "event" && "name" in abi && abi.name === name); if (!fragment && throwIfNotFound) { throw new Error("Could not find fragment"); } return fragment; } /** * Converts percentage value to bps * @param percent * @returns */ function percentToBps(percent) { return (0, tronweb_1.BigNumber)(percent).times(100).toFixed(0); } /** * Coverts bps value to percentage * @param bps * @returns */ function bpsToPercent(bps) { return (0, tronweb_1.BigNumber)(bps).div(100).toFixed(); } function toHex(value) { return tronweb_1.utils.address.toHex(value); } function fromHex(value) { return tronweb_1.utils.address.fromHex(value); } function isEmailValid(value) { return /^[a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,10}$/.test(value); } function areDatesOfSameDay(date1, date2) { return (date1.getUTCDay() === date2.getUTCDay() && date1.getUTCMonth() === date2.getUTCMonth() && date1.getUTCFullYear() === date2.getUTCFullYear()); } const tokenToDecimalMap = new Map([ ["T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb", 6], ["TXYZopYRdj2D9XRtbG411XZZ3kM5VkAeBf", 6], ["TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t", 6], ["TNUC9Qb1rRpS5CbWLmNMxXBjyFoydXjWFR", 6], ["TYsbWxNnyTgsZaTFaue9hqpxkU3Fkco94a", 6], ["TXL6rJbvmjD46zeN1JssfgxvSo99qC8MRT", 18], ]); async function getTokenDecimals(tronWeb, tokenAddress) { const address = fromHex(tokenAddress); if (tokenToDecimalMap.has(address)) { return tokenToDecimalMap.get(address); } const tokenContract = new tronweb_1.Contract(tronWeb, artifacts_1.TRC20_ABI, address); const decimals = await tokenContract.decimals().call(); tokenToDecimalMap.set(address, Number(decimals.toString())); return Number(decimals.toString()); } function fromTronTransaction(from) { return { ...from, raw_data: { ...from.raw_data, contract: from.raw_data.contract.map((c) => ({ ...c, parameter: { ...c.parameter, value: { ...c.parameter.value }, }, })), fee_limit: from.raw_data.fee_limit ? Number(from.raw_data.fee_limit) : 0, }, }; } function toTronSignedTransaction(from) { return { ...from, raw_data: { ...from.raw_data, contract: from.raw_data.contract.map((c) => ({ ...c, parameter: { ...c.parameter, value: { ...c.parameter.value }, }, type: toTronContractType(c.type), })), }, }; } function toTronContractType(type) { // Check if the provided type exists in the ContractType enum if (type in tronweb_1.Types.ContractType) { // Return the corresponding enum value return tronweb_1.Types.ContractType[type]; } else { // Return UNRECOGNIZED if the type is not found return tronweb_1.Types.ContractType.UNRECOGNIZED; } } function broadcastReturnResponseCodeToName(code) { switch (code) { case 0: return "SUCCESS"; case 1: return "SIGERROR"; case 2: return "CONTRACT_VALIDATE_ERROR"; case 3: return "CONTRACT_EXE_ERROR"; case 4: return "BANDWITH_ERROR"; case 5: return "DUP_TRANSACTION_ERROR"; case 6: return "TAPOS_ERROR"; case 7: return "TOO_BIG_TRANSACTION_ERROR"; case 8: return "TRANSACTION_EXPIRATION_ERROR"; case 9: return "SERVER_BUSY"; case 10: return "NO_CONNECTION"; case 11: return "NOT_ENOUGH_EFFECTIVE_CONNECTION"; case 20: return "OTHER_ERROR"; default: return "OTHER_ERROR"; } }