validations.js 3.4 KB

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