coinley-checkout 0.3.8 → 0.4.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.
@@ -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 = {
@@ -182,16 +209,13 @@ const detectWallets = () => {
182
209
  return wallets;
183
210
  }
184
211
  try {
185
- console.log("=== WALLET DETECTION DEBUG ===");
186
- console.log("window.ethereum:", window.ethereum);
187
- console.log("window.tronWeb:", window.tronWeb);
188
- console.log("window.tronLink:", window.tronLink);
189
- console.log("window.algorand:", window.algorand);
212
+ console.log("=== ENHANCED WALLET DETECTION DEBUG ===");
190
213
  if (window.ethereum) {
191
214
  if (window.ethereum.isMetaMask) {
192
215
  wallets[WALLET_TYPES.METAMASK] = true;
193
- console.log("✅ MetaMask detected via isMetaMask");
194
- } else if (window.ethereum.providers) {
216
+ console.log("✅ MetaMask detected via direct isMetaMask");
217
+ }
218
+ if (window.ethereum.providers && Array.isArray(window.ethereum.providers)) {
195
219
  const metamaskProvider = window.ethereum.providers.find((p2) => p2.isMetaMask);
196
220
  if (metamaskProvider) {
197
221
  wallets[WALLET_TYPES.METAMASK] = true;
@@ -200,32 +224,43 @@ const detectWallets = () => {
200
224
  }
201
225
  }
202
226
  if (window.ethereum) {
203
- if (window.ethereum.isTrust) {
204
- wallets[WALLET_TYPES.TRUST_WALLET] = true;
205
- console.log("✅ Trust Wallet detected via isTrust");
206
- } else if (window.ethereum.isTrustWallet) {
207
- wallets[WALLET_TYPES.TRUST_WALLET] = true;
208
- console.log("✅ Trust Wallet detected via isTrustWallet");
209
- } else if (window.trustwallet) {
227
+ if (window.ethereum.isTrust || window.ethereum.isTrustWallet) {
210
228
  wallets[WALLET_TYPES.TRUST_WALLET] = true;
211
- console.log("✅ Trust Wallet detected via window.trustwallet");
212
- } else if (window.ethereum.providers) {
213
- const trustProvider = window.ethereum.providers.find((p2) => p2.isTrust || p2.isTrustWallet);
229
+ console.log("✅ Trust Wallet detected via direct property");
230
+ }
231
+ if (window.ethereum.providers && Array.isArray(window.ethereum.providers)) {
232
+ const trustProvider = window.ethereum.providers.find(
233
+ (p2) => p2.isTrust || p2.isTrustWallet || p2.constructor && p2.constructor.name === "TrustWallet"
234
+ );
214
235
  if (trustProvider) {
215
236
  wallets[WALLET_TYPES.TRUST_WALLET] = true;
216
237
  console.log("✅ Trust Wallet detected via providers array");
217
238
  }
218
239
  }
240
+ if (navigator.userAgent && navigator.userAgent.includes("Trust")) {
241
+ wallets[WALLET_TYPES.TRUST_WALLET] = true;
242
+ console.log("✅ Trust Wallet detected via user agent");
243
+ }
244
+ if (window.ethereum.isTrustWallet || window.trustwallet) {
245
+ wallets[WALLET_TYPES.TRUST_WALLET] = true;
246
+ console.log("✅ Trust Wallet detected via specific methods");
247
+ }
219
248
  }
220
- if (window.tronWeb) {
249
+ if (window.tronWeb && window.tronWeb.defaultAddress) {
221
250
  wallets[WALLET_TYPES.TRONLINK] = true;
222
251
  console.log("✅ TronLink detected via tronWeb");
223
- } else if (window.tronLink) {
252
+ }
253
+ if (window.tronLink) {
224
254
  wallets[WALLET_TYPES.TRONLINK] = true;
225
- console.log("✅ TronLink detected via tronLink");
226
- } else if (window.tron) {
255
+ console.log("✅ TronLink detected via tronLink object");
256
+ }
257
+ if (window.tron) {
227
258
  wallets[WALLET_TYPES.TRONLINK] = true;
228
- console.log("✅ TronLink detected via tron");
259
+ console.log("✅ TronLink detected via tron object");
260
+ }
261
+ if (window.tronWeb && (window.tronWeb.ready || window.tronWeb.installed)) {
262
+ wallets[WALLET_TYPES.TRONLINK] = true;
263
+ console.log("✅ TronLink detected via ready/installed properties");
229
264
  }
230
265
  if (window.algorand) {
231
266
  if (window.algorand.isLute) {
@@ -236,8 +271,19 @@ const detectWallets = () => {
236
271
  console.log("✅ Algorand wallet detected (assuming Lute)");
237
272
  }
238
273
  }
274
+ if (window.navigator && window.navigator.userAgent) {
275
+ const userAgent = window.navigator.userAgent.toLowerCase();
276
+ if (userAgent.includes("trustwallet")) {
277
+ wallets[WALLET_TYPES.TRUST_WALLET] = true;
278
+ console.log("✅ Trust Wallet detected via mobile user agent");
279
+ }
280
+ if (userAgent.includes("tronlink")) {
281
+ wallets[WALLET_TYPES.TRONLINK] = true;
282
+ console.log("✅ TronLink detected via mobile user agent");
283
+ }
284
+ }
239
285
  console.log("Final wallet detection results:", wallets);
240
- console.log("=== END WALLET DETECTION DEBUG ===");
286
+ console.log("=== END ENHANCED WALLET DETECTION DEBUG ===");
241
287
  } catch (error) {
242
288
  console.warn("Error detecting wallets:", error);
243
289
  }
@@ -403,12 +449,20 @@ const switchEVMNetwork = (networkConfig) => __async(void 0, null, function* () {
403
449
  });
404
450
  const sendTransaction = (walletConnection, transactionData) => __async(void 0, null, function* () {
405
451
  const { walletType, network, address } = walletConnection;
406
- const { to, amount, tokenAddress, tokenDecimals } = transactionData;
452
+ const { to, amount, tokenAddress, tokenDecimals, currency } = transactionData;
407
453
  console.log("sendTransaction called with:", { walletConnection, transactionData });
408
454
  switch (walletType) {
409
455
  case WALLET_TYPES.METAMASK:
410
456
  case WALLET_TYPES.TRUST_WALLET:
411
- 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
+ );
412
466
  case WALLET_TYPES.TRONLINK:
413
467
  return yield sendTronTransaction(to, amount, tokenAddress, tokenDecimals);
414
468
  case WALLET_TYPES.LUTE:
@@ -417,29 +471,64 @@ const sendTransaction = (walletConnection, transactionData) => __async(void 0, n
417
471
  throw new Error(`Unsupported wallet type: ${walletType}`);
418
472
  }
419
473
  });
420
- const sendEVMTransactionNative = (from, to, amount, tokenAddress, tokenDecimals) => __async(void 0, null, function* () {
421
- 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
+ });
422
484
  if (typeof window === "undefined" || !window.ethereum) {
423
485
  throw new Error("Ethereum provider not found");
424
486
  }
425
487
  try {
426
- 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") {
427
506
  console.log("Preparing ERC20 token transfer...");
428
- const decimals = tokenDecimals || 18;
507
+ const decimals = actualDecimals || 18;
429
508
  const amountHex = calculateTokenAmount(amount, decimals);
430
509
  const transferMethodId = "0xa9059cbb";
431
- const paddedToAddress = to.replace("0x", "").padStart(64, "0");
510
+ const paddedToAddress = to.replace("0x", "").toLowerCase().padStart(64, "0");
432
511
  const paddedAmount = amountHex.replace("0x", "").padStart(64, "0");
433
512
  const data = transferMethodId + paddedToAddress + paddedAmount;
434
- 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
+ });
435
523
  const txHash = yield window.ethereum.request({
436
524
  method: "eth_sendTransaction",
437
525
  params: [{
438
526
  from,
439
- to: tokenAddress,
527
+ to: actualTokenAddress,
528
+ // Send to token contract
440
529
  data,
441
530
  gas: "0x15F90"
442
- // 90000 gas limit
531
+ // 90000 gas limit for token transfers
443
532
  }]
444
533
  });
445
534
  console.log("ERC20 transaction successful:", txHash);
@@ -467,7 +556,10 @@ const sendEVMTransactionNative = (from, to, amount, tokenAddress, tokenDecimals)
467
556
  throw new Error("Transaction was rejected by user");
468
557
  }
469
558
  if (error.message && error.message.includes("insufficient funds")) {
470
- 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.");
471
563
  }
472
564
  throw new Error(`Transaction failed: ${error.message || "Unknown error"}`);
473
565
  }