Skip to content

Instantly share code, notes, and snippets.

@StephenFluin
Created July 18, 2023 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StephenFluin/d1dfe5c4039803f59c9f82d3f7e1c847 to your computer and use it in GitHub Desktop.
Save StephenFluin/d1dfe5c4039803f59c9f82d3f7e1c847 to your computer and use it in GitHub Desktop.
A quick example of writing an Axelar Executable that only accepts messages from a single source
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {AxelarExecutable} from "@axelar-network/axelar-gmp-sdk-solidity/contracts/executable/AxelarExecutable.sol";
import {IAxelarGateway} from "@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IAxelarGateway.sol";
import {IAxelarGasService} from "@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IAxelarGasService.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract SenderReceiver is AxelarExecutable, Ownable {
IAxelarGasService public immutable gasService;
string public message;
bytes32 public expectedSource;
constructor(address gateway_, address gasService_)
AxelarExecutable(gateway_)
{
gasService = IAxelarGasService(gasService_);
}
function establishConnection(
string calldata sourceChain,
string calldata sourceAddress
) public onlyOwner {
expectedSource = keccak256(abi.encode(sourceChain, sourceAddress));
}
function sendMessage(
string calldata destinationChain,
string calldata destinationAddress,
string calldata message_
) external payable {
bytes memory payload = abi.encode(message_);
gasService.payNativeGasForContractCall{value: msg.value}(
address(this),
destinationChain,
destinationAddress,
payload,
msg.sender
);
gateway.callContract(destinationChain, destinationAddress, payload);
}
function _execute(
string calldata sourceChain,
string calldata sourceAddress,
bytes calldata payload_
) internal override {
require(
expectedSource == keccak256(abi.encode(sourceChain, sourceAddress)),
"Source was not as expected"
);
message = abi.decode(payload_, (string));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment