coinley-checkout 0.3.6 → 0.3.7

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.
@@ -95,7 +95,6 @@ const TOKEN_CONFIG = {
95
95
  },
96
96
  [NETWORK_TYPES.ALGORAND]: {
97
97
  address: "312769",
98
- // USDT ASA ID on Algorand
99
98
  decimals: 6
100
99
  }
101
100
  },
@@ -114,7 +113,6 @@ const TOKEN_CONFIG = {
114
113
  },
115
114
  [NETWORK_TYPES.ALGORAND]: {
116
115
  address: "31566704",
117
- // USDC ASA ID on Algorand
118
116
  decimals: 6
119
117
  }
120
118
  },
@@ -165,34 +163,14 @@ const TOKEN_CONFIG = {
165
163
  }
166
164
  }
167
165
  };
168
- const getWeb3Instance = () => __async(void 0, null, function* () {
169
- try {
170
- if (typeof window === "undefined" || !window.ethereum) {
171
- throw new Error("No ethereum provider found");
172
- }
173
- let Web3Class = null;
174
- try {
175
- const Web3Module = yield import("web3");
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.");
187
- }
188
- const web3Instance = new Web3Class(window.ethereum);
189
- console.log("Web3 instance created successfully:", web3Instance);
190
- return web3Instance;
191
- } catch (error) {
192
- console.error("Failed to load Web3:", error);
193
- throw new Error(`Web3 initialization failed: ${error.message}. Please ensure MetaMask is installed and running.`);
194
- }
195
- });
166
+ const toHex = (num) => {
167
+ return "0x" + Math.floor(parseFloat(num) * Math.pow(10, 18)).toString(16);
168
+ };
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);
173
+ };
196
174
  const detectWallets = () => {
197
175
  const wallets = {
198
176
  [WALLET_TYPES.METAMASK]: false,
@@ -240,9 +218,6 @@ const connectMetaMask = (network) => __async(void 0, null, function* () {
240
218
  if (typeof window === "undefined" || !window.ethereum) {
241
219
  throw new Error("MetaMask is not installed. Please install MetaMask extension.");
242
220
  }
243
- if (!window.ethereum.isMetaMask) {
244
- console.warn("MetaMask not detected as primary provider");
245
- }
246
221
  try {
247
222
  console.log("Requesting accounts from MetaMask...");
248
223
  const accounts = yield window.ethereum.request({
@@ -250,7 +225,7 @@ const connectMetaMask = (network) => __async(void 0, null, function* () {
250
225
  });
251
226
  console.log("Accounts received:", accounts);
252
227
  if (!accounts || accounts.length === 0) {
253
- throw new Error("No accounts found. Please unlock MetaMask and ensure you have at least one account.");
228
+ throw new Error("No accounts found. Please unlock MetaMask.");
254
229
  }
255
230
  const networkConfig = NETWORK_CONFIG[network];
256
231
  if (networkConfig && networkConfig.chainId) {
@@ -267,10 +242,10 @@ const connectMetaMask = (network) => __async(void 0, null, function* () {
267
242
  } catch (error) {
268
243
  console.error("MetaMask connection error:", error);
269
244
  if (error.code === 4001) {
270
- throw new Error("Connection rejected by user. Please accept the connection request in MetaMask.");
245
+ throw new Error("Connection rejected by user.");
271
246
  }
272
247
  if (error.code === -32002) {
273
- throw new Error("Connection request already pending. Please check MetaMask.");
248
+ throw new Error("Connection request pending. Please check MetaMask.");
274
249
  }
275
250
  throw new Error(`Failed to connect MetaMask: ${error.message}`);
276
251
  }
@@ -278,7 +253,7 @@ const connectMetaMask = (network) => __async(void 0, null, function* () {
278
253
  const connectTronLink = () => __async(void 0, null, function* () {
279
254
  var _a;
280
255
  if (typeof window === "undefined" || !window.tronWeb) {
281
- throw new Error("TronLink is not installed. Please install TronLink extension.");
256
+ throw new Error("TronLink is not installed.");
282
257
  }
283
258
  let attempts = 0;
284
259
  const maxAttempts = 10;
@@ -287,11 +262,11 @@ const connectTronLink = () => __async(void 0, null, function* () {
287
262
  attempts++;
288
263
  }
289
264
  if (!window.tronWeb.ready) {
290
- throw new Error("TronLink is not ready. Please unlock your TronLink wallet and try again.");
265
+ throw new Error("TronLink is not ready. Please unlock your wallet.");
291
266
  }
292
267
  const address = (_a = window.tronWeb.defaultAddress) == null ? void 0 : _a.base58;
293
268
  if (!address) {
294
- throw new Error("No account found in TronLink. Please make sure you have an account set up.");
269
+ throw new Error("No account found in TronLink.");
295
270
  }
296
271
  return {
297
272
  address,
@@ -301,7 +276,7 @@ const connectTronLink = () => __async(void 0, null, function* () {
301
276
  });
302
277
  const connectTrustWallet = (network) => __async(void 0, null, function* () {
303
278
  if (typeof window === "undefined" || !window.ethereum || !window.ethereum.isTrust) {
304
- throw new Error("Trust Wallet is not installed. Please install Trust Wallet extension.");
279
+ throw new Error("Trust Wallet is not installed.");
305
280
  }
306
281
  try {
307
282
  const accounts = yield window.ethereum.request({
@@ -328,7 +303,7 @@ const connectTrustWallet = (network) => __async(void 0, null, function* () {
328
303
  });
329
304
  const connectLute = () => __async(void 0, null, function* () {
330
305
  if (typeof window === "undefined" || !window.algorand || !window.algorand.isLute) {
331
- throw new Error("Lute wallet is not installed. Please install Lute wallet extension.");
306
+ throw new Error("Lute wallet is not installed.");
332
307
  }
333
308
  try {
334
309
  const accounts = yield window.algorand.connect();
@@ -358,7 +333,6 @@ const switchEVMNetwork = (networkConfig) => __async(void 0, null, function* () {
358
333
  } catch (switchError) {
359
334
  console.error("Network switch error:", switchError);
360
335
  if (switchError.code === 4902) {
361
- console.log("Network not found, attempting to add:", networkConfig.chainName);
362
336
  try {
363
337
  yield window.ethereum.request({
364
338
  method: "wallet_addEthereumChain",
@@ -366,13 +340,12 @@ const switchEVMNetwork = (networkConfig) => __async(void 0, null, function* () {
366
340
  });
367
341
  console.log("Network added successfully");
368
342
  } catch (addError) {
369
- console.error("Failed to add network:", addError);
370
- throw new Error(`Failed to add ${networkConfig.chainName} to wallet. Please add it manually.`);
343
+ throw new Error(`Failed to add ${networkConfig.chainName} to wallet.`);
371
344
  }
372
345
  } else if (switchError.code === 4001) {
373
- throw new Error("User rejected network switch request. Please switch network manually in MetaMask.");
346
+ throw new Error("User rejected network switch request.");
374
347
  } else {
375
- throw new Error(`Failed to switch to ${networkConfig.chainName}. Please switch manually in MetaMask.`);
348
+ throw new Error(`Failed to switch to ${networkConfig.chainName}.`);
376
349
  }
377
350
  }
378
351
  });
@@ -383,7 +356,7 @@ const sendTransaction = (walletConnection, transactionData) => __async(void 0, n
383
356
  switch (walletType) {
384
357
  case WALLET_TYPES.METAMASK:
385
358
  case WALLET_TYPES.TRUST_WALLET:
386
- return yield sendEVMTransaction(address, to, amount, tokenAddress, tokenDecimals);
359
+ return yield sendEVMTransactionNative(address, to, amount, tokenAddress, tokenDecimals);
387
360
  case WALLET_TYPES.TRONLINK:
388
361
  return yield sendTronTransaction(to, amount, tokenAddress, tokenDecimals);
389
362
  case WALLET_TYPES.LUTE:
@@ -392,53 +365,49 @@ const sendTransaction = (walletConnection, transactionData) => __async(void 0, n
392
365
  throw new Error(`Unsupported wallet type: ${walletType}`);
393
366
  }
394
367
  });
395
- const sendEVMTransaction = (from, to, amount, tokenAddress, tokenDecimals) => __async(void 0, null, function* () {
396
- console.log("sendEVMTransaction called with:", { from, to, amount, tokenAddress, tokenDecimals });
368
+ const sendEVMTransactionNative = (from, to, amount, tokenAddress, tokenDecimals) => __async(void 0, null, function* () {
369
+ console.log("sendEVMTransactionNative called with:", { from, to, amount, tokenAddress, tokenDecimals });
397
370
  if (typeof window === "undefined" || !window.ethereum) {
398
371
  throw new Error("Ethereum provider not found");
399
372
  }
400
373
  try {
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()
406
- );
407
- console.log("Amount calculated:", {
408
- originalAmount: amount,
409
- decimals,
410
- calculatedAmount: amountBN.toString()
411
- });
412
374
  if (tokenAddress && tokenAddress !== "native") {
413
375
  console.log("Preparing ERC20 token transfer...");
414
- const tokenABI = [
415
- {
416
- constant: false,
417
- inputs: [
418
- { name: "_to", type: "address" },
419
- { name: "_value", type: "uint256" }
420
- ],
421
- name: "transfer",
422
- outputs: [{ name: "", type: "bool" }],
423
- payable: false,
424
- stateMutability: "nonpayable",
425
- type: "function"
426
- }
427
- ];
428
- const contract = new web3.eth.Contract(tokenABI, tokenAddress);
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);
432
- return receipt.transactionHash;
376
+ const decimals = tokenDecimals || 18;
377
+ const amountHex = calculateTokenAmount(amount, decimals);
378
+ const transferMethodId = "0xa9059cbb";
379
+ const paddedToAddress = to.replace("0x", "").padStart(64, "0");
380
+ const paddedAmount = amountHex.replace("0x", "").padStart(64, "0");
381
+ const data = transferMethodId + paddedToAddress + paddedAmount;
382
+ console.log("ERC20 transaction data:", { amountHex, data });
383
+ const txHash = yield window.ethereum.request({
384
+ method: "eth_sendTransaction",
385
+ params: [{
386
+ from,
387
+ to: tokenAddress,
388
+ data,
389
+ gas: "0x15F90"
390
+ // 90000 gas limit
391
+ }]
392
+ });
393
+ console.log("ERC20 transaction successful:", txHash);
394
+ return txHash;
433
395
  } else {
434
396
  console.log("Preparing native token transfer...");
435
- const receipt = yield web3.eth.sendTransaction({
436
- from,
437
- to,
438
- value: amountBN.toString()
397
+ const amountHex = toHex(amount);
398
+ console.log("Native transaction amount:", { amount, amountHex });
399
+ const txHash = yield window.ethereum.request({
400
+ method: "eth_sendTransaction",
401
+ params: [{
402
+ from,
403
+ to,
404
+ value: amountHex,
405
+ gas: "0x5208"
406
+ // 21000 gas limit for simple transfer
407
+ }]
439
408
  });
440
- console.log("Native transaction successful:", receipt.transactionHash);
441
- return receipt.transactionHash;
409
+ console.log("Native transaction successful:", txHash);
410
+ return txHash;
442
411
  }
443
412
  } catch (error) {
444
413
  console.error("EVM transaction error:", error);
@@ -448,9 +417,6 @@ const sendEVMTransaction = (from, to, amount, tokenAddress, tokenDecimals) => __
448
417
  if (error.message && error.message.includes("insufficient funds")) {
449
418
  throw new Error("Insufficient funds to complete the transaction");
450
419
  }
451
- if (error.message && error.message.includes("gas")) {
452
- throw new Error("Transaction failed due to gas estimation issues. Please try again.");
453
- }
454
420
  throw new Error(`Transaction failed: ${error.message || "Unknown error"}`);
455
421
  }
456
422
  });