coinley-checkout 0.3.9 → 0.4.1

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,9 +167,36 @@ const toHex = (num) => {
167
167
  return "0x" + Math.floor(parseFloat(num) * Math.pow(10, 18)).toString(16);
168
168
  };
169
169
  const calculateTokenAmount = (amount, decimals) => {
170
- const multiplier = Math.pow(10, decimals || 18);
171
- const amountInSmallestUnit = Math.floor(parseFloat(amount) * multiplier);
172
- return "0x" + amountInSmallestUnit.toString(16);
170
+ console.log("calculateTokenAmount called with:", { amount, decimals });
171
+ const tokenDecimals = parseInt(decimals) || 18;
172
+ const amountNum = parseFloat(amount);
173
+ if (isNaN(amountNum) || amountNum <= 0) {
174
+ throw new Error(`Invalid amount: ${amount}`);
175
+ }
176
+ const multiplier = Math.pow(10, tokenDecimals);
177
+ const amountInSmallestUnit = Math.floor(amountNum * multiplier);
178
+ const hexAmount = "0x" + amountInSmallestUnit.toString(16);
179
+ console.log("Amount calculation:", {
180
+ originalAmount: amount,
181
+ decimals: tokenDecimals,
182
+ multiplier,
183
+ calculatedAmount: amountInSmallestUnit,
184
+ hexAmount
185
+ });
186
+ return hexAmount;
187
+ };
188
+ const getTokenConfig = (currency, network) => {
189
+ console.log("getTokenConfig called with:", { currency, network });
190
+ const tokenConfig = TOKEN_CONFIG[currency];
191
+ if (!tokenConfig) {
192
+ throw new Error(`Unsupported currency: ${currency}`);
193
+ }
194
+ const networkConfig = tokenConfig[network];
195
+ if (!networkConfig) {
196
+ throw new Error(`Currency ${currency} not supported on network ${network}`);
197
+ }
198
+ console.log("Token config found:", networkConfig);
199
+ return networkConfig;
173
200
  };
174
201
  const detectWallets = () => {
175
202
  const wallets = {
@@ -422,12 +449,20 @@ const switchEVMNetwork = (networkConfig) => __async(void 0, null, function* () {
422
449
  });
423
450
  const sendTransaction = (walletConnection, transactionData) => __async(void 0, null, function* () {
424
451
  const { walletType, network, address } = walletConnection;
425
- const { to, amount, tokenAddress, tokenDecimals } = transactionData;
452
+ const { to, amount, tokenAddress, tokenDecimals, currency } = transactionData;
426
453
  console.log("sendTransaction called with:", { walletConnection, transactionData });
427
454
  switch (walletType) {
428
455
  case WALLET_TYPES.METAMASK:
429
456
  case WALLET_TYPES.TRUST_WALLET:
430
- return yield sendEVMTransactionNative(address, to, amount, tokenAddress, tokenDecimals);
457
+ return yield sendEVMTransactionNative(
458
+ address,
459
+ to,
460
+ amount,
461
+ tokenAddress,
462
+ tokenDecimals,
463
+ currency,
464
+ network
465
+ );
431
466
  case WALLET_TYPES.TRONLINK:
432
467
  return yield sendTronTransaction(to, amount, tokenAddress, tokenDecimals);
433
468
  case WALLET_TYPES.LUTE:
@@ -436,29 +471,64 @@ const sendTransaction = (walletConnection, transactionData) => __async(void 0, n
436
471
  throw new Error(`Unsupported wallet type: ${walletType}`);
437
472
  }
438
473
  });
439
- const sendEVMTransactionNative = (from, to, amount, tokenAddress, tokenDecimals) => __async(void 0, null, function* () {
440
- console.log("sendEVMTransactionNative called with:", { from, to, amount, tokenAddress, tokenDecimals });
474
+ const sendEVMTransactionNative = (from, to, amount, tokenAddress, tokenDecimals, currency, network) => __async(void 0, null, function* () {
475
+ console.log("sendEVMTransactionNative called with:", {
476
+ from,
477
+ to,
478
+ amount,
479
+ tokenAddress,
480
+ tokenDecimals,
481
+ currency,
482
+ network
483
+ });
441
484
  if (typeof window === "undefined" || !window.ethereum) {
442
485
  throw new Error("Ethereum provider not found");
443
486
  }
444
487
  try {
445
- if (tokenAddress && tokenAddress !== "native") {
488
+ let actualTokenAddress = tokenAddress;
489
+ let actualDecimals = tokenDecimals;
490
+ if (currency && network) {
491
+ try {
492
+ const tokenConfig = getTokenConfig(currency, network);
493
+ actualTokenAddress = tokenConfig.address;
494
+ actualDecimals = tokenConfig.decimals;
495
+ console.log("Using token config:", {
496
+ currency,
497
+ network,
498
+ address: actualTokenAddress,
499
+ decimals: actualDecimals
500
+ });
501
+ } catch (configError) {
502
+ console.warn("Could not get token config, using provided values:", configError.message);
503
+ }
504
+ }
505
+ if (actualTokenAddress && actualTokenAddress !== "native") {
446
506
  console.log("Preparing ERC20 token transfer...");
447
- const decimals = tokenDecimals || 18;
507
+ const decimals = actualDecimals || 18;
448
508
  const amountHex = calculateTokenAmount(amount, decimals);
449
509
  const transferMethodId = "0xa9059cbb";
450
- const paddedToAddress = to.replace("0x", "").padStart(64, "0");
510
+ const paddedToAddress = to.replace("0x", "").toLowerCase().padStart(64, "0");
451
511
  const paddedAmount = amountHex.replace("0x", "").padStart(64, "0");
452
512
  const data = transferMethodId + paddedToAddress + paddedAmount;
453
- console.log("ERC20 transaction data:", { amountHex, data });
513
+ console.log("ERC20 transaction details:", {
514
+ currency,
515
+ tokenAddress: actualTokenAddress,
516
+ decimals,
517
+ amountHex,
518
+ transferMethodId,
519
+ paddedToAddress,
520
+ paddedAmount,
521
+ data
522
+ });
454
523
  const txHash = yield window.ethereum.request({
455
524
  method: "eth_sendTransaction",
456
525
  params: [{
457
526
  from,
458
- to: tokenAddress,
527
+ to: actualTokenAddress,
528
+ // Send to token contract
459
529
  data,
460
530
  gas: "0x15F90"
461
- // 90000 gas limit
531
+ // 90000 gas limit for token transfers
462
532
  }]
463
533
  });
464
534
  console.log("ERC20 transaction successful:", txHash);
@@ -486,7 +556,10 @@ const sendEVMTransactionNative = (from, to, amount, tokenAddress, tokenDecimals)
486
556
  throw new Error("Transaction was rejected by user");
487
557
  }
488
558
  if (error.message && error.message.includes("insufficient funds")) {
489
- throw new Error("Insufficient funds to complete the transaction");
559
+ throw new Error("Insufficient balance to complete the transaction");
560
+ }
561
+ if (error.message && error.message.includes("gas")) {
562
+ throw new Error("Transaction failed due to gas estimation issues. Please try again.");
490
563
  }
491
564
  throw new Error(`Transaction failed: ${error.message || "Unknown error"}`);
492
565
  }