dracoder-web3-package 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,431 @@
1
+ # Rocketchain-package.
2
+
3
+ ## Summary
4
+
5
+ _Package used for web3 provider integration between client side and API._
6
+
7
+ A repository to integrate required interaction between platform user (on client app) and blockchain actions signatures.
8
+ Then the info generated and logs are sent to the related API endpoints.
9
+
10
+ ## Index
11
+
12
+ * Installation
13
+ * Specification
14
+
15
+ ## Installation
16
+
17
+ ```
18
+ npm install rocketchain-package
19
+ ```
20
+
21
+ #### Dependencies used:
22
+
23
+ Axios, web3
24
+
25
+ ## Specification
26
+
27
+ ### Function: setClientServerUrl
28
+
29
+ Sets the client server url received. setClientServerUrl(param1)
30
+
31
+ #### Parameters:
32
+
33
+ param1: client server url address
34
+
35
+ #### Example:
36
+
37
+ ```
38
+ setClientServerUrl("http://localhost:5000")
39
+ ```
40
+
41
+ #### Return:
42
+
43
+ No return.
44
+
45
+ ### Function: metamaskConnect
46
+
47
+ Checks if client app user is logged in with Metamask (to be able to receive transaction signature requests) and returns
48
+ a promise with the user wallet address. If not, Metamask prompts asking to log in.
49
+
50
+ #### Parameters:
51
+
52
+ No parameters
53
+
54
+ #### Example:
55
+
56
+ ```
57
+ const someFunctionThatNeedsTheUserWalletAddress = async() => {
58
+ const walletAddress = await metamaskConnect()
59
+ ...
60
+ }
61
+ ```
62
+
63
+ #### Return:
64
+
65
+ Returns a promise.
66
+
67
+ Success:
68
+
69
+ ```
70
+ console.log(walletAddress)
71
+ 0xECf8C3205B230a39a71354214561dAA83433Ae3B
72
+ ```
73
+
74
+ Failure - string with error description if fails (depending on where the problem was)
75
+
76
+ ```
77
+ console.log(connect)
78
+ "There is no account to connect to"
79
+ ```
80
+
81
+ ```
82
+ console.log(connect)
83
+ "There was a problem connecting to your Metamask wallet. Do you have Metamask installed?"
84
+ ```
85
+
86
+ ### Function: clientTokenTransfer
87
+
88
+ Transfer -that must be signed- of some amount of token from the user to another receiver
89
+
90
+ clientTokenTransfer(param1, param2, param3)
91
+
92
+ #### Parameters:
93
+
94
+ * param1: address receiver of the tokens
95
+ * param2: address of the token contract that will be transferred
96
+ * param3: amount of token to be transferred
97
+
98
+ #### Example:
99
+
100
+ ```
101
+ tokenTransfer(0xe4513a7BCd98dE50Fe7984C82dAf5611beBa7170, 0xa6c99e129cecf74865f6ed847a62c63cd6e0c045, 17)
102
+ ```
103
+
104
+ #### Return:
105
+
106
+ Returns a promise:
107
+
108
+ Success: transaction object from Blockchain
109
+
110
+ ```
111
+ {
112
+ "blockHash": "0x59ae5fc6af98787806d2733e97bc749e057ef0bf6d1c85d2f42f3eed92b9cba1",
113
+ "blockNumber": 9245548,
114
+ "contractAddress": null,
115
+ "cumulativeGasUsed": 8907523,
116
+ "effectiveGasPrice": "0x3b9aca0e",
117
+ "from": "0x212f260404a05e954bc9285bfae89d6b2bb45134",
118
+ "gasUsed": 34494,
119
+ "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000008000000000000010000008000000000000000000000000000000000040000000000000000000000000000000000000010000000000000000000000100000000004000000000004000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000",
120
+ "status": true,
121
+ "to": "0xa6c99e129cecf74865f6ed847a62c63cd6e0c045",
122
+ "transactionHash": "0xe8992e16afc720381390cb54f1bb3a6a0e3a30fc143797dced38417b2a02f99e",
123
+ "transactionIndex": 37,
124
+ "type": "0x2",
125
+ "events": {
126
+ "Transfer": {
127
+ "address": "0xa6C99E129cecF74865F6eD847a62c63cD6E0C045",
128
+ "blockHash": "0x59ae5fc6af98787806d2733e97bc749e057ef0bf6d1c85d2f42f3eed92b9cba1",
129
+ "blockNumber": 9245548,
130
+ "logIndex": 92,
131
+ "removed": false,
132
+ "transactionHash": "0xe8992e16afc720381390cb54f1bb3a6a0e3a30fc143797dced38417b2a02f99e",
133
+ "transactionIndex": 37,
134
+ "id": "log_696ad3a5",
135
+ "returnValues": {
136
+ "0": "0x212f260404A05E954BC9285BFAe89D6B2bB45134",
137
+ "1": "0xe4513a7BCd98dE50Fe7984C82dAf5611beBa7170",
138
+ "2": "17",
139
+ "from": "0x212f260404A05E954BC9285BFAe89D6B2bB45134",
140
+ "to": "0xe4513a7BCd98dE50Fe7984C82dAf5611beBa7170",
141
+ "value": "17"
142
+ },
143
+ "event": "Transfer",
144
+ "signature": "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
145
+ "raw": {
146
+ "data": "0x0000000000000000000000000000000000000000000000000000000000000011",
147
+ "topics": [
148
+ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
149
+ "0x000000000000000000000000212f260404a05e954bc9285bfae89d6b2bb45134",
150
+ "0x000000000000000000000000e4513a7bcd98de50fe7984c82daf5611beba7170"
151
+ ]
152
+ }
153
+ }
154
+ }
155
+ }
156
+ ```
157
+
158
+ Error: The log depends on where the error was produced
159
+
160
+ ### Function: tokenCreate
161
+
162
+ Creates a token using given token parameters and the user wallet address to relate the request
163
+
164
+ tokenCreate(param1, param2, param3, param4, param5)
165
+
166
+ #### Parameters:
167
+
168
+ * param1: Name given to the token by the user
169
+ * param2: total amount of tokens that will be created
170
+ * param3: Symbol of the token given by the user
171
+ * param4: Decimals of the token
172
+ * param5: wallet address of the user that requested the token creation
173
+
174
+ #### Example:
175
+
176
+ ```
177
+ const RocketchainPackage = require('rocketchain-package');
178
+ const someFunction = async () => {
179
+ const createdToken = await RocketchainPackage.tokenCreate(
180
+ "Token Doc",
181
+ "44",
182
+ "TkD",
183
+ "0",
184
+ "0xe4513a7BCd98dE50Fe7984C82dAf5611beBa7170",
185
+ );
186
+ }
187
+ ```
188
+
189
+ #### Return:
190
+
191
+ Returns a promise.
192
+
193
+ Database object with all the creation Blockchain data related
194
+
195
+ ```
196
+ console.log(createdToken);
197
+ {
198
+ "data": {
199
+ "is_transferred": false,
200
+ "_id": "613628fcf7a6782270588ee1",
201
+ "token_address": "0x85398185A1CBE64D7f8Bf4aD668AB7352A93F589",
202
+ "wallet_address": "0xe4513a7BCd98dE50Fe7984C82dAf5611beBa7170",
203
+ "amount": "44",
204
+ "log_id": "613628fcf7a6782270588edf",
205
+ "created_at": "2021-09-06T12:42:55.000Z",
206
+ "__v": 0
207
+ },
208
+ "status": 200,
209
+ "statusText": "OK",
210
+ "headers": {
211
+ "cache-control": "no-cache, private",
212
+ "content-length": "278",
213
+ "content-type": "text/html; charset=UTF-8"
214
+ },
215
+ "config": {
216
+ "url": "http://rocketchain-server.test/api/token_creation_response",
217
+ "method": "post",
218
+ "data": "{\"nameOfToken\":\"Token Doc\",\"numberOfToken\":\"44\",\"symbolOfToken\":\"TkD\",\"numberOfDecimals\":\"0\",\"userWalletAddress\":\"0xe4513a7BCd98dE50Fe7984C82dAf5611beBa7170\"}",
219
+ "headers": {
220
+ "Accept": "application/json, text/plain, */*",
221
+ "Content-Type": "application/json"
222
+ },
223
+ "transformRequest": [
224
+ null
225
+ ],
226
+ "transformResponse": [
227
+ null
228
+ ],
229
+ "timeout": 0,
230
+ "xsrfCookieName": "XSRF-TOKEN",
231
+ "xsrfHeaderName": "X-XSRF-TOKEN",
232
+ "maxContentLength": -1,
233
+ "maxBodyLength": -1,
234
+ "transitional": {
235
+ "silentJSONParsing": true,
236
+ "forcedJSONParsing": true,
237
+ "clarifyTimeoutError": false
238
+ }
239
+ },
240
+ "request": {}
241
+ }
242
+ ```
243
+
244
+ ### Function: tokenTransferWithTokenAddressOnly
245
+
246
+ Transfer linked with the token creation (as a second step) process
247
+
248
+ tokenTransferWithTokenAddressOnly(param1)
249
+
250
+ #### Parameters:
251
+
252
+ param1: token smart contract address.
253
+
254
+ (Retrieved from tokenCreate method response => data.token_address)
255
+
256
+ #### Example:
257
+
258
+ ```
259
+ const RocketchainPackage = require('rocketchain-package');
260
+ const someFunction = async data => {
261
+ const tokenTransferred = await RocketchainPackage.tokenTransferWithTokenAddressOnly("0x85398185A1CBE64D7f8Bf4aD668AB7352A93F589");
262
+ };
263
+ ```
264
+
265
+ #### Return:
266
+
267
+ Returns a promise.
268
+
269
+ Database object with all the Blockchain transfer data related
270
+
271
+ ```
272
+ console.log(tokenTransferred);
273
+ {
274
+ "blockHash": "0xd5da8d878e62ec7868e80d0df0676fe9f52a21af4a1277c40ab2cb1b7d092b95",
275
+ "blockNumber": 9245705,
276
+ "contractAddress": null,
277
+ "cumulativeGasUsed": 3396087,
278
+ "effectiveGasPrice": "0x3b9aca0d",
279
+ "from": "0x212f260404a05e954bc9285bfae89d6b2bb45134",
280
+ "gasUsed": 46794,
281
+ "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000010004008000000000000000000000000000000000040000000000000000000000000000000000000010000000000000000000000100000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000002000000000000000000000000000000000000010000002000000000000000000000000000000000000000000000000000000000000000000000000000",
282
+ "status": true,
283
+ "to": "0x85398185a1cbe64d7f8bf4ad668ab7352a93f589",
284
+ "transactionHash": "0x6cea70d5436e706a659ff339fc7b012576eafc6b28bcf9c93355fd19ad2a800b",
285
+ "transactionIndex": 24,
286
+ "type": "0x0",
287
+ "events": {
288
+ "Transfer": {
289
+ "address": "0x85398185A1CBE64D7f8Bf4aD668AB7352A93F589",
290
+ "blockHash": "0xd5da8d878e62ec7868e80d0df0676fe9f52a21af4a1277c40ab2cb1b7d092b95",
291
+ "blockNumber": 9245705,
292
+ "logIndex": 30,
293
+ "removed": false,
294
+ "transactionHash": "0x6cea70d5436e706a659ff339fc7b012576eafc6b28bcf9c93355fd19ad2a800b",
295
+ "transactionIndex": 24,
296
+ "id": "log_0dd77e89",
297
+ "returnValues": {
298
+ "0": "0x212f260404A05E954BC9285BFAe89D6B2bB45134",
299
+ "1": "0xe4513a7BCd98dE50Fe7984C82dAf5611beBa7170",
300
+ "2": "44",
301
+ "from": "0x212f260404A05E954BC9285BFAe89D6B2bB45134",
302
+ "to": "0xe4513a7BCd98dE50Fe7984C82dAf5611beBa7170",
303
+ "value": "44"
304
+ },
305
+ "event": "Transfer",
306
+ "signature": "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
307
+ "raw": {
308
+ "data": "0x000000000000000000000000000000000000000000000000000000000000002c",
309
+ "topics": [
310
+ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
311
+ "0x000000000000000000000000212f260404a05e954bc9285bfae89d6b2bb45134",
312
+ "0x000000000000000000000000e4513a7bcd98de50fe7984c82daf5611beba7170"
313
+ ]
314
+ }
315
+ }
316
+ }
317
+ }
318
+ ```
319
+
320
+ ### Function: getTokenOwnersBalance
321
+
322
+ Retrieves all the accounts that have owned (0 balance now) or currently own (positive balance) the token specified by
323
+ the token contract address.
324
+
325
+ getTokenOwnersBalance(param1)
326
+
327
+ #### Parameters:
328
+
329
+ param1: smart contract token address.
330
+
331
+ #### Example:
332
+
333
+ ```
334
+ const RocketchainPackage = require('rocketchain-package');
335
+ const someFunction = async () => {
336
+ const ownersArray = await RocketchainPackage.getTokenOwnersBalance(0xa6c99e129cecf74865f6ed847a62c63cd6e0c045);
337
+ };
338
+ ```
339
+
340
+ #### Return:
341
+
342
+ Returns a promise.
343
+
344
+ Array of objects. Each object represents a pair of owner-balance values.
345
+
346
+ ```
347
+ console.log(ownersArray);
348
+ [{"owner":"0xe4513a7BCd98dE50Fe7984C82dAf5611beBa7170","balance":"41"},{"owner":"0x212f260404A05E954BC9285BFAe89D6B2bB45134","balance":"5959"}]
349
+ ```
350
+
351
+ ### Function: isMetamaskInstalled
352
+
353
+ Checks if Metamask is installed in order to proceed with the rest of functionalities
354
+
355
+ isMetamaskInstalled()
356
+
357
+ #### Parameters:
358
+
359
+ No parameters required.
360
+
361
+ #### Example:
362
+
363
+ ```
364
+ const RocketchainPackage = require('rocketchain-package');
365
+ const someFunction = async () => {
366
+ const checkMetamask = await RocketchainPackage.isMetamaskInstalled();
367
+ };
368
+ ```
369
+
370
+ #### Return:
371
+
372
+ Returns a response depending on Metamask is/is not installed.
373
+
374
+ ```
375
+ { ok: false, message: "You don't have Metamask installed." }
376
+ ```
377
+
378
+ ### Function: checkWalletFormat
379
+
380
+ Checks if the wallet provided has the proper structure and format.
381
+
382
+ checkWalletFormat(param1)
383
+
384
+ #### Parameters:
385
+
386
+ param1: wallet address.
387
+
388
+ #### Example:
389
+
390
+ ```
391
+ const RocketchainPackage = require('rocketchain-package');
392
+ const someFunction = async () => {
393
+ const formatVerification = await RocketchainPackage.checkWalletFormat(0xLZf8C3205B230a39a71354214561dAA83433Ae4K);
394
+ };
395
+ ```
396
+
397
+ #### Return
398
+
399
+ Returns a response depending on whether the data provided has the right format or not.
400
+
401
+ ```
402
+ { ok: true, message: "Proper address format" }
403
+ ```
404
+
405
+ ### Function: selectOrAddPolygonMainNetwork
406
+
407
+ Selects or adds the Polygon Matic Mainnet to the Metamask wallet.
408
+
409
+ selectOrAddPolygonMainNetwork()
410
+
411
+ ### Parameters:
412
+
413
+ No parameters required.
414
+
415
+ ### Example:
416
+
417
+ ```
418
+ const RocketchainPackage = require('rocketchain-package');
419
+ const someFunction = async () => {
420
+ const selectOrAddPolygonMainNetwork = await RocketchainPackage.selectOrAddPolygonMainNetwork();
421
+ };
422
+ ```
423
+
424
+ ### Return
425
+
426
+ Returns a response depending on whether the Polygon main network was previously added or needs to be added.
427
+
428
+ ```
429
+ { ok: true, message: "Matic Mainnet added successfully", response: ... }
430
+ ```
431
+
@@ -0,0 +1,422 @@
1
+ 'use strict'
2
+
3
+ const factoryAddress = '0x5d34107B7dbCf7793c170140541DC5502f038Ee9';
4
+ const abiFactory = [
5
+ {
6
+ "anonymous": false,
7
+ "inputs": [
8
+ {
9
+ "indexed": false,
10
+ "internalType": "address",
11
+ "name": "tokenAddress",
12
+ "type": "address"
13
+ }
14
+ ],
15
+ "name": "TokenCreated",
16
+ "type": "event"
17
+ },
18
+ {
19
+ "constant": false,
20
+ "inputs": [
21
+ {
22
+ "internalType": "string",
23
+ "name": "name",
24
+ "type": "string"
25
+ },
26
+ {
27
+ "internalType": "string",
28
+ "name": "symbol",
29
+ "type": "string"
30
+ },
31
+ {
32
+ "internalType": "uint8",
33
+ "name": "decimals",
34
+ "type": "uint8"
35
+ },
36
+ {
37
+ "internalType": "uint256",
38
+ "name": "totalSupply",
39
+ "type": "uint256"
40
+ },
41
+ {
42
+ "internalType": "address",
43
+ "name": "issuer",
44
+ "type": "address"
45
+ }
46
+ ],
47
+ "name": "deployNewToken",
48
+ "outputs": [
49
+ {
50
+ "internalType": "address",
51
+ "name": "",
52
+ "type": "address"
53
+ }
54
+ ],
55
+ "payable": false,
56
+ "stateMutability": "nonpayable",
57
+ "type": "function"
58
+ }
59
+ ];
60
+ const abiToken = [
61
+ {
62
+ "inputs": [
63
+ {
64
+ "internalType": "string",
65
+ "name": "_name",
66
+ "type": "string"
67
+ },
68
+ {
69
+ "internalType": "string",
70
+ "name": "_symbol",
71
+ "type": "string"
72
+ },
73
+ {
74
+ "internalType": "uint8",
75
+ "name": "_decimals",
76
+ "type": "uint8"
77
+ },
78
+ {
79
+ "internalType": "uint256",
80
+ "name": "totalSupply",
81
+ "type": "uint256"
82
+ },
83
+ {
84
+ "internalType": "address",
85
+ "name": "issuer",
86
+ "type": "address"
87
+ }
88
+ ],
89
+ "payable": false,
90
+ "stateMutability": "nonpayable",
91
+ "type": "constructor"
92
+ },
93
+ {
94
+ "anonymous": false,
95
+ "inputs": [
96
+ {
97
+ "indexed": true,
98
+ "internalType": "address",
99
+ "name": "owner",
100
+ "type": "address"
101
+ },
102
+ {
103
+ "indexed": true,
104
+ "internalType": "address",
105
+ "name": "spender",
106
+ "type": "address"
107
+ },
108
+ {
109
+ "indexed": false,
110
+ "internalType": "uint256",
111
+ "name": "value",
112
+ "type": "uint256"
113
+ }
114
+ ],
115
+ "name": "Approval",
116
+ "type": "event"
117
+ },
118
+ {
119
+ "anonymous": false,
120
+ "inputs": [
121
+ {
122
+ "indexed": true,
123
+ "internalType": "address",
124
+ "name": "from",
125
+ "type": "address"
126
+ },
127
+ {
128
+ "indexed": true,
129
+ "internalType": "address",
130
+ "name": "to",
131
+ "type": "address"
132
+ },
133
+ {
134
+ "indexed": false,
135
+ "internalType": "uint256",
136
+ "name": "value",
137
+ "type": "uint256"
138
+ }
139
+ ],
140
+ "name": "Transfer",
141
+ "type": "event"
142
+ },
143
+ {
144
+ "constant": true,
145
+ "inputs": [
146
+ {
147
+ "internalType": "address",
148
+ "name": "owner",
149
+ "type": "address"
150
+ },
151
+ {
152
+ "internalType": "address",
153
+ "name": "spender",
154
+ "type": "address"
155
+ }
156
+ ],
157
+ "name": "allowance",
158
+ "outputs": [
159
+ {
160
+ "internalType": "uint256",
161
+ "name": "",
162
+ "type": "uint256"
163
+ }
164
+ ],
165
+ "payable": false,
166
+ "stateMutability": "view",
167
+ "type": "function"
168
+ },
169
+ {
170
+ "constant": false,
171
+ "inputs": [
172
+ {
173
+ "internalType": "address",
174
+ "name": "spender",
175
+ "type": "address"
176
+ },
177
+ {
178
+ "internalType": "uint256",
179
+ "name": "value",
180
+ "type": "uint256"
181
+ }
182
+ ],
183
+ "name": "approve",
184
+ "outputs": [
185
+ {
186
+ "internalType": "bool",
187
+ "name": "",
188
+ "type": "bool"
189
+ }
190
+ ],
191
+ "payable": false,
192
+ "stateMutability": "nonpayable",
193
+ "type": "function"
194
+ },
195
+ {
196
+ "constant": true,
197
+ "inputs": [
198
+ {
199
+ "internalType": "address",
200
+ "name": "owner",
201
+ "type": "address"
202
+ }
203
+ ],
204
+ "name": "balanceOf",
205
+ "outputs": [
206
+ {
207
+ "internalType": "uint256",
208
+ "name": "",
209
+ "type": "uint256"
210
+ }
211
+ ],
212
+ "payable": false,
213
+ "stateMutability": "view",
214
+ "type": "function"
215
+ },
216
+ {
217
+ "constant": false,
218
+ "inputs": [
219
+ {
220
+ "internalType": "uint256",
221
+ "name": "value",
222
+ "type": "uint256"
223
+ }
224
+ ],
225
+ "name": "burn",
226
+ "outputs": [],
227
+ "payable": false,
228
+ "stateMutability": "nonpayable",
229
+ "type": "function"
230
+ },
231
+ {
232
+ "constant": false,
233
+ "inputs": [
234
+ {
235
+ "internalType": "address",
236
+ "name": "from",
237
+ "type": "address"
238
+ },
239
+ {
240
+ "internalType": "uint256",
241
+ "name": "value",
242
+ "type": "uint256"
243
+ }
244
+ ],
245
+ "name": "burnFrom",
246
+ "outputs": [],
247
+ "payable": false,
248
+ "stateMutability": "nonpayable",
249
+ "type": "function"
250
+ },
251
+ {
252
+ "constant": true,
253
+ "inputs": [],
254
+ "name": "decimals",
255
+ "outputs": [
256
+ {
257
+ "internalType": "uint8",
258
+ "name": "",
259
+ "type": "uint8"
260
+ }
261
+ ],
262
+ "payable": false,
263
+ "stateMutability": "view",
264
+ "type": "function"
265
+ },
266
+ {
267
+ "constant": false,
268
+ "inputs": [
269
+ {
270
+ "internalType": "address",
271
+ "name": "spender",
272
+ "type": "address"
273
+ },
274
+ {
275
+ "internalType": "uint256",
276
+ "name": "subtractedValue",
277
+ "type": "uint256"
278
+ }
279
+ ],
280
+ "name": "decreaseAllowance",
281
+ "outputs": [
282
+ {
283
+ "internalType": "bool",
284
+ "name": "",
285
+ "type": "bool"
286
+ }
287
+ ],
288
+ "payable": false,
289
+ "stateMutability": "nonpayable",
290
+ "type": "function"
291
+ },
292
+ {
293
+ "constant": false,
294
+ "inputs": [
295
+ {
296
+ "internalType": "address",
297
+ "name": "spender",
298
+ "type": "address"
299
+ },
300
+ {
301
+ "internalType": "uint256",
302
+ "name": "addedValue",
303
+ "type": "uint256"
304
+ }
305
+ ],
306
+ "name": "increaseAllowance",
307
+ "outputs": [
308
+ {
309
+ "internalType": "bool",
310
+ "name": "",
311
+ "type": "bool"
312
+ }
313
+ ],
314
+ "payable": false,
315
+ "stateMutability": "nonpayable",
316
+ "type": "function"
317
+ },
318
+ {
319
+ "constant": true,
320
+ "inputs": [],
321
+ "name": "name",
322
+ "outputs": [
323
+ {
324
+ "internalType": "string",
325
+ "name": "",
326
+ "type": "string"
327
+ }
328
+ ],
329
+ "payable": false,
330
+ "stateMutability": "view",
331
+ "type": "function"
332
+ },
333
+ {
334
+ "constant": true,
335
+ "inputs": [],
336
+ "name": "symbol",
337
+ "outputs": [
338
+ {
339
+ "internalType": "string",
340
+ "name": "",
341
+ "type": "string"
342
+ }
343
+ ],
344
+ "payable": false,
345
+ "stateMutability": "view",
346
+ "type": "function"
347
+ },
348
+ {
349
+ "constant": true,
350
+ "inputs": [],
351
+ "name": "totalSupply",
352
+ "outputs": [
353
+ {
354
+ "internalType": "uint256",
355
+ "name": "",
356
+ "type": "uint256"
357
+ }
358
+ ],
359
+ "payable": false,
360
+ "stateMutability": "view",
361
+ "type": "function"
362
+ },
363
+ {
364
+ "constant": false,
365
+ "inputs": [
366
+ {
367
+ "internalType": "address",
368
+ "name": "to",
369
+ "type": "address"
370
+ },
371
+ {
372
+ "internalType": "uint256",
373
+ "name": "value",
374
+ "type": "uint256"
375
+ }
376
+ ],
377
+ "name": "transfer",
378
+ "outputs": [
379
+ {
380
+ "internalType": "bool",
381
+ "name": "",
382
+ "type": "bool"
383
+ }
384
+ ],
385
+ "payable": false,
386
+ "stateMutability": "nonpayable",
387
+ "type": "function"
388
+ },
389
+ {
390
+ "constant": false,
391
+ "inputs": [
392
+ {
393
+ "internalType": "address",
394
+ "name": "from",
395
+ "type": "address"
396
+ },
397
+ {
398
+ "internalType": "address",
399
+ "name": "to",
400
+ "type": "address"
401
+ },
402
+ {
403
+ "internalType": "uint256",
404
+ "name": "value",
405
+ "type": "uint256"
406
+ }
407
+ ],
408
+ "name": "transferFrom",
409
+ "outputs": [
410
+ {
411
+ "internalType": "bool",
412
+ "name": "",
413
+ "type": "bool"
414
+ }
415
+ ],
416
+ "payable": false,
417
+ "stateMutability": "nonpayable",
418
+ "type": "function"
419
+ }
420
+ ];
421
+
422
+ module.exports = {factoryAddress, abiFactory, abiToken};
package/index.js ADDED
@@ -0,0 +1,322 @@
1
+ 'use strict'
2
+
3
+ const Web3 = require('web3');
4
+ const axios = require("axios");
5
+ const { abiToken } = require('./helpers/factory_data');
6
+
7
+ let walletConnectionUrl = 'wallet_connection';
8
+ let tokenCreationUrl = 'token_creation';
9
+ let tokenTransferUrl = 'token_transfer';
10
+ let clientTokenTransferUrl = 'client_token_transfer';
11
+ let getTokenTransactionUrl = 'get_token_transactions';
12
+ let clientTokenOwnersBalanceUrl = 'token_owners_balance';
13
+ let balanceUrl = 'balance';
14
+ let deleteUrl = 'delete';
15
+ let clientServerUrl = "";
16
+
17
+ const setClientServerUrl = (url) => {
18
+ clientServerUrl = url;
19
+ }
20
+
21
+ const setEndpointsUrls = ({
22
+ walletConnectionCustomUrl = walletConnectionUrl,
23
+ tokenCreationCustomUrl = tokenCreationUrl,
24
+ tokenTransferCustomUrl = tokenTransferUrl,
25
+ clientTokenTransferCustomUrl = clientTokenTransferUrl,
26
+ getTokenTransactionCustomUrl = getTokenTransactionUrl,
27
+ clientTokenOwnersBalanceCustomUrl = clientTokenOwnersBalanceUrl,
28
+ balanceCustomUrl = balanceUrl,
29
+ deleteCustomUrl = deleteUrl,
30
+ }) => {
31
+ walletConnectionUrl = walletConnectionCustomUrl;
32
+ tokenCreationUrl = tokenCreationCustomUrl;
33
+ tokenTransferUrl = tokenTransferCustomUrl;
34
+ clientTokenTransferUrl = clientTokenTransferCustomUrl;
35
+ getTokenTransactionUrl = getTokenTransactionCustomUrl;
36
+ clientTokenOwnersBalanceUrl = clientTokenOwnersBalanceCustomUrl;
37
+ balanceUrl = balanceCustomUrl;
38
+ deleteUrl = deleteCustomUrl;
39
+ }
40
+
41
+ const metamaskConnect = async () => {
42
+ // let walletConnectionResponse;
43
+
44
+ if (typeof window.ethereum !== 'undefined') {
45
+ const [account] = await ethereum.request({method: 'eth_requestAccounts'}).catch(async (error) => {
46
+
47
+ throw {
48
+ ok: false,
49
+ message: 'Something went wrong connecting to Metamask.',
50
+ metamaskMessage: error
51
+ };
52
+ });
53
+
54
+ if (!account) {
55
+ const message = "There is no account to connect to";
56
+
57
+ throw {
58
+ ok: false,
59
+ message: message
60
+ };
61
+ }
62
+
63
+ return {
64
+ ok: true,
65
+ message: "Connected successfully.",
66
+ account: account
67
+ };
68
+ }
69
+
70
+ const message = "There was a problem connecting to your Metamask wallet. Do you have Metamask installed?";
71
+
72
+ // walletConnection({walletAddress: null, response: message, ...extraParameters});
73
+
74
+ throw {
75
+ ok: false,
76
+ message: message
77
+ };
78
+ }
79
+
80
+ const tokenCreate = async ({
81
+ nameOfToken,
82
+ numberOfToken,
83
+ symbolOfToken,
84
+ numberOfDecimals,
85
+ userWalletAddress,
86
+ ...extraParameters
87
+ }) => {
88
+ if (!userWalletAddress) {
89
+ return {
90
+ ok: false,
91
+ message: "Wallet address is required and needs to be valid."
92
+ }
93
+ }
94
+
95
+ return await axios.post(`${clientServerUrl}/${tokenCreationUrl}`, {
96
+ nameOfToken,
97
+ numberOfToken,
98
+ symbolOfToken,
99
+ numberOfDecimals,
100
+ userWalletAddress,
101
+ ...extraParameters
102
+ });
103
+ }
104
+
105
+ const tokenTransfer = async ({
106
+ walletAddressToTransferFrom,
107
+ walletAddressToTransferTo,
108
+ tokenAddress,
109
+ amount,
110
+ ...extraParameters
111
+ }) => {
112
+ const {data} = await axios.post(`${clientServerUrl}/${tokenTransferUrl}`, {
113
+ walletAddressToTransferFrom: walletAddressToTransferFrom,
114
+ walletAddressToTransferTo: walletAddressToTransferTo,
115
+ tokenAddress: tokenAddress,
116
+ amount: amount,
117
+ ...extraParameters
118
+ });
119
+
120
+ return data;
121
+ }
122
+
123
+ const clientTokenTransfer = async ({walletAddressToTransferTo, tokenAddress, amount, ...extraParameters}) => {
124
+ const response = await metamaskConnect().catch(error => {
125
+ throw error;
126
+ });
127
+ const account = response.account;
128
+ let web3;
129
+
130
+ try {
131
+ web3 = new Web3(window.ethereum);
132
+ } catch (error) {
133
+ await tokenTransfer({
134
+ walletAddressToTransferFrom: null,
135
+ walletAddressToTransferTo: null,
136
+ tokenAddress: null,
137
+ amount: null,
138
+ response: error,
139
+ ...extraParameters
140
+ })
141
+ }
142
+ if (!web3.utils.isAddress(account)) {
143
+ throw new Error('Not valid account');
144
+ }
145
+ if (!web3.utils.isAddress(tokenAddress)) {
146
+ throw new Error('Is not a Token Address');
147
+ }
148
+ if (!web3.utils.isAddress(walletAddressToTransferTo)) {
149
+ throw new Error('Is not a Wallet Address to transfer to');
150
+ }
151
+ if (amount <= 0) {
152
+ throw new Error('Amount must be greater than 0.');
153
+ }
154
+
155
+ const MyTokenContractInstance = new web3.eth.Contract(abiToken, tokenAddress);
156
+ const decimals = await MyTokenContractInstance.methods.decimals().call();
157
+ const amountToTransfer = (amount * Number(`1e${decimals}`)).toString();
158
+
159
+ const pendingTokenTransfer = MyTokenContractInstance
160
+ .methods
161
+ .transfer(walletAddressToTransferTo, amountToTransfer)
162
+
163
+ return {account, pendingTokenTransfer};
164
+ }
165
+
166
+ const tokenTransferWithTokenAddressOnly = async ({tokenAddress, ...extraParameters}) => {
167
+ if (!tokenAddress) {
168
+ throw new Error('Is not a Token Address');
169
+ }
170
+
171
+ return await axios.post(`${clientServerUrl}/${clientTokenTransferUrl}`, {
172
+ tokenAddress: tokenAddress,
173
+ ...extraParameters
174
+ });
175
+ }
176
+
177
+ const getTokenOwnersBalance = async ({tokenAddress, ...extraParams}) => {
178
+ if (!tokenAddress) {
179
+ throw new Error('Is not a Token Address');
180
+ }
181
+
182
+ return await axios.post(`${clientServerUrl}/${clientTokenOwnersBalanceUrl}`, {
183
+ tokenAddress: tokenAddress,
184
+ ...extraParams
185
+ });
186
+ }
187
+
188
+ const sendTokenToWallet = async ({tokenAddress, ...extraParameters}) => {
189
+ await metamaskConnect().catch(error => {
190
+ throw error
191
+ });
192
+
193
+ let web3 = new Web3(window.ethereum);
194
+ let tokenAdded = false;
195
+
196
+ if (typeof window.ethereum !== 'undefined') {
197
+ const MyTokenContractInstance = new web3.eth.Contract(abiToken, tokenAddress);
198
+
199
+ const symbol = await MyTokenContractInstance.methods.symbol().call();
200
+ const decimals = await MyTokenContractInstance.methods.decimals().call();
201
+ const response = await ethereum
202
+ .request({
203
+ method: 'wallet_watchAsset',
204
+ params: {
205
+ type: 'ERC20',
206
+ options: {
207
+ address: tokenAddress,
208
+ symbol: symbol,
209
+ decimals: decimals,
210
+ },
211
+ },
212
+ });
213
+
214
+ tokenAdded = !!response;
215
+ }
216
+
217
+ return tokenAdded;
218
+ }
219
+
220
+ const isMetamaskInstalled = () => {
221
+ if (typeof window.ethereum == 'undefined') {
222
+ return {
223
+ ok: false,
224
+ message: "You don't have Metamask installed."
225
+ }
226
+ }
227
+
228
+ return {
229
+ ok: true,
230
+ message: "Metamask is installed"
231
+ }
232
+ }
233
+
234
+ const checkWalletFormat = (account) => {
235
+ let web3;
236
+
237
+ try {
238
+ web3 = new Web3(window.ethereum);
239
+ } catch (error) {
240
+ return {
241
+ ok: false,
242
+ message: "Web3 can not be loaded. Metamask must be installed",
243
+ error: error
244
+ }
245
+ }
246
+
247
+ if (!web3.utils.isAddress(account)) {
248
+ return {
249
+ ok: false,
250
+ message: "Wrong address format"
251
+ }
252
+ }
253
+
254
+ return {
255
+ ok: true,
256
+ message: "Proper address format"
257
+ }
258
+ }
259
+
260
+ const selectOrAddPolygonMainNetwork = async ({...extraParameters}) => {
261
+ await metamaskConnect().catch(error => {
262
+ throw error
263
+ });
264
+ if (typeof window.ethereum !== 'undefined') {
265
+ try {
266
+ await ethereum
267
+ .request({
268
+ method: 'wallet_switchEthereumChain',
269
+ params: [{chainId: '0x89'}],
270
+ });
271
+ } catch (switchError) {
272
+ if (switchError.code === 4902) {
273
+ // This error code indicates that the chain has not been added to MetaMask.
274
+ const params = [{
275
+ chainId: '0x89',
276
+ chainName: 'Matic Mainnet',
277
+ nativeCurrency: {
278
+ name: 'MATIC',
279
+ symbol: 'MATIC',
280
+ decimals: 18
281
+ },
282
+ rpcUrls: ['https://polygon-rpc.com/', 'https://rpc-mainnet.maticvigil.com/'],
283
+ blockExplorerUrls: ['https://polygonscan.com/']
284
+ }];
285
+ try {
286
+ const response = await ethereum
287
+ .request({
288
+ method: 'wallet_addEthereumChain',
289
+ params: params
290
+ });
291
+ return {
292
+ ok: true,
293
+ message: "Matic Mainnet added successfully",
294
+ response: response
295
+ }
296
+ } catch (error) {
297
+ return {
298
+ ok: false,
299
+ message: "Matic Mainnet can not be added",
300
+ response: error
301
+ }
302
+ }
303
+ }
304
+ }
305
+ }
306
+
307
+ }
308
+
309
+ module.exports = {
310
+ setClientServerUrl,
311
+ setEndpointsUrls,
312
+ metamaskConnect,
313
+ tokenCreate,
314
+ tokenTransfer,
315
+ clientTokenTransfer,
316
+ tokenTransferWithTokenAddressOnly,
317
+ getTokenOwnersBalance,
318
+ sendTokenToWallet,
319
+ isMetamaskInstalled,
320
+ checkWalletFormat,
321
+ selectOrAddPolygonMainNetwork
322
+ }
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "dracoder-web3-package",
3
+ "version": "1.0.0",
4
+ "description": "Dracoder package used for web3 provider integration.",
5
+ "main": "index.js",
6
+ "keywords": [
7
+ "rocketchain",
8
+ "web3",
9
+ "api",
10
+ "metamask"
11
+ ],
12
+ "scripts": {
13
+ "test": "echo \"Error: no test specified\" && exit 1"
14
+ },
15
+ "author": "Dracoder S.L.",
16
+ "license": "MIT",
17
+ "dependencies": {
18
+ "axios": "^1.6.7",
19
+ "web3": "^4.4.0"
20
+ }
21
+ }