Frames

Untitled

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