Skip to content

Instantly share code, notes, and snippets.

@Genesis3800
Created August 23, 2023 10:25
Show Gist options
  • Save Genesis3800/e783ff07e8e0a76a9b7c1862ee90ee33 to your computer and use it in GitHub Desktop.
Save Genesis3800/e783ff07e8e0a76a9b7c1862ee90ee33 to your computer and use it in GitHub Desktop.
transfer function se pehle ka kaam ho gaya
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
pragma abicoder v2;
import "https://github.com/LayerZero-Labs/solidity-examples/blob/main/contracts/lzApp/NonblockingLzApp.sol";
/// @title A LayerZero example sending a cross chain message from a source chain to a destination chain to increment a counter
contract LayerZeroSwap_Mumbai is NonblockingLzApp {
address payable deployer;
uint16 public destChainId;
bytes payload;
address payable contractAddress = payable(address(this));
ILayerZeroEndpoint public immutable endpoint;
//Variables for testing, remove later on
uint public valueReceived;
uint public MATICtransferred;
address public _recipient;
// Events
event MATICsent(uint indexed _value, address indexed _to);
event msgRecFromSepolia(uint indexed _value, address indexed _to);
// address public destAddress;
constructor(address _lzEndpoint) NonblockingLzApp(_lzEndpoint) {
deployer = payable(msg.sender);
endpoint = ILayerZeroEndpoint(_lzEndpoint);
//If Source == Sepolia, then Destination Chain = Mumbai
if (_lzEndpoint == 0xae92d5aD7583AD66E49A0c67BAd18F6ba52dDDc1) destChainId = 10109;
//If Source == Mumbai, then Destination Chain = Sepolia
if (_lzEndpoint == 0xf69186dfBa60DdB133E91E9A4B5673624293d8F8) destChainId = 10161;
}
function swapTo_ETH(address Receiver) public payable {
require(msg.value >= 5 ether, "Please send at least 5 MATIC");
uint value = msg.value;
bytes memory trustedRemote = trustedRemoteLookup[destChainId];
require(trustedRemote.length != 0, "LzApp: destination chain is not a trusted source");
_checkPayloadSize(destChainId, payload.length);
// The message is encoded as bytes and stored in the "payload" variable.
payload = abi.encode(Receiver, value);
endpoint.send{value: 15 ether}(destChainId, trustedRemote, payload, contractAddress, address(0x0), bytes(""));
emit MATICsent(value, Receiver);
}
function _nonblockingLzReceive(uint16 _srcChainId, bytes memory _srcAddress, uint64 _nonce, bytes memory _payload) internal override {
(address Receiver , uint Value) = abi.decode(_payload, (address, uint));
address payable recipient = payable(Receiver);
valueReceived = Value;
_recipient = recipient;
emit msgRecFromSepolia(valueReceived, _recipient);
// recipient.transfer(MATIC_transferred);
}
receive() external payable {
}
function withdrawAll() external onlyOwner {
deployer.transfer(address(this).balance);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment