dracoder-web3-package 2.0.1 → 2.0.3
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 +75 -375
- package/index.js +68 -80
- package/package.json +3 -2
package/README.md
CHANGED
@@ -1,431 +1,131 @@
|
|
1
|
-
#
|
1
|
+
# web3-integration-package
|
2
2
|
|
3
|
-
##
|
3
|
+
## Resumen
|
4
4
|
|
5
|
-
|
5
|
+
_Paquete utilizado para la integración del proveedor web3 entre el lado del cliente y la API._
|
6
6
|
|
7
|
-
|
8
|
-
Then the info generated and logs are sent to the related API endpoints.
|
7
|
+
Este repositorio proporciona las herramientas necesarias para conectar a los usuarios con el ecosistema blockchain de manera fluida y segura. Facilita la integración de funcionalidades de firma de transacciones blockchain en aplicaciones cliente, permitiendo que la información y los registros generados se comuniquen eficientemente con los endpoints de la API necesarios.
|
9
8
|
|
10
|
-
##
|
9
|
+
## Índice
|
11
10
|
|
12
|
-
|
13
|
-
|
11
|
+
- Instalación
|
12
|
+
- Funciones
|
14
13
|
|
15
|
-
##
|
14
|
+
## Instalación
|
16
15
|
|
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:
|
16
|
+
`npm install dracoder-web3-package`
|
105
17
|
|
106
|
-
|
18
|
+
# Funciones
|
107
19
|
|
108
|
-
|
20
|
+
# `isMetamaskInstalled`
|
109
21
|
|
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)
|
22
|
+
Verifica si Metamask está instalado en el navegador del usuario.
|
165
23
|
|
166
|
-
|
24
|
+
### Uso
|
167
25
|
|
168
|
-
|
169
|
-
|
170
|
-
|
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
|
-
}
|
26
|
+
```javascript
|
27
|
+
const { isMetamaskInstalled } = require("dracoder-web3-package");
|
28
|
+
console.log(isMetamaskInstalled());
|
187
29
|
```
|
188
30
|
|
189
|
-
|
31
|
+
### Retorno
|
190
32
|
|
191
|
-
|
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
|
-
```
|
33
|
+
- `{ "ok": true, "message": "Metamask is installed" }` si Metamask está instalado.
|
34
|
+
- `{ "ok": false, "message": "You don't have Metamask installed." }` si Metamask no está instalado.
|
264
35
|
|
265
|
-
|
36
|
+
# `getMetamaskAccount`
|
266
37
|
|
267
|
-
|
38
|
+
Obtiene la cuenta de Metamask conectada actualmente.
|
268
39
|
|
269
|
-
|
40
|
+
### Uso
|
270
41
|
|
42
|
+
```javascript
|
43
|
+
const { getMetamaskAccount } = require("dracoder-web3-package");
|
44
|
+
getMetamaskAccount().then(console.log).catch(console.error);
|
271
45
|
```
|
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
46
|
|
325
|
-
|
47
|
+
### Retorno
|
326
48
|
|
327
|
-
|
49
|
+
- `{ "ok": true, "message": "Connected successfully.", "account": "0x..." }` si se obtiene la cuenta con éxito.
|
50
|
+
- `{ "ok": false, "message": "Error message }` en caso de error.
|
328
51
|
|
329
|
-
|
52
|
+
# `clientTokenTransfer`
|
330
53
|
|
331
|
-
|
332
|
-
|
333
|
-
```
|
334
|
-
const RocketchainPackage = require('rocketchain-package');
|
335
|
-
const someFunction = async () => {
|
336
|
-
const ownersArray = await RocketchainPackage.getTokenOwnersBalance(0xa6c99e129cecf74865f6ed847a62c63cd6e0c045);
|
337
|
-
};
|
338
|
-
```
|
54
|
+
Realiza una transferencia de tokens desde la cuenta conectada a una dirección específica.
|
339
55
|
|
340
|
-
|
56
|
+
### Parametros
|
341
57
|
|
342
|
-
|
58
|
+
- `walletAddressToTransferTo` : Dirección de la billetera receptora.
|
59
|
+
- `tokenAddress` : Dirección del contrato del token.
|
60
|
+
- `amount` : Cantidad de tokens a transferir.
|
343
61
|
|
344
|
-
|
62
|
+
### Uso
|
345
63
|
|
64
|
+
```javascript
|
65
|
+
const { clientTokenTransfer } = require("dracoder-web3-package");
|
66
|
+
clientTokenTransfer(walletAddressToTransferTo, tokenAddress, amount)
|
67
|
+
.then(console.log)
|
68
|
+
.catch(console.error);
|
346
69
|
```
|
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
70
|
|
355
|
-
|
71
|
+
### Retorno
|
356
72
|
|
357
|
-
|
73
|
+
- `{ "ok": true, "message": "Transfer successful", "transactionHash": "0x..." }` si la transferencia es exitosa.
|
74
|
+
- `{ "ok": false, "message": "Transfer failed", "error": error }` si falla la transferencia.
|
358
75
|
|
359
|
-
|
76
|
+
# `sendTokenToWallet`
|
360
77
|
|
361
|
-
|
78
|
+
Intenta añadir un token a Metamask.
|
362
79
|
|
363
|
-
|
364
|
-
const RocketchainPackage = require('rocketchain-package');
|
365
|
-
const someFunction = async () => {
|
366
|
-
const checkMetamask = await RocketchainPackage.isMetamaskInstalled();
|
367
|
-
};
|
368
|
-
```
|
80
|
+
### Parametros
|
369
81
|
|
370
|
-
|
82
|
+
- `tokenAddress` : Dirección del contrato del token.
|
371
83
|
|
372
|
-
|
84
|
+
### Uso
|
373
85
|
|
374
|
-
```
|
375
|
-
{
|
86
|
+
```javascript
|
87
|
+
const { sendTokenToWallet } = require("dracoder-web3-package");
|
88
|
+
sendTokenToWallet(tokenAddress).then(console.log).catch(console.error);
|
376
89
|
```
|
377
90
|
|
378
|
-
###
|
91
|
+
### Retorno
|
379
92
|
|
380
|
-
|
93
|
+
- `{ true }` si el token se añadió correctamente.
|
94
|
+
- `{ [Objeto] de error }` si falla la operación.
|
381
95
|
|
382
|
-
checkWalletFormat
|
96
|
+
# `checkWalletFormat`
|
383
97
|
|
384
|
-
|
98
|
+
Verifica si una dirección de billetera tiene el formato correcto.
|
385
99
|
|
386
|
-
|
100
|
+
### Parametros
|
387
101
|
|
388
|
-
|
102
|
+
- `account` : Dirección de la billetera a verificar.
|
389
103
|
|
390
|
-
|
391
|
-
const RocketchainPackage = require('rocketchain-package');
|
392
|
-
const someFunction = async () => {
|
393
|
-
const formatVerification = await RocketchainPackage.checkWalletFormat(0xLZf8C3205B230a39a71354214561dAA83433Ae4K);
|
394
|
-
};
|
395
|
-
```
|
396
|
-
|
397
|
-
#### Return
|
104
|
+
### Uso
|
398
105
|
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
{ ok: true, message: "Proper address format" }
|
106
|
+
```javascript
|
107
|
+
const { checkWalletFormat } = require("dracoder-web3-package");
|
108
|
+
console.log(checkWalletFormat(account));
|
403
109
|
```
|
404
110
|
|
405
|
-
###
|
406
|
-
|
407
|
-
Selects or adds the Polygon Matic Mainnet to the Metamask wallet.
|
111
|
+
### Retorno
|
408
112
|
|
409
|
-
|
113
|
+
- `{ "ok": true, "message": "Proper address format" }` si el formato es correcto.
|
114
|
+
- `{ "ok": false, "message": "Wrong address format" }` si el formato es incorrecto.
|
410
115
|
|
411
|
-
|
116
|
+
# `selectOrAddPolygonMainNetwork`
|
412
117
|
|
413
|
-
|
118
|
+
Selecciona o añade la red principal de Polygon en Metamask.
|
414
119
|
|
415
|
-
###
|
120
|
+
### Uso
|
416
121
|
|
122
|
+
```javascript
|
123
|
+
const { selectOrAddPolygonMainNetwork } = require("dracoder-web3-package");
|
124
|
+
selectOrAddPolygonMainNetwork().then(console.log).catch(console.error);
|
417
125
|
```
|
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
126
|
|
428
|
-
|
429
|
-
{ ok: true, message: "Matic Mainnet added successfully", response: ... }
|
430
|
-
```
|
127
|
+
### Retorno
|
431
128
|
|
129
|
+
- `{ "ok": true, "message": "Already on polygon main network" }` si ya está en la red principal de Polygon.
|
130
|
+
- `{ "ok": true, "message": "Matic MainNet added successfully", "response": response }` si se añade la red de Polygon con éxito.
|
131
|
+
- `{ "ok": false, "message": "Matic MainNet cannot be added", "response": error }` si no se puede añadir la red.
|
package/index.js
CHANGED
@@ -1,29 +1,30 @@
|
|
1
1
|
"use strict";
|
2
2
|
|
3
|
-
const {Web3} = require("web3");
|
4
|
-
const {
|
3
|
+
const { Web3 } = require("web3");
|
4
|
+
const { isAddress } = require("web3-validator");
|
5
|
+
const { abiToken } = require("./helpers/factory_data");
|
5
6
|
|
6
7
|
const METAMASK_NOT_INSTALLED_MESSAGE = {
|
7
8
|
ok: false,
|
8
9
|
message: "You don't have Metamask installed.",
|
9
|
-
}
|
10
|
+
};
|
10
11
|
|
11
|
-
const Metamask = window.ethereum
|
12
|
+
const Metamask = window.ethereum;
|
12
13
|
|
13
14
|
const checkIfMetamaskIsAvailable = () => {
|
14
|
-
return typeof Metamask !== "undefined"
|
15
|
-
}
|
15
|
+
return typeof Metamask !== "undefined";
|
16
|
+
};
|
16
17
|
|
17
18
|
const getMetamaskAccount = async () => {
|
18
|
-
const [account] = await Metamask
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
19
|
+
const [account] = await Metamask.request({
|
20
|
+
method: "eth_requestAccounts",
|
21
|
+
}).catch(async (error) => {
|
22
|
+
throw {
|
23
|
+
ok: false,
|
24
|
+
message: "Something went wrong connecting to Metamask.",
|
25
|
+
metamaskMessage: error?.message,
|
26
|
+
};
|
27
|
+
});
|
27
28
|
if (!account) {
|
28
29
|
throw {
|
29
30
|
ok: false,
|
@@ -38,16 +39,18 @@ const getMetamaskAccount = async () => {
|
|
38
39
|
};
|
39
40
|
|
40
41
|
const clientTokenTransfer = async ({
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
42
|
+
walletAddressToTransferTo,
|
43
|
+
tokenAddress,
|
44
|
+
amount,
|
45
|
+
}) => {
|
45
46
|
if (!checkIfMetamaskIsAvailable()) {
|
46
|
-
return METAMASK_NOT_INSTALLED_MESSAGE
|
47
|
+
return METAMASK_NOT_INSTALLED_MESSAGE;
|
47
48
|
}
|
49
|
+
|
48
50
|
const response = await getMetamaskAccount().catch((error) => {
|
49
51
|
throw error;
|
50
52
|
});
|
53
|
+
|
51
54
|
const account = response.account;
|
52
55
|
let web3;
|
53
56
|
|
@@ -57,15 +60,18 @@ const clientTokenTransfer = async ({
|
|
57
60
|
throw error;
|
58
61
|
}
|
59
62
|
|
60
|
-
if (!
|
63
|
+
if (!isAddress(account)) {
|
61
64
|
throw new Error("Not valid account");
|
62
65
|
}
|
63
|
-
|
66
|
+
|
67
|
+
if (!isAddress(tokenAddress)) {
|
64
68
|
throw new Error("Is not a Token Address");
|
65
69
|
}
|
66
|
-
|
70
|
+
|
71
|
+
if (!isAddress(walletAddressToTransferTo)) {
|
67
72
|
throw new Error("Is not a Wallet Address to transfer to");
|
68
73
|
}
|
74
|
+
|
69
75
|
if (amount <= 0) {
|
70
76
|
throw new Error("Amount must be greater than 0.");
|
71
77
|
}
|
@@ -98,30 +104,27 @@ const clientTokenTransfer = async ({
|
|
98
104
|
}
|
99
105
|
};
|
100
106
|
|
101
|
-
const sendTokenToWallet = async ({tokenAddress}) => {
|
102
|
-
|
103
|
-
|
107
|
+
const sendTokenToWallet = async ({ tokenAddress }) => {
|
108
|
+
// Cambiar a red Polygon
|
109
|
+
const polygonResponse = await selectOrAddPolygonMainNetwork();
|
110
|
+
if (!polygonResponse.ok) {
|
111
|
+
throw polygonResponse;
|
104
112
|
}
|
105
113
|
|
106
|
-
await getMetamaskAccount().catch((error) => {
|
107
|
-
throw error;
|
108
|
-
});
|
109
|
-
|
110
114
|
let web3 = new Web3(Metamask);
|
111
115
|
let tokenAdded = false;
|
112
116
|
|
113
|
-
const MyTokenContractInstance = new web3.eth.Contract(
|
114
|
-
abiToken,
|
115
|
-
tokenAddress
|
116
|
-
);
|
117
|
+
const MyTokenContractInstance = new web3.eth.Contract(abiToken, tokenAddress);
|
117
118
|
|
118
119
|
try {
|
119
120
|
const symbol = await MyTokenContractInstance.methods.symbol().call();
|
120
|
-
const decimals = web3.utils.toNumber(
|
121
|
+
const decimals = web3.utils.toNumber(
|
122
|
+
await MyTokenContractInstance.methods.decimals().call()
|
123
|
+
);
|
121
124
|
const response = await Metamask.request({
|
122
|
-
method:
|
125
|
+
method: "wallet_watchAsset",
|
123
126
|
params: {
|
124
|
-
type:
|
127
|
+
type: "ERC20",
|
125
128
|
options: {
|
126
129
|
address: tokenAddress,
|
127
130
|
symbol: symbol,
|
@@ -131,39 +134,23 @@ const sendTokenToWallet = async ({tokenAddress}) => {
|
|
131
134
|
});
|
132
135
|
tokenAdded = !!response;
|
133
136
|
} catch (error) {
|
134
|
-
return error
|
137
|
+
return error;
|
135
138
|
}
|
136
139
|
return tokenAdded;
|
137
|
-
|
138
140
|
};
|
139
141
|
|
140
142
|
const isMetamaskInstalled = () => {
|
141
143
|
if (!checkIfMetamaskIsAvailable()) {
|
142
|
-
return METAMASK_NOT_INSTALLED_MESSAGE
|
144
|
+
return METAMASK_NOT_INSTALLED_MESSAGE;
|
143
145
|
}
|
144
146
|
return {
|
145
147
|
ok: true,
|
146
148
|
message: "Metamask is installed",
|
147
149
|
};
|
148
|
-
|
149
150
|
};
|
150
151
|
|
151
152
|
const checkWalletFormat = (account) => {
|
152
|
-
|
153
|
-
if (!checkIfMetamaskIsAvailable()) {
|
154
|
-
return METAMASK_NOT_INSTALLED_MESSAGE
|
155
|
-
}
|
156
|
-
try {
|
157
|
-
web3 = new Web3(Metamask);
|
158
|
-
} catch (error) {
|
159
|
-
return {
|
160
|
-
ok: false,
|
161
|
-
message: "Web3 can not be loaded. Metamask must be installed",
|
162
|
-
error: error,
|
163
|
-
};
|
164
|
-
}
|
165
|
-
|
166
|
-
if (!web3.utils.isAddress(account)) {
|
153
|
+
if (!isAddress(account)) {
|
167
154
|
return {
|
168
155
|
ok: false,
|
169
156
|
message: "Wrong address format",
|
@@ -178,57 +165,58 @@ const checkWalletFormat = (account) => {
|
|
178
165
|
|
179
166
|
const selectOrAddPolygonMainNetwork = async () => {
|
180
167
|
if (!checkIfMetamaskIsAvailable()) {
|
181
|
-
return METAMASK_NOT_INSTALLED_MESSAGE
|
168
|
+
return METAMASK_NOT_INSTALLED_MESSAGE;
|
182
169
|
}
|
183
170
|
await getMetamaskAccount().catch((error) => {
|
184
171
|
throw error;
|
185
172
|
});
|
186
173
|
try {
|
174
|
+
// Primero intentar cambiar a la red de Polygon si ya existe
|
187
175
|
await Metamask.request({
|
188
176
|
method: "wallet_switchEthereumChain",
|
189
|
-
params: [{chainId: "0x89"}],
|
177
|
+
params: [{ chainId: "0x89" }], // ChainId de Polygon Mainnet en hexadecimal
|
190
178
|
});
|
191
179
|
return {
|
192
180
|
ok: true,
|
193
181
|
message: "Already on polygon main network",
|
194
182
|
};
|
195
183
|
} catch (switchError) {
|
184
|
+
// Si el error es 4902, significa que la red no está añadida
|
196
185
|
if (switchError.code === 4902) {
|
197
|
-
const params = [
|
198
|
-
{
|
199
|
-
chainId: "0x89",
|
200
|
-
chainName: "Matic MainNet",
|
201
|
-
nativeCurrency: {
|
202
|
-
name: "MATIC",
|
203
|
-
symbol: "MATIC",
|
204
|
-
decimals: 18,
|
205
|
-
},
|
206
|
-
rpcUrls: [
|
207
|
-
"https://polygon-rpc.com/",
|
208
|
-
"https://rpc-mainnet.maticvigil.com/",
|
209
|
-
],
|
210
|
-
blockExplorerUrls: ["https://polygonscan.com/"],
|
211
|
-
},
|
212
|
-
];
|
213
186
|
try {
|
214
187
|
const response = await Metamask.request({
|
215
188
|
method: "wallet_addEthereumChain",
|
216
|
-
params:
|
189
|
+
params: [
|
190
|
+
{
|
191
|
+
chainId: "0x89",
|
192
|
+
chainName: "Polygon",
|
193
|
+
nativeCurrency: {
|
194
|
+
name: "POL",
|
195
|
+
symbol: "POL",
|
196
|
+
decimals: 18,
|
197
|
+
},
|
198
|
+
rpcUrls: ["https://polygon.llamarpc.com"],
|
199
|
+
blockExplorerUrls: ["https://polygonscan.com/"],
|
200
|
+
iconUrls: [
|
201
|
+
"https://polygonscan.com/assets/poly/images/svg/logos/chain-light.svg",
|
202
|
+
],
|
203
|
+
},
|
204
|
+
],
|
217
205
|
});
|
218
206
|
return {
|
219
207
|
ok: true,
|
220
|
-
message: "
|
208
|
+
message: "Polygon MainNet added successfully",
|
221
209
|
response: response,
|
222
210
|
};
|
223
211
|
} catch (error) {
|
224
212
|
return {
|
225
213
|
ok: false,
|
226
|
-
message:
|
227
|
-
|
214
|
+
message: error.message,
|
215
|
+
code: error.code,
|
228
216
|
};
|
229
217
|
}
|
230
|
-
}else{
|
231
|
-
throw switchError
|
218
|
+
} else {
|
219
|
+
throw switchError;
|
232
220
|
}
|
233
221
|
}
|
234
222
|
};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "dracoder-web3-package",
|
3
|
-
"version": "2.0.
|
3
|
+
"version": "2.0.3",
|
4
4
|
"description": "Dracoder package used for web3 provider integration.",
|
5
5
|
"main": "index.js",
|
6
6
|
"keywords": [
|
@@ -16,7 +16,8 @@
|
|
16
16
|
"author": "Dracoder S.L.",
|
17
17
|
"license": "MIT",
|
18
18
|
"dependencies": {
|
19
|
-
"web3": "^4.4.0"
|
19
|
+
"web3": "^4.4.0",
|
20
|
+
"web3-validator": "^2.0.6"
|
20
21
|
},
|
21
22
|
"devDependencies": {
|
22
23
|
"@babel/core": "^7.15.0",
|