validations.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { BigNumber } from 'bignumber.js';
  2. import validator from 'validator';
  3. import { ADDRESS_PREFIX } from './address.js';
  4. import { AbiParamsCommon } from '../types/ABI.js';
  5. import { IBigNumber } from '../types/TronWeb.js';
  6. import type { EventQueryDataType, MapEventQueryDataType } from '../types/UtilsTypes.js';
  7. export function isValidURL(url: string): boolean {
  8. if (typeof url !== 'string') return false;
  9. return validator.isURL(url.toString(), {
  10. protocols: ['http', 'https'],
  11. require_tld: false,
  12. });
  13. }
  14. export function isObject(obj: unknown): obj is Record<number | string | symbol, unknown> {
  15. return obj === Object(obj) && Object.prototype.toString.call(obj) !== '[object Array]';
  16. }
  17. export function isArray(array: unknown): array is unknown[] {
  18. return Array.isArray(array);
  19. }
  20. export function isJson(string: string): boolean {
  21. try {
  22. return !!JSON.parse(string);
  23. } catch (ex) {
  24. return false;
  25. }
  26. }
  27. export function isBoolean(bool: unknown): bool is boolean {
  28. return typeof bool === 'boolean';
  29. }
  30. export function isBigNumber(number: unknown): number is IBigNumber {
  31. return !!number && (number instanceof BigNumber || (number.constructor && number.constructor.name === 'BigNumber'));
  32. }
  33. export function isString(string: unknown): string is string {
  34. return typeof string === 'string' || (!!string && string.constructor && string.constructor.name === 'String');
  35. }
  36. export function isFunction(obj: unknown): obj is (...args: unknown[]) => unknown {
  37. return typeof obj === 'function';
  38. }
  39. export function isHex(string: string): string is string {
  40. return typeof string === 'string' && !isNaN(parseInt(string, 16)) && /^(0x|)[a-fA-F0-9]+$/.test(string);
  41. }
  42. export function isInteger(number: unknown): number is number {
  43. if (number === null) return false;
  44. return Number.isInteger(Number(number));
  45. }
  46. export function hasProperty(obj: object, property: string | number | symbol) {
  47. return Object.prototype.hasOwnProperty.call(obj, property);
  48. }
  49. export function hasProperties(obj: object, ...properties: (string | number | symbol)[]) {
  50. return (
  51. properties.length &&
  52. !properties
  53. .map((property) => {
  54. return hasProperty(obj, property);
  55. })
  56. .includes(false)
  57. );
  58. }
  59. export function mapEvent(event: EventQueryDataType) {
  60. const data: MapEventQueryDataType = {
  61. block: event.block_number,
  62. timestamp: event.block_timestamp,
  63. contract: event.contract_address,
  64. name: event.event_name,
  65. transaction: event.transaction_id,
  66. result: event.result,
  67. resourceNode: event.resource_Node || (event._unconfirmed ? 'fullNode' : 'solidityNode'),
  68. };
  69. if (event._unconfirmed) {
  70. data.unconfirmed = event._unconfirmed;
  71. }
  72. if (event._fingerprint) {
  73. data.fingerprint = event._fingerprint;
  74. }
  75. return data;
  76. }
  77. export function parseEvent(event: EventQueryDataType, { inputs: abi }: { inputs: ReadonlyArray<AbiParamsCommon> }) {
  78. if (!event.result) return event;
  79. if (isObject(event.result)) {
  80. for (let i = 0; i < abi.length; i++) {
  81. const obj = abi[i];
  82. if (obj.type == 'address' && obj.name in event.result)
  83. event.result[obj.name] = ADDRESS_PREFIX + (event.result[obj.name] as string).substr(2).toLowerCase();
  84. }
  85. } else if (isArray(event.result)) {
  86. event.result = (event.result as string[]).reduce<Record<string, string>>((obj, result, index) => {
  87. const { name, type } = abi[index];
  88. if (type == 'address') result = ADDRESS_PREFIX + result.substr(2).toLowerCase();
  89. obj[name] = result;
  90. return obj;
  91. }, {});
  92. }
  93. return event;
  94. }
  95. export function padLeft(input: string | number, padding: string, amount: number): string {
  96. let res = input.toString();
  97. while (res.length < amount) res = padding + res;
  98. return res;
  99. }
  100. export function isNotNullOrUndefined(val: unknown): boolean {
  101. return val !== null && typeof val !== 'undefined';
  102. }
  103. export async function sleep(millis = 1000): Promise<any> {
  104. return new Promise((resolve) => setTimeout(resolve, millis));
  105. }