Skip to content

Commit

Permalink
Create cross_chain_bridge.sol
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Nov 22, 2024
1 parent 4045028 commit 03692e5
Showing 1 changed file with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract CrossChainBridge {
event TransferInitiated(address indexed sender, uint256 amount, string targetChain, address targetAddress);
event TransferCompleted(address indexed receiver, uint256 amount, string sourceChain);

struct Transfer {
address sender;
uint256 amount;
string targetChain;
address targetAddress;
bool completed;
}

mapping(bytes32 => Transfer) public transfers;

// Initiate a cross-chain transfer
function initiateTransfer(uint256 _amount, string memory _targetChain, address _targetAddress) public {
bytes32 transferId = keccak256(abi.encodePacked(msg.sender, _amount, _targetChain, _targetAddress, block.timestamp));
require(transfers[transferId].sender == address(0), "Transfer already initiated.");

transfers[transferId] = Transfer(msg.sender, _amount, _targetChain, _targetAddress, false);
emit TransferInitiated(msg.sender, _amount, _targetChain, _targetAddress);
}

// Complete a cross-chain transfer
function completeTransfer(bytes32 _transferId) public {
Transfer storage transfer = transfers[_transferId];
require(transfer.sender != address(0), "Transfer does not exist.");
require(!transfer.completed, "Transfer already completed.");

transfer.completed = true;
payable(transfer.targetAddress).transfer(transfer.amount);
emit TransferCompleted(transfer.targetAddress, transfer.amount, "Ethereum");
}

// Get transfer details
function getTransferDetails(bytes32 _transferId) public view returns (address, uint256, string memory, address, bool) {
Transfer storage transfer = transfers[_transferId];
return (transfer.sender, transfer.amount, transfer.targetChain, transfer.targetAddress, transfer.completed);
}

// Fallback function to receive Ether
receive() external payable {}
}

0 comments on commit 03692e5

Please sign in to comment.