Last active
February 20, 2024 01:19
-
-
Save jboetticher/0188244031df80e9b180568e30bfa7a5 to your computer and use it in GitHub Desktop.
A contract that sends and stores a string between chains using the Axelar Network.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity 0.8.9; | |
import { IAxelarExecutable } from "https://github.com/axelarnetwork/axelar-gmp-sdk-solidity/blob/main/contracts/executable/AxelarExecutable.sol"; | |
import { StringToAddress, AddressToString } from "https://github.com/axelarnetwork/axelar-gmp-sdk-solidity/blob/main/contracts/utils/AddressString.sol"; | |
import { IERC20 } from "https://github.com/axelarnetwork/axelar-cgp-solidity/blob/main/contracts/interfaces/IERC20.sol"; | |
import { IAxelarGateway } from "https://github.com/axelarnetwork/axelar-cgp-solidity/blob/main/contracts/interfaces/IAxelarGateway.sol"; | |
import { IAxelarGasService } from "https://github.com/axelarnetwork/axelar-cgp-solidity/blob/main/contracts/interfaces/IAxelarGasService.sol"; | |
// Packages and sends a string from one chain to another. | |
contract SimpleGeneralMessage is IAxelarExecutable { | |
using AddressToString for address; | |
using StringToAddress for string; | |
// Axelar's gas microservice contract | |
IAxelarGasService gasService; | |
// The last message we received from Axelar | |
string public lastMessage; | |
constructor(address gateway_, address gasService_) | |
IAxelarExecutable(address(0)) | |
{ | |
gasService = IAxelarGasService(gasService_); | |
// We are explicitly setting the gateway for demonstration purposes | |
// You could also place gateway_ into the IAxelarExecutable constructor | |
gateway = IAxelarGateway(gateway_); | |
} | |
// Locks and sends a token | |
// REMEMBER TO SEND VALUE AS GAS PAYMENT | |
function sendMessage( | |
string memory message, | |
string memory destinationAddress, | |
string memory destinationChain | |
) external payable { | |
// Create the payload. | |
bytes memory payload = abi.encode(message); | |
// Pay for gas on destination chain | |
// You may want to use the SDK to find out how much gas you need | |
gasService.payNativeGasForContractCall{value: msg.value}( | |
address(this), | |
destinationChain, | |
destinationAddress, | |
payload, | |
msg.sender | |
); | |
// Call gateway contract to call remote contract | |
gateway.callContract(destinationChain, destinationAddress, payload); | |
} | |
// This is automatically executed by Axelar Relay Services if gas was paid for | |
function _execute( | |
string memory, /*sourceChain*/ | |
string memory, /*sourceAddress*/ | |
bytes calldata payload | |
) internal override { | |
//Decode the payload. | |
lastMessage = abi.decode(payload, (string)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment