From 4a06edada54c9bd9aacb03a74d5c3dd613ed886a Mon Sep 17 00:00:00 2001 From: Happy Date: Wed, 31 Jul 2024 17:38:20 +0530 Subject: [PATCH] complete swap in --- .../EditorContainers/HomePage/Broker.js | 66 +++++++++++-------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/newIDE/app/src/MainFrame/EditorContainers/HomePage/Broker.js b/newIDE/app/src/MainFrame/EditorContainers/HomePage/Broker.js index 5d07130..2a7617b 100644 --- a/newIDE/app/src/MainFrame/EditorContainers/HomePage/Broker.js +++ b/newIDE/app/src/MainFrame/EditorContainers/HomePage/Broker.js @@ -58,36 +58,44 @@ const SwapInterface = () => { return; } - let tokenAddress; - if (sourceAsset === "flip.eth") { - tokenAddress = '0xdC27c60956cB065D19F08bb69a707E37b36d8086'; - } else if (sourceAsset === "usdt.eth") { - tokenAddress = "0x27CEA6Eb8a21Aae05Eb29C91c5CA10592892F584"; - } else { - alert("We don't switch from this source now, we are working on it...."); - return; - } - - const erc20Abi = [ - "function transfer(address to, uint256 amount) public returns (bool)", - "function balanceOf(address addr) view returns (uint)" - ]; - - const tokenContract = new ethers.Contract(tokenAddress, erc20Abi, signer); - console.log('Token Contract:', tokenContract); - - const amountInUnits = ethers.parseUnits(amount, 6); - console.log('Recipient:', depositAddr); - console.log('Amount in units:', amountInUnits.toString()); - try { - console.log('Preparing to send transaction with:', { depositAddr, amountInUnits }); - const tx = await tokenContract.transfer(depositAddr, amountInUnits); - console.log('Transaction:', tx); - console.log(await signer, "h"); - console.log(provider, "h"); - await tx.wait(); - setStatus('Transaction confirmed!'); + // Check if the source asset is Sepolia ETH' + if (sourceAsset === "eth.eth") { + + const tx = await signer.sendTransaction({ + to: depositAddr, + value: ethers.parseEther(amount) // ETH has 18 decimals + }); + console.log('Transaction:', tx); + await tx.wait(); + setStatus('Transaction confirmed!'); + return; + }else { + // Handle ERC-20 token transfers + let tokenAddress; + if (sourceAsset === "flip.eth") { + tokenAddress = '0xdC27c60956cB065D19F08bb69a707E37b36d8086'; + } else if (sourceAsset === "usdt.eth") { + tokenAddress = "0x27CEA6Eb8a21Aae05Eb29C91c5CA10592892F584"; + } else { + alert("We don't switch from this source now, we are working on it...."); + return; + } + + // ERC-20 contract ABI + const erc20Abi = [ + "function transfer(address to, uint256 amount) public returns (bool)", + "function balanceOf(address addr) view returns (uint)" + ]; + + const tokenContract = new ethers.Contract(tokenAddress, erc20Abi, signer); + const amountInUnits = ethers.parseUnits(amount, 6); + const tx = await tokenContract.transfer(depositAddr, amountInUnits); + console.log('Transaction:', tx); + await tx.wait(); + setStatus('Transaction confirmed!'); + } + } catch (error) { console.error('Error sending token:', error); setStatus('Error sending token');