Skip to content

Commit

Permalink
complete swap in
Browse files Browse the repository at this point in the history
  • Loading branch information
harpreettrader committed Jul 31, 2024
1 parent 5da340f commit 4a06eda
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions newIDE/app/src/MainFrame/EditorContainers/HomePage/Broker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 4a06eda

Please sign in to comment.