dracoder-web3-package 2.0.3 → 2.0.4

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.
Files changed (2) hide show
  1. package/index.js +90 -22
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -4,6 +4,26 @@ const { Web3 } = require("web3");
4
4
  const { isAddress } = require("web3-validator");
5
5
  const { abiToken } = require("./helpers/factory_data");
6
6
 
7
+ // Variable para guardar
8
+ // la configuración de red
9
+ let networkConfig = null;
10
+
11
+ // Configuración por defecto
12
+ const defaultNetworkConfig = {
13
+ chainId: "0x89",
14
+ chainName: "Polygon Mainnet",
15
+ nativeCurrency: {
16
+ name: "POL",
17
+ symbol: "POL",
18
+ decimals: 18,
19
+ },
20
+ rpcUrls: ["https://polygon-rpc.com/"],
21
+ blockExplorerUrls: ["https://polygonscan.com/"],
22
+ iconUrls: [
23
+ "https://polygonscan.com/assets/poly/images/svg/logos/chain-light.svg",
24
+ ],
25
+ };
26
+
7
27
  const METAMASK_NOT_INSTALLED_MESSAGE = {
8
28
  ok: false,
9
29
  message: "You don't have Metamask installed.",
@@ -167,18 +187,25 @@ const selectOrAddPolygonMainNetwork = async () => {
167
187
  if (!checkIfMetamaskIsAvailable()) {
168
188
  return METAMASK_NOT_INSTALLED_MESSAGE;
169
189
  }
190
+
170
191
  await getMetamaskAccount().catch((error) => {
171
192
  throw error;
172
193
  });
194
+
195
+ const pickedNetworkConfig = getNetworkConfig();
196
+ const addedSuccessMsg = `${pickedNetworkConfig.chainName} added successfully`;
197
+ const switchedSuccessMsg = `${pickedNetworkConfig.chainName} established successfully`;
198
+
173
199
  try {
174
200
  // Primero intentar cambiar a la red de Polygon si ya existe
175
201
  await Metamask.request({
176
202
  method: "wallet_switchEthereumChain",
177
- params: [{ chainId: "0x89" }], // ChainId de Polygon Mainnet en hexadecimal
203
+ params: [{ chainId: pickedNetworkConfig.chainId }],
178
204
  });
205
+
179
206
  return {
180
207
  ok: true,
181
- message: "Already on polygon main network",
208
+ message: switchedSuccessMsg,
182
209
  };
183
210
  } catch (switchError) {
184
211
  // Si el error es 4902, significa que la red no está añadida
@@ -186,33 +213,31 @@ const selectOrAddPolygonMainNetwork = async () => {
186
213
  try {
187
214
  const response = await Metamask.request({
188
215
  method: "wallet_addEthereumChain",
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
- ],
216
+ params: [pickedNetworkConfig],
205
217
  });
206
218
  return {
207
219
  ok: true,
208
- message: "Polygon MainNet added successfully",
220
+ message: addedSuccessMsg,
209
221
  response: response,
210
222
  };
211
223
  } catch (error) {
224
+ const errorCode = error.code;
225
+
226
+ if (errorCode === -32603) {
227
+ // El código -32603 es un bug de Metamask
228
+ // https://github.com/MetaMask/metamask-extension/issues/33213
229
+ // https://github.com/MetaMask/metamask-extension/issues/32165
230
+ // La red realmente se ha añadido correctamente
231
+ return {
232
+ ok: true,
233
+ message: addedSuccessMsg,
234
+ };
235
+ }
236
+
212
237
  return {
213
238
  ok: false,
214
239
  message: error.message,
215
- code: error.code,
240
+ code: errorCode,
216
241
  };
217
242
  }
218
243
  } else {
@@ -221,10 +246,53 @@ const selectOrAddPolygonMainNetwork = async () => {
221
246
  }
222
247
  };
223
248
 
249
+ // Función para obtener la configuración actual
250
+ const getNetworkConfig = () => {
251
+ return networkConfig || defaultNetworkConfig;
252
+ };
253
+
254
+ // Función para configurar la red desde el proyecto
255
+ const setNetworkConfig = (config) => {
256
+ if (!config || typeof config !== "object") {
257
+ console.warn(
258
+ "Configuración de red inválida, usando configuración por defecto"
259
+ );
260
+
261
+ networkConfig = { ...defaultNetworkConfig };
262
+
263
+ return false;
264
+ }
265
+
266
+ // Validar que tenga los campos requeridos
267
+ const requiredFields = ["chainId", "chainName", "rpcUrls"];
268
+ const missingFields = requiredFields.filter((field) => !config[field]);
269
+
270
+ if (missingFields.length > 0) {
271
+ console.warn(
272
+ `Faltan campos requeridos en la configuración: ${missingFields.join(
273
+ ", "
274
+ )}`
275
+ );
276
+
277
+ networkConfig = { ...defaultNetworkConfig };
278
+
279
+ return false;
280
+ }
281
+
282
+ // Fusionar con configuración por defecto para campos opcionales
283
+ networkConfig = {
284
+ ...defaultNetworkConfig,
285
+ ...config,
286
+ };
287
+
288
+ return true;
289
+ };
290
+
224
291
  module.exports = {
225
- clientTokenTransfer,
292
+ setNetworkConfig,
293
+ checkWalletFormat,
226
294
  sendTokenToWallet,
295
+ clientTokenTransfer,
227
296
  isMetamaskInstalled,
228
- checkWalletFormat,
229
297
  selectOrAddPolygonMainNetwork,
230
298
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dracoder-web3-package",
3
- "version": "2.0.3",
3
+ "version": "2.0.4",
4
4
  "description": "Dracoder package used for web3 provider integration.",
5
5
  "main": "index.js",
6
6
  "keywords": [