coinley-checkout 0.3.5 → 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,19 +163,14 @@ const TOKEN_CONFIG = {
|
|
165
163
|
}
|
166
164
|
}
|
167
165
|
};
|
168
|
-
const
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
} catch (error) {
|
177
|
-
console.error("Failed to load Web3:", error);
|
178
|
-
throw new Error("Web3 is required for blockchain transactions. Please install MetaMask or another Web3 wallet.");
|
179
|
-
}
|
180
|
-
});
|
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
|
+
};
|
181
174
|
const detectWallets = () => {
|
182
175
|
const wallets = {
|
183
176
|
[WALLET_TYPES.METAMASK]: false,
|
@@ -225,9 +218,6 @@ const connectMetaMask = (network) => __async(void 0, null, function* () {
|
|
225
218
|
if (typeof window === "undefined" || !window.ethereum) {
|
226
219
|
throw new Error("MetaMask is not installed. Please install MetaMask extension.");
|
227
220
|
}
|
228
|
-
if (!window.ethereum.isMetaMask) {
|
229
|
-
console.warn("MetaMask not detected as primary provider");
|
230
|
-
}
|
231
221
|
try {
|
232
222
|
console.log("Requesting accounts from MetaMask...");
|
233
223
|
const accounts = yield window.ethereum.request({
|
@@ -235,7 +225,7 @@ const connectMetaMask = (network) => __async(void 0, null, function* () {
|
|
235
225
|
});
|
236
226
|
console.log("Accounts received:", accounts);
|
237
227
|
if (!accounts || accounts.length === 0) {
|
238
|
-
throw new Error("No accounts found. Please unlock MetaMask
|
228
|
+
throw new Error("No accounts found. Please unlock MetaMask.");
|
239
229
|
}
|
240
230
|
const networkConfig = NETWORK_CONFIG[network];
|
241
231
|
if (networkConfig && networkConfig.chainId) {
|
@@ -252,10 +242,10 @@ const connectMetaMask = (network) => __async(void 0, null, function* () {
|
|
252
242
|
} catch (error) {
|
253
243
|
console.error("MetaMask connection error:", error);
|
254
244
|
if (error.code === 4001) {
|
255
|
-
throw new Error("Connection rejected by user.
|
245
|
+
throw new Error("Connection rejected by user.");
|
256
246
|
}
|
257
247
|
if (error.code === -32002) {
|
258
|
-
throw new Error("Connection request
|
248
|
+
throw new Error("Connection request pending. Please check MetaMask.");
|
259
249
|
}
|
260
250
|
throw new Error(`Failed to connect MetaMask: ${error.message}`);
|
261
251
|
}
|
@@ -263,7 +253,7 @@ const connectMetaMask = (network) => __async(void 0, null, function* () {
|
|
263
253
|
const connectTronLink = () => __async(void 0, null, function* () {
|
264
254
|
var _a;
|
265
255
|
if (typeof window === "undefined" || !window.tronWeb) {
|
266
|
-
throw new Error("TronLink is not installed.
|
256
|
+
throw new Error("TronLink is not installed.");
|
267
257
|
}
|
268
258
|
let attempts = 0;
|
269
259
|
const maxAttempts = 10;
|
@@ -272,11 +262,11 @@ const connectTronLink = () => __async(void 0, null, function* () {
|
|
272
262
|
attempts++;
|
273
263
|
}
|
274
264
|
if (!window.tronWeb.ready) {
|
275
|
-
throw new Error("TronLink is not ready. Please unlock your
|
265
|
+
throw new Error("TronLink is not ready. Please unlock your wallet.");
|
276
266
|
}
|
277
267
|
const address = (_a = window.tronWeb.defaultAddress) == null ? void 0 : _a.base58;
|
278
268
|
if (!address) {
|
279
|
-
throw new Error("No account found in TronLink.
|
269
|
+
throw new Error("No account found in TronLink.");
|
280
270
|
}
|
281
271
|
return {
|
282
272
|
address,
|
@@ -286,7 +276,7 @@ const connectTronLink = () => __async(void 0, null, function* () {
|
|
286
276
|
});
|
287
277
|
const connectTrustWallet = (network) => __async(void 0, null, function* () {
|
288
278
|
if (typeof window === "undefined" || !window.ethereum || !window.ethereum.isTrust) {
|
289
|
-
throw new Error("Trust Wallet is not installed.
|
279
|
+
throw new Error("Trust Wallet is not installed.");
|
290
280
|
}
|
291
281
|
try {
|
292
282
|
const accounts = yield window.ethereum.request({
|
@@ -313,7 +303,7 @@ const connectTrustWallet = (network) => __async(void 0, null, function* () {
|
|
313
303
|
});
|
314
304
|
const connectLute = () => __async(void 0, null, function* () {
|
315
305
|
if (typeof window === "undefined" || !window.algorand || !window.algorand.isLute) {
|
316
|
-
throw new Error("Lute wallet is not installed.
|
306
|
+
throw new Error("Lute wallet is not installed.");
|
317
307
|
}
|
318
308
|
try {
|
319
309
|
const accounts = yield window.algorand.connect();
|
@@ -343,7 +333,6 @@ const switchEVMNetwork = (networkConfig) => __async(void 0, null, function* () {
|
|
343
333
|
} catch (switchError) {
|
344
334
|
console.error("Network switch error:", switchError);
|
345
335
|
if (switchError.code === 4902) {
|
346
|
-
console.log("Network not found, attempting to add:", networkConfig.chainName);
|
347
336
|
try {
|
348
337
|
yield window.ethereum.request({
|
349
338
|
method: "wallet_addEthereumChain",
|
@@ -351,23 +340,23 @@ const switchEVMNetwork = (networkConfig) => __async(void 0, null, function* () {
|
|
351
340
|
});
|
352
341
|
console.log("Network added successfully");
|
353
342
|
} catch (addError) {
|
354
|
-
|
355
|
-
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.`);
|
356
344
|
}
|
357
345
|
} else if (switchError.code === 4001) {
|
358
|
-
throw new Error("User rejected network switch request.
|
346
|
+
throw new Error("User rejected network switch request.");
|
359
347
|
} else {
|
360
|
-
throw new Error(`Failed to switch to ${networkConfig.chainName}
|
348
|
+
throw new Error(`Failed to switch to ${networkConfig.chainName}.`);
|
361
349
|
}
|
362
350
|
}
|
363
351
|
});
|
364
352
|
const sendTransaction = (walletConnection, transactionData) => __async(void 0, null, function* () {
|
365
353
|
const { walletType, network, address } = walletConnection;
|
366
354
|
const { to, amount, tokenAddress, tokenDecimals } = transactionData;
|
355
|
+
console.log("sendTransaction called with:", { walletConnection, transactionData });
|
367
356
|
switch (walletType) {
|
368
357
|
case WALLET_TYPES.METAMASK:
|
369
358
|
case WALLET_TYPES.TRUST_WALLET:
|
370
|
-
return yield
|
359
|
+
return yield sendEVMTransactionNative(address, to, amount, tokenAddress, tokenDecimals);
|
371
360
|
case WALLET_TYPES.TRONLINK:
|
372
361
|
return yield sendTronTransaction(to, amount, tokenAddress, tokenDecimals);
|
373
362
|
case WALLET_TYPES.LUTE:
|
@@ -376,47 +365,59 @@ const sendTransaction = (walletConnection, transactionData) => __async(void 0, n
|
|
376
365
|
throw new Error(`Unsupported wallet type: ${walletType}`);
|
377
366
|
}
|
378
367
|
});
|
379
|
-
const
|
368
|
+
const sendEVMTransactionNative = (from, to, amount, tokenAddress, tokenDecimals) => __async(void 0, null, function* () {
|
369
|
+
console.log("sendEVMTransactionNative called with:", { from, to, amount, tokenAddress, tokenDecimals });
|
380
370
|
if (typeof window === "undefined" || !window.ethereum) {
|
381
371
|
throw new Error("Ethereum provider not found");
|
382
372
|
}
|
383
373
|
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))
|
388
|
-
);
|
389
374
|
if (tokenAddress && tokenAddress !== "native") {
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
375
|
+
console.log("Preparing ERC20 token transfer...");
|
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;
|
407
395
|
} else {
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
396
|
+
console.log("Preparing native token transfer...");
|
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
|
+
}]
|
412
408
|
});
|
413
|
-
|
409
|
+
console.log("Native transaction successful:", txHash);
|
410
|
+
return txHash;
|
414
411
|
}
|
415
412
|
} catch (error) {
|
413
|
+
console.error("EVM transaction error:", error);
|
416
414
|
if (error.code === 4001) {
|
417
415
|
throw new Error("Transaction was rejected by user");
|
418
416
|
}
|
419
|
-
|
417
|
+
if (error.message && error.message.includes("insufficient funds")) {
|
418
|
+
throw new Error("Insufficient funds to complete the transaction");
|
419
|
+
}
|
420
|
+
throw new Error(`Transaction failed: ${error.message || "Unknown error"}`);
|
420
421
|
}
|
421
422
|
});
|
422
423
|
const sendTronTransaction = (to, amount, tokenAddress, tokenDecimals) => __async(void 0, null, function* () {
|