coinley-checkout 0.3.5 → 0.3.6

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.
@@ -167,15 +167,30 @@ const TOKEN_CONFIG = {
167
167
  };
168
168
  const getWeb3Instance = () => __async(void 0, null, function* () {
169
169
  try {
170
- if (typeof window !== "undefined" && window.ethereum) {
170
+ if (typeof window === "undefined" || !window.ethereum) {
171
+ throw new Error("No ethereum provider found");
172
+ }
173
+ let Web3Class = null;
174
+ try {
171
175
  const Web3Module = yield import("web3");
172
- const Web3 = Web3Module.default || Web3Module.Web3 || Web3Module;
173
- return new Web3(window.ethereum);
176
+ Web3Class = Web3Module.default || Web3Module.Web3 || Web3Module;
177
+ console.log("Web3 imported via dynamic import:", Web3Class);
178
+ } catch (importError) {
179
+ console.log("Dynamic import failed, trying global Web3:", importError);
180
+ if (typeof window !== "undefined" && window.Web3) {
181
+ Web3Class = window.Web3;
182
+ console.log("Using global Web3:", Web3Class);
183
+ }
184
+ }
185
+ if (!Web3Class) {
186
+ throw new Error("Web3 not available. Please ensure web3 is installed or MetaMask is running.");
174
187
  }
175
- throw new Error("No ethereum provider found");
188
+ const web3Instance = new Web3Class(window.ethereum);
189
+ console.log("Web3 instance created successfully:", web3Instance);
190
+ return web3Instance;
176
191
  } catch (error) {
177
192
  console.error("Failed to load Web3:", error);
178
- throw new Error("Web3 is required for blockchain transactions. Please install MetaMask or another Web3 wallet.");
193
+ throw new Error(`Web3 initialization failed: ${error.message}. Please ensure MetaMask is installed and running.`);
179
194
  }
180
195
  });
181
196
  const detectWallets = () => {
@@ -364,6 +379,7 @@ const switchEVMNetwork = (networkConfig) => __async(void 0, null, function* () {
364
379
  const sendTransaction = (walletConnection, transactionData) => __async(void 0, null, function* () {
365
380
  const { walletType, network, address } = walletConnection;
366
381
  const { to, amount, tokenAddress, tokenDecimals } = transactionData;
382
+ console.log("sendTransaction called with:", { walletConnection, transactionData });
367
383
  switch (walletType) {
368
384
  case WALLET_TYPES.METAMASK:
369
385
  case WALLET_TYPES.TRUST_WALLET:
@@ -377,16 +393,24 @@ const sendTransaction = (walletConnection, transactionData) => __async(void 0, n
377
393
  }
378
394
  });
379
395
  const sendEVMTransaction = (from, to, amount, tokenAddress, tokenDecimals) => __async(void 0, null, function* () {
396
+ console.log("sendEVMTransaction called with:", { from, to, amount, tokenAddress, tokenDecimals });
380
397
  if (typeof window === "undefined" || !window.ethereum) {
381
398
  throw new Error("Ethereum provider not found");
382
399
  }
383
400
  try {
384
- const Web3 = yield getWeb3Instance();
385
- const web3 = new Web3(window.ethereum);
386
- const amountInWei = web3.utils.toBN(
387
- Math.floor(parseFloat(amount) * Math.pow(10, tokenDecimals || 18))
401
+ const web3 = yield getWeb3Instance();
402
+ console.log("Web3 instance obtained for transaction");
403
+ const decimals = tokenDecimals || 18;
404
+ const amountBN = web3.utils.toBN(
405
+ Math.floor(parseFloat(amount) * Math.pow(10, decimals)).toString()
388
406
  );
407
+ console.log("Amount calculated:", {
408
+ originalAmount: amount,
409
+ decimals,
410
+ calculatedAmount: amountBN.toString()
411
+ });
389
412
  if (tokenAddress && tokenAddress !== "native") {
413
+ console.log("Preparing ERC20 token transfer...");
390
414
  const tokenABI = [
391
415
  {
392
416
  constant: false,
@@ -402,21 +426,32 @@ const sendEVMTransaction = (from, to, amount, tokenAddress, tokenDecimals) => __
402
426
  }
403
427
  ];
404
428
  const contract = new web3.eth.Contract(tokenABI, tokenAddress);
405
- const receipt = yield contract.methods.transfer(to, amountInWei).send({ from });
429
+ console.log("Contract created, sending transaction...");
430
+ const receipt = yield contract.methods.transfer(to, amountBN.toString()).send({ from });
431
+ console.log("ERC20 transaction successful:", receipt.transactionHash);
406
432
  return receipt.transactionHash;
407
433
  } else {
434
+ console.log("Preparing native token transfer...");
408
435
  const receipt = yield web3.eth.sendTransaction({
409
436
  from,
410
437
  to,
411
- value: amountInWei
438
+ value: amountBN.toString()
412
439
  });
440
+ console.log("Native transaction successful:", receipt.transactionHash);
413
441
  return receipt.transactionHash;
414
442
  }
415
443
  } catch (error) {
444
+ console.error("EVM transaction error:", error);
416
445
  if (error.code === 4001) {
417
446
  throw new Error("Transaction was rejected by user");
418
447
  }
419
- throw new Error(`Transaction failed: ${error.message}`);
448
+ if (error.message && error.message.includes("insufficient funds")) {
449
+ throw new Error("Insufficient funds to complete the transaction");
450
+ }
451
+ if (error.message && error.message.includes("gas")) {
452
+ throw new Error("Transaction failed due to gas estimation issues. Please try again.");
453
+ }
454
+ throw new Error(`Transaction failed: ${error.message || "Unknown error"}`);
420
455
  }
421
456
  });
422
457
  const sendTronTransaction = (to, amount, tokenAddress, tokenDecimals) => __async(void 0, null, function* () {