// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/Address.sol)
pragma solidity ^0.8.1;
/**
* @dev Collection of functions related to the address type
*/
library Address {
/**
* @dev Returns true if `account` is a contract.
*
* [IMPORTANT]
* ====
* It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*
* Among others, `isContract` will return false for the following
* types of addresses:
*
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
*
* Furthermore, `isContract` will also return true if the target contract within
* the same transaction is already scheduled for destruction by `SELFDESTRUCT`,
* which only has an effect at the end of a transaction.
* ====
*
* [IMPORTANT]
* ====
* You shouldn't rely on `isContract` to protect against flash loan attacks!
*
* Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
* like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
* constructor.
* ====
*/
function isContract(address account) internal view returns (bool) {
// This method relies on extcodesize/address.code.length, which returns 0
// for contracts in construction, since the code is only stored at the end
// of the constructor execution.
return account.code.length > 0;
}
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.8.0/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
(bool success, ) = recipient.call{value: amount}("");
require(success, "Address: unable to send value, recipient may have reverted");
}
/**
* @dev Performs a Solidity function call using a low level `call`. A
* plain `call` is an unsafe replacement for a function call: use this
* function instead.
*
* If `target` reverts with a revert reason, it is bubbled up by this
* function (like regular Solidity function calls).
*
* Returns the raw returned data. To convert to the expected return value,
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
*
* Requirements:
*
* - `target` must be a contract.
* - calling `target` with `data` must not revert.
*
* _Available since v3.1._
*/
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, "Address: low-level call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
* `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but also transferring `value` wei to `target`.
*
* Requirements:
*
* - the calling contract must have an ETH balance of at least `value`.
* - the called Solidity function must be `payable`.
*
* _Available since v3.1._
*/
function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
/**
* @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
* with `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCallWithValue(
address target,
bytes memory data,
uint256 value,
string memory errorMessage
) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(
address target,
bytes memory data,
string memory errorMessage
) internal view returns (bytes memory) {
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a delegate call.
*
* _Available since v3.4._
*/
function functionDelegateCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
/**
* @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
* the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
*
* _Available since v4.8._
*/
function verifyCallResultFromTarget(
address target,
bool success,
bytes memory returndata,
string memory errorMessage
) internal view returns (bytes memory) {
if (success) {
if (returndata.length == 0) {
// only check isContract if the call was successful and the return data is empty
// otherwise we already know that it was a contract
require(isContract(target), "Address: call to non-contract");
}
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
/**
* @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
* revert reason or using the provided one.
*
* _Available since v4.3._
*/
function verifyCallResult(
bool success,
bytes memory returndata,
string memory errorMessage
) internal pure returns (bytes memory) {
if (success) {
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
function _revert(bytes memory returndata, string memory errorMessage) private pure {
// Look for revert reason and bubble it up if present
if (returndata.length > 0) {
// The easiest way to bubble the revert reason is using memory via assembly
/// @solidity memory-safe-assembly
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
/// @title Address to Bytes32 Library
/// @notice A library that converts address to bytes32 and bytes32 to address
library AddressBytes32 {
/// @notice Converts an address to bytes32
/// @param addr The address to be converted
function toBytes32(address addr) internal pure returns (bytes32) {
return bytes32(uint256(uint160(addr)));
}
function toAddress(bytes32 b) internal pure returns (address) {
return address(uint160(uint256(b)));
}
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
/// @title String to Address Library
/// @notice A library that a string to addresses
library AddressString {
error AddressString__InvalidByteValue(bytes1 b);
/// @notice Converts an hex string to address
/// @param _hexString The hex string to be converted
function toAddress(
string memory _hexString
) internal pure returns (address) {
bytes memory byte_sString = bytes(_hexString);
uint160 _parsedBytes = 0;
for (uint256 i = 0; i < byte_sString.length; i += 2) {
_parsedBytes *= 256;
uint8 byte_Value = parseByteToUint8(byte_sString[i]);
byte_Value *= 16;
byte_Value += parseByteToUint8(byte_sString[i + 1]);
_parsedBytes += byte_Value;
}
return address(bytes20(_parsedBytes));
}
/// @notice Converts a bytes1 to uint8
/// @param _byte The byte value to convert
function parseByteToUint8(bytes1 _byte) internal pure returns (uint8) {
if (uint8(_byte) >= 48 && uint8(_byte) <= 57) {
return uint8(_byte) - 48;
} else if (uint8(_byte) >= 65 && uint8(_byte) <= 70) {
return uint8(_byte) - 55;
} else if (uint8(_byte) >= 97 && uint8(_byte) <= 102) {
return uint8(_byte) - 87;
} else {
revert AddressString__InvalidByteValue(_byte);
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import { IAxelarGateway } from '../interfaces/IAxelarGateway.sol';
import { IAxelarExecutable } from '../interfaces/IAxelarExecutable.sol';
contract AxelarExecutable is IAxelarExecutable {
IAxelarGateway public immutable gateway;
constructor(address gateway_) {
if (gateway_ == address(0)) revert InvalidAddress();
gateway = IAxelarGateway(gateway_);
}
function execute(
bytes32 commandId,
string calldata sourceChain,
string calldata sourceAddress,
bytes calldata payload
) external {
bytes32 payloadHash = keccak256(payload);
if (!gateway.validateContractCall(commandId, sourceChain, sourceAddress, payloadHash))
revert NotApprovedByGateway();
_execute(sourceChain, sourceAddress, payload);
}
function executeWithToken(
bytes32 commandId,
string calldata sourceChain,
string calldata sourceAddress,
bytes calldata payload,
string calldata tokenSymbol,
uint256 amount
) external {
bytes32 payloadHash = keccak256(payload);
if (
!gateway.validateContractCallAndMint(
commandId,
sourceChain,
sourceAddress,
payloadHash,
tokenSymbol,
amount
)
) revert NotApprovedByGateway();
_executeWithToken(sourceChain, sourceAddress, payload, tokenSymbol, amount);
}
function _execute(
string calldata sourceChain,
string calldata sourceAddress,
bytes calldata payload
) internal virtual {}
function _executeWithToken(
string calldata sourceChain,
string calldata sourceAddress,
bytes calldata payload,
string calldata tokenSymbol,
uint256 amount
) internal virtual {}
}
// SPDX-License-Identifier: MIT
// modified from https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/structs/BitMaps.sol
pragma solidity ^0.8.18;
type BitMap256 is uint256;
using BitMaps for BitMap256 global;
library BitMaps {
/**
* @dev Returns whether the bit at `index` is set.
*/
function get(BitMap256 bitmap, uint8 index) internal pure returns (bool) {
uint256 mask = 1 << index;
return BitMap256.unwrap(bitmap) & mask != 0;
}
/**
* @dev Sets the bit at `index`.
*/
function set(BitMap256 bitmap, uint8 index) internal pure returns (BitMap256) {
uint256 mask = 1 << index;
return BitMap256.wrap(BitMap256.unwrap(bitmap) | mask);
}
}
// SPDX-License-Identifier: Unlicense
/*
* @title Solidity Bytes Arrays Utils
* @author Gonçalo Sá <goncalo.sa@consensys.net>
*
* @dev Bytes tightly packed arrays utility library for ethereum contracts written in Solidity.
* The library lets you concatenate, slice and type cast bytes arrays both in memory and storage.
*/
pragma solidity >=0.8.0 <0.9.0;
library BytesLib {
function concat(bytes memory _preBytes, bytes memory _postBytes) internal pure returns (bytes memory) {
bytes memory tempBytes;
assembly {
// Get a location of some free memory and store it in tempBytes as
// Solidity does for memory variables.
tempBytes := mload(0x40)
// Store the length of the first bytes array at the beginning of
// the memory for tempBytes.
let length := mload(_preBytes)
mstore(tempBytes, length)
// Maintain a memory counter for the current write location in the
// temp bytes array by adding the 32 bytes for the array length to
// the starting location.
let mc := add(tempBytes, 0x20)
// Stop copying when the memory counter reaches the length of the
// first bytes array.
let end := add(mc, length)
for {
// Initialize a copy counter to the start of the _preBytes data,
// 32 bytes into its memory.
let cc := add(_preBytes, 0x20)
} lt(mc, end) {
// Increase both counters by 32 bytes each iteration.
mc := add(mc, 0x20)
cc := add(cc, 0x20)
} {
// Write the _preBytes data into the tempBytes memory 32 bytes
// at a time.
mstore(mc, mload(cc))
}
// Add the length of _postBytes to the current length of tempBytes
// and store it as the new length in the first 32 bytes of the
// tempBytes memory.
length := mload(_postBytes)
mstore(tempBytes, add(length, mload(tempBytes)))
// Move the memory counter back from a multiple of 0x20 to the
// actual end of the _preBytes data.
mc := end
// Stop copying when the memory counter reaches the new combined
// length of the arrays.
end := add(mc, length)
for {
let cc := add(_postBytes, 0x20)
} lt(mc, end) {
mc := add(mc, 0x20)
cc := add(cc, 0x20)
} {
mstore(mc, mload(cc))
}
// Update the free-memory pointer by padding our last write location
// to 32 bytes: add 31 bytes to the end of tempBytes to move to the
// next 32 byte block, then round down to the nearest multiple of
// 32. If the sum of the length of the two arrays is zero then add
// one before rounding down to leave a blank 32 bytes (the length block with 0).
mstore(
0x40,
and(
add(add(end, iszero(add(length, mload(_preBytes)))), 31),
not(31) // Round down to the nearest 32 bytes.
)
)
}
return tempBytes;
}
function concatStorage(bytes storage _preBytes, bytes memory _postBytes) internal {
assembly {
// Read the first 32 bytes of _preBytes storage, which is the length
// of the array. (We don't need to use the offset into the slot
// because arrays use the entire slot.)
let fslot := sload(_preBytes.slot)
// Arrays of 31 bytes or less have an even value in their slot,
// while longer arrays have an odd value. The actual length is
// the slot divided by two for odd values, and the lowest order
// byte divided by two for even values.
// If the slot is even, bitwise and the slot with 255 and divide by
// two to get the length. If the slot is odd, bitwise and the slot
// with -1 and divide by two.
let slength := div(and(fslot, sub(mul(0x100, iszero(and(fslot, 1))), 1)), 2)
let mlength := mload(_postBytes)
let newlength := add(slength, mlength)
// slength can contain both the length and contents of the array
// if length < 32 bytes so let's prepare for that
// v. http://solidity.readthedocs.io/en/latest/miscellaneous.html#layout-of-state-variables-in-storage
switch add(lt(slength, 32), lt(newlength, 32))
case 2 {
// Since the new array still fits in the slot, we just need to
// update the contents of the slot.
// uint256(bytes_storage) = uint256(bytes_storage) + uint256(bytes_memory) + new_length
sstore(
_preBytes.slot,
// all the modifications to the slot are inside this
// next block
add(
// we can just add to the slot contents because the
// bytes we want to change are the LSBs
fslot,
add(
mul(
div(
// load the bytes from memory
mload(add(_postBytes, 0x20)),
// zero all bytes to the right
exp(0x100, sub(32, mlength))
),
// and now shift left the number of bytes to
// leave space for the length in the slot
exp(0x100, sub(32, newlength))
),
// increase length by the double of the memory
// bytes length
mul(mlength, 2)
)
)
)
}
case 1 {
// The stored value fits in the slot, but the combined value
// will exceed it.
// get the keccak hash to get the contents of the array
mstore(0x0, _preBytes.slot)
let sc := add(keccak256(0x0, 0x20), div(slength, 32))
// save new length
sstore(_preBytes.slot, add(mul(newlength, 2), 1))
// The contents of the _postBytes array start 32 bytes into
// the structure. Our first read should obtain the `submod`
// bytes that can fit into the unused space in the last word
// of the stored array. To get this, we read 32 bytes starting
// from `submod`, so the data we read overlaps with the array
// contents by `submod` bytes. Masking the lowest-order
// `submod` bytes allows us to add that value directly to the
// stored value.
let submod := sub(32, slength)
let mc := add(_postBytes, submod)
let end := add(_postBytes, mlength)
let mask := sub(exp(0x100, submod), 1)
sstore(sc, add(and(fslot, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00), and(mload(mc), mask)))
for {
mc := add(mc, 0x20)
sc := add(sc, 1)
} lt(mc, end) {
sc := add(sc, 1)
mc := add(mc, 0x20)
} {
sstore(sc, mload(mc))
}
mask := exp(0x100, sub(mc, end))
sstore(sc, mul(div(mload(mc), mask), mask))
}
default {
// get the keccak hash to get the contents of the array
mstore(0x0, _preBytes.slot)
// Start copying to the last used word of the stored array.
let sc := add(keccak256(0x0, 0x20), div(slength, 32))
// save new length
sstore(_preBytes.slot, add(mul(newlength, 2), 1))
// Copy over the first `submod` bytes of the new data as in
// case 1 above.
let slengthmod := mod(slength, 32)
let mlengthmod := mod(mlength, 32)
let submod := sub(32, slengthmod)
let mc := add(_postBytes, submod)
let end := add(_postBytes, mlength)
let mask := sub(exp(0x100, submod), 1)
sstore(sc, add(sload(sc), and(mload(mc), mask)))
for {
sc := add(sc, 1)
mc := add(mc, 0x20)
} lt(mc, end) {
sc := add(sc, 1)
mc := add(mc, 0x20)
} {
sstore(sc, mload(mc))
}
mask := exp(0x100, sub(mc, end))
sstore(sc, mul(div(mload(mc), mask), mask))
}
}
}
function slice(
bytes memory _bytes,
uint _start,
uint _length
) internal pure returns (bytes memory) {
require(_length + 31 >= _length, "slice_overflow");
require(_bytes.length >= _start + _length, "slice_outOfBounds");
bytes memory tempBytes;
assembly {
switch iszero(_length)
case 0 {
// Get a location of some free memory and store it in tempBytes as
// Solidity does for memory variables.
tempBytes := mload(0x40)
// The first word of the slice result is potentially a partial
// word read from the original array. To read it, we calculate
// the length of that partial word and start copying that many
// bytes into the array. The first word we copy will start with
// data we don't care about, but the last `lengthmod` bytes will
// land at the beginning of the contents of the new array. When
// we're done copying, we overwrite the full first word with
// the actual length of the slice.
let lengthmod := and(_length, 31)
// The multiplication in the next line is necessary
// because when slicing multiples of 32 bytes (lengthmod == 0)
// the following copy loop was copying the origin's length
// and then ending prematurely not copying everything it should.
let mc := add(add(tempBytes, lengthmod), mul(0x20, iszero(lengthmod)))
let end := add(mc, _length)
for {
// The multiplication in the next line has the same exact purpose
// as the one above.
let cc := add(add(add(_bytes, lengthmod), mul(0x20, iszero(lengthmod))), _start)
} lt(mc, end) {
mc := add(mc, 0x20)
cc := add(cc, 0x20)
} {
mstore(mc, mload(cc))
}
mstore(tempBytes, _length)
//update free-memory pointer
//allocating the array padded to 32 bytes like the compiler does now
mstore(0x40, and(add(mc, 31), not(31)))
}
//if we want a zero-length slice let's just return a zero-length array
default {
tempBytes := mload(0x40)
//zero out the 32 bytes slice we are about to return
//we need to do it because Solidity does not garbage collect
mstore(tempBytes, 0)
mstore(0x40, add(tempBytes, 0x20))
}
}
return tempBytes;
}
function toAddress(bytes memory _bytes, uint _start) internal pure returns (address) {
require(_bytes.length >= _start + 20, "toAddress_outOfBounds");
address tempAddress;
assembly {
tempAddress := div(mload(add(add(_bytes, 0x20), _start)), 0x1000000000000000000000000)
}
return tempAddress;
}
function toUint8(bytes memory _bytes, uint _start) internal pure returns (uint8) {
require(_bytes.length >= _start + 1, "toUint8_outOfBounds");
uint8 tempUint;
assembly {
tempUint := mload(add(add(_bytes, 0x1), _start))
}
return tempUint;
}
function toUint16(bytes memory _bytes, uint _start) internal pure returns (uint16) {
require(_bytes.length >= _start + 2, "toUint16_outOfBounds");
uint16 tempUint;
assembly {
tempUint := mload(add(add(_bytes, 0x2), _start))
}
return tempUint;
}
function toUint32(bytes memory _bytes, uint _start) internal pure returns (uint32) {
require(_bytes.length >= _start + 4, "toUint32_outOfBounds");
uint32 tempUint;
assembly {
tempUint := mload(add(add(_bytes, 0x4), _start))
}
return tempUint;
}
function toUint64(bytes memory _bytes, uint _start) internal pure returns (uint64) {
require(_bytes.length >= _start + 8, "toUint64_outOfBounds");
uint64 tempUint;
assembly {
tempUint := mload(add(add(_bytes, 0x8), _start))
}
return tempUint;
}
function toUint96(bytes memory _bytes, uint _start) internal pure returns (uint96) {
require(_bytes.length >= _start + 12, "toUint96_outOfBounds");
uint96 tempUint;
assembly {
tempUint := mload(add(add(_bytes, 0xc), _start))
}
return tempUint;
}
function toUint128(bytes memory _bytes, uint _start) internal pure returns (uint128) {
require(_bytes.length >= _start + 16, "toUint128_outOfBounds");
uint128 tempUint;
assembly {
tempUint := mload(add(add(_bytes, 0x10), _start))
}
return tempUint;
}
function toUint256(bytes memory _bytes, uint _start) internal pure returns (uint) {
require(_bytes.length >= _start + 32, "toUint256_outOfBounds");
uint tempUint;
assembly {
tempUint := mload(add(add(_bytes, 0x20), _start))
}
return tempUint;
}
function toBytes32(bytes memory _bytes, uint _start) internal pure returns (bytes32) {
require(_bytes.length >= _start + 32, "toBytes32_outOfBounds");
bytes32 tempBytes32;
assembly {
tempBytes32 := mload(add(add(_bytes, 0x20), _start))
}
return tempBytes32;
}
function equal(bytes memory _preBytes, bytes memory _postBytes) internal pure returns (bool) {
bool success = true;
assembly {
let length := mload(_preBytes)
// if lengths don't match the arrays are not equal
switch eq(length, mload(_postBytes))
case 1 {
// cb is a circuit breaker in the for loop since there's
// no said feature for inline assembly loops
// cb = 1 - don't breaker
// cb = 0 - break
let cb := 1
let mc := add(_preBytes, 0x20)
let end := add(mc, length)
for {
let cc := add(_postBytes, 0x20)
// the next line is the loop condition:
// while(uint256(mc < end) + cb == 2)
} eq(add(lt(mc, end), cb), 2) {
mc := add(mc, 0x20)
cc := add(cc, 0x20)
} {
// if any of these checks fails then arrays are not equal
if iszero(eq(mload(mc), mload(cc))) {
// unsuccess:
success := 0
cb := 0
}
}
}
default {
// unsuccess:
success := 0
}
}
return success;
}
function equalStorage(bytes storage _preBytes, bytes memory _postBytes) internal view returns (bool) {
bool success = true;
assembly {
// we know _preBytes_offset is 0
let fslot := sload(_preBytes.slot)
// Decode the length of the stored array like in concatStorage().
let slength := div(and(fslot, sub(mul(0x100, iszero(and(fslot, 1))), 1)), 2)
let mlength := mload(_postBytes)
// if lengths don't match the arrays are not equal
switch eq(slength, mlength)
case 1 {
// slength can contain both the length and contents of the array
// if length < 32 bytes so let's prepare for that
// v. http://solidity.readthedocs.io/en/latest/miscellaneous.html#layout-of-state-variables-in-storage
if iszero(iszero(slength)) {
switch lt(slength, 32)
case 1 {
// blank the last byte which is the length
fslot := mul(div(fslot, 0x100), 0x100)
if iszero(eq(fslot, mload(add(_postBytes, 0x20)))) {
// unsuccess:
success := 0
}
}
default {
// cb is a circuit breaker in the for loop since there's
// no said feature for inline assembly loops
// cb = 1 - don't breaker
// cb = 0 - break
let cb := 1
// get the keccak hash to get the contents of the array
mstore(0x0, _preBytes.slot)
let sc := keccak256(0x0, 0x20)
let mc := add(_postBytes, 0x20)
let end := add(mc, mlength)
// the next line is the loop condition:
// while(uint256(mc < end) + cb == 2)
for {
} eq(add(lt(mc, end), cb), 2) {
sc := add(sc, 1)
mc := add(mc, 0x20)
} {
if iszero(eq(sload(sc), mload(mc))) {
// unsuccess:
success := 0
cb := 0
}
}
}
}
}
default {
// unsuccess:
success := 0
}
}
return success;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {IAny2EVMMessageReceiver} from "../interfaces/IAny2EVMMessageReceiver.sol";
import {Client} from "../libraries/Client.sol";
import {IERC165} from "../../vendor/openzeppelin-solidity/v4.8.3/contracts/utils/introspection/IERC165.sol";
/// @title CCIPReceiver - Base contract for CCIP applications that can receive messages.
abstract contract CCIPReceiver is IAny2EVMMessageReceiver, IERC165 {
address internal immutable i_ccipRouter;
constructor(address router) {
if (router == address(0)) revert InvalidRouter(address(0));
i_ccipRouter = router;
}
/// @notice IERC165 supports an interfaceId
/// @param interfaceId The interfaceId to check
/// @return true if the interfaceId is supported
/// @dev Should indicate whether the contract implements IAny2EVMMessageReceiver
/// e.g. return interfaceId == type(IAny2EVMMessageReceiver).interfaceId || interfaceId == type(IERC165).interfaceId
/// This allows CCIP to check if ccipReceive is available before calling it.
/// If this returns false or reverts, only tokens are transferred to the receiver.
/// If this returns true, tokens are transferred and ccipReceive is called atomically.
/// Additionally, if the receiver address does not have code associated with
/// it at the time of execution (EXTCODESIZE returns 0), only tokens will be transferred.
function supportsInterface(bytes4 interfaceId) public pure virtual override returns (bool) {
return interfaceId == type(IAny2EVMMessageReceiver).interfaceId || interfaceId == type(IERC165).interfaceId;
}
/// @inheritdoc IAny2EVMMessageReceiver
function ccipReceive(Client.Any2EVMMessage calldata message) external virtual override onlyRouter {
_ccipReceive(message);
}
/// @notice Override this function in your implementation.
/// @param message Any2EVMMessage
function _ccipReceive(Client.Any2EVMMessage memory message) internal virtual;
/////////////////////////////////////////////////////////////////////
// Plumbing
/////////////////////////////////////////////////////////////////////
/// @notice Return the current router
/// @return CCIP router address
function getRouter() public view returns (address) {
return address(i_ccipRouter);
}
error InvalidRouter(address router);
/// @dev only calls from the set router are accepted.
modifier onlyRouter() {
if (msg.sender != address(i_ccipRouter)) revert InvalidRouter(msg.sender);
_;
}
}
// SPDX-License-Identifier: LZBL-1.2
pragma solidity ^0.8.18;
library CalldataBytesLib {
function toU8(bytes calldata _bytes, uint256 _start) internal pure returns (uint8) {
return uint8(_bytes[_start]);
}
function toU16(bytes calldata _bytes, uint256 _start) internal pure returns (uint16) {
unchecked {
uint256 end = _start + 2;
return uint16(bytes2(_bytes[_start:end]));
}
}
function toU32(bytes calldata _bytes, uint256 _start) internal pure returns (uint32) {
unchecked {
uint256 end = _start + 4;
return uint32(bytes4(_bytes[_start:end]));
}
}
function toU64(bytes calldata _bytes, uint256 _start) internal pure returns (uint64) {
unchecked {
uint256 end = _start + 8;
return uint64(bytes8(_bytes[_start:end]));
}
}
function toU128(bytes calldata _bytes, uint256 _start) internal pure returns (uint128) {
unchecked {
uint256 end = _start + 16;
return uint128(bytes16(_bytes[_start:end]));
}
}
function toU256(bytes calldata _bytes, uint256 _start) internal pure returns (uint256) {
unchecked {
uint256 end = _start + 32;
return uint256(bytes32(_bytes[_start:end]));
}
}
function toAddr(bytes calldata _bytes, uint256 _start) internal pure returns (address) {
unchecked {
uint256 end = _start + 20;
return address(bytes20(_bytes[_start:end]));
}
}
function toB32(bytes calldata _bytes, uint256 _start) internal pure returns (bytes32) {
unchecked {
uint256 end = _start + 32;
return bytes32(_bytes[_start:end]);
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// End consumer library.
library Client {
/// @dev RMN depends on this struct, if changing, please notify the RMN maintainers.
struct EVMTokenAmount {
address token; // token address on the local chain.
uint256 amount; // Amount of tokens.
}
struct Any2EVMMessage {
bytes32 messageId; // MessageId corresponding to ccipSend on source.
uint64 sourceChainSelector; // Source chain selector.
bytes sender; // abi.decode(sender) if coming from an EVM chain.
bytes data; // payload sent in original message.
EVMTokenAmount[] destTokenAmounts; // Tokens and their amounts in their destination chain representation.
}
// If extraArgs is empty bytes, the default is 200k gas limit.
struct EVM2AnyMessage {
bytes receiver; // abi.encode(receiver address) for dest EVM chains
bytes data; // Data payload
EVMTokenAmount[] tokenAmounts; // Token transfers
address feeToken; // Address of feeToken. address(0) means you will send msg.value.
bytes extraArgs; // Populate this with _argsToBytes(EVMExtraArgsV1)
}
// bytes4(keccak256("CCIP EVMExtraArgsV1"));
bytes4 public constant EVM_EXTRA_ARGS_V1_TAG = 0x97a657c9;
struct EVMExtraArgsV1 {
uint256 gasLimit;
}
function _argsToBytes(EVMExtraArgsV1 memory extraArgs) internal pure returns (bytes memory bts) {
return abi.encodeWithSelector(EVM_EXTRA_ARGS_V1_TAG, extraArgs);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.4) (utils/Context.sol)
pragma solidity ^0.8.0;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Counters.sol)
pragma solidity ^0.8.0;
/**
* @title Counters
* @author Matt Condon (@shrugs)
* @dev Provides counters that can only be incremented, decremented or reset. This can be used e.g. to track the number
* of elements in a mapping, issuing ERC721 ids, or counting request ids.
*
* Include with `using Counters for Counters.Counter;`
*/
library Counters {
struct Counter {
// This variable should never be directly accessed by users of the library: interactions must be restricted to
// the library's function. As of Solidity v0.5.2, this cannot be enforced, though there is a proposal to add
// this feature: see https://github.com/ethereum/solidity/issues/4637
uint256 _value; // default: 0
}
function current(Counter storage counter) internal view returns (uint256) {
return counter._value;
}
function increment(Counter storage counter) internal {
unchecked {
counter._value += 1;
}
}
function decrement(Counter storage counter) internal {
uint256 value = counter._value;
require(value > 0, "Counter: decrement overflow");
unchecked {
counter._value = value - 1;
}
}
function reset(Counter storage counter) internal {
counter._value = 0;
}
}
// SPDX-License-Identifier: LZBL-1.2
pragma solidity ^0.8.18;
import { BytesLib } from "solidity-bytes-utils/contracts/BytesLib.sol";
import { BitMap256 } from "@layerzerolabs/lz-evm-protocol-v2/contracts/messagelib/libs/BitMaps.sol";
import { CalldataBytesLib } from "@layerzerolabs/lz-evm-protocol-v2/contracts/libs/CalldataBytesLib.sol";
library DVNOptions {
using CalldataBytesLib for bytes;
using BytesLib for bytes;
uint8 internal constant WORKER_ID = 2;
uint8 internal constant OPTION_TYPE_PRECRIME = 1;
error DVN_InvalidDVNIdx();
error DVN_InvalidDVNOptions(uint256 cursor);
/// @dev group dvn options by its idx
/// @param _options [dvn_id][dvn_option][dvn_id][dvn_option]...
/// dvn_option = [option_size][dvn_idx][option_type][option]
/// option_size = len(dvn_idx) + len(option_type) + len(option)
/// dvn_id: uint8, dvn_idx: uint8, option_size: uint16, option_type: uint8, option: bytes
/// @return dvnOptions the grouped options, still share the same format of _options
/// @return dvnIndices the dvn indices
function groupDVNOptionsByIdx(
bytes memory _options
) internal pure returns (bytes[] memory dvnOptions, uint8[] memory dvnIndices) {
if (_options.length == 0) return (dvnOptions, dvnIndices);
uint8 numDVNs = getNumDVNs(_options);
// if there is only 1 dvn, we can just return the whole options
if (numDVNs == 1) {
dvnOptions = new bytes[](1);
dvnOptions[0] = _options;
dvnIndices = new uint8[](1);
dvnIndices[0] = _options.toUint8(3); // dvn idx
return (dvnOptions, dvnIndices);
}
// otherwise, we need to group the options by dvn_idx
dvnIndices = new uint8[](numDVNs);
dvnOptions = new bytes[](numDVNs);
unchecked {
uint256 cursor = 0;
uint256 start = 0;
uint8 lastDVNIdx = 255; // 255 is an invalid dvn_idx
while (cursor < _options.length) {
++cursor; // skip worker_id
// optionLength asserted in getNumDVNs (skip check)
uint16 optionLength = _options.toUint16(cursor);
cursor += 2;
// dvnIdx asserted in getNumDVNs (skip check)
uint8 dvnIdx = _options.toUint8(cursor);
// dvnIdx must equal to the lastDVNIdx for the first option
// so it is always skipped in the first option
// this operation slices out options whenever the scan finds a different lastDVNIdx
if (lastDVNIdx == 255) {
lastDVNIdx = dvnIdx;
} else if (dvnIdx != lastDVNIdx) {
uint256 len = cursor - start - 3; // 3 is for worker_id and option_length
bytes memory opt = _options.slice(start, len);
_insertDVNOptions(dvnOptions, dvnIndices, lastDVNIdx, opt);
// reset the start and lastDVNIdx
start += len;
lastDVNIdx = dvnIdx;
}
cursor += optionLength;
}
// skip check the cursor here because the cursor is asserted in getNumDVNs
// if we have reached the end of the options, we need to process the last dvn
uint256 size = cursor - start;
bytes memory op = _options.slice(start, size);
_insertDVNOptions(dvnOptions, dvnIndices, lastDVNIdx, op);
// revert dvnIndices to start from 0
for (uint8 i = 0; i < numDVNs; ++i) {
--dvnIndices[i];
}
}
}
function _insertDVNOptions(
bytes[] memory _dvnOptions,
uint8[] memory _dvnIndices,
uint8 _dvnIdx,
bytes memory _newOptions
) internal pure {
// dvnIdx starts from 0 but default value of dvnIndices is 0,
// so we tell if the slot is empty by adding 1 to dvnIdx
if (_dvnIdx == 255) revert DVN_InvalidDVNIdx();
uint8 dvnIdxAdj = _dvnIdx + 1;
for (uint256 j = 0; j < _dvnIndices.length; ++j) {
uint8 index = _dvnIndices[j];
if (dvnIdxAdj == index) {
_dvnOptions[j] = abi.encodePacked(_dvnOptions[j], _newOptions);
break;
} else if (index == 0) {
// empty slot, that means it is the first time we see this dvn
_dvnIndices[j] = dvnIdxAdj;
_dvnOptions[j] = _newOptions;
break;
}
}
}
/// @dev get the number of unique dvns
/// @param _options the format is the same as groupDVNOptionsByIdx
function getNumDVNs(bytes memory _options) internal pure returns (uint8 numDVNs) {
uint256 cursor = 0;
BitMap256 bitmap;
// find number of unique dvn_idx
unchecked {
while (cursor < _options.length) {
++cursor; // skip worker_id
uint16 optionLength = _options.toUint16(cursor);
cursor += 2;
if (optionLength < 2) revert DVN_InvalidDVNOptions(cursor); // at least 1 byte for dvn_idx and 1 byte for option_type
uint8 dvnIdx = _options.toUint8(cursor);
// if dvnIdx is not set, increment numDVNs
// max num of dvns is 255, 255 is an invalid dvn_idx
// The order of the dvnIdx is not required to be sequential, as enforcing the order may weaken
// the composability of the options. e.g. if we refrain from enforcing the order, an OApp that has
// already enforced certain options can append additional options to the end of the enforced
// ones without restrictions.
if (dvnIdx == 255) revert DVN_InvalidDVNIdx();
if (!bitmap.get(dvnIdx)) {
++numDVNs;
bitmap = bitmap.set(dvnIdx);
}
cursor += optionLength;
}
}
if (cursor != _options.length) revert DVN_InvalidDVNOptions(cursor);
}
/// @dev decode the next dvn option from _options starting from the specified cursor
/// @param _options the format is the same as groupDVNOptionsByIdx
/// @param _cursor the cursor to start decoding
/// @return optionType the type of the option
/// @return option the option
/// @return cursor the cursor to start decoding the next option
function nextDVNOption(
bytes calldata _options,
uint256 _cursor
) internal pure returns (uint8 optionType, bytes calldata option, uint256 cursor) {
unchecked {
// skip worker id
cursor = _cursor + 1;
// read option size
uint16 size = _options.toU16(cursor);
cursor += 2;
// read option type
optionType = _options.toU8(cursor + 1); // skip dvn_idx
// startCursor and endCursor are used to slice the option from _options
uint256 startCursor = cursor + 2; // skip option type and dvn_idx
uint256 endCursor = cursor + size;
option = _options[startCursor:endCursor];
cursor += size;
}
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/cryptography/ECDSA.sol)
pragma solidity ^0.8.0;
import "../Strings.sol";
/**
* @dev Elliptic Curve Digital Signature Algorithm (ECDSA) operations.
*
* These functions can be used to verify that a message was signed by the holder
* of the private keys of a given address.
*/
library ECDSA {
enum RecoverError {
NoError,
InvalidSignature,
InvalidSignatureLength,
InvalidSignatureS,
InvalidSignatureV // Deprecated in v4.8
}
function _throwError(RecoverError error) private pure {
if (error == RecoverError.NoError) {
return; // no error: do nothing
} else if (error == RecoverError.InvalidSignature) {
revert("ECDSA: invalid signature");
} else if (error == RecoverError.InvalidSignatureLength) {
revert("ECDSA: invalid signature length");
} else if (error == RecoverError.InvalidSignatureS) {
revert("ECDSA: invalid signature 's' value");
}
}
/**
* @dev Returns the address that signed a hashed message (`hash`) with
* `signature` or error string. This address can then be used for verification purposes.
*
* The `ecrecover` EVM opcode allows for malleable (non-unique) signatures:
* this function rejects them by requiring the `s` value to be in the lower
* half order, and the `v` value to be either 27 or 28.
*
* IMPORTANT: `hash` _must_ be the result of a hash operation for the
* verification to be secure: it is possible to craft signatures that
* recover to arbitrary addresses for non-hashed data. A safe way to ensure
* this is by receiving a hash of the original message (which may otherwise
* be too long), and then calling {toEthSignedMessageHash} on it.
*
* Documentation for signature generation:
* - with https://web3js.readthedocs.io/en/v1.3.4/web3-eth-accounts.html#sign[Web3.js]
* - with https://docs.ethers.io/v5/api/signer/#Signer-signMessage[ethers]
*
* _Available since v4.3._
*/
function tryRecover(bytes32 hash, bytes memory signature) internal pure returns (address, RecoverError) {
if (signature.length == 65) {
bytes32 r;
bytes32 s;
uint8 v;
// ecrecover takes the signature parameters, and the only way to get them
// currently is to use assembly.
/// @solidity memory-safe-assembly
assembly {
r := mload(add(signature, 0x20))
s := mload(add(signature, 0x40))
v := byte(0, mload(add(signature, 0x60)))
}
return tryRecover(hash, v, r, s);
} else {
return (address(0), RecoverError.InvalidSignatureLength);
}
}
/**
* @dev Returns the address that signed a hashed message (`hash`) with
* `signature`. This address can then be used for verification purposes.
*
* The `ecrecover` EVM opcode allows for malleable (non-unique) signatures:
* this function rejects them by requiring the `s` value to be in the lower
* half order, and the `v` value to be either 27 or 28.
*
* IMPORTANT: `hash` _must_ be the result of a hash operation for the
* verification to be secure: it is possible to craft signatures that
* recover to arbitrary addresses for non-hashed data. A safe way to ensure
* this is by receiving a hash of the original message (which may otherwise
* be too long), and then calling {toEthSignedMessageHash} on it.
*/
function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
(address recovered, RecoverError error) = tryRecover(hash, signature);
_throwError(error);
return recovered;
}
/**
* @dev Overload of {ECDSA-tryRecover} that receives the `r` and `vs` short-signature fields separately.
*
* See https://eips.ethereum.org/EIPS/eip-2098[EIP-2098 short signatures]
*
* _Available since v4.3._
*/
function tryRecover(bytes32 hash, bytes32 r, bytes32 vs) internal pure returns (address, RecoverError) {
bytes32 s = vs & bytes32(0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
uint8 v = uint8((uint256(vs) >> 255) + 27);
return tryRecover(hash, v, r, s);
}
/**
* @dev Overload of {ECDSA-recover} that receives the `r and `vs` short-signature fields separately.
*
* _Available since v4.2._
*/
function recover(bytes32 hash, bytes32 r, bytes32 vs) internal pure returns (address) {
(address recovered, RecoverError error) = tryRecover(hash, r, vs);
_throwError(error);
return recovered;
}
/**
* @dev Overload of {ECDSA-tryRecover} that receives the `v`,
* `r` and `s` signature fields separately.
*
* _Available since v4.3._
*/
function tryRecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal pure returns (address, RecoverError) {
// EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature
// unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines
// the valid range for s in (301): 0 < s < secp256k1n ÷ 2 + 1, and for v in (302): v ∈ {27, 28}. Most
// signatures from current libraries generate a unique signature with an s-value in the lower half order.
//
// If your library generates malleable signatures, such as s-values in the upper range, calculate a new s-value
// with 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1 and flip v from 27 to 28 or
// vice versa. If your library also generates signatures with 0/1 for v instead 27/28, add 27 to v to accept
// these malleable signatures as well.
if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
return (address(0), RecoverError.InvalidSignatureS);
}
// If the signature is valid (and not malleable), return the signer address
address signer = ecrecover(hash, v, r, s);
if (signer == address(0)) {
return (address(0), RecoverError.InvalidSignature);
}
return (signer, RecoverError.NoError);
}
/**
* @dev Overload of {ECDSA-recover} that receives the `v`,
* `r` and `s` signature fields separately.
*/
function recover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal pure returns (address) {
(address recovered, RecoverError error) = tryRecover(hash, v, r, s);
_throwError(error);
return recovered;
}
/**
* @dev Returns an Ethereum Signed Message, created from a `hash`. This
* produces hash corresponding to the one signed with the
* https://eth.wiki/json-rpc/API#eth_sign[`eth_sign`]
* JSON-RPC method as part of EIP-191.
*
* See {recover}.
*/
function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32 message) {
// 32 is the length in bytes of hash,
// enforced by the type signature above
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, "\x19Ethereum Signed Message:\n32")
mstore(0x1c, hash)
message := keccak256(0x00, 0x3c)
}
}
/**
* @dev Returns an Ethereum Signed Message, created from `s`. This
* produces hash corresponding to the one signed with the
* https://eth.wiki/json-rpc/API#eth_sign[`eth_sign`]
* JSON-RPC method as part of EIP-191.
*
* See {recover}.
*/
function toEthSignedMessageHash(bytes memory s) internal pure returns (bytes32) {
return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n", Strings.toString(s.length), s));
}
/**
* @dev Returns an Ethereum Signed Typed Data, created from a
* `domainSeparator` and a `structHash`. This produces hash corresponding
* to the one signed with the
* https://eips.ethereum.org/EIPS/eip-712[`eth_signTypedData`]
* JSON-RPC method as part of EIP-712.
*
* See {recover}.
*/
function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32 data) {
/// @solidity memory-safe-assembly
assembly {
let ptr := mload(0x40)
mstore(ptr, "\x19\x01")
mstore(add(ptr, 0x02), domainSeparator)
mstore(add(ptr, 0x22), structHash)
data := keccak256(ptr, 0x42)
}
}
/**
* @dev Returns an Ethereum Signed Data with intended validator, created from a
* `validator` and `data` according to the version 0 of EIP-191.
*
* See {recover}.
*/
function toDataWithIntendedValidatorHash(address validator, bytes memory data) internal pure returns (bytes32) {
return keccak256(abi.encodePacked("\x19\x00", validator, data));
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/cryptography/EIP712.sol)
pragma solidity ^0.8.8;
import "./ECDSA.sol";
import "../ShortStrings.sol";
import "../../interfaces/IERC5267.sol";
/**
* @dev https://eips.ethereum.org/EIPS/eip-712[EIP 712] is a standard for hashing and signing of typed structured data.
*
* The encoding specified in the EIP is very generic, and such a generic implementation in Solidity is not feasible,
* thus this contract does not implement the encoding itself. Protocols need to implement the type-specific encoding
* they need in their contracts using a combination of `abi.encode` and `keccak256`.
*
* This contract implements the EIP 712 domain separator ({_domainSeparatorV4}) that is used as part of the encoding
* scheme, and the final step of the encoding to obtain the message digest that is then signed via ECDSA
* ({_hashTypedDataV4}).
*
* The implementation of the domain separator was designed to be as efficient as possible while still properly updating
* the chain id to protect against replay attacks on an eventual fork of the chain.
*
* NOTE: This contract implements the version of the encoding known as "v4", as implemented by the JSON RPC method
* https://docs.metamask.io/guide/signing-data.html[`eth_signTypedDataV4` in MetaMask].
*
* NOTE: In the upgradeable version of this contract, the cached values will correspond to the address, and the domain
* separator of the implementation contract. This will cause the `_domainSeparatorV4` function to always rebuild the
* separator from the immutable values, which is cheaper than accessing a cached version in cold storage.
*
* _Available since v3.4._
*
* @custom:oz-upgrades-unsafe-allow state-variable-immutable state-variable-assignment
*/
abstract contract EIP712 is IERC5267 {
using ShortStrings for *;
bytes32 private constant _TYPE_HASH =
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
// Cache the domain separator as an immutable value, but also store the chain id that it corresponds to, in order to
// invalidate the cached domain separator if the chain id changes.
bytes32 private immutable _cachedDomainSeparator;
uint256 private immutable _cachedChainId;
address private immutable _cachedThis;
bytes32 private immutable _hashedName;
bytes32 private immutable _hashedVersion;
ShortString private immutable _name;
ShortString private immutable _version;
string private _nameFallback;
string private _versionFallback;
/**
* @dev Initializes the domain separator and parameter caches.
*
* The meaning of `name` and `version` is specified in
* https://eips.ethereum.org/EIPS/eip-712#definition-of-domainseparator[EIP 712]:
*
* - `name`: the user readable name of the signing domain, i.e. the name of the DApp or the protocol.
* - `version`: the current major version of the signing domain.
*
* NOTE: These parameters cannot be changed except through a xref:learn::upgrading-smart-contracts.adoc[smart
* contract upgrade].
*/
constructor(string memory name, string memory version) {
_name = name.toShortStringWithFallback(_nameFallback);
_version = version.toShortStringWithFallback(_versionFallback);
_hashedName = keccak256(bytes(name));
_hashedVersion = keccak256(bytes(version));
_cachedChainId = block.chainid;
_cachedDomainSeparator = _buildDomainSeparator();
_cachedThis = address(this);
}
/**
* @dev Returns the domain separator for the current chain.
*/
function _domainSeparatorV4() internal view returns (bytes32) {
if (address(this) == _cachedThis && block.chainid == _cachedChainId) {
return _cachedDomainSeparator;
} else {
return _buildDomainSeparator();
}
}
function _buildDomainSeparator() private view returns (bytes32) {
return keccak256(abi.encode(_TYPE_HASH, _hashedName, _hashedVersion, block.chainid, address(this)));
}
/**
* @dev Given an already https://eips.ethereum.org/EIPS/eip-712#definition-of-hashstruct[hashed struct], this
* function returns the hash of the fully encoded EIP712 message for this domain.
*
* This hash can be used together with {ECDSA-recover} to obtain the signer of a message. For example:
*
* ```solidity
* bytes32 digest = _hashTypedDataV4(keccak256(abi.encode(
* keccak256("Mail(address to,string contents)"),
* mailTo,
* keccak256(bytes(mailContents))
* )));
* address signer = ECDSA.recover(digest, signature);
* ```
*/
function _hashTypedDataV4(bytes32 structHash) internal view virtual returns (bytes32) {
return ECDSA.toTypedDataHash(_domainSeparatorV4(), structHash);
}
/**
* @dev See {EIP-5267}.
*
* _Available since v4.9._
*/
function eip712Domain()
public
view
virtual
override
returns (
bytes1 fields,
string memory name,
string memory version,
uint256 chainId,
address verifyingContract,
bytes32 salt,
uint256[] memory extensions
)
{
return (
hex"0f", // 01111
_name.toStringWithFallback(_nameFallback),
_version.toStringWithFallback(_versionFallback),
block.chainid,
address(this),
bytes32(0),
new uint256[](0)
);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/ERC20.sol)
pragma solidity ^0.8.0;
import "./IERC20.sol";
import "./extensions/IERC20Metadata.sol";
import "../../utils/Context.sol";
/**
* @dev Implementation of the {IERC20} interface.
*
* This implementation is agnostic to the way tokens are created. This means
* that a supply mechanism has to be added in a derived contract using {_mint}.
* For a generic mechanism see {ERC20PresetMinterPauser}.
*
* TIP: For a detailed writeup see our guide
* https://forum.openzeppelin.com/t/how-to-implement-erc20-supply-mechanisms/226[How
* to implement supply mechanisms].
*
* The default value of {decimals} is 18. To change this, you should override
* this function so it returns a different value.
*
* We have followed general OpenZeppelin Contracts guidelines: functions revert
* instead returning `false` on failure. This behavior is nonetheless
* conventional and does not conflict with the expectations of ERC20
* applications.
*
* Additionally, an {Approval} event is emitted on calls to {transferFrom}.
* This allows applications to reconstruct the allowance for all accounts just
* by listening to said events. Other implementations of the EIP may not emit
* these events, as it isn't required by the specification.
*
* Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
* functions have been added to mitigate the well-known issues around setting
* allowances. See {IERC20-approve}.
*/
contract ERC20 is Context, IERC20, IERC20Metadata {
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
uint256 private _totalSupply;
string private _name;
string private _symbol;
/**
* @dev Sets the values for {name} and {symbol}.
*
* All two of these values are immutable: they can only be set once during
* construction.
*/
constructor(string memory name_, string memory symbol_) {
_name = name_;
_symbol = symbol_;
}
/**
* @dev Returns the name of the token.
*/
function name() public view virtual override returns (string memory) {
return _name;
}
/**
* @dev Returns the symbol of the token, usually a shorter version of the
* name.
*/
function symbol() public view virtual override returns (string memory) {
return _symbol;
}
/**
* @dev Returns the number of decimals used to get its user representation.
* For example, if `decimals` equals `2`, a balance of `505` tokens should
* be displayed to a user as `5.05` (`505 / 10 ** 2`).
*
* Tokens usually opt for a value of 18, imitating the relationship between
* Ether and Wei. This is the default value returned by this function, unless
* it's overridden.
*
* NOTE: This information is only used for _display_ purposes: it in
* no way affects any of the arithmetic of the contract, including
* {IERC20-balanceOf} and {IERC20-transfer}.
*/
function decimals() public view virtual override returns (uint8) {
return 18;
}
/**
* @dev See {IERC20-totalSupply}.
*/
function totalSupply() public view virtual override returns (uint256) {
return _totalSupply;
}
/**
* @dev See {IERC20-balanceOf}.
*/
function balanceOf(address account) public view virtual override returns (uint256) {
return _balances[account];
}
/**
* @dev See {IERC20-transfer}.
*
* Requirements:
*
* - `to` cannot be the zero address.
* - the caller must have a balance of at least `amount`.
*/
function transfer(address to, uint256 amount) public virtual override returns (bool) {
address owner = _msgSender();
_transfer(owner, to, amount);
return true;
}
/**
* @dev See {IERC20-allowance}.
*/
function allowance(address owner, address spender) public view virtual override returns (uint256) {
return _allowances[owner][spender];
}
/**
* @dev See {IERC20-approve}.
*
* NOTE: If `amount` is the maximum `uint256`, the allowance is not updated on
* `transferFrom`. This is semantically equivalent to an infinite approval.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function approve(address spender, uint256 amount) public virtual override returns (bool) {
address owner = _msgSender();
_approve(owner, spender, amount);
return true;
}
/**
* @dev See {IERC20-transferFrom}.
*
* Emits an {Approval} event indicating the updated allowance. This is not
* required by the EIP. See the note at the beginning of {ERC20}.
*
* NOTE: Does not update the allowance if the current allowance
* is the maximum `uint256`.
*
* Requirements:
*
* - `from` and `to` cannot be the zero address.
* - `from` must have a balance of at least `amount`.
* - the caller must have allowance for ``from``'s tokens of at least
* `amount`.
*/
function transferFrom(address from, address to, uint256 amount) public virtual override returns (bool) {
address spender = _msgSender();
_spendAllowance(from, spender, amount);
_transfer(from, to, amount);
return true;
}
/**
* @dev Atomically increases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {IERC20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
address owner = _msgSender();
_approve(owner, spender, allowance(owner, spender) + addedValue);
return true;
}
/**
* @dev Atomically decreases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {IERC20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `spender` must have allowance for the caller of at least
* `subtractedValue`.
*/
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
address owner = _msgSender();
uint256 currentAllowance = allowance(owner, spender);
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
unchecked {
_approve(owner, spender, currentAllowance - subtractedValue);
}
return true;
}
/**
* @dev Moves `amount` of tokens from `from` to `to`.
*
* This internal function is equivalent to {transfer}, and can be used to
* e.g. implement automatic token fees, slashing mechanisms, etc.
*
* Emits a {Transfer} event.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `from` must have a balance of at least `amount`.
*/
function _transfer(address from, address to, uint256 amount) internal virtual {
require(from != address(0), "ERC20: transfer from the zero address");
require(to != address(0), "ERC20: transfer to the zero address");
_beforeTokenTransfer(from, to, amount);
uint256 fromBalance = _balances[from];
require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
unchecked {
_balances[from] = fromBalance - amount;
// Overflow not possible: the sum of all balances is capped by totalSupply, and the sum is preserved by
// decrementing then incrementing.
_balances[to] += amount;
}
emit Transfer(from, to, amount);
_afterTokenTransfer(from, to, amount);
}
/** @dev Creates `amount` tokens and assigns them to `account`, increasing
* the total supply.
*
* Emits a {Transfer} event with `from` set to the zero address.
*
* Requirements:
*
* - `account` cannot be the zero address.
*/
function _mint(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: mint to the zero address");
_beforeTokenTransfer(address(0), account, amount);
_totalSupply += amount;
unchecked {
// Overflow not possible: balance + amount is at most totalSupply + amount, which is checked above.
_balances[account] += amount;
}
emit Transfer(address(0), account, amount);
_afterTokenTransfer(address(0), account, amount);
}
/**
* @dev Destroys `amount` tokens from `account`, reducing the
* total supply.
*
* Emits a {Transfer} event with `to` set to the zero address.
*
* Requirements:
*
* - `account` cannot be the zero address.
* - `account` must have at least `amount` tokens.
*/
function _burn(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: burn from the zero address");
_beforeTokenTransfer(account, address(0), amount);
uint256 accountBalance = _balances[account];
require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
unchecked {
_balances[account] = accountBalance - amount;
// Overflow not possible: amount <= accountBalance <= totalSupply.
_totalSupply -= amount;
}
emit Transfer(account, address(0), amount);
_afterTokenTransfer(account, address(0), amount);
}
/**
* @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
*
* This internal function is equivalent to `approve`, and can be used to
* e.g. set automatic allowances for certain subsystems, etc.
*
* Emits an {Approval} event.
*
* Requirements:
*
* - `owner` cannot be the zero address.
* - `spender` cannot be the zero address.
*/
function _approve(address owner, address spender, uint256 amount) internal virtual {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
/**
* @dev Updates `owner` s allowance for `spender` based on spent `amount`.
*
* Does not update the allowance amount in case of infinite allowance.
* Revert if not enough allowance is available.
*
* Might emit an {Approval} event.
*/
function _spendAllowance(address owner, address spender, uint256 amount) internal virtual {
uint256 currentAllowance = allowance(owner, spender);
if (currentAllowance != type(uint256).max) {
require(currentAllowance >= amount, "ERC20: insufficient allowance");
unchecked {
_approve(owner, spender, currentAllowance - amount);
}
}
}
/**
* @dev Hook that is called before any transfer of tokens. This includes
* minting and burning.
*
* Calling conditions:
*
* - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* will be transferred to `to`.
* - when `from` is zero, `amount` tokens will be minted for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens will be burned.
* - `from` and `to` are never both zero.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual {}
/**
* @dev Hook that is called after any transfer of tokens. This includes
* minting and burning.
*
* Calling conditions:
*
* - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* has been transferred to `to`.
* - when `from` is zero, `amount` tokens have been minted for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens have been burned.
* - `from` and `to` are never both zero.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _afterTokenTransfer(address from, address to, uint256 amount) internal virtual {}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.4) (token/ERC20/extensions/ERC20Permit.sol)
pragma solidity ^0.8.0;
import "./IERC20Permit.sol";
import "../ERC20.sol";
import "../../../utils/cryptography/ECDSA.sol";
import "../../../utils/cryptography/EIP712.sol";
import "../../../utils/Counters.sol";
/**
* @dev Implementation of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
* https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
*
* Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
* presenting a message signed by the account. By not relying on `{IERC20-approve}`, the token holder account doesn't
* need to send a transaction, and thus is not required to hold Ether at all.
*
* _Available since v3.4._
*/
abstract contract ERC20Permit is ERC20, IERC20Permit, EIP712 {
using Counters for Counters.Counter;
mapping(address => Counters.Counter) private _nonces;
// solhint-disable-next-line var-name-mixedcase
bytes32 private constant _PERMIT_TYPEHASH =
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
/**
* @dev In previous versions `_PERMIT_TYPEHASH` was declared as `immutable`.
* However, to ensure consistency with the upgradeable transpiler, we will continue
* to reserve a slot.
* @custom:oz-renamed-from _PERMIT_TYPEHASH
*/
// solhint-disable-next-line var-name-mixedcase
bytes32 private _PERMIT_TYPEHASH_DEPRECATED_SLOT;
/**
* @dev Initializes the {EIP712} domain separator using the `name` parameter, and setting `version` to `"1"`.
*
* It's a good idea to use the same `name` that is defined as the ERC20 token name.
*/
constructor(string memory name) EIP712(name, "1") {}
/**
* @inheritdoc IERC20Permit
*/
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public virtual override {
require(block.timestamp <= deadline, "ERC20Permit: expired deadline");
bytes32 structHash = keccak256(abi.encode(_PERMIT_TYPEHASH, owner, spender, value, _useNonce(owner), deadline));
bytes32 hash = _hashTypedDataV4(structHash);
address signer = ECDSA.recover(hash, v, r, s);
require(signer == owner, "ERC20Permit: invalid signature");
_approve(owner, spender, value);
}
/**
* @inheritdoc IERC20Permit
*/
function nonces(address owner) public view virtual override returns (uint256) {
return _nonces[owner].current();
}
/**
* @inheritdoc IERC20Permit
*/
// solhint-disable-next-line func-name-mixedcase
function DOMAIN_SEPARATOR() external view override returns (bytes32) {
return _domainSeparatorV4();
}
/**
* @dev "Consume a nonce": return the current value and increment.
*
* _Available since v4.1._
*/
function _useNonce(address owner) internal virtual returns (uint256 current) {
Counters.Counter storage nonce = _nonces[owner];
current = nonce.current();
nonce.increment();
}
}
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.7.6;
library ExcessivelySafeCall {
uint constant LOW_28_MASK = 0x00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
/// @notice Use when you _really_ really _really_ don't trust the called
/// contract. This prevents the called contract from causing reversion of
/// the caller in as many ways as we can.
/// @dev The main difference between this and a solidity low-level call is
/// that we limit the number of bytes that the callee can cause to be
/// copied to caller memory. This prevents stupid things like malicious
/// contracts returning 10,000,000 bytes causing a local OOG when copying
/// to memory.
/// @param _target The address to call
/// @param _gas The amount of gas to forward to the remote contract
/// @param _maxCopy The maximum number of bytes of returndata to copy
/// to memory.
/// @param _calldata The data to send to the remote contract
/// @return success and returndata, as `.call()`. Returndata is capped to
/// `_maxCopy` bytes.
function excessivelySafeCall(
address _target,
uint _gas,
uint16 _maxCopy,
bytes memory _calldata
) internal returns (bool, bytes memory) {
// set up for assembly call
uint _toCopy;
bool _success;
bytes memory _returnData = new bytes(_maxCopy);
// dispatch message to recipient
// by assembly calling "handle" function
// we call via assembly to avoid memcopying a very large returndata
// returned by a malicious contract
assembly {
_success := call(
_gas, // gas
_target, // recipient
0, // ether value
add(_calldata, 0x20), // inloc
mload(_calldata), // inlen
0, // outloc
0 // outlen
)
// limit our copy to 256 bytes
_toCopy := returndatasize()
if gt(_toCopy, _maxCopy) {
_toCopy := _maxCopy
}
// Store the length of the copied bytes
mstore(_returnData, _toCopy)
// copy the bytes from returndata[0:_toCopy]
returndatacopy(add(_returnData, 0x20), 0, _toCopy)
}
return (_success, _returnData);
}
/// @notice Use when you _really_ really _really_ don't trust the called
/// contract. This prevents the called contract from causing reversion of
/// the caller in as many ways as we can.
/// @dev The main difference between this and a solidity low-level call is
/// that we limit the number of bytes that the callee can cause to be
/// copied to caller memory. This prevents stupid things like malicious
/// contracts returning 10,000,000 bytes causing a local OOG when copying
/// to memory.
/// @param _target The address to call
/// @param _gas The amount of gas to forward to the remote contract
/// @param _maxCopy The maximum number of bytes of returndata to copy
/// to memory.
/// @param _calldata The data to send to the remote contract
/// @return success and returndata, as `.call()`. Returndata is capped to
/// `_maxCopy` bytes.
function excessivelySafeStaticCall(
address _target,
uint _gas,
uint16 _maxCopy,
bytes memory _calldata
) internal view returns (bool, bytes memory) {
// set up for assembly call
uint _toCopy;
bool _success;
bytes memory _returnData = new bytes(_maxCopy);
// dispatch message to recipient
// by assembly calling "handle" function
// we call via assembly to avoid memcopying a very large returndata
// returned by a malicious contract
assembly {
_success := staticcall(
_gas, // gas
_target, // recipient
add(_calldata, 0x20), // inloc
mload(_calldata), // inlen
0, // outloc
0 // outlen
)
// limit our copy to 256 bytes
_toCopy := returndatasize()
if gt(_toCopy, _maxCopy) {
_toCopy := _maxCopy
}
// Store the length of the copied bytes
mstore(_returnData, _toCopy)
// copy the bytes from returndata[0:_toCopy]
returndatacopy(add(_returnData, 0x20), 0, _toCopy)
}
return (_success, _returnData);
}
/**
* @notice Swaps function selectors in encoded contract calls
* @dev Allows reuse of encoded calldata for functions with identical
* argument types but different names. It simply swaps out the first 4 bytes
* for the new selector. This function modifies memory in place, and should
* only be used with caution.
* @param _newSelector The new 4-byte selector
* @param _buf The encoded contract args
*/
function swapSelector(bytes4 _newSelector, bytes memory _buf) internal pure {
require(_buf.length >= 4);
uint _mask = LOW_28_MASK;
assembly {
// load the first word of
let _word := mload(add(_buf, 0x20))
// mask out the top 4 bytes
// /x
_word := and(_word, _mask)
_word := or(_newSelector, _word)
mstore(add(_buf, 0x20), _word)
}
}
}
// SPDX-License-Identifier: LZBL-1.2
pragma solidity ^0.8.18;
import { CalldataBytesLib } from "../../libs/CalldataBytesLib.sol";
library ExecutorOptions {
using CalldataBytesLib for bytes;
uint8 internal constant WORKER_ID = 1;
uint8 internal constant OPTION_TYPE_LZRECEIVE = 1;
uint8 internal constant OPTION_TYPE_NATIVE_DROP = 2;
uint8 internal constant OPTION_TYPE_LZCOMPOSE = 3;
uint8 internal constant OPTION_TYPE_ORDERED_EXECUTION = 4;
error Executor_InvalidLzReceiveOption();
error Executor_InvalidNativeDropOption();
error Executor_InvalidLzComposeOption();
/// @dev decode the next executor option from the options starting from the specified cursor
/// @param _options [executor_id][executor_option][executor_id][executor_option]...
/// executor_option = [option_size][option_type][option]
/// option_size = len(option_type) + len(option)
/// executor_id: uint8, option_size: uint16, option_type: uint8, option: bytes
/// @param _cursor the cursor to start decoding from
/// @return optionType the type of the option
/// @return option the option of the executor
/// @return cursor the cursor to start decoding the next executor option
function nextExecutorOption(
bytes calldata _options,
uint256 _cursor
) internal pure returns (uint8 optionType, bytes calldata option, uint256 cursor) {
unchecked {
// skip worker id
cursor = _cursor + 1;
// read option size
uint16 size = _options.toU16(cursor);
cursor += 2;
// read option type
optionType = _options.toU8(cursor);
// startCursor and endCursor are used to slice the option from _options
uint256 startCursor = cursor + 1; // skip option type
uint256 endCursor = cursor + size;
option = _options[startCursor:endCursor];
cursor += size;
}
}
function decodeLzReceiveOption(bytes calldata _option) internal pure returns (uint128 gas, uint128 value) {
if (_option.length != 16 && _option.length != 32) revert Executor_InvalidLzReceiveOption();
gas = _option.toU128(0);
value = _option.length == 32 ? _option.toU128(16) : 0;
}
function decodeNativeDropOption(bytes calldata _option) internal pure returns (uint128 amount, bytes32 receiver) {
if (_option.length != 48) revert Executor_InvalidNativeDropOption();
amount = _option.toU128(0);
receiver = _option.toB32(16);
}
function decodeLzComposeOption(
bytes calldata _option
) internal pure returns (uint16 index, uint128 gas, uint128 value) {
if (_option.length != 18 && _option.length != 34) revert Executor_InvalidLzComposeOption();
index = _option.toU16(0);
gas = _option.toU128(2);
value = _option.length == 34 ? _option.toU128(18) : 0;
}
function encodeLzReceiveOption(uint128 _gas, uint128 _value) internal pure returns (bytes memory) {
return _value == 0 ? abi.encodePacked(_gas) : abi.encodePacked(_gas, _value);
}
function encodeNativeDropOption(uint128 _amount, bytes32 _receiver) internal pure returns (bytes memory) {
return abi.encodePacked(_amount, _receiver);
}
function encodeLzComposeOption(uint16 _index, uint128 _gas, uint128 _value) internal pure returns (bytes memory) {
return _value == 0 ? abi.encodePacked(_index, _gas) : abi.encodePacked(_index, _gas, _value);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @title GasEstimationType
* @notice This enum represents the gas estimation types for different chains.
*/
enum GasEstimationType {
Default,
OptimismEcotone,
OptimismBedrock,
Arbitrum,
Scroll
}
/**
* @title GasInfo
* @notice This struct represents the gas pricing information for a specific chain.
* @dev Smaller uint types are used for efficient struct packing to save storage costs.
*/
struct GasInfo {
/// @dev Custom gas pricing rule, such as L1 data fee on L2s
uint64 gasEstimationType;
/// @dev Scalar value needed for specific gas estimation types, expected to be less than 1e10
uint64 l1FeeScalar;
/// @dev Axelar base fee for cross-chain message approval on destination, in terms of source native gas token
uint128 axelarBaseFee;
/// @dev Gas price of destination chain, in terms of the source chain token, i.e dest_gas_price * dest_token_market_price / src_token_market_price
uint128 relativeGasPrice;
/// @dev Needed for specific gas estimation types. Blob base fee of destination chain, in terms of the source chain token, i.e dest_blob_base_fee * dest_token_market_price / src_token_market_price
uint128 relativeBlobBaseFee;
/// @dev Axelar express fee for express execution, in terms of source chain token
uint128 expressFee;
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
import {IGlacisRouter} from "../interfaces/IGlacisRouter.sol";
import {IGlacisAdapter} from "../interfaces/IGlacisAdapter.sol";
import {GlacisCommons} from "../commons/GlacisCommons.sol";
import {GlacisRemoteCounterpartManager} from "../managers/GlacisRemoteCounterpartManager.sol";
error GlacisAbstractAdapter__OnlyGlacisRouterAllowed();
error GlacisAbstractAdapter__OnlyAdapterAllowed();
error GlacisAbstractAdapter__DestinationChainIdNotValid();
error GlacisAbstractAdapter__SourceChainNotRegistered();
error GlacisAbstractAdapter__IDArraysMustBeSameLength();
error GlacisAbstractAdapter__NoRemoteAdapterForChainId(uint256 chainId);
error GlacisAbstractAdapter__ChainIsNotAvailable(uint256 toChainId);
/// @title Glacis Abstract Adapter for all GMPs
/// @notice All adapters inheriting from this contract will be able to receive GlacisRouter requests through _sendMessage
/// function
abstract contract GlacisAbstractAdapter is
GlacisRemoteCounterpartManager,
IGlacisAdapter
{
IGlacisRouter public immutable GLACIS_ROUTER;
/// @param _glacisRouter This chain's glacis router
/// @param _owner This adapter's owner
constructor(IGlacisRouter _glacisRouter, address _owner) {
_transferOwnership(_owner);
GLACIS_ROUTER = _glacisRouter;
}
/// @notice Dispatches payload to specified Glacis chain ID and address through a Glacis Adapter implementation
/// @param toChainId Destination chain (Glacis ID)
/// @param refundAddress The address to refund native asset surplus
/// @param payload Payload to send
function sendMessage(
uint256 toChainId,
address refundAddress,
GlacisCommons.CrossChainGas memory incentives,
bytes memory payload
) external payable override onlyGlacisRouter {
_sendMessage(toChainId, refundAddress, incentives, payload);
}
/// @notice Dispatches payload to specified Glacis chain ID and address through a Glacis Adapter implementation
/// @param toChainId Destination chain (Glacis ID)
/// @param refundAddress The address to refund native asset surplus
/// @param payload Payload to send
function _sendMessage(
uint256 toChainId,
address refundAddress,
GlacisCommons.CrossChainGas memory incentives,
bytes memory payload
) internal virtual;
/// @dev Verifies that the sender request is always GlacisRouter
modifier onlyGlacisRouter() {
if (msg.sender != address(GLACIS_ROUTER))
revert GlacisAbstractAdapter__OnlyGlacisRouterAllowed();
_;
}
/// @dev Verifies that the source address of the request is an authorized adapter
/// @param sourceAddress Source address
modifier onlyAuthorizedAdapter(uint256 chainId, bytes32 sourceAddress) {
if (
chainId == 0 ||
remoteCounterpart[chainId] == bytes32(0) ||
sourceAddress != remoteCounterpart[chainId]
) {
revert GlacisAbstractAdapter__OnlyAdapterAllowed();
}
_;
}
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
import {GlacisCommons} from "../commons/GlacisCommons.sol";
import {IGlacisRouterEvents} from "../interfaces/IGlacisRouter.sol";
import {AddressBytes32} from "../libraries/AddressBytes32.sol";
import {Ownable2Step} from "@openzeppelin/contracts/access/Ownable2Step.sol";
error GlacisAbstractRouter__InvalidAdapterAddress(); //0xa46f71e2
error GlacisAbstractRouter__GMPIDCannotBeZero(); //0x4332f55b
error GlacisAbstractRouter__GMPIDTooHigh();
/// @title Glacis Abstract Router
/// @notice A base class for the GlacisRouter
abstract contract GlacisAbstractRouter is
GlacisCommons,
IGlacisRouterEvents,
Ownable2Step
{
using AddressBytes32 for address;
uint256 internal immutable GLACIS_CHAIN_ID;
mapping(uint8 => address) public glacisGMPIdToAdapter;
mapping(address => uint8) public adapterToGlacisGMPId;
uint256 private nonce;
/// @param chainID The chain ID that will be injected in messages
constructor(uint256 chainID) {
// @dev Must store chain ID due to possibility of hard fork
GLACIS_CHAIN_ID = chainID;
}
/// @notice Registers a GMP adapter
/// @param glacisGMPId The Glacis ID of the GMP
/// @param glacisAdapter The address of the deployed adapter
function registerAdapter(
uint8 glacisGMPId,
address glacisAdapter
) external virtual onlyOwner {
if (glacisAdapter == address(0))
revert GlacisAbstractRouter__InvalidAdapterAddress();
if (glacisGMPId == 0) revert GlacisAbstractRouter__GMPIDCannotBeZero();
if (glacisGMPId > GlacisCommons.GLACIS_RESERVED_IDS) revert GlacisAbstractRouter__GMPIDTooHigh();
// Unregister previous adapter
address previousAdapter = glacisGMPIdToAdapter[glacisGMPId];
delete adapterToGlacisGMPId[previousAdapter];
delete glacisGMPIdToAdapter[glacisGMPId];
// Adds new adapter
glacisGMPIdToAdapter[glacisGMPId] = glacisAdapter;
adapterToGlacisGMPId[glacisAdapter] = glacisGMPId;
emit GlacisAbstractRouter__AdapterRegistered(glacisGMPId, glacisAdapter, previousAdapter);
}
/// @notice Unregisters a GMP adapter
/// @param glacisGMPId The Glacis ID of the GMP
function unRegisterAdapter(
uint8 glacisGMPId
) external virtual onlyOwner {
address adapter = glacisGMPIdToAdapter[glacisGMPId];
if (adapter == address(0))
revert GlacisAbstractRouter__InvalidAdapterAddress();
if (glacisGMPId == 0) revert GlacisAbstractRouter__GMPIDCannotBeZero();
delete glacisGMPIdToAdapter[glacisGMPId];
delete adapterToGlacisGMPId[adapter];
emit GlacisAbstractRouter__AdapterUnregistered(glacisGMPId, adapter);
}
/// @notice Creates a messageId
/// @dev this Id is used to Identify identical messages on the destination chain and to verify that a retry message
/// has identical data
/// @param toChainId The destination chain of the message
/// @param to The destination address of the message
/// @param payload The payload of the message
/// @return messageId , messageNonce : The message Id and the message nonce, this two parameters will be required
/// if implementing message retrying
function _createGlacisMessageId(
uint256 toChainId,
bytes32 to,
bytes memory payload
) internal returns (bytes32 messageId, uint256 messageNonce) {
messageNonce = nonce++;
messageId = keccak256(
abi.encode(
toChainId,
GLACIS_CHAIN_ID,
to,
keccak256(payload),
msg.sender,
messageNonce
)
);
emit GlacisAbstractRouter__MessageIdCreated(
messageId,
msg.sender.toBytes32(),
messageNonce
);
}
/// @notice Validates a message Id
/// @param messageId The Message Id of the message
/// @param toChainId The destination chain of the message
/// @param fromChainId The source chain of the message
/// @param to The destination address of the message
/// @param messageNonce The nonce of the message
/// @param payload The payload of the message
/// @return true if the message Id is valid, false otherwise
function validateGlacisMessageId(
bytes32 messageId,
uint256 toChainId,
uint256 fromChainId,
bytes32 to,
uint256 messageNonce,
bytes memory payload
) public view returns (bool) {
return
_validateGlacisMessageId(
messageId,
toChainId,
fromChainId,
to,
msg.sender.toBytes32(),
messageNonce,
payload
);
}
/// @notice Validates a message Id
/// @param messageId The Message Id of the message
/// @param toChainId The destination chain of the message
/// @param fromChainId The source chain of the message
/// @param to The destination address of the message
/// @param messageSender The sender of the message
/// @param messageNonce The nonce of the message
/// @param payload The payload of the message
/// @return true if the message Id is valid, false otherwise
function _validateGlacisMessageId(
bytes32 messageId,
uint256 toChainId,
uint256 fromChainId,
bytes32 to,
bytes32 messageSender,
uint256 messageNonce,
bytes memory payload
) internal pure returns (bool) {
bytes32 id = keccak256(
abi.encode(
toChainId,
fromChainId,
to,
keccak256(payload),
messageSender,
messageNonce
)
);
return id == messageId;
}
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
import {GlacisCommons} from "../commons/GlacisCommons.sol";
import {IGlacisAccessControlClient} from "../interfaces/IGlacisAccessControlClient.sol";
/// @title Glacis Access Control Client
/// @dev This contract encapsulates Glacis Access Control client logic. Contracts inheriting this will have access to
/// Glacis Access control features
abstract contract GlacisAccessControlClient is GlacisCommons, IGlacisAccessControlClient {
mapping(uint256 => mapping(bytes32 => mapping(address => bool))) public allowedRoutes;
bytes32 constant internal WILD_BYTES = bytes32(uint256(WILDCARD));
address constant internal WILD_ADDR = address(WILDCARD);
/// @notice Adds an allowed route for this client
/// @param route Route to be added
function _addAllowedRoute(
GlacisRoute memory route
) internal {
allowedRoutes[route.fromChainId][route.fromAddress][route.fromAdapter] = true;
}
/// @notice Removes an allowed route for this client
/// @param route Allowed route to be removed
function _removeAllowedRoute(
GlacisRoute calldata route
) internal {
allowedRoutes[route.fromChainId][route.fromAddress][route.fromAdapter] = false;
}
/// @notice Queries if a route from path GMP+Chain+Address is allowed for this client
/// @param route_ Incoming message route
/// @return True if route is allowed, false otherwise
function isAllowedRoute(
GlacisRoute memory route_,
bytes memory // payload
) public view override returns (bool) {
return
allowedRoutes[route_.fromChainId][route_.fromAddress][route_.fromAdapter] ||
allowedRoutes[WILDCARD][route_.fromAddress][route_.fromAdapter] ||
allowedRoutes[WILDCARD][WILD_BYTES][route_.fromAdapter] ||
allowedRoutes[route_.fromChainId][WILD_BYTES][route_.fromAdapter] ||
(uint160(route_.fromAdapter) <= GLACIS_RESERVED_IDS && (
allowedRoutes[route_.fromChainId][route_.fromAddress][WILD_ADDR] ||
allowedRoutes[route_.fromChainId][WILD_BYTES][WILD_ADDR] ||
allowedRoutes[WILDCARD][WILD_BYTES][WILD_ADDR]
));
}
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
import {AxelarExecutable} from "@axelar-network/axelar-gmp-sdk-solidity/contracts/executable/AxelarExecutable.sol";
import {IAxelarGasService} from "@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IAxelarGasService.sol";
import {GlacisAbstractAdapter__IDArraysMustBeSameLength, GlacisAbstractAdapter__DestinationChainIdNotValid, GlacisAbstractAdapter__NoRemoteAdapterForChainId, GlacisAbstractAdapter__ChainIsNotAvailable} from "./GlacisAbstractAdapter.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
import {AddressString} from "../libraries/AddressString.sol";
import {GlacisAbstractAdapter} from "./GlacisAbstractAdapter.sol";
import {IGlacisRouter} from "../routers/GlacisRouter.sol";
import {AddressBytes32} from "../libraries/AddressBytes32.sol";
import {GlacisCommons} from "../commons/GlacisCommons.sol";
/// @title Glacis Adapter for Axelar
/// @notice A Glacis Adapter for the Axelar network. Sends messages through the Axelar Gateway's callContract() and
/// receives Axelar requests through _execute()
contract GlacisAxelarAdapter is GlacisAbstractAdapter, AxelarExecutable {
using Strings for address;
using AddressString for string;
using AddressBytes32 for bytes32;
using AddressBytes32 for address;
IAxelarGasService public immutable GAS_SERVICE;
mapping(uint256 => string) internal glacisChainIdToAdapterChainId;
mapping(string => uint256) public adapterChainIdToGlacisChainId;
event GlacisAxelarAdapter__SetGlacisChainIDs(uint256[] chainIDs, string[] chainLabels);
/// @param _glacisRouter This chain's glacis router
/// @param _axelarGateway This chain's axelar gateway
/// @param _axelarGasReceiver This chain's axelar gas receiver
/// @param _owner This adapter's owner
constructor(
address _glacisRouter,
address _axelarGateway,
address _axelarGasReceiver,
address _owner
)
AxelarExecutable(_axelarGateway)
GlacisAbstractAdapter(IGlacisRouter(_glacisRouter), _owner)
{
GAS_SERVICE = IAxelarGasService(_axelarGasReceiver);
}
/// @notice Sets the corresponding Axelar chain label for the specified Glacis chain ID
/// @param chainIDs Glacis chain IDs
/// @param chainLabels Axelar corresponding chain labels
function setGlacisChainIds(
uint256[] memory chainIDs,
string[] memory chainLabels
) external onlyOwner {
uint256 chainIdLen = chainIDs.length;
if (chainIdLen != chainLabels.length)
revert GlacisAbstractAdapter__IDArraysMustBeSameLength();
for (uint256 i; i < chainIdLen; ) {
uint256 chainId = chainIDs[i];
string memory chainLabel = chainLabels[i];
if (chainId == 0)
revert GlacisAbstractAdapter__DestinationChainIdNotValid();
glacisChainIdToAdapterChainId[chainId] = chainLabel;
adapterChainIdToGlacisChainId[chainLabel] = chainId;
unchecked {
++i;
}
}
emit GlacisAxelarAdapter__SetGlacisChainIDs(chainIDs, chainLabels);
}
/// @notice Gets the corresponding Axelar chain label for the specified Glacis chain ID
/// @param chainId Glacis chain ID
/// @return The corresponding Axelar label
function adapterChainID(
uint256 chainId
) external view returns (string memory) {
return glacisChainIdToAdapterChainId[chainId];
}
/// @notice Queries if the specified Glacis chain ID is supported by this adapter
/// @param chainId Glacis chain ID
/// @return True if chain is supported, false otherwise
function chainIsAvailable(
uint256 chainId
) public view virtual returns (bool) {
return bytes(glacisChainIdToAdapterChainId[chainId]).length > 0;
}
/// @notice Dispatches payload to specified Glacis chain ID and address through Axelar GMP
/// @param toChainId Destination chain (Glacis ID)
/// @param refundAddress The address to refund native asset surplus
/// @param payload Payload to send
function _sendMessage(
uint256 toChainId,
address refundAddress,
GlacisCommons.CrossChainGas memory,
bytes memory payload
) internal override {
string memory destinationChain = glacisChainIdToAdapterChainId[
toChainId
];
if (remoteCounterpart[toChainId] == bytes32(0))
revert GlacisAbstractAdapter__NoRemoteAdapterForChainId(toChainId);
string memory destinationAddress = remoteCounterpart[toChainId]
.toAddress()
.toHexString();
if (bytes(destinationChain).length == 0)
revert GlacisAbstractAdapter__ChainIsNotAvailable(toChainId);
if (msg.value > 0) {
GAS_SERVICE.payNativeGasForContractCall{value: msg.value}(
address(this),
destinationChain,
destinationAddress,
payload,
refundAddress
);
}
gateway.callContract(destinationChain, destinationAddress, payload);
}
/// @notice Receives route request from Axelar and routes it to GlacisRouter
/// @param sourceChain Source chain (Axelar chain label)
/// @param sourceAddress Source address on remote chain
/// @param payload Payload to route
function _execute(
string calldata sourceChain,
string calldata sourceAddress,
bytes calldata payload
)
internal
override
onlyAuthorizedAdapter(
adapterChainIdToGlacisChainId[sourceChain],
_toLowerCase(string(sourceAddress[2:42])).toAddress().toBytes32()
)
{
GLACIS_ROUTER.receiveMessage(adapterChainIdToGlacisChainId[sourceChain], payload);
}
/// @notice Converts a string to lowercase
/// @param str The string to convert to lowercase
function _toLowerCase(
string memory str
) internal pure returns (string memory) {
bytes memory bStr = bytes(str);
bytes memory bLower = new bytes(bStr.length);
uint256 bStrLen = bStr.length;
for (uint256 i; i < bStrLen; ) {
unchecked {
// Uppercase character...
if ((uint8(bStr[i]) >= 65) && (uint8(bStr[i]) <= 90)) {
// So we add 32 to make it lowercase
bLower[i] = bytes1(uint8(bStr[i]) + 32);
} else {
bLower[i] = bStr[i];
}
++i;
}
}
return string(bLower);
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.18;
import {GlacisAbstractAdapter} from "./GlacisAbstractAdapter.sol";
import {IGlacisRouter} from "../routers/GlacisRouter.sol";
import {IRouterClient} from "@chainlink/contracts-ccip/src/v0.8/ccip/interfaces/IRouterClient.sol";
import {Client} from "@chainlink/contracts-ccip/src/v0.8/ccip/libraries/Client.sol";
import {CCIPReceiver} from "@chainlink/contracts-ccip/src/v0.8/ccip/applications/CCIPReceiver.sol";
import {GlacisAbstractAdapter__IDArraysMustBeSameLength, GlacisAbstractAdapter__DestinationChainIdNotValid, GlacisAbstractAdapter__ChainIsNotAvailable, GlacisAbstractAdapter__NoRemoteAdapterForChainId} from "./GlacisAbstractAdapter.sol";
import {AddressBytes32} from "../libraries/AddressBytes32.sol";
import {GlacisCommons} from "../commons/GlacisCommons.sol";
error GlacisCCIPAdapter__GlacisFeeExtrapolationFailed(
uint256 currentBalance,
uint256 calculatedFees
);
error GlacisCCIPAdapter__RefundAddressMustReceiveNativeCurrency();
error GlacisCCIPAdapter__PaymentTooSmallForAnyDestinationExecution();
/// @title Glacis Adapter for CCIP GMP
/// @notice A Glacis Adapter for CCIP. Sends messages through the CCIP router's ccipSend() and receives
/// messages via _ccipReceive()
contract GlacisCCIPAdapter is GlacisAbstractAdapter, CCIPReceiver {
using AddressBytes32 for address;
mapping(uint256 => uint64) internal glacisChainIdToAdapterChainId;
mapping(uint64 => uint256) public adapterChainIdToGlacisChainId;
// CCIP caps at 3 million gas: https://docs.chain.link/ccip/service-limits
uint256 public ccipGasLimit = 3_000_000;
event GlacisCCIPAdapter__ExtrapolatedGasLimit(
uint256 extrapolation,
uint256 messageValue
);
event GlacisCCIPAdapter__SetGlacisChainIDs(uint256[] chainIDs, uint64[] chainSelectors);
/// @param _glacisRouter This chain's glacis router
/// @param _ccipRouter This chain's CCIP router
/// @param _owner This adapter's owner
constructor(
address _glacisRouter,
address _ccipRouter,
address _owner
)
GlacisAbstractAdapter(IGlacisRouter(_glacisRouter), _owner)
CCIPReceiver(_ccipRouter)
{}
/// @notice Sets the corresponding CCIP selectors for the specified Glacis chain ID
/// @param chainIDs Glacis chain IDs
/// @param chainSelectors Corresponding CCIP chain selectors
function setGlacisChainIds(
uint256[] memory chainIDs,
uint64[] memory chainSelectors
) external onlyOwner {
uint256 chainIdLen = chainIDs.length;
if (chainIdLen != chainSelectors.length)
revert GlacisAbstractAdapter__IDArraysMustBeSameLength();
for (uint256 i; i < chainIdLen; ) {
uint256 chainId = chainIDs[i];
uint64 selector = chainSelectors[i];
if (chainId == 0)
revert GlacisAbstractAdapter__DestinationChainIdNotValid();
glacisChainIdToAdapterChainId[chainId] = selector;
adapterChainIdToGlacisChainId[selector] = chainId;
unchecked {
++i;
}
}
emit GlacisCCIPAdapter__SetGlacisChainIDs(chainIDs, chainSelectors);
}
/// @notice Gets the corresponding CCIP chain selector for the specified Glacis chain ID
/// @param chainId Glacis chain ID
/// @return The corresponding CCIP chain ID
function adapterChainID(uint256 chainId) external view returns (uint64) {
return glacisChainIdToAdapterChainId[chainId];
}
/// @notice Queries if the specified Glacis chain ID is supported by this adapter
/// @param chainId Glacis chain ID
/// @return True if chain is supported, false otherwise
function chainIsAvailable(
uint256 chainId
) public view virtual returns (bool) {
return glacisChainIdToAdapterChainId[chainId] != 0;
}
/// @notice Dispatch payload to specified Glacis chain ID and address through CCIP
/// @param toChainId Destination chain (Glacis ID)
/// @param payload Payload to send
function _sendMessage(
uint256 toChainId,
address refundAddress,
GlacisCommons.CrossChainGas memory incentives,
bytes memory payload
) internal override {
bytes32 remoteAdapter = remoteCounterpart[toChainId];
uint64 destinationChain = glacisChainIdToAdapterChainId[toChainId];
if (remoteAdapter == bytes32(0))
revert GlacisAbstractAdapter__NoRemoteAdapterForChainId(toChainId);
if (destinationChain == 0)
revert GlacisAbstractAdapter__ChainIsNotAvailable(toChainId);
// Initialize a router client instance to interact with cross-chain router
IRouterClient router = IRouterClient(getRouter());
Client.EVM2AnyMessage memory evm2AnyMessage;
uint256 fees;
// Use incentives if available
if (incentives.gasLimit > 0) {
// Create an EVM2AnyMessage struct in memory with necessary information for sending a cross-chain message
evm2AnyMessage = Client.EVM2AnyMessage({
receiver: abi.encode(remoteAdapter), // ABI-encoded receiver address
data: payload,
tokenAmounts: new Client.EVMTokenAmount[](0), // Empty array as no tokens are transferred
// NOTE: extraArgs is subject to changes by CCIP in the future.
// We are not supposed to hard code this, but it's hard to get around. We will likely have to
// regularly redeploy this adapter.
extraArgs: Client._argsToBytes(
// Additional arguments, setting gas limit
// Unfortunately required: https://docs.chain.link/ccip/best-practices#setting-gaslimit
// Also note that unspent gas is NOT REFUNDED
Client.EVMExtraArgsV1({gasLimit: incentives.gasLimit})
),
// Set the feeToken to a feeTokenAddress, indicating specific asset will be used for fees
feeToken: address(0)
});
// Get the fee required to send the CCIP message
fees = router.getFee(destinationChain, evm2AnyMessage);
if (fees > msg.value)
revert GlacisCCIPAdapter__PaymentTooSmallForAnyDestinationExecution();
}
// Otherwise, attempt to extrapolate (not the recommended path)
else {
uint256 extrapolation = extrapolateGasLimitFromValue(
msg.value,
destinationChain,
payload
);
emit GlacisCCIPAdapter__ExtrapolatedGasLimit(
extrapolation,
msg.value
);
// Create an EVM2AnyMessage struct in memory with necessary information for sending a cross-chain message
evm2AnyMessage = Client.EVM2AnyMessage({
receiver: abi.encode(remoteCounterpart[toChainId]), // ABI-encoded receiver address
data: payload,
tokenAmounts: new Client.EVMTokenAmount[](0), // Empty array as no tokens are transferred
// NOTE: extraArgs is subject to changes by CCIP in the future.
// We are not supposed to hard code this, but it's hard to get around. We will likely have to
// regularly redeploy this adapter.
extraArgs: Client._argsToBytes(
// Additional arguments, setting gas limit
// Unfortunately required: https://docs.chain.link/ccip/best-practices#setting-gaslimit
// Also note that unspent gas is NOT REFUNDED
Client.EVMExtraArgsV1({gasLimit: extrapolation})
),
// Set the feeToken to a feeTokenAddress, indicating specific asset will be used for fees
feeToken: address(0)
});
// Get the fee required to send the CCIP message
fees = router.getFee(destinationChain, evm2AnyMessage);
if (fees > msg.value)
revert GlacisCCIPAdapter__GlacisFeeExtrapolationFailed(
msg.value,
fees
);
}
// Send the CCIP message through the router and store the returned CCIP message ID
router.ccipSend{value: fees}(destinationChain, evm2AnyMessage);
// Forward any remaining balance to user
uint256 refund = msg.value - fees;
if (refund > 0) {
(bool successful, ) = address(refundAddress).call{value: refund}(
""
);
if (!successful)
revert GlacisCCIPAdapter__RefundAddressMustReceiveNativeCurrency();
}
}
/// @notice Handles a received message from CCIP
/// @param any2EvmMessage The CCIP formatted message
function _ccipReceive(
Client.Any2EVMMessage memory any2EvmMessage
)
internal
override
onlyAuthorizedAdapter(
adapterChainIdToGlacisChainId[any2EvmMessage.sourceChainSelector],
address(abi.decode(any2EvmMessage.sender, (address))).toBytes32()
)
{
GLACIS_ROUTER.receiveMessage(
adapterChainIdToGlacisChainId[any2EvmMessage.sourceChainSelector],
any2EvmMessage.data
);
}
/// @notice Extrapolates destination chain's gas limit from an amount of the origin chain's gas token
/// for a specific cross-chain transaction
/// @param value The amount of the origin chain's gas token to use to pay for destination gas fees
/// @param destinationChain The destination chain's CCIP chain ID
/// @param payload The bytes payload to send across chains in this message
/// @notice The CCIP fees are linearly calculated, so we can calculate the amount given. Unfortunately,
/// we have to assume that the fee formula stay the same forever. This may not be the case
function extrapolateGasLimitFromValue(
uint256 value,
uint64 destinationChain,
bytes memory payload
) public view returns (uint256) {
IRouterClient router = IRouterClient(getRouter());
uint256 feeAt0GasLimit = router.getFee(
destinationChain,
Client.EVM2AnyMessage({
receiver: abi.encode(remoteCounterpart[destinationChain]), // ABI-encoded receiver address
data: payload,
tokenAmounts: new Client.EVMTokenAmount[](0), // Empty array as no tokens are transferred
// NOTE: extraArgs is subject to changes by CCIP in the future.
// We are not supposed to hard code this, but it's hard to get around. We will likely have to
// regularly redeploy this adapter.
extraArgs: Client._argsToBytes(
// Additional arguments, setting gas limit
// Unfortunately required: https://docs.chain.link/ccip/best-practices#setting-gaslimit
// Also note that unspent gas is NOT REFUNDED
Client.EVMExtraArgsV1({gasLimit: 0})
),
// Set the feeToken to a feeTokenAddress, indicating specific asset will be used for fees
feeToken: address(0)
})
);
uint256 feeAt100kGasLimit = router.getFee(
destinationChain,
Client.EVM2AnyMessage({
receiver: abi.encode(remoteCounterpart[destinationChain]),
data: payload,
tokenAmounts: new Client.EVMTokenAmount[](0),
extraArgs: Client._argsToBytes(
Client.EVMExtraArgsV1({gasLimit: 100_000})
),
feeToken: address(0)
})
);
if (feeAt0GasLimit > value) {
revert GlacisCCIPAdapter__PaymentTooSmallForAnyDestinationExecution();
}
uint256 m = (feeAt100kGasLimit - feeAt0GasLimit) / 100_000 + 1;
// Calculates x = (y-b) / m, but increased m by 0.5% to overestimate value needed
uint256 gasLimit = (value - feeAt0GasLimit) / (m + (m / 200));
if (gasLimit > ccipGasLimit) return ccipGasLimit;
else return gasLimit;
}
/// @notice Sets the CCIP Gas limit (described here: https://docs.chain.link/ccip/service-limits)
/// @param gasLimit New CCIP gas limit
function setCCIPGasLimit(uint256 gasLimit) public onlyOwner {
ccipGasLimit = gasLimit;
}
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
import {IGlacisRouter} from "../interfaces/IGlacisRouter.sol";
import {IGlacisClient} from "../interfaces/IGlacisClient.sol";
import {GlacisAccessControlClient} from "../client/GlacisAccessControlClient.sol";
error GlacisClient__CanOnlyBeCalledByRouter();
error GlacisClient__InvalidRouterAddress();
/// @title Glacis Client
/// @dev This contract encapsulates Glacis client side logic, contracts inheriting this will have access to all
/// Glacis features
abstract contract GlacisClient is GlacisAccessControlClient, IGlacisClient {
address public immutable GLACIS_ROUTER;
event GlacisClient__MessageRouted(
bytes32 indexed messageId,
uint256 toChainId,
bytes32 to
);
event GlacisClient__MessageArrived(
address[] fromAdapters,
uint256 fromChainId,
bytes32 fromAddress
);
/// @param _glacisRouter This chain's deployment of the GlacisRouter
/// @param _quorum The initial default quorum for this client. If dynamic quorum is to be implemented (depending on payload)
/// this value can be ignored and set to 0
constructor(
address _glacisRouter,
uint256 _quorum
) GlacisAccessControlClient() IGlacisClient(_quorum) {
if (_glacisRouter == address(0))
revert GlacisClient__InvalidRouterAddress();
GLACIS_ROUTER = _glacisRouter;
}
/// @notice Routes the payload to the specific address on destination chain through GlacisRouter using a single specified GMP
/// @param chainId Destination chain (Glacis chain ID)
/// @param to Destination address on remote chain
/// @param payload Payload to be routed
/// @param adapter Glacis ID of the GMP to be used for the routing
/// @param refundAddress Address to refund excess gas payment
/// @param gasPayment Amount of gas to cover source and destination gas fees (excess will be refunded)
function _routeSingle(
uint256 chainId,
bytes32 to,
bytes memory payload,
address adapter,
address refundAddress,
uint256 gasPayment
) internal returns (bytes32) {
address[] memory adapters = new address[](1);
adapters[0] = adapter;
CrossChainGas[] memory fees = new CrossChainGas[](1);
fees[0] = CrossChainGas({
gasLimit: 0,
nativeCurrencyValue: uint128(gasPayment)
});
(bytes32 messageId,) = IGlacisRouter(GLACIS_ROUTER).route{
value: gasPayment
}(chainId, to, payload, adapters, fees, refundAddress, false);
emit GlacisClient__MessageRouted(messageId, chainId, to);
return messageId;
}
/// @notice Routes the payload to the specific address on destination chain through GlacisRouter using
/// specified GMPs.
/// @param chainId Destination chain (Glacis chain ID)
/// @param to Destination address on remote chain
/// @param payload Payload to be routed
/// @param adapters The adapters to use for redundant routing
/// @param fees Payment for each GMP to cover source and destination gas fees (excess will be refunded)
/// @param refundAddress Address to refund excess gas payment
/// @param gasPayment Amount of gas to cover source and destination gas fees (excess will be refunded)
function _routeRedundant(
uint256 chainId,
bytes32 to,
bytes memory payload,
address[] memory adapters,
CrossChainGas[] memory fees,
address refundAddress,
uint256 gasPayment
) internal returns (bytes32) {
(bytes32 messageId,) = IGlacisRouter(GLACIS_ROUTER).route{
value: gasPayment
}(chainId, to, payload, adapters, fees, refundAddress, false);
emit GlacisClient__MessageRouted(messageId, chainId, to);
return messageId;
}
/// @notice Routes the payload to the specific address on destination chain through GlacisRouter using GMPs
/// specified in gmps array
/// @param chainId Destination chain (Glacis chain ID)
/// @param to Destination address on remote chain
/// @param payload Payload to be routed
/// @param adapters An array of custom adapters to be used for the routing
/// @param fees Payment for each GMP to cover source and destination gas fees (excess will be refunded)
/// @param refundAddress Address to refund excess gas payment
/// @param retryable True to enable retry feature for this message
/// @param gasPayment Amount of gas to cover source and destination gas fees (excess will be refunded)
function _route(
uint256 chainId,
bytes32 to,
bytes memory payload,
address[] memory adapters,
CrossChainGas[] memory fees,
address refundAddress,
bool retryable,
uint256 gasPayment
) internal returns (bytes32,uint256) {
(bytes32 messageId,uint256 nonce) = IGlacisRouter(GLACIS_ROUTER).route{
value: gasPayment
}(chainId, to, payload, adapters, fees, refundAddress, retryable);
emit GlacisClient__MessageRouted(messageId, chainId, to);
return (messageId,nonce);
}
/// @notice Routes the payload to the specific address on destination chain through GlacisRouter using GMPs
/// specified in gmps array
/// @param chainId Destination chain (Glacis chain ID)
/// @param to Destination address on remote chain
/// @param payload Payload to be routed
/// @param adapters An array of adapters to be used for the routing
/// @param fees Payment for each GMP to cover source and destination gas fees (excess will be refunded)
/// @param refundAddress Address to refund excess gas payment
/// @param messageId The message ID of the message to retry
/// @param nonce The nonce emitted by the original sent message
/// @param gasPayment Amount of gas to cover source and destination gas fees (excess will be refunded)
function _retryRoute(
uint256 chainId,
bytes32 to,
bytes memory payload,
address[] memory adapters,
CrossChainGas[] memory fees,
address refundAddress,
bytes32 messageId,
uint256 nonce,
uint256 gasPayment
) internal returns (bytes32) {
IGlacisRouter(GLACIS_ROUTER).routeRetry{value: gasPayment}(
chainId,
to,
payload,
adapters,
fees,
refundAddress,
messageId,
nonce
);
emit GlacisClient__MessageRouted(messageId, chainId, to);
return messageId;
}
/// @notice Receives message from GMP(s) through GlacisRouter
/// @param fromAdapters addresses of the adapters sent this message (that reached quorum requirements)
/// @param fromChainId Source chain (Glacis chain ID)
/// @param fromAddress Source address on source chain
/// @param payload Routed payload
function receiveMessage(
address[] memory fromAdapters,
uint256 fromChainId,
bytes32 fromAddress,
bytes memory payload
) external virtual override {
if (msg.sender != GLACIS_ROUTER)
revert GlacisClient__CanOnlyBeCalledByRouter();
_receiveMessage(fromAdapters, fromChainId, fromAddress, payload);
emit GlacisClient__MessageArrived(fromAdapters, fromChainId, fromAddress);
}
/// @notice Receives message from GMP(s) through GlacisRouter
/// @param fromAdapters Adapter addresses
/// @param fromChainId Source chain (Glacis chain ID)
/// @param fromAddress Source address on source chain
/// @param payload Routed payload
function _receiveMessage(
address[] memory fromAdapters,
uint256 fromChainId,
bytes32 fromAddress,
bytes memory payload
) internal virtual {}
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
import {GlacisClient} from "./GlacisClient.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {GlacisCommons} from "../commons/GlacisCommons.sol";
/// @title Glacis Ownable Client
/// @dev This contract encapsulates Glacis client side logic, contracts inheriting this will have access to all
/// Glacis features
/// @notice This contract is ownable
abstract contract GlacisClientOwnable is GlacisClient, Ownable {
/// @param _glacisRouter This chain's deployment of the GlacisRouter
/// @param _quorum The default quorum that you would like. If you implement dynamic quorum, this value can be ignored and
/// set to 0
/// @param _owner The owner of this contract
constructor(
address _glacisRouter,
uint256 _quorum,
address _owner
) GlacisClient(_glacisRouter, _quorum) {
_transferOwnership(_owner);
}
/// @notice Add an allowed route for this client
/// @param allowedRoute Route to be added
function addAllowedRoute(
GlacisCommons.GlacisRoute memory allowedRoute
) external onlyOwner {
_addAllowedRoute(allowedRoute);
}
/// @notice Removes an allowed route for this client
/// @param route Allowed route to be removed
function removeAllowedRoute(
GlacisCommons.GlacisRoute calldata route
) external onlyOwner {
_removeAllowedRoute(route);
}
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
/// @title Glacis Commons
/// @dev Contract for utility functions and structures common to Glacis Client and Infrastructure
contract GlacisCommons {
struct GlacisData {
bytes32 messageId;
uint256 nonce;
bytes32 originalFrom;
bytes32 originalTo;
}
struct GlacisTokenData {
address glacisToken;
uint256 glacisTokenAmount;
}
struct GlacisRoute {
uint256 fromChainId; // WILDCARD means any chain
bytes32 fromAddress; // WILDCARD means any address
address fromAdapter; // WILDCARD means any GMP, can also hold address
}
struct CrossChainGas {
uint128 gasLimit;
uint128 nativeCurrencyValue;
}
uint160 constant public WILDCARD = type(uint160).max;
uint256 constant public GLACIS_RESERVED_IDS = 248;
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
import {GlacisAbstractAdapter} from "./GlacisAbstractAdapter.sol";
import {IGlacisRouter} from "../routers/GlacisRouter.sol";
import {IMailbox} from "@hyperlane-xyz/core/contracts/interfaces/IMailbox.sol";
import {StandardHookMetadata} from "@hyperlane-xyz/core/contracts/hooks/libs/StandardHookMetadata.sol";
import {TypeCasts} from "@hyperlane-xyz/core/contracts/libs/TypeCasts.sol";
import {IInterchainSecurityModule} from "@hyperlane-xyz/core/contracts/interfaces/IInterchainSecurityModule.sol";
import {IPostDispatchHook} from "@hyperlane-xyz/core/contracts/interfaces/hooks/IPostDispatchHook.sol";
import {GlacisAbstractAdapter__IDArraysMustBeSameLength, GlacisAbstractAdapter__DestinationChainIdNotValid, GlacisAbstractAdapter__NoRemoteAdapterForChainId, GlacisAbstractAdapter__ChainIsNotAvailable} from "./GlacisAbstractAdapter.sol";
import {GlacisCommons} from "../commons/GlacisCommons.sol";
error GlacisHyperlaneAdapter__OnlyMailboxAllowed();
error GlacisHyperlaneAdapter__FeeNotEnough();
error GlacisHyperlaneAdapter__RefundAddressMustReceiveNativeCurrency();
/// @title Glacis Adapter for Hyperlane
/// @notice A Glacis Adapter for the cannonical Hyperlane network. Sends messages through dispatch() and receives
/// messages via handle()
/// @notice Opted to create our own mailbox client because Hyperlane's base Mailbox refund address was static
contract GlacisHyperlaneAdapter is GlacisAbstractAdapter {
// Required by Hyperlane, if kept as 0 then it will use the default router.
IInterchainSecurityModule public interchainSecurityModule;
IMailbox public immutable MAIL_BOX;
uint32 public immutable LOCAL_DOMAIN;
uint256 internal constant DEFAULT_GAS_LIMIT = 350_000;
mapping(uint256 => uint32) public glacisChainIdToAdapterChainId;
mapping(uint32 => uint256) public adapterChainIdToGlacisChainId;
/// @param _glacisRouter This chain's glacis router
/// @param _hyperlaneMailbox This chain's hyperlane router
/// @param _owner This adapter's owner
constructor(
address _glacisRouter,
address _hyperlaneMailbox,
address _owner
) GlacisAbstractAdapter(IGlacisRouter(_glacisRouter), _owner) {
MAIL_BOX = IMailbox(_hyperlaneMailbox);
LOCAL_DOMAIN = MAIL_BOX.localDomain();
}
/// @notice Sets the corresponding Hyperlane domain for the specified Glacis chain ID
/// @param chainIds Glacis chain IDs
/// @param domains Hyperlane corresponding chain domains
function setGlacisChainIds(
uint256[] memory chainIds,
uint32[] memory domains
) public onlyOwner {
uint256 chainIdLen = chainIds.length;
if (chainIdLen != domains.length)
revert GlacisAbstractAdapter__IDArraysMustBeSameLength();
for (uint256 i; i < chainIdLen; ) {
uint256 chainId = chainIds[i];
uint32 chainLabel = domains[i];
if (chainId == 0)
revert GlacisAbstractAdapter__DestinationChainIdNotValid();
glacisChainIdToAdapterChainId[chainId] = chainLabel;
adapterChainIdToGlacisChainId[chainLabel] = chainId;
unchecked {
++i;
}
}
}
/// @notice Gets the corresponding Hyperlane domain ID for the specified Glacis chain ID
/// @param chainId Glacis chain ID
/// @return The corresponding Hyperlane domain ID
function adapterChainID(uint256 chainId) external view returns (uint32) {
return glacisChainIdToAdapterChainId[chainId];
}
/// @notice Queries if the specified Glacis chain ID is supported by this adapter
/// @param chainId Glacis chain ID
/// @return True if chain is supported, false otherwise
function chainIsAvailable(
uint256 chainId
) public view virtual returns (bool) {
return glacisChainIdToAdapterChainId[chainId] != 0;
}
/// @notice Dispatch payload to specified Glacis chain ID and address through Hyperlane
/// @param toChainId Destination chain (Glacis ID)
/// @param refundAddress The address to send excess fee payments to
/// @param payload Payload to send
function _sendMessage(
uint256 toChainId,
address refundAddress,
GlacisCommons.CrossChainGas memory crossChainGas,
bytes memory payload
) internal override onlyGlacisRouter {
uint32 destinationDomain = glacisChainIdToAdapterChainId[toChainId];
bytes32 destinationAddress = remoteCounterpart[toChainId];
if (destinationAddress == bytes32(0))
revert GlacisAbstractAdapter__NoRemoteAdapterForChainId(toChainId);
if (destinationDomain == 0)
revert GlacisAbstractAdapter__ChainIsNotAvailable(toChainId);
// Generate metadata using refundAddress
uint256 gasLimit = crossChainGas.gasLimit == 0 ? DEFAULT_GAS_LIMIT : crossChainGas.gasLimit;
bytes memory metadata = StandardHookMetadata.formatMetadata({
_msgValue: 0, // unused by us
_gasLimit: gasLimit, // override default gas limit
_refundAddress: refundAddress, // override default refund address
_customMetadata: "" // none needed for default hook
});
// Ensure that we have enough of the required fee (will revert if not this value)
uint256 nativePriceQuote = MAIL_BOX.quoteDispatch(
destinationDomain,
destinationAddress,
payload,
metadata
);
if (msg.value < nativePriceQuote) {
revert GlacisHyperlaneAdapter__FeeNotEnough();
}
// Send message across chains
MAIL_BOX.dispatch{value: nativePriceQuote}(
destinationDomain,
destinationAddress,
payload,
metadata,
IPostDispatchHook(address(0)) // hook
);
// Send rest to refund address
if (msg.value > nativePriceQuote) {
(bool successful, ) = address(refundAddress).call{
value: msg.value - nativePriceQuote
}("");
if (!successful)
revert GlacisHyperlaneAdapter__RefundAddressMustReceiveNativeCurrency();
}
}
/// @notice Receives messages from Hyperlane
/// @param _origin The hyperlane domain ID
/// @param _sender The bytes32 representation of the origin's sender address
/// @param _message The bytes payload of the message
function handle(
uint32 _origin,
bytes32 _sender,
bytes calldata _message
)
external
payable
onlyAuthorizedAdapter(adapterChainIdToGlacisChainId[_origin], _sender)
{
if (msg.sender != address(MAIL_BOX)) {
revert GlacisHyperlaneAdapter__OnlyMailboxAllowed();
}
GLACIS_ROUTER.receiveMessage(adapterChainIdToGlacisChainId[_origin], _message);
}
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
import {IGlacisRouter} from "../../interfaces/IGlacisRouter.sol";
import {GlacisAbstractAdapter} from "../GlacisAbstractAdapter.sol";
import {SimpleNonblockingLzApp} from "./SimpleNonblockingLzApp.sol";
import {GlacisAbstractAdapter__IDArraysMustBeSameLength, GlacisAbstractAdapter__DestinationChainIdNotValid, GlacisAbstractAdapter__ChainIsNotAvailable, GlacisAbstractAdapter__NoRemoteAdapterForChainId} from "../GlacisAbstractAdapter.sol";
import {AddressBytes32} from "../../libraries/AddressBytes32.sol";
import {GlacisCommons} from "../../commons/GlacisCommons.sol";
/// @title Glacis Adapter for Layer Zero
/// @notice A Glacis Adapter for the LayerZero V1. Sends messages through _lzSend() and receives
/// messages via _nonblockingLzReceive()
contract GlacisLayerZeroAdapter is
SimpleNonblockingLzApp,
GlacisAbstractAdapter
{
using AddressBytes32 for bytes32;
constructor(
address _glacisRouter,
address _lzEndpoint,
address _owner
)
SimpleNonblockingLzApp(_lzEndpoint)
GlacisAbstractAdapter(IGlacisRouter(_glacisRouter), _owner)
{}
mapping(uint256 => uint16) internal glacisChainIdToAdapterChainId;
mapping(uint16 => uint256) public adapterChainIdToGlacisChainId;
bytes public adapterParams = bytes("");
event GlacisLayerZeroAdapter__SetGlacisChainIDs(uint256[] chainIDs, uint16[] lzIDs);
/// @notice Sets the corresponding LayerZero chain ID for the specified Glacis chain ID
/// @param chainIDs Glacis chain IDs
/// @param lzIDs Layer Zero chain IDs
function setGlacisChainIds(
uint256[] calldata chainIDs,
uint16[] calldata lzIDs
) external onlyOwner {
uint256 glacisIDsLen = chainIDs.length;
if (glacisIDsLen != lzIDs.length)
revert GlacisAbstractAdapter__IDArraysMustBeSameLength();
for (uint256 i; i < glacisIDsLen; ) {
uint256 glacisID = chainIDs[i];
uint16 lzID = lzIDs[i];
if (glacisID == 0)
revert GlacisAbstractAdapter__DestinationChainIdNotValid();
glacisChainIdToAdapterChainId[glacisID] = lzID;
adapterChainIdToGlacisChainId[lzID] = glacisID;
unchecked {
++i;
}
}
emit GlacisLayerZeroAdapter__SetGlacisChainIDs(chainIDs, lzIDs);
}
/// @notice Gets the corresponding LayerZero chain ID for the specified Glacis chain ID
/// @param chainId Glacis chain ID
/// @return The corresponding LayerZero chain Id as bytes32
function adapterChainID(uint256 chainId) external view returns (uint16) {
return glacisChainIdToAdapterChainId[chainId];
}
/// @notice Queries if the specified Glacis chain ID is supported by this adapter
/// @param chainId Glacis chain ID
/// @return True if chain is supported, false otherwise
function chainIsAvailable(uint256 chainId) public view returns (bool) {
return glacisChainIdToAdapterChainId[chainId] != 0;
}
/// @notice Dispatch payload to specified Glacis chain ID and address through LayerZero GMP
/// @param toChainId Destination chain (Glacis ID)
/// @param refundAddress The address to refund native asset surplus
/// @param payload Payload to send
function _sendMessage(
uint256 toChainId,
address refundAddress,
GlacisCommons.CrossChainGas memory,
bytes memory payload
) internal override {
bytes32 remoteCounterpart = remoteCounterpart[toChainId];
uint16 _dstchainId = glacisChainIdToAdapterChainId[toChainId];
if (remoteCounterpart == bytes32(0))
revert GlacisAbstractAdapter__NoRemoteAdapterForChainId(toChainId);
if (_dstchainId == 0)
revert GlacisAbstractAdapter__ChainIsNotAvailable(toChainId);
_lzSend({
_dstChainId: _dstchainId,
_dstChainAddress: remoteCounterpart.toAddress(),
_payload: payload,
_refundAddress: payable(refundAddress),
_zroPaymentAddress: address(0),
_adapterParams: adapterParams,
_nativeFee: msg.value
});
}
/// @notice Receives route message from LayerZero and routes it to GlacisRouter
/// @param srcChainId Source chain (LayerZero ID)
/// @param sourceAddress Source address on remote chain
/// @param payload Payload to route
function _nonblockingLzReceive(
uint16 srcChainId,
bytes memory sourceAddress, // srcAddress, will be the other adapter
uint64,
bytes memory payload
)
internal
override
// Only supports EVMs
onlyAuthorizedAdapter(
adapterChainIdToGlacisChainId[srcChainId],
bytes32(bytes20(sourceAddress)) >> 96
)
{
GLACIS_ROUTER.receiveMessage(
adapterChainIdToGlacisChainId[srcChainId],
payload
);
}
/// Sets the adapter parameters for LayerZero messages.
/// @param params The desired adapter params.
function setAdapterParams(bytes memory params) external onlyOwner {
adapterParams = params;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import {IGlacisRouter} from "../../interfaces/IGlacisRouter.sol";
import {OAppNoPeer} from "./v2/OAppNoPeer.sol";
import {Origin} from "@layerzerolabs/oapp-evm/contracts/oapp/interfaces/IOAppReceiver.sol";
import {MessagingParams} from "@layerzerolabs/lz-evm-protocol-v2/contracts/interfaces/ILayerZeroEndpointV2.sol";
import {OptionsBuilder} from "@layerzerolabs/oapp-evm/contracts/oapp/libs/OptionsBuilder.sol";
import {GlacisAbstractAdapter} from "../GlacisAbstractAdapter.sol";
import {AddressBytes32} from "../../libraries/AddressBytes32.sol";
import {GlacisAbstractAdapter__IDArraysMustBeSameLength, GlacisAbstractAdapter__DestinationChainIdNotValid, GlacisAbstractAdapter__ChainIsNotAvailable, GlacisAbstractAdapter__NoRemoteAdapterForChainId, GlacisAbstractAdapter__OnlyAdapterAllowed} from "../GlacisAbstractAdapter.sol";
import {GlacisCommons} from "../../commons/GlacisCommons.sol";
error GlacisLayerZeroV2Adapter__PeersDisabledUseCounterpartInstead();
contract GlacisLayerZeroV2Adapter is OAppNoPeer, GlacisAbstractAdapter {
using AddressBytes32 for bytes32;
using OptionsBuilder for bytes;
constructor(
address _glacisRouter,
address _lzEndpoint,
address _owner
)
OAppNoPeer(_lzEndpoint, _owner)
GlacisAbstractAdapter(IGlacisRouter(_glacisRouter), _owner)
{}
uint128 internal constant DEFAULT_GAS_LIMIT = 350_000;
mapping(uint256 => uint32) internal glacisChainIdToAdapterChainId;
mapping(uint32 => uint256) public adapterChainIdToGlacisChainId;
event GlacisLayerZeroV2Adapter__SetGlacisChainIDs(
uint256[] chainIDs,
uint32[] lzIDs
);
/// @notice Sets the corresponding LayerZero chain ID for the specified Glacis chain ID
/// @param chainIDs Glacis chain IDs
/// @param lzIDs Layer Zero chain IDs
function setGlacisChainIds(
uint256[] calldata chainIDs,
uint32[] calldata lzIDs
) external onlyOwner {
uint256 glacisIDsLen = chainIDs.length;
if (glacisIDsLen != lzIDs.length)
revert GlacisAbstractAdapter__IDArraysMustBeSameLength();
for (uint256 i; i < glacisIDsLen; ) {
uint256 glacisID = chainIDs[i];
uint32 lzID = lzIDs[i];
if (glacisID == 0)
revert GlacisAbstractAdapter__DestinationChainIdNotValid();
glacisChainIdToAdapterChainId[glacisID] = lzID;
adapterChainIdToGlacisChainId[lzID] = glacisID;
unchecked {
++i;
}
}
emit GlacisLayerZeroV2Adapter__SetGlacisChainIDs(chainIDs, lzIDs);
}
/// @notice Gets the corresponding LayerZero chain ID for the specified Glacis chain ID
/// @param chainId Glacis chain ID
/// @return The corresponding LayerZero chain Id as bytes32
function adapterChainID(uint256 chainId) external view returns (uint32) {
return glacisChainIdToAdapterChainId[chainId];
}
/// @notice Queries if the specified Glacis chain ID is supported by this adapter
/// @param chainId Glacis chain ID
/// @return True if chain is supported, false otherwise
function chainIsAvailable(uint256 chainId) public view returns (bool) {
return glacisChainIdToAdapterChainId[chainId] != 0;
}
/// @notice Dispatch payload to specified Glacis chain ID and address through LayerZero GMP
/// @param toChainId Destination chain (Glacis ID)
/// @param refundAddress The address to refund native asset surplus
/// @param payload Payload to send
function _sendMessage(
uint256 toChainId,
address refundAddress,
GlacisCommons.CrossChainGas memory gas,
bytes memory payload
) internal override {
bytes32 remoteCounterpart = remoteCounterpart[toChainId];
uint32 _dstEid = glacisChainIdToAdapterChainId[toChainId];
if (remoteCounterpart == bytes32(0))
revert GlacisAbstractAdapter__NoRemoteAdapterForChainId(toChainId);
if (_dstEid == 0)
revert GlacisAbstractAdapter__ChainIsNotAvailable(toChainId);
uint128 expectedGasLimit = gas.gasLimit == 0 ? DEFAULT_GAS_LIMIT : gas.gasLimit;
// solhint-disable-next-line check-send-result
endpoint.send{value: msg.value}(
MessagingParams(
_dstEid,
remoteCounterpart,
payload,
OptionsBuilder.newOptions().addExecutorLzReceiveOption(expectedGasLimit, 0),
false
),
refundAddress
);
}
/**
* @dev Called when data is received from the protocol. It overrides the equivalent function in the parent contract.
* Protocol messages are defined as packets, comprised of the following parameters.
* @param _origin A struct containing information about where the packet came from.
* @param payload Encoded message.
*/
function _lzReceive(
Origin calldata _origin,
bytes32, // guid
bytes calldata payload,
address, // Executor address as specified by the OApp.
bytes calldata // Any extra data or options to trigger on receipt.
)
internal
override
onlyAuthorizedAdapter(
adapterChainIdToGlacisChainId[_origin.srcEid],
_origin.sender
)
{
GLACIS_ROUTER.receiveMessage(
adapterChainIdToGlacisChainId[_origin.srcEid],
payload
);
}
/**
* @notice Checks if the path initialization is allowed based on the provided origin.
* @param origin The origin information containing the source endpoint and sender address.
* @return Whether the path has been initialized.
*
* @dev This indicates to the endpoint that the OApp has enabled msgs for this particular path to be received.
* @dev This defaults to assuming if a peer has been set, its initialized.
*/
function allowInitializePath(
Origin calldata origin
) public view override returns (bool) {
return
remoteCounterpart[adapterChainIdToGlacisChainId[origin.srcEid]] ==
origin.sender;
}
function peers(uint32 _eid) external view returns (bytes32 peer) {
return remoteCounterpart[adapterChainIdToGlacisChainId[_eid]];
}
function setPeer(uint32, bytes32) external view onlyOwner {
revert GlacisLayerZeroV2Adapter__PeersDisabledUseCounterpartInstead();
}
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
import {Ownable2Step} from "@openzeppelin/contracts/access/Ownable2Step.sol";
import {IGlacisRemoteCounterpartManager} from "../interfaces/IGlacisRemoteCounterpartManager.sol";
error GlacisRemoteCounterpartManager__RemoteCounterpartCannotHaveChainIdZero();
error GlacisRemoteCounterpartManager__CounterpartsAndChainIDsMustHaveSameLength();
/// @title Glacis Remote Counterpart Manager
/// @notice An inheritable contract that allows an owner to add and remove remote counterparts
/// @notice Is an ownable contract
contract GlacisRemoteCounterpartManager is
IGlacisRemoteCounterpartManager,
Ownable2Step
{
mapping(uint256 => bytes32) internal remoteCounterpart;
/// @notice Adds an authorized glacis counterpart component in a remote chain that interacts with this component
/// @param chainIDs An array with chains of the glacis remote components
/// @param counterpart An array of addresses of the glacis components on remote chains
function addRemoteCounterparts(
uint256[] calldata chainIDs,
bytes32[] calldata counterpart
) external onlyOwner {
if (chainIDs.length != counterpart.length)
revert GlacisRemoteCounterpartManager__CounterpartsAndChainIDsMustHaveSameLength();
for (uint256 i; i < chainIDs.length; ++i) {
if (chainIDs[i] == 0)
revert GlacisRemoteCounterpartManager__RemoteCounterpartCannotHaveChainIdZero();
remoteCounterpart[chainIDs[i]] = counterpart[i];
}
}
/// @notice Removes an authorized glacis counterpart component on remote chain that this components interacts with
/// @param chainId The chainId to remove the remote component
function removeRemoteCounterpart(uint256 chainId) external onlyOwner {
if (chainId == 0)
revert GlacisRemoteCounterpartManager__RemoteCounterpartCannotHaveChainIdZero();
delete remoteCounterpart[chainId];
}
/// @notice Gets an authorized glacis counterpart component on remote chain that this components interacts with
/// @param chainId The chainId to of the remote component
function getRemoteCounterpart(
uint256 chainId
) public view returns (bytes32) {
return remoteCounterpart[chainId];
}
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
import {GlacisAbstractRouter} from "./GlacisAbstractRouter.sol";
import {IGlacisClient} from "../interfaces/IGlacisClient.sol";
import {IGlacisAdapter} from "../interfaces/IGlacisAdapter.sol";
import {IGlacisRouter} from "../interfaces/IGlacisRouter.sol";
import {AddressBytes32} from "../libraries/AddressBytes32.sol";
error GlacisRouter__GMPNotSupported(); //0xed2e8008
error GlacisRouter__RouteDoesNotExist(); //0xeb470cd2
error GlacisRouter__ClientDeniedRoute();
error GlacisRouter__NotOwnerOfMessageToRetry();
error GlacisRouter__MessageInputNotIdenticalForRetry();
error GlacisRouter__MessageAlreadyReceivedFromGMP();
error GlacisRouter__MessageIdNotValid();
error GlacisRouter__FeeArrayMustEqualGMPArray();
error GlacisRouter__GMPCountMustBeAtLeastOne();
error GlacisRouter__FeeSumMustBeEqualToValue();
error GlacisRouter__DestinationRetryNotSatisfied(
bool quorumSatisfied,
bool notExecuted,
bool quorumIsNotZero
);
/// @title Glacis Router
/// @notice A central router to send and receive cross-chain messages
contract GlacisRouter is GlacisAbstractRouter, IGlacisRouter {
using AddressBytes32 for address;
using AddressBytes32 for bytes32;
// Sending messages
mapping(bytes32 => address) public messageSenders;
// Receiving messages
mapping(bytes32 => MessageData) private messageReceipts;
mapping(bytes32 => mapping(address => bool))
private receivedCustomAdapterMessages;
mapping(bytes32 => address[]) private receivedAdaptersList;
struct MessageData {
uint248 uniqueMessagesReceived;
bool executed;
}
/// @param _owner The owner of this contract
constructor(address _owner) GlacisAbstractRouter(block.chainid) {
_transferOwnership(_owner);
}
/// @notice Routes the payload to the specific address on the destination chain using specified adapters
/// @param chainId Destination chain (EIP-155)
/// @param to Destination address on remote chain
/// @param payload Payload to be routed
/// @param adapters An array of adapters to be used for the routing (addresses 0x01-0xF8 for Glacis adapters
/// or specific addresses for custom adapters)
/// @param fees Array of fees to be sent to each GMP & custom adapter for routing (must be same length as gmps)
/// @param refundAddress An address for native currency to be sent to that are greater than fees charged. If it is a
/// contract it needs to support receive function, tx will revert otherwise
/// @param retryable True if this message could pottentially be retried
function route(
uint256 chainId,
bytes32 to,
bytes memory payload,
address[] memory adapters,
GlacisRouter.CrossChainGas[] memory fees,
address refundAddress,
bool retryable
) public payable virtual returns (bytes32 messageId, uint256 nonce) {
// Validate input
validateFeesInput(adapters.length, fees);
bytes32 from = msg.sender.toBytes32();
(messageId, nonce) = _createGlacisMessageId(chainId, to, payload);
// Store messageId owner if retryable flagged
if (retryable) {
messageSenders[messageId] = msg.sender;
}
_processRouting(
chainId,
// @notice This follows GlacisData (within GlacisCommons) + payload
abi.encode(messageId, nonce, from, to, payload),
adapters,
fees,
refundAddress
);
// Emit Glacis message dispatched event
emit GlacisRouter__MessageDispatched(
messageId,
from,
chainId,
to,
payload,
adapters,
fees,
refundAddress,
retryable
);
return (messageId, nonce);
}
/// @notice Retries routing the payload to the specific address on destination chain
/// using specified GMPs and quorum
/// @param chainId Destination chain (Glacis chain ID)
/// @param to Destination address on remote chain
/// @param payload Payload to be routed
/// @param adapters An array of custom adapters to be used for the routing
/// @param fees Array of fees to be sent to each GMP for routing (must be same length as gmps)
/// @param refundAddress An (ideally EOA) address for native currency to be sent to that are greater than fees charged
/// @param messageId The messageId to retry
/// @param nonce Unique value for this message routing
/// @return A tuple with a bytes32 messageId and a uint256 nonce
function routeRetry(
uint256 chainId,
bytes32 to,
bytes memory payload,
address[] memory adapters,
GlacisRouter.CrossChainGas[] memory fees,
address refundAddress,
bytes32 messageId,
uint256 nonce
) public payable virtual returns (bytes32, uint256) {
// Validate input
validateFeesInput(adapters.length, fees);
address ownerOfMessageToRetry = messageSenders[messageId];
if (ownerOfMessageToRetry != msg.sender)
revert GlacisRouter__NotOwnerOfMessageToRetry();
if (
!validateGlacisMessageId(
messageId,
chainId,
GLACIS_CHAIN_ID,
to,
nonce,
payload
)
) revert GlacisRouter__MessageInputNotIdenticalForRetry();
bytes32 from = msg.sender.toBytes32();
bytes memory glacisPackedPayload = abi.encode(
messageId,
nonce,
from,
to,
payload
);
_processRouting(
chainId,
glacisPackedPayload,
adapters,
fees,
refundAddress
);
// Emit Glacis message retried event
emit GlacisRouter__MessageRetried(
messageId,
from,
chainId,
to,
payload,
adapters,
fees,
refundAddress
);
// There is no need to check that this has been retried before. Retry as many times as desired.
return (messageId, nonce);
}
/// @notice Performs actual message dispatching to all the required adapters
/// @param chainId Destination chain (Glacis chain ID)
/// @param glacisPackedPayload Payload with embedded glacis data to be routed
/// @param adapters An array of custom adapters to be used for the routing
/// @param fees Payment for each GMP to cover source and destination gas fees (excess will be refunded)
/// @param refundAddress Address to refund excess gas payment
function _processRouting(
uint256 chainId,
bytes memory glacisPackedPayload,
address[] memory adapters,
CrossChainGas[] memory fees,
address refundAddress
) internal {
uint256 adaptersLength = adapters.length;
for (uint8 adapterIndex; adapterIndex < adaptersLength; ) {
address adapter = adapters[adapterIndex];
// If adapter address is a reserved ID, we override it with a Glacis default adapter
if (uint160(adapter) <= GLACIS_RESERVED_IDS) {
adapter = glacisGMPIdToAdapter[uint8(uint160(adapter))];
if (adapter == address(0))
revert GlacisRouter__GMPNotSupported();
} else if (adapter == address(0))
revert GlacisRouter__RouteDoesNotExist();
IGlacisAdapter(adapter).sendMessage{
value: fees[adapterIndex].nativeCurrencyValue
}(chainId, refundAddress, fees[adapterIndex], glacisPackedPayload);
// This is acceptable, as there is no "continue" within the for loop
unchecked {
++adapterIndex;
}
}
}
/// @notice Receives a cross chain message from an IGlacisAdapter.
/// @param fromChainId Source chain (Glacis chain ID)
/// @param glacisPayload Received payload with embedded GlacisData
function receiveMessage(
uint256 fromChainId,
bytes memory glacisPayload
) public {
// Decode sent data
(GlacisData memory glacisData, bytes memory payload) = abi.decode(
glacisPayload,
(GlacisData, bytes)
);
// Get the client
IGlacisClient client = IGlacisClient(glacisData.originalTo.toAddress());
// Verifies that the sender is an allowed route
uint8 gmpId = adapterToGlacisGMPId[msg.sender];
bool routeAllowed = client.isAllowedRoute(
GlacisRoute(fromChainId, glacisData.originalFrom, msg.sender),
payload
);
if (!routeAllowed && gmpId != 0) {
routeAllowed = client.isAllowedRoute(
GlacisRoute(
fromChainId,
glacisData.originalFrom,
address(uint160(gmpId))
),
payload
);
}
if (!routeAllowed) revert GlacisRouter__ClientDeniedRoute();
// Check if the message per GMP is unique
MessageData memory currentReceipt = messageReceipts[
glacisData.messageId
];
// Ensures that the message hasn't come from the same adapter again
if (receivedCustomAdapterMessages[glacisData.messageId][msg.sender])
revert GlacisRouter__MessageAlreadyReceivedFromGMP();
// Get the quorum requirements
uint256 quorum = client.getQuorum(
glacisData,
payload,
currentReceipt.uniqueMessagesReceived
);
receivedCustomAdapterMessages[glacisData.messageId][msg.sender] = true;
currentReceipt.uniqueMessagesReceived += 1;
receivedAdaptersList[glacisData.messageId].push(msg.sender);
emit GlacisRouter__ReceivedMessage(
glacisData.messageId,
glacisData.originalFrom,
fromChainId,
msg.sender,
glacisData.originalTo
);
// Verify that the messageID can be calculated from the data provided,
if (
!_validateGlacisMessageId(
glacisData.messageId,
GLACIS_CHAIN_ID,
fromChainId,
glacisData.originalTo,
glacisData.originalFrom,
glacisData.nonce,
payload
)
) revert GlacisRouter__MessageIdNotValid();
if (
currentReceipt.uniqueMessagesReceived == quorum &&
!currentReceipt.executed
) {
currentReceipt.executed = true;
messageReceipts[glacisData.messageId] = currentReceipt;
client.receiveMessage(
receivedAdaptersList[glacisData.messageId],
fromChainId,
glacisData.originalFrom,
payload
);
emit GlacisRouter__ExecutedMessage(
glacisData.messageId,
glacisData.originalFrom,
fromChainId,
msg.sender,
glacisData.originalTo
);
} else {
messageReceipts[glacisData.messageId] = currentReceipt;
}
}
/// @notice Retries execution of a cross-chain message without incrementing quorum.
/// @param fromChainId Source chain (Glacis chain ID)
/// @param glacisPayload Received payload with embedded GlacisData
function retryReceiveMessage(
uint256 fromChainId,
bytes memory glacisPayload
) public {
// Decode sent data
(GlacisData memory glacisData, bytes memory payload) = abi.decode(
glacisPayload,
(GlacisData, bytes)
);
// Get the client
IGlacisClient client = IGlacisClient(glacisData.originalTo.toAddress());
// Check satisfaction of current receipt
MessageData memory currentReceipt = messageReceipts[
glacisData.messageId
];
// Get the quorum requirements
uint256 quorum = client.getQuorum(
glacisData,
payload,
currentReceipt.uniqueMessagesReceived
);
// Verify that the messageID can be calculated from the data provided,
if (
!_validateGlacisMessageId(
glacisData.messageId,
GLACIS_CHAIN_ID,
fromChainId,
glacisData.originalTo,
glacisData.originalFrom,
glacisData.nonce,
payload
)
) revert GlacisRouter__MessageIdNotValid();
// Execute if quorum is satisfied
if (
currentReceipt.uniqueMessagesReceived >= quorum &&
!currentReceipt.executed &&
quorum > 0
) {
currentReceipt.executed = true;
messageReceipts[glacisData.messageId] = currentReceipt;
client.receiveMessage(
receivedAdaptersList[glacisData.messageId],
fromChainId,
glacisData.originalFrom,
payload
);
} else
revert GlacisRouter__DestinationRetryNotSatisfied(
currentReceipt.uniqueMessagesReceived >= quorum,
!currentReceipt.executed,
quorum > 0
);
}
/// @notice Validates that all the fees sum up to the total payment
/// @param adaptersLength The length of the gmps array + custom adapters array
/// @param fees The fees array
function validateFeesInput(
uint256 adaptersLength,
CrossChainGas[] memory fees
) internal {
if (adaptersLength == 0)
revert GlacisRouter__GMPCountMustBeAtLeastOne();
if (adaptersLength != fees.length)
revert GlacisRouter__FeeArrayMustEqualGMPArray();
uint256 feeSum;
for (uint8 i; i < adaptersLength; ) {
feeSum += fees[i].nativeCurrencyValue;
unchecked {
++i;
}
}
if (feeSum != msg.value)
revert GlacisRouter__FeeSumMustBeEqualToValue();
}
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
import {IGlacisTokenMediator} from "../interfaces/IGlacisTokenMediator.sol";
import {IGlacisTokenClient} from "../interfaces/IGlacisTokenClient.sol";
import {GlacisClient} from "../client/GlacisClient.sol";
import {GlacisCommons} from "../commons/GlacisCommons.sol";
error GlacisTokenClient__CanOnlyBeCalledByTokenRouter();
/// @title Glacis Token Client
/// @dev This contract encapsulates Glacis Token Passing client logic, contracts inheriting this will have access to all
/// Glacis Token Passing and Message Passing features
abstract contract GlacisTokenClient is GlacisClient, IGlacisTokenClient {
address public immutable GLACIS_TOKEN_ROUTER;
event GlacisTokenClient__MessageRouted(
bytes32 indexed messageId,
uint256 toChainId,
bytes32 to
);
constructor(
address glacisTokenMediator_,
address glacisRouter_,
uint256 quorum
) GlacisClient(glacisRouter_, quorum) {
GLACIS_TOKEN_ROUTER = glacisTokenMediator_;
}
/// @notice Convenient method - Routes message and tokens to destination through GlacisTokenMediator using specified GMP without any additional feature
/// @param chainId Destination chain (Glacis chain ID)
/// @param to Destination address on remote chain
/// @param payload Payload to be routed
/// @param adapter Glacis ID or address of the adapter of the GMP to be used for the routing
/// @param refundAddress Address to refund excess gas payment
/// @param token Token (implementing XERC20 standard) to be sent to remote contract
/// @param tokenAmount Amount of token to send to remote contract
/// @param gasPayment Amount of gas to cover source and destination gas fees (excess will be refunded)
function _routeWithTokensSingle(
uint256 chainId,
bytes32 to,
bytes memory payload,
address adapter,
address refundAddress,
address token,
uint256 tokenAmount,
uint256 gasPayment
) internal returns (bytes32,uint256) {
address[] memory adapters = new address[](1);
adapters[0] = adapter;
CrossChainGas[] memory fees = new CrossChainGas[](1);
fees[0] = CrossChainGas({
gasLimit: 0,
nativeCurrencyValue: uint128(msg.value)
});
return
_routeWithTokens(
chainId,
to,
payload,
adapters,
fees,
refundAddress,
token,
tokenAmount,
gasPayment
);
}
/// @notice Convenient method - Routes message and tokens to destination through GlacisTokenMediator using specified GMPs with redundancy feature
/// @param chainId Destination chain (Glacis chain ID)
/// @param to Destination address on remote chain
/// @param adapters The GMP Ids to use for routing
/// @param fees Payment for each GMP to cover source and destination gas fees (excess will be refunded)
/// @param refundAddress Address to refund excess gas payment
/// @param token A token address inheriting GlacisToken or GlacisTokenProxy standard (xERC-20)
/// @param tokenAmount Amount of token to send to remote contract
/// @param gasPayment Amount of gas to cover source and destination gas fees (excess will be refunded)
function _routeWithTokensRedundant(
uint256 chainId,
bytes32 to,
bytes memory payload,
address[] memory adapters,
CrossChainGas[] memory fees,
address refundAddress,
address token,
uint256 tokenAmount,
uint256 gasPayment
) internal returns (bytes32,uint256) {
return
_routeWithTokens(
chainId,
to,
payload,
adapters,
fees,
refundAddress,
token,
tokenAmount,
gasPayment
);
}
/// @notice Routes message and tokens to destination through GlacisTokenMediator using any feature
/// @param chainId Destination chain (Glacis chain ID)
/// @param to Destination address on remote chain
/// @param payload The bytes payload to send across chains
/// @param adapters An array of adapters to be used for the routing
/// @param fees An array of values to send to the gmps & custom adapters. Should sum to the gasPayment
/// @param refundAddress Address to refund excess gas payment
/// @param token A token address inheriting GlacisToken or GlacisTokenProxy standard (xERC-20)
/// @param tokenAmount Amount of token to send to remote contract
/// @param gasPayment Amount of gas to cover source and destination gas fees (excess will be refunded)
function _routeWithTokens(
uint256 chainId,
bytes32 to,
bytes memory payload,
address[] memory adapters,
CrossChainGas[] memory fees,
address refundAddress,
address token,
uint256 tokenAmount,
uint256 gasPayment
) internal returns (bytes32, uint256) {
(bytes32 messageId, uint256 nonce) = IGlacisTokenMediator(GLACIS_TOKEN_ROUTER).route{
value: gasPayment
}(chainId, to, payload, adapters, fees, refundAddress, token, tokenAmount);
emit GlacisTokenClient__MessageRouted(messageId, chainId, to);
return (messageId,nonce);
}
/// @notice Routes the payload to the specific address on destination chain through GlacisRouter using GMPs
/// specified in gmps array
/// @param chainId Destination chain (Glacis chain ID)
/// @param to Destination address on remote chain
/// @param payload The bytes payload to send across chains
/// @param adapters An array of custom adapters to be used for the routing
/// @param fees An array of values to send to the gmps & custom adapters. Should sum to the gasPayment
/// @param refundAddress Address to refund excess gas payment
/// @param messageId The message ID of the message to retry
/// @param nonce The nonce emitted by the original sent message
/// @param token A token address inheriting GlacisToken or GlacisTokenProxy standard (xERC-20)
/// @param tokenAmount Amount of token to send to remote contract
/// @param gasPayment Amount of gas to cover source and destination gas fees (excess will be refunded)
function _retryRouteWithTokens(
uint256 chainId,
bytes32 to,
bytes memory payload,
address[] memory adapters,
CrossChainGas[] memory fees,
address refundAddress,
bytes32 messageId,
uint256 nonce,
address token,
uint256 tokenAmount,
uint256 gasPayment
) internal returns (bytes32) {
IGlacisTokenMediator(GLACIS_TOKEN_ROUTER).routeRetry{value: gasPayment}(
chainId,
to,
payload,
adapters,
fees,
refundAddress,
messageId,
nonce,
token,
tokenAmount
);
emit GlacisTokenClient__MessageRouted(messageId, chainId, to);
return messageId;
}
/// @notice Receives message from GMP(s) through GlacisTokenMediator
/// @param fromAdapters Addresses that sent this message (that reached quorum requirements)
/// @param fromChainId Source chain (Glacis chain ID)
/// @param fromAddress Source address on source chain
/// @param payload Routed payload
/// @param token The address of the token being sent across chains
/// @param tokenAmount The amount of the token being sent across chains
function receiveMessageWithTokens(
address[] memory fromAdapters,
uint256 fromChainId,
bytes32 fromAddress,
bytes memory payload,
address token,
uint256 tokenAmount
) external {
if (msg.sender != GLACIS_TOKEN_ROUTER)
revert GlacisTokenClient__CanOnlyBeCalledByTokenRouter();
_receiveMessageWithTokens(
fromAdapters,
fromChainId,
fromAddress,
payload,
token,
tokenAmount
);
}
/// @notice Receives message from GMP(s) through GlacisTokenMediator
/// @param fromAdapters Adapter addresses
/// @param fromChainId Source chain (Glacis chain ID)
/// @param fromAddress Source address on source chain
/// @param payload Routed payload
/// @param token The address of the token being sent across chains
/// @param tokenAmount The amount of the token being sent across chains
function _receiveMessageWithTokens(
address[] memory fromAdapters,
uint256 fromChainId,
bytes32 fromAddress,
bytes memory payload,
address token,
uint256 tokenAmount
) internal virtual {}
/// @notice The quorum of messages that the contract expects with a specific message from the
/// token router
/// @param glacisData The glacis config data that comes with the message
function getQuorum(
GlacisCommons.GlacisData memory glacisData,
bytes memory payload,
uint256 uniqueMessagesReceived,
address, // token
uint256 // tokenAmount
) external view virtual override returns (uint256) {
return getQuorum(glacisData, payload, uniqueMessagesReceived);
}
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
import {GlacisTokenClient} from "./GlacisTokenClient.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {GlacisCommons} from "../commons/GlacisCommons.sol";
/// @title Glacis Ownable Token Client
/// @dev This contract encapsulates Glacis Token Passing client logic, contracts inheriting this will have access to all
/// Glacis Token Passing and Message Passing features
/// @notice This contract is Ownable
abstract contract GlacisTokenClientOwnable is GlacisTokenClient, Ownable {
constructor(
address glacisTokenMediator_,
address glacisRouter_,
uint256 quorum,
address owner_
) GlacisTokenClient(glacisTokenMediator_, glacisRouter_, quorum) {
_transferOwnership(owner_);
}
/// @notice Add an allowed route for this client
/// @param allowedRoute Route to be added
function addAllowedRoute(
GlacisCommons.GlacisRoute memory allowedRoute
) external onlyOwner {
_addAllowedRoute(allowedRoute);
}
/// @notice Removes an allowed route for this client
/// @param route Allowed route to be removed
function removeAllowedRoute(
GlacisCommons.GlacisRoute calldata route
) external onlyOwner {
_removeAllowedRoute(route);
}
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
import {IGlacisTokenClient} from "../interfaces/IGlacisTokenClient.sol";
import {GlacisRouter} from "../routers/GlacisRouter.sol";
import {IGlacisTokenMediator} from "../interfaces/IGlacisTokenMediator.sol";
import {IGlacisClient} from "../interfaces/IGlacisClient.sol";
import {IXERC20, IXERC20GlacisExtension} from "../interfaces/IXERC20.sol";
import {GlacisCommons} from "../commons/GlacisCommons.sol";
import {GlacisRemoteCounterpartManager} from "../managers/GlacisRemoteCounterpartManager.sol";
import {GlacisClient__CanOnlyBeCalledByRouter} from "../client/GlacisClient.sol";
import {AddressBytes32} from "../libraries/AddressBytes32.sol";
error GlacisTokenMediator__OnlyTokenMediatorAllowed();
error GlacisTokenMediator__IncorrectTokenVariant(bytes32, uint256);
error GlacisTokenMediator__DestinationChainUnavailable();
/// @title Glacis Token Mediator
/// @notice A middleware contract that formats Glacis messages to include XERC20 support
contract GlacisTokenMediator is
IGlacisTokenMediator,
GlacisRemoteCounterpartManager,
IGlacisClient,
GlacisCommons
{
using AddressBytes32 for address;
using AddressBytes32 for bytes32;
/// @param _glacisRouter This chain's deployment of the GlacisRouter
/// @param _quorum The default quorum that you would like. If you implement dynamic quorum, this value can be ignored and
/// set to 0
/// @param _owner The owner of this contract
constructor(
address _glacisRouter,
uint256 _quorum,
address _owner
) IGlacisClient(_quorum) {
// Approve conversation between token routers in all chains through all GMPs
GLACIS_ROUTER = _glacisRouter;
_transferOwnership(_owner);
}
address public immutable GLACIS_ROUTER;
/// @notice Routes the payload to the specific address on destination chain through GlacisRouter using GMPs
/// specified in gmps array
/// @param chainId Destination chain (Glacis chain ID)
/// @param to Destination address on remote chain
/// @param payload Payload to be routed
/// @param adapters An array of custom adapters to be used for the routing
/// @param fees Payment for each GMP & custom adapter to cover source and destination gas fees (excess will be refunded)
/// @param refundAddress Address to refund excess gas payment
/// @param token Token (implementing XERC20 standard) to be sent to remote contract
/// @param tokenAmount Amount of token to send to remote contract
/// @return A tuple with a bytes32 messageId and a uint256 nonce
function route(
uint256 chainId,
bytes32 to,
bytes memory payload,
address[] memory adapters,
GlacisCommons.CrossChainGas[] memory fees,
address refundAddress,
address token,
uint256 tokenAmount
) public payable virtual returns (bytes32, uint256) {
bytes32 destinationTokenMediator = remoteCounterpart[chainId];
if (destinationTokenMediator == bytes32(0))
revert GlacisTokenMediator__DestinationChainUnavailable();
IXERC20(token).burn(msg.sender, tokenAmount);
bytes memory tokenPayload = packTokenPayload(
chainId,
to,
token,
tokenAmount,
payload
);
emit GlacisTokenMediator__TokensBurnt(msg.sender, token, tokenAmount);
return
GlacisRouter(GLACIS_ROUTER).route{value: msg.value}(
chainId,
destinationTokenMediator,
tokenPayload,
adapters,
fees,
refundAddress,
true // Token Mediator always enables retry
);
}
/// @notice Retries routing the payload to the specific address on destination chain using specified GMPs
/// @param chainId Destination chain (Glacis chain ID)
/// @param to Destination address on remote chain
/// @param payload Payload to be routed
/// @param adapters An array of adapters to be used for the routing
/// @param fees Payment for each GMP to cover source and destination gas fees (excess will be refunded)
/// @param refundAddress Address to refund excess gas payment
/// @param messageId The message ID of the message to retry
/// @param nonce The nonce emitted by the original message routing
/// @param token Token (implementing XERC20 standard) to be sent to remote contract
/// @param tokenAmount Amount of token to send to remote contract
/// @return A tuple with a bytes32 messageId and a uint256 nonce
function routeRetry(
uint256 chainId,
bytes32 to,
bytes memory payload,
address[] memory adapters,
GlacisCommons.CrossChainGas[] memory fees,
address refundAddress,
bytes32 messageId,
uint256 nonce,
address token,
uint256 tokenAmount
) public payable virtual returns (bytes32, uint256) {
// Pack with a function (otherwise stack too deep)
bytes memory tokenPayload = packTokenPayload(
chainId,
to,
token,
tokenAmount,
payload
);
// Use helper function (otherwise stack too deep)
return
_routeRetry(
chainId,
tokenPayload,
adapters,
fees,
refundAddress,
messageId,
nonce
);
}
/// @notice An internal routing function that helps with stack too deep
/// @param chainId Destination chain (Glacis chain ID)
/// @param tokenPayload Formatted payload to be routed
/// @param adapters An array of custom adapters to be used for the routing
/// @param fees Payment for each GMP to cover source and destination gas fees (excess will be refunded)
/// @param refundAddress Address to refund excess gas payment
/// @param messageId The message ID of the message to retry
/// @param nonce The nonce emitted by the original message routing
/// @return A bytes32 messageId
function _routeRetry(
uint256 chainId,
bytes memory tokenPayload,
address[] memory adapters,
GlacisCommons.CrossChainGas[] memory fees,
address refundAddress,
bytes32 messageId,
uint256 nonce
) private returns (bytes32, uint256) {
bytes32 destinationTokenMediator = remoteCounterpart[chainId];
if (destinationTokenMediator == bytes32(0))
revert GlacisTokenMediator__DestinationChainUnavailable();
return
GlacisRouter(GLACIS_ROUTER).routeRetry{value: msg.value}(
chainId,
destinationTokenMediator,
tokenPayload,
adapters,
fees,
refundAddress,
messageId,
nonce
);
}
/// @notice Receives a cross chain message from an IGlacisAdapter.
/// @param fromAdapters Used adapter addresses for routing
/// @param fromChainId Source chain (Glacis chain ID)
/// @param fromChainId Source address
/// @param payload Received payload from Glacis Router
function receiveMessage(
address[] memory fromAdapters,
uint256 fromChainId,
bytes32 fromAddress,
bytes memory payload
) public override {
// Ensure that the executor is the glacis router and that the source is from an accepted mediator.
if (msg.sender != GLACIS_ROUTER)
revert GlacisClient__CanOnlyBeCalledByRouter();
if (fromAddress != remoteCounterpart[fromChainId]) {
revert GlacisTokenMediator__OnlyTokenMediatorAllowed();
}
(
bytes32 to,
bytes32 originalFrom,
bytes32 sourceToken,
bytes32 token,
uint256 tokenAmount,
bytes memory originalPayload
) = abi.decode(
payload,
(bytes32, bytes32, bytes32, bytes32, uint256, bytes)
);
// Ensure that the destination token accepts the source token.
if (
sourceToken != token &&
sourceToken != getTokenVariant(token.toAddress(), fromChainId)
) {
revert GlacisTokenMediator__IncorrectTokenVariant(
sourceToken,
fromChainId
);
}
// Mint & execute
address toAddress = to.toAddress();
IXERC20(token.toAddress()).mint(toAddress, tokenAmount);
emit GlacisTokenMediator__TokensMinted(
toAddress,
token.toAddress(),
tokenAmount
);
IGlacisTokenClient client = IGlacisTokenClient(toAddress);
if (toAddress.code.length > 0) {
client.receiveMessageWithTokens(
fromAdapters,
fromChainId,
originalFrom,
originalPayload,
token.toAddress(),
tokenAmount
);
}
}
/// @notice The quorum of messages that the contract expects with a specific message from the
/// token router
/// @param glacisData The glacis config data that comes with the message
/// @param payload The payload that comes with the message
function getQuorum(
GlacisData memory glacisData,
bytes memory payload,
uint256 uniqueMessagesReceived
) public view override returns (uint256) {
(
bytes32 to,
bytes32 originalFrom,
,
bytes32 token,
uint256 tokenAmount,
bytes memory originalPayload
) = decodeTokenPayload(payload);
glacisData.originalFrom = originalFrom;
glacisData.originalTo = to;
// If the destination smart contract is an EOA, then we assume "1".
address toAddress = to.toAddress();
if (toAddress.code.length == 0) {
return 1;
}
return
IGlacisTokenClient(toAddress).getQuorum(
glacisData,
originalPayload,
uniqueMessagesReceived,
token.toAddress(),
tokenAmount
);
}
/// @notice Queries if a route from path GMP+Chain+Address is allowed for this client
/// @param route_ Origin route for the message
/// @param payload message payload
/// @return True if route is allowed, false otherwise
function isAllowedRoute(
GlacisRoute memory route_,
bytes memory payload
) external view returns (bool) {
// First checks to ensure that the GlacisTokenMediator is speaking to a registered remote version
if (route_.fromAddress != remoteCounterpart[route_.fromChainId])
return false;
(
bytes32 to,
bytes32 originalFrom,
,
,
,
bytes memory originalPayload
) = decodeTokenPayload(payload);
// If the destination smart contract is an EOA, then we can only allow adapters that are using
// our canonical GMPs. Otherwise, we would allow malicious custom adapters.
address toAddress = to.toAddress();
if (toAddress.code.length == 0)
return
GlacisRouter(GLACIS_ROUTER).adapterToGlacisGMPId(
route_.fromAdapter
) >
0 ||
(uint160(route_.fromAdapter) <= GLACIS_RESERVED_IDS &&
GlacisRouter(GLACIS_ROUTER).glacisGMPIdToAdapter(
uint8(uint160(route_.fromAdapter))
) !=
address(0));
// Forwards check to the token client
return
IGlacisTokenClient(toAddress).isAllowedRoute(
GlacisRoute(
route_.fromChainId,
originalFrom,
route_.fromAdapter
),
originalPayload
);
}
/// @notice Determines if a token from a chain ID is a token variant for this chain's token
/// @param token The address of the token in question
/// @param chainId The chain ID that the token in question is deployed
function getTokenVariant(
address token,
uint256 chainId
) internal view returns (bytes32 destinationToken) {
try IXERC20GlacisExtension(token).getTokenVariant(chainId) returns (
bytes32 variant
) {
if (variant == bytes32(0)) destinationToken = token.toBytes32();
else destinationToken = variant;
} catch {
destinationToken = token.toBytes32();
}
}
/// @notice Packs a token payload (helps with stack too deep)
/// @param chainId Destination chain (Glacis chain ID)
/// @param to Destination address on remote chain
/// @param token Token (implementing XERC20 standard) to be sent to remote contract
/// @param tokenAmount Amount of token to send to remote contract
/// @param payload Payload to be routed
function packTokenPayload(
uint256 chainId,
bytes32 to,
address token,
uint256 tokenAmount,
bytes memory payload
) internal view returns (bytes memory) {
return
abi.encode(
to,
msg.sender.toBytes32(),
token.toBytes32(),
getTokenVariant(token, chainId),
tokenAmount,
payload
);
}
/// @notice Decodes a received token payload
/// @param payload The payload
function decodeTokenPayload(
bytes memory payload
)
internal
pure
returns (
bytes32 to,
bytes32 originalFrom,
bytes32 sourceToken,
bytes32 token,
uint256 tokenAmount,
bytes memory originalPayload
)
{
(
to,
originalFrom,
sourceToken,
token,
tokenAmount,
originalPayload
) = abi.decode(
payload,
(bytes32, bytes32, bytes32, bytes32, uint256, bytes)
);
}
}
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.18;
import {IWormholeRelayer} from "./IWormholeRelayer.sol";
import {IWormholeReceiver} from "./IWormholeReceiver.sol";
import {GlacisAbstractAdapter} from "../GlacisAbstractAdapter.sol";
import {IGlacisRouter} from "../../routers/GlacisRouter.sol";
import {GlacisAbstractAdapter__IDArraysMustBeSameLength, GlacisAbstractAdapter__DestinationChainIdNotValid, GlacisAbstractAdapter__SourceChainNotRegistered, GlacisAbstractAdapter__ChainIsNotAvailable, GlacisAbstractAdapter__NoRemoteAdapterForChainId} from "../GlacisAbstractAdapter.sol";
import {AddressBytes32} from "../../libraries/AddressBytes32.sol";
import {GlacisCommons} from "../../commons/GlacisCommons.sol";
error GlacisWormholeAdapter__OnlyRelayerAllowed();
error GlacisWormholeAdapter__AlreadyProcessedVAA();
error GlacisWormholeAdapter__NotEnoughValueForCrossChainTransaction();
error GlacisWormholeAdapter__RefundAddressMustReceiveNativeCurrency();
/// @title Glacis Adapter for Wormhole
/// @notice A Glacis Adapter for the Wormhole network. Sends messages through the Wormhole Relayer's
/// sendPayloadToEvm() and receives messages via receiveWormholeMessages()
contract GlacisWormholeAdapter is IWormholeReceiver, GlacisAbstractAdapter {
using AddressBytes32 for bytes32;
IWormholeRelayer public immutable WORMHOLE_RELAYER;
mapping(bytes32 => bool) public seenDeliveryVaaHashes;
mapping(uint256 => uint16) public glacisChainIdToAdapterChainId;
mapping(uint16 => uint256) public adapterChainIdToGlacisChainId;
uint256 internal constant GAS_LIMIT = 900000;
uint16 internal immutable WORMHOLE_CHAIN_ID;
uint256 internal constant RECEIVER_VALUE = 0;
event GlacisWormholeAdapter__SetGlacisChainIDs(uint256[] chainIDs, uint16[] whIDs);
constructor(
IGlacisRouter _glacisRouter,
address _wormholeRelayer,
uint16 wormholeChainId,
address owner_
) GlacisAbstractAdapter(_glacisRouter, owner_) {
WORMHOLE_RELAYER = IWormholeRelayer(_wormholeRelayer);
WORMHOLE_CHAIN_ID = wormholeChainId;
}
/// @notice Dispatch payload to specified Glacis chain ID and address through Wormhole GMP
/// @param toChainId Destination chain (Glacis ID)
/// @param refundAddress The address to refund native asset surplus
/// @param payload Payload to send
function _sendMessage(
uint256 toChainId,
address refundAddress,
GlacisCommons.CrossChainGas memory incentives,
bytes memory payload
) internal override {
uint16 _dstchainId = glacisChainIdToAdapterChainId[toChainId];
bytes32 counterpart = remoteCounterpart[toChainId];
if (_dstchainId == 0)
revert GlacisAbstractAdapter__ChainIsNotAvailable(toChainId);
if (counterpart == bytes32(0))
revert GlacisAbstractAdapter__NoRemoteAdapterForChainId(toChainId);
uint256 selectedGasLimit = incentives.gasLimit > 0
? incentives.gasLimit
: GAS_LIMIT;
(uint256 nativePriceQuote, ) = WORMHOLE_RELAYER.quoteEVMDeliveryPrice(
_dstchainId,
RECEIVER_VALUE,
selectedGasLimit
);
if (nativePriceQuote > msg.value)
revert GlacisWormholeAdapter__NotEnoughValueForCrossChainTransaction();
// Will use the given gas limit, otherwise it will automatically set the
// gas limit to 900k (not recommended)
WORMHOLE_RELAYER.sendPayloadToEvm{value: nativePriceQuote}(
_dstchainId,
counterpart.toAddress(),
payload,
RECEIVER_VALUE,
selectedGasLimit,
WORMHOLE_CHAIN_ID,
refundAddress
);
if (msg.value > nativePriceQuote) {
(bool successful, ) = address(refundAddress).call{
value: msg.value - nativePriceQuote
}("");
if (!successful)
revert GlacisWormholeAdapter__RefundAddressMustReceiveNativeCurrency();
}
}
/// @notice Receives route message from Wormhole and routes it to GlacisRouter
/// @param payload Payload to route
/// @param sourceAddress Source address on remote chain
/// @param sourceChain Source chain (Wormhole ID)
/// @param deliveryHash Wormhole delivery hash
function receiveWormholeMessages(
bytes memory payload,
bytes[] memory, // Not using additional VAAs
bytes32 sourceAddress,
uint16 sourceChain,
bytes32 deliveryHash
)
external
payable
onlyAuthorizedAdapter(
adapterChainIdToGlacisChainId[sourceChain],
sourceAddress
)
{
if (msg.sender != address(WORMHOLE_RELAYER))
revert GlacisWormholeAdapter__OnlyRelayerAllowed();
if (seenDeliveryVaaHashes[deliveryHash])
revert GlacisWormholeAdapter__AlreadyProcessedVAA();
uint256 sourceChainGlacisId = adapterChainIdToGlacisChainId[
sourceChain
];
if (sourceChainGlacisId == 0)
revert GlacisAbstractAdapter__SourceChainNotRegistered();
// Ensure no duplicate deliveries
seenDeliveryVaaHashes[deliveryHash] = true;
// Forward to the router
GLACIS_ROUTER.receiveMessage(sourceChainGlacisId, payload);
}
/// @notice Sets the corresponding Wormhole chain label for the specified Glacis chain ID
/// @param chainIDs Glacis chain IDs
/// @param whIDs Wormhole corresponding chain IDs
function setGlacisChainIds(
uint256[] memory chainIDs,
uint16[] memory whIDs
) external onlyOwner {
uint256 len = chainIDs.length;
if (len != whIDs.length)
revert GlacisAbstractAdapter__IDArraysMustBeSameLength();
for (uint256 i; i < len; ) {
uint256 gID = chainIDs[i];
uint16 whID = whIDs[i];
if (gID == 0)
revert GlacisAbstractAdapter__DestinationChainIdNotValid();
glacisChainIdToAdapterChainId[gID] = whID;
adapterChainIdToGlacisChainId[whID] = gID;
unchecked {
++i;
}
}
emit GlacisWormholeAdapter__SetGlacisChainIDs(chainIDs, whIDs);
}
/// @notice Gets the corresponding Wormhole chain ID for the specified Glacis chain ID
/// @param chainId Glacis chain ID
/// @return The corresponding Wormhole chain ID
function adapterChainID(uint256 chainId) external view returns (uint16) {
return glacisChainIdToAdapterChainId[chainId];
}
/// @notice Queries if the specified Glacis chain ID is supported by this adapter
/// @param chainId Glacis chain ID
/// @return True if chain is supported, false otherwise
function chainIsAvailable(uint256 chainId) public view returns (bool) {
return glacisChainIdToAdapterChainId[chainId] != 0;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {Client} from "../libraries/Client.sol";
/// @notice Application contracts that intend to receive messages from
/// the router should implement this interface.
interface IAny2EVMMessageReceiver {
/// @notice Called by the Router to deliver a message.
/// If this reverts, any token transfers also revert. The message
/// will move to a FAILED state and become available for manual execution.
/// @param message CCIP Message
/// @dev Note ensure you check the msg.sender is the OffRampRouter
function ccipReceive(Client.Any2EVMMessage calldata message) external;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import { IAxelarGateway } from './IAxelarGateway.sol';
interface IAxelarExecutable {
error InvalidAddress();
error NotApprovedByGateway();
function gateway() external view returns (IAxelarGateway);
function execute(
bytes32 commandId,
string calldata sourceChain,
string calldata sourceAddress,
bytes calldata payload
) external;
function executeWithToken(
bytes32 commandId,
string calldata sourceChain,
string calldata sourceAddress,
bytes calldata payload,
string calldata tokenSymbol,
uint256 amount
) external;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import { GasInfo } from '../types/GasEstimationTypes.sol';
import { IInterchainGasEstimation } from './IInterchainGasEstimation.sol';
import { IUpgradable } from './IUpgradable.sol';
/**
* @title IAxelarGasService Interface
* @notice This is an interface for the AxelarGasService contract which manages gas payments
* and refunds for cross-chain communication on the Axelar network.
* @dev This interface inherits IUpgradable
*/
interface IAxelarGasService is IInterchainGasEstimation, IUpgradable {
error InvalidAddress();
error NotCollector();
error InvalidAmounts();
error InvalidGasUpdates();
error InvalidParams();
error InsufficientGasPayment(uint256 required, uint256 provided);
event GasPaidForContractCall(
address indexed sourceAddress,
string destinationChain,
string destinationAddress,
bytes32 indexed payloadHash,
address gasToken,
uint256 gasFeeAmount,
address refundAddress
);
event GasPaidForContractCallWithToken(
address indexed sourceAddress,
string destinationChain,
string destinationAddress,
bytes32 indexed payloadHash,
string symbol,
uint256 amount,
address gasToken,
uint256 gasFeeAmount,
address refundAddress
);
event NativeGasPaidForContractCall(
address indexed sourceAddress,
string destinationChain,
string destinationAddress,
bytes32 indexed payloadHash,
uint256 gasFeeAmount,
address refundAddress
);
event NativeGasPaidForContractCallWithToken(
address indexed sourceAddress,
string destinationChain,
string destinationAddress,
bytes32 indexed payloadHash,
string symbol,
uint256 amount,
uint256 gasFeeAmount,
address refundAddress
);
event GasPaidForExpressCall(
address indexed sourceAddress,
string destinationChain,
string destinationAddress,
bytes32 indexed payloadHash,
address gasToken,
uint256 gasFeeAmount,
address refundAddress
);
event GasPaidForExpressCallWithToken(
address indexed sourceAddress,
string destinationChain,
string destinationAddress,
bytes32 indexed payloadHash,
string symbol,
uint256 amount,
address gasToken,
uint256 gasFeeAmount,
address refundAddress
);
event NativeGasPaidForExpressCall(
address indexed sourceAddress,
string destinationChain,
string destinationAddress,
bytes32 indexed payloadHash,
uint256 gasFeeAmount,
address refundAddress
);
event NativeGasPaidForExpressCallWithToken(
address indexed sourceAddress,
string destinationChain,
string destinationAddress,
bytes32 indexed payloadHash,
string symbol,
uint256 amount,
uint256 gasFeeAmount,
address refundAddress
);
event GasAdded(
bytes32 indexed txHash,
uint256 indexed logIndex,
address gasToken,
uint256 gasFeeAmount,
address refundAddress
);
event NativeGasAdded(bytes32 indexed txHash, uint256 indexed logIndex, uint256 gasFeeAmount, address refundAddress);
event ExpressGasAdded(
bytes32 indexed txHash,
uint256 indexed logIndex,
address gasToken,
uint256 gasFeeAmount,
address refundAddress
);
event NativeExpressGasAdded(
bytes32 indexed txHash,
uint256 indexed logIndex,
uint256 gasFeeAmount,
address refundAddress
);
event Refunded(
bytes32 indexed txHash,
uint256 indexed logIndex,
address payable receiver,
address token,
uint256 amount
);
/**
* @notice Pay for gas for any type of contract execution on a destination chain.
* @dev This function is called on the source chain before calling the gateway to execute a remote contract.
* @dev If estimateOnChain is true, the function will estimate the gas cost and revert if the payment is insufficient.
* @param sender The address making the payment
* @param destinationChain The target chain where the contract call will be made
* @param destinationAddress The target address on the destination chain
* @param payload Data payload for the contract call
* @param executionGasLimit The gas limit for the contract call
* @param estimateOnChain Flag to enable on-chain gas estimation
* @param refundAddress The address where refunds, if any, should be sent
* @param params Additional parameters for gas payment. This can be left empty for normal contract call payments.
*/
function payGas(
address sender,
string calldata destinationChain,
string calldata destinationAddress,
bytes calldata payload,
uint256 executionGasLimit,
bool estimateOnChain,
address refundAddress,
bytes calldata params
) external payable;
/**
* @notice Pay for gas using ERC20 tokens for a contract call on a destination chain.
* @dev This function is called on the source chain before calling the gateway to execute a remote contract.
* @param sender The address making the payment
* @param destinationChain The target chain where the contract call will be made
* @param destinationAddress The target address on the destination chain
* @param payload Data payload for the contract call
* @param gasToken The address of the ERC20 token used to pay for gas
* @param gasFeeAmount The amount of tokens to pay for gas
* @param refundAddress The address where refunds, if any, should be sent
*/
function payGasForContractCall(
address sender,
string calldata destinationChain,
string calldata destinationAddress,
bytes calldata payload,
address gasToken,
uint256 gasFeeAmount,
address refundAddress
) external;
/**
* @notice Pay for gas using ERC20 tokens for a contract call with tokens on a destination chain.
* @dev This function is called on the source chain before calling the gateway to execute a remote contract.
* @param sender The address making the payment
* @param destinationChain The target chain where the contract call with tokens will be made
* @param destinationAddress The target address on the destination chain
* @param payload Data payload for the contract call with tokens
* @param symbol The symbol of the token to be sent with the call
* @param amount The amount of tokens to be sent with the call
* @param gasToken The address of the ERC20 token used to pay for gas
* @param gasFeeAmount The amount of tokens to pay for gas
* @param refundAddress The address where refunds, if any, should be sent
*/
function payGasForContractCallWithToken(
address sender,
string calldata destinationChain,
string calldata destinationAddress,
bytes calldata payload,
string calldata symbol,
uint256 amount,
address gasToken,
uint256 gasFeeAmount,
address refundAddress
) external;
/**
* @notice Pay for gas using native currency for a contract call on a destination chain.
* @dev This function is called on the source chain before calling the gateway to execute a remote contract.
* @param sender The address making the payment
* @param destinationChain The target chain where the contract call will be made
* @param destinationAddress The target address on the destination chain
* @param payload Data payload for the contract call
* @param refundAddress The address where refunds, if any, should be sent
*/
function payNativeGasForContractCall(
address sender,
string calldata destinationChain,
string calldata destinationAddress,
bytes calldata payload,
address refundAddress
) external payable;
/**
* @notice Pay for gas using native currency for a contract call with tokens on a destination chain.
* @dev This function is called on the source chain before calling the gateway to execute a remote contract.
* @param sender The address making the payment
* @param destinationChain The target chain where the contract call with tokens will be made
* @param destinationAddress The target address on the destination chain
* @param payload Data payload for the contract call with tokens
* @param symbol The symbol of the token to be sent with the call
* @param amount The amount of tokens to be sent with the call
* @param refundAddress The address where refunds, if any, should be sent
*/
function payNativeGasForContractCallWithToken(
address sender,
string calldata destinationChain,
string calldata destinationAddress,
bytes calldata payload,
string calldata symbol,
uint256 amount,
address refundAddress
) external payable;
/**
* @notice Pay for gas using ERC20 tokens for an express contract call on a destination chain.
* @dev This function is called on the source chain before calling the gateway to express execute a remote contract.
* @param sender The address making the payment
* @param destinationChain The target chain where the contract call will be made
* @param destinationAddress The target address on the destination chain
* @param payload Data payload for the contract call
* @param gasToken The address of the ERC20 token used to pay for gas
* @param gasFeeAmount The amount of tokens to pay for gas
* @param refundAddress The address where refunds, if any, should be sent
*/
function payGasForExpressCall(
address sender,
string calldata destinationChain,
string calldata destinationAddress,
bytes calldata payload,
address gasToken,
uint256 gasFeeAmount,
address refundAddress
) external;
/**
* @notice Pay for gas using ERC20 tokens for an express contract call with tokens on a destination chain.
* @dev This function is called on the source chain before calling the gateway to express execute a remote contract.
* @param sender The address making the payment
* @param destinationChain The target chain where the contract call with tokens will be made
* @param destinationAddress The target address on the destination chain
* @param payload Data payload for the contract call with tokens
* @param symbol The symbol of the token to be sent with the call
* @param amount The amount of tokens to be sent with the call
* @param gasToken The address of the ERC20 token used to pay for gas
* @param gasFeeAmount The amount of tokens to pay for gas
* @param refundAddress The address where refunds, if any, should be sent
*/
function payGasForExpressCallWithToken(
address sender,
string calldata destinationChain,
string calldata destinationAddress,
bytes calldata payload,
string calldata symbol,
uint256 amount,
address gasToken,
uint256 gasFeeAmount,
address refundAddress
) external;
/**
* @notice Pay for gas using native currency for an express contract call on a destination chain.
* @dev This function is called on the source chain before calling the gateway to execute a remote contract.
* @param sender The address making the payment
* @param destinationChain The target chain where the contract call will be made
* @param destinationAddress The target address on the destination chain
* @param payload Data payload for the contract call
* @param refundAddress The address where refunds, if any, should be sent
*/
function payNativeGasForExpressCall(
address sender,
string calldata destinationChain,
string calldata destinationAddress,
bytes calldata payload,
address refundAddress
) external payable;
/**
* @notice Pay for gas using native currency for an express contract call with tokens on a destination chain.
* @dev This function is called on the source chain before calling the gateway to execute a remote contract.
* @param sender The address making the payment
* @param destinationChain The target chain where the contract call with tokens will be made
* @param destinationAddress The target address on the destination chain
* @param payload Data payload for the contract call with tokens
* @param symbol The symbol of the token to be sent with the call
* @param amount The amount of tokens to be sent with the call
* @param refundAddress The address where refunds, if any, should be sent
*/
function payNativeGasForExpressCallWithToken(
address sender,
string calldata destinationChain,
string calldata destinationAddress,
bytes calldata payload,
string calldata symbol,
uint256 amount,
address refundAddress
) external payable;
/**
* @notice Add additional gas payment using ERC20 tokens after initiating a cross-chain call.
* @dev This function can be called on the source chain after calling the gateway to execute a remote contract.
* @param txHash The transaction hash of the cross-chain call
* @param logIndex The log index for the cross-chain call
* @param gasToken The ERC20 token address used to add gas
* @param gasFeeAmount The amount of tokens to add as gas
* @param refundAddress The address where refunds, if any, should be sent
*/
function addGas(
bytes32 txHash,
uint256 logIndex,
address gasToken,
uint256 gasFeeAmount,
address refundAddress
) external;
/**
* @notice Add additional gas payment using native currency after initiating a cross-chain call.
* @dev This function can be called on the source chain after calling the gateway to execute a remote contract.
* @param txHash The transaction hash of the cross-chain call
* @param logIndex The log index for the cross-chain call
* @param refundAddress The address where refunds, if any, should be sent
*/
function addNativeGas(
bytes32 txHash,
uint256 logIndex,
address refundAddress
) external payable;
/**
* @notice Add additional gas payment using ERC20 tokens after initiating an express cross-chain call.
* @dev This function can be called on the source chain after calling the gateway to express execute a remote contract.
* @param txHash The transaction hash of the cross-chain call
* @param logIndex The log index for the cross-chain call
* @param gasToken The ERC20 token address used to add gas
* @param gasFeeAmount The amount of tokens to add as gas
* @param refundAddress The address where refunds, if any, should be sent
*/
function addExpressGas(
bytes32 txHash,
uint256 logIndex,
address gasToken,
uint256 gasFeeAmount,
address refundAddress
) external;
/**
* @notice Add additional gas payment using native currency after initiating an express cross-chain call.
* @dev This function can be called on the source chain after calling the gateway to express execute a remote contract.
* @param txHash The transaction hash of the cross-chain call
* @param logIndex The log index for the cross-chain call
* @param refundAddress The address where refunds, if any, should be sent
*/
function addNativeExpressGas(
bytes32 txHash,
uint256 logIndex,
address refundAddress
) external payable;
/**
* @notice Updates the gas price for a specific chain.
* @dev This function is called by the gas oracle to update the gas prices for a specific chains.
* @param chains Array of chain names
* @param gasUpdates Array of gas updates
*/
function updateGasInfo(string[] calldata chains, GasInfo[] calldata gasUpdates) external;
/**
* @notice Allows the gasCollector to collect accumulated fees from the contract.
* @dev Use address(0) as the token address for native currency.
* @param receiver The address to receive the collected fees
* @param tokens Array of token addresses to be collected
* @param amounts Array of amounts to be collected for each respective token address
*/
function collectFees(
address payable receiver,
address[] calldata tokens,
uint256[] calldata amounts
) external;
/**
* @notice Refunds gas payment to the receiver in relation to a specific cross-chain transaction.
* @dev Only callable by the gasCollector.
* @dev Use address(0) as the token address to refund native currency.
* @param txHash The transaction hash of the cross-chain call
* @param logIndex The log index for the cross-chain call
* @param receiver The address to receive the refund
* @param token The token address to be refunded
* @param amount The amount to refund
*/
function refund(
bytes32 txHash,
uint256 logIndex,
address payable receiver,
address token,
uint256 amount
) external;
/**
* @notice Returns the address of the designated gas collector.
* @return address of the gas collector
*/
function gasCollector() external returns (address);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import { IGovernable } from './IGovernable.sol';
import { IImplementation } from './IImplementation.sol';
interface IAxelarGateway is IImplementation, IGovernable {
/**********\
|* Errors *|
\**********/
error NotSelf();
error InvalidCodeHash();
error SetupFailed();
error InvalidAuthModule();
error InvalidTokenDeployer();
error InvalidAmount();
error InvalidChainId();
error InvalidCommands();
error TokenDoesNotExist(string symbol);
error TokenAlreadyExists(string symbol);
error TokenDeployFailed(string symbol);
error TokenContractDoesNotExist(address token);
error BurnFailed(string symbol);
error MintFailed(string symbol);
error InvalidSetMintLimitsParams();
error ExceedMintLimit(string symbol);
/**********\
|* Events *|
\**********/
event TokenSent(
address indexed sender,
string destinationChain,
string destinationAddress,
string symbol,
uint256 amount
);
event ContractCall(
address indexed sender,
string destinationChain,
string destinationContractAddress,
bytes32 indexed payloadHash,
bytes payload
);
event ContractCallWithToken(
address indexed sender,
string destinationChain,
string destinationContractAddress,
bytes32 indexed payloadHash,
bytes payload,
string symbol,
uint256 amount
);
event Executed(bytes32 indexed commandId);
event TokenDeployed(string symbol, address tokenAddresses);
event ContractCallApproved(
bytes32 indexed commandId,
string sourceChain,
string sourceAddress,
address indexed contractAddress,
bytes32 indexed payloadHash,
bytes32 sourceTxHash,
uint256 sourceEventIndex
);
event ContractCallApprovedWithMint(
bytes32 indexed commandId,
string sourceChain,
string sourceAddress,
address indexed contractAddress,
bytes32 indexed payloadHash,
string symbol,
uint256 amount,
bytes32 sourceTxHash,
uint256 sourceEventIndex
);
event ContractCallExecuted(bytes32 indexed commandId);
event TokenMintLimitUpdated(string symbol, uint256 limit);
event OperatorshipTransferred(bytes newOperatorsData);
event Upgraded(address indexed implementation);
/********************\
|* Public Functions *|
\********************/
function sendToken(
string calldata destinationChain,
string calldata destinationAddress,
string calldata symbol,
uint256 amount
) external;
function callContract(
string calldata destinationChain,
string calldata contractAddress,
bytes calldata payload
) external;
function callContractWithToken(
string calldata destinationChain,
string calldata contractAddress,
bytes calldata payload,
string calldata symbol,
uint256 amount
) external;
function isContractCallApproved(
bytes32 commandId,
string calldata sourceChain,
string calldata sourceAddress,
address contractAddress,
bytes32 payloadHash
) external view returns (bool);
function isContractCallAndMintApproved(
bytes32 commandId,
string calldata sourceChain,
string calldata sourceAddress,
address contractAddress,
bytes32 payloadHash,
string calldata symbol,
uint256 amount
) external view returns (bool);
function validateContractCall(
bytes32 commandId,
string calldata sourceChain,
string calldata sourceAddress,
bytes32 payloadHash
) external returns (bool);
function validateContractCallAndMint(
bytes32 commandId,
string calldata sourceChain,
string calldata sourceAddress,
bytes32 payloadHash,
string calldata symbol,
uint256 amount
) external returns (bool);
/***********\
|* Getters *|
\***********/
function authModule() external view returns (address);
function tokenDeployer() external view returns (address);
function tokenMintLimit(string memory symbol) external view returns (uint256);
function tokenMintAmount(string memory symbol) external view returns (uint256);
function allTokensFrozen() external view returns (bool);
function implementation() external view returns (address);
function tokenAddresses(string memory symbol) external view returns (address);
function tokenFrozen(string memory symbol) external view returns (bool);
function isCommandExecuted(bytes32 commandId) external view returns (bool);
/************************\
|* Governance Functions *|
\************************/
function setTokenMintLimits(string[] calldata symbols, uint256[] calldata limits) external;
function upgrade(
address newImplementation,
bytes32 newImplementationCodeHash,
bytes calldata setupParams
) external;
/**********************\
|* External Functions *|
\**********************/
function execute(bytes calldata input) external;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// General interface for upgradable contracts
interface IContractIdentifier {
/**
* @notice Returns the contract ID. It can be used as a check during upgrades.
* @dev Meant to be overridden in derived contracts.
* @return bytes32 The contract ID
*/
function contractId() external pure returns (bytes32);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165[EIP].
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others ({ERC165Checker}).
*
* For an implementation, see {ERC165}.
*/
interface IERC165 {
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address to, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `from` to `to` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 amount) external returns (bool);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Metadata.sol)
pragma solidity ^0.8.0;
import "../IERC20.sol";
/**
* @dev Interface for the optional metadata functions from the ERC20 standard.
*
* _Available since v4.1._
*/
interface IERC20Metadata is IERC20 {
/**
* @dev Returns the name of the token.
*/
function name() external view returns (string memory);
/**
* @dev Returns the symbol of the token.
*/
function symbol() external view returns (string memory);
/**
* @dev Returns the decimals places of the token.
*/
function decimals() external view returns (uint8);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.4) (token/ERC20/extensions/IERC20Permit.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
* https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
*
* Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
* presenting a message signed by the account. By not relying on {IERC20-approve}, the token holder account doesn't
* need to send a transaction, and thus is not required to hold Ether at all.
*
* ==== Security Considerations
*
* There are two important considerations concerning the use of `permit`. The first is that a valid permit signature
* expresses an allowance, and it should not be assumed to convey additional meaning. In particular, it should not be
* considered as an intention to spend the allowance in any specific way. The second is that because permits have
* built-in replay protection and can be submitted by anyone, they can be frontrun. A protocol that uses permits should
* take this into consideration and allow a `permit` call to fail. Combining these two aspects, a pattern that may be
* generally recommended is:
*
* ```solidity
* function doThingWithPermit(..., uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) public {
* try token.permit(msg.sender, address(this), value, deadline, v, r, s) {} catch {}
* doThing(..., value);
* }
*
* function doThing(..., uint256 value) public {
* token.safeTransferFrom(msg.sender, address(this), value);
* ...
* }
* ```
*
* Observe that: 1) `msg.sender` is used as the owner, leaving no ambiguity as to the signer intent, and 2) the use of
* `try/catch` allows the permit to fail and makes the code tolerant to frontrunning. (See also
* {SafeERC20-safeTransferFrom}).
*
* Additionally, note that smart contract wallets (such as Argent or Safe) are not able to produce permit signatures, so
* contracts should have entry points that don't rely on permit.
*/
interface IERC20Permit {
/**
* @dev Sets `value` as the allowance of `spender` over ``owner``'s tokens,
* given ``owner``'s signed approval.
*
* IMPORTANT: The same issues {IERC20-approve} has related to transaction
* ordering also apply here.
*
* Emits an {Approval} event.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `deadline` must be a timestamp in the future.
* - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner`
* over the EIP712-formatted function arguments.
* - the signature must use ``owner``'s current nonce (see {nonces}).
*
* For more information on the signature format, see the
* https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP
* section].
*
* CAUTION: See Security Considerations above.
*/
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
/**
* @dev Returns the current nonce for `owner`. This value must be
* included whenever a signature is generated for {permit}.
*
* Every successful call to {permit} increases ``owner``'s nonce by one. This
* prevents a signature from being used multiple times.
*/
function nonces(address owner) external view returns (uint256);
/**
* @dev Returns the domain separator used in the encoding of the signature for {permit}, as defined by {EIP712}.
*/
// solhint-disable-next-line func-name-mixedcase
function DOMAIN_SEPARATOR() external view returns (bytes32);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (interfaces/IERC5267.sol)
pragma solidity ^0.8.0;
interface IERC5267 {
/**
* @dev MAY be emitted to signal that the domain could have changed.
*/
event EIP712DomainChanged();
/**
* @dev returns the fields and values that describe the domain separator used by this contract for EIP-712
* signature.
*/
function eip712Domain()
external
view
returns (
bytes1 fields,
string memory name,
string memory version,
uint256 chainId,
address verifyingContract,
bytes32 salt,
uint256[] memory extensions
);
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
import {GlacisCommons} from "../commons/GlacisCommons.sol";
/// @title IGlacisAccessControlClient
/// @notice An interface that determines Glacis' required access control
interface IGlacisAccessControlClient {
/// @notice Adds an allowed route for this client
/// @notice Queries if a route from path GMP+Chain+Address is allowed for this client
/// @param route The origin route for the message
/// @param payload The payload of a message
/// @return True if route is allowed, false otherwise
function isAllowedRoute(
GlacisCommons.GlacisRoute memory route,
bytes memory payload
) external view returns (bool);
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
import {GlacisCommons} from "../commons/GlacisCommons.sol";
/// @title IGlacisAdapter
/// @notice An interface that defines the GMP modules (adapters) that the GlacisRouter interacts with.
interface IGlacisAdapter {
/// Determines If a chain is available through this adapter
/// @param chainId The Glacis chain ID
/// @return True if the chain is available, false otherwise
function chainIsAvailable(uint256 chainId) external view returns (bool);
/// Sends a payload across chains to the destination router.
/// @param chainId The Glacis chain ID
/// @param refundAddress The address to refund excessive gas payment to
/// @param payload The data packet to send across chains
function sendMessage(
uint256 chainId,
address refundAddress,
GlacisCommons.CrossChainGas memory incentives,
bytes memory payload
) external payable;
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
import {GlacisCommons} from "../commons/GlacisCommons.sol";
import {IGlacisAccessControlClient} from "../interfaces/IGlacisAccessControlClient.sol";
/// @title IGlacisClient
/// @notice An interface that defines the GMP modules (adapters) that the GlacisRouter interacts with.
abstract contract IGlacisClient is IGlacisAccessControlClient {
uint256 private immutable DEFAULT_QUORUM;
/// @param _defaultQuorum The default quorum that you would like. If you implement dynamic quorum, this value can be ignored
/// and set to 0
constructor(uint256 _defaultQuorum) {
DEFAULT_QUORUM = _defaultQuorum;
}
/// @notice Receives message from GMP(s) through GlacisRouter
/// @param fromAdapters Used adapters that sent this message (that reached quorum requirements)
/// @param fromChainId Source chain (Glacis chain ID)
/// @param fromAddress Source address on source chain
/// @param payload Routed payload
function receiveMessage(
address[] calldata fromAdapters,
uint256 fromChainId,
bytes32 fromAddress,
bytes calldata payload
) external virtual;
/// @notice The quorum of messages that the contract expects with a specific message
function getQuorum(
GlacisCommons.GlacisData memory, // glacis data
bytes memory, // payload
uint256 // unique messages received so far (for dynamic quorum, usually unused)
) public view virtual returns (uint256) {
return DEFAULT_QUORUM;
}
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
/// @title IGlacisRemoteCounterpartManager
/// @notice An interface that defines the existence and addition of a contract's remote counterparts
interface IGlacisRemoteCounterpartManager {
/// @notice Adds an authorized glacis counterpart component in a remote chain that interacts with this component
/// @param chainIDs An array with chains of the glacis remote components
/// @param glacisComponents An array of addresses of the glacis components on remote chains
function addRemoteCounterparts(
uint256[] calldata chainIDs,
bytes32[] calldata glacisComponents
) external;
/// @notice Removes an authorized glacis counterpart component on remote chain that this components interacts with
/// @param chainId The chainId to remove the remote component
function removeRemoteCounterpart(uint256 chainId) external;
/// @notice Gets an authorized glacis counterpart component on remote chain that this components interacts with
/// @param chainId The chainId to of the remote component
function getRemoteCounterpart(uint256 chainId) external returns (bytes32);
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
import {GlacisCommons} from "../commons/GlacisCommons.sol";
/// @title IGlacisRouterEvents
/// @notice An interface that defines a GlacisRouter's events
abstract contract IGlacisRouterEvents is GlacisCommons
{
event GlacisAbstractRouter__MessageIdCreated(
bytes32 indexed messageId,
bytes32 indexed sender,
uint256 nonce
);
event GlacisAbstractRouter__AdapterRegistered(
uint8 indexed gmpId,
address indexed adapterAddress,
address indexed previousAddress
);
event GlacisAbstractRouter__AdapterUnregistered(
uint8 indexed gmpId,
address indexed adapterAddress
);
event GlacisRouter__ReceivedMessage(
bytes32 indexed messageId,
bytes32 indexed from,
uint256 indexed fromChainId,
address adapter,
bytes32 to
);
event GlacisRouter__ExecutedMessage(
bytes32 indexed messageId,
bytes32 indexed from,
uint256 indexed fromChainId,
address adapter,
bytes32 to
);
event GlacisRouter__MessageDispatched(
bytes32 indexed messageId,
bytes32 indexed from,
uint256 indexed toChainId,
bytes32 to,
bytes data,
address[] adapters,
CrossChainGas[] fees,
address refundAddress,
bool retryable
);
event GlacisRouter__MessageRetried(
bytes32 indexed messageId,
bytes32 indexed from,
uint256 indexed toChainId,
bytes32 to,
bytes data,
address[] adapters,
CrossChainGas[] fees,
address refundAddress
);
}
/// @title IGlacisRouter
/// @notice An interface that defines an interface that sends and receives messages across chains
interface IGlacisRouter {
/// @notice Routes the payload to the specific address on the destination chain
/// using specified adapters
/// @param chainId Destination chain (EIP-155)
/// @param to Destination address on remote chain
/// @param payload Payload to be routed
/// @param adapters An array of adapters to be used for the routing (addresses 0x01-0xF8 for Glacis adapters
/// or specific addresses for custom adapters)
/// @param fees Array of fees to be sent to each GMP & custom adapter for routing (must be same length as gmps)
/// @param refundAddress An address for native currency to be sent to that are greater than fees charged. If it is a
/// contract it needs to support receive function, reverted otherwise
/// @param retryable True if this message could pottentially be retried
/// @return A tuple with a bytes32 messageId and a uint256 nonce
function route(
uint256 chainId,
bytes32 to,
bytes memory payload,
address[] memory adapters,
GlacisCommons.CrossChainGas[] memory fees,
address refundAddress,
bool retryable
) external payable returns (bytes32, uint256);
/// @notice Retries routing the payload to the specific address on destination chain
/// using specified GMPs and quorum
/// @param chainId Destination chain (EIP-155)
/// @param to Destination address on remote chain
/// @param payload Payload to be routed
/// @param adapters An array of adapters to be used for the routing (addresses 0x01-0xF8 for Glacis adapters
/// or specific addresses for custom adapters)
/// @param fees Array of fees to be sent to each GMP & custom adapter for routing (must be same length as gmps)
/// @param refundAddress An address for native currency to be sent to that are greater than fees charged. If it is a
/// contract it needs to support receive function, tx will revert otherwise
/// @param messageId The messageId to retry
/// @param nonce Unique value for this message routing
/// @return A tuple with a bytes32 messageId and a uint256 nonce
function routeRetry(
uint256 chainId,
bytes32 to,
bytes memory payload,
address[] memory adapters,
GlacisCommons.CrossChainGas[] memory fees,
address refundAddress,
bytes32 messageId,
uint256 nonce
) external payable returns (bytes32, uint256);
/// @notice Receives a cross chain message from an IGlacisAdapter.
/// @param fromChainId Source chain (EIP-155)
/// @param glacisPayload Received payload with embedded GlacisData
function receiveMessage(
uint256 fromChainId,
bytes memory glacisPayload
) external;
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
import {GlacisCommons} from "../commons/GlacisCommons.sol";
import {IGlacisAccessControlClient} from "./IGlacisAccessControlClient.sol";
/// @title IGlacisTokenClient
/// @notice An interface that defines the GMP modules (adapters) that the GlacisRouter interacts with.
/// @notice Should be paired with the IGlacisClient abstract smart contract.
interface IGlacisTokenClient is IGlacisAccessControlClient {
/// @notice Receives message from GMP(s) through GlacisRouter
/// @param fromAdapters Addresses of the adapters that sent this message (that reached quorum requirements)
/// @param fromChainId Source chain (Glacis chain ID)
/// @param fromAddress Source address on source chain
/// @param payload Routed payload
function receiveMessageWithTokens(
address[] memory fromAdapters,
uint256 fromChainId,
bytes32 fromAddress,
bytes calldata payload,
address token,
uint256 amount
) external;
/// @notice The quorum of messages that the contract expects with a specific message from the
/// token router
function getQuorum(
GlacisCommons.GlacisData memory,
bytes memory,
uint256,
address,
uint256
) external view returns (uint256);
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
import {GlacisCommons} from "../commons/GlacisCommons.sol";
/// @title IGlacisTokenMediator
/// @notice An interface of a mediator that sends XERC20s with a payload across chains
interface IGlacisTokenMediator {
event GlacisTokenMediator__TokensBurnt(
address indexed from,
address indexed token,
uint256 amount
);
event GlacisTokenMediator__TokensMinted(
address indexed to,
address indexed token,
uint256 amount
);
/// @notice Routes the payload to the specific address on destination chain through GlacisRouter using GMPs
/// specified in gmps array
/// @param chainId Destination chain (Glacis chain ID)
/// @param to Destination address on remote chain
/// @param payload Payload to be routed
/// @param adapters An array of custom adapters to be used for the routing
/// @param fees Payment for each GMP & custom adapter to cover source and destination gas fees (excess will be refunded)
/// @param refundAddress Address to refund excess gas payment
/// @param token Token (implementing XERC20 standard) to be sent to remote contract
/// @param tokenAmount Amount of token to send to remote contract
/// @return A tuple with a bytes32 messageId and a uint256 nonce
function route(
uint256 chainId,
bytes32 to,
bytes memory payload,
address[] memory adapters,
GlacisCommons.CrossChainGas[] memory fees,
address refundAddress,
address token,
uint256 tokenAmount
) external payable returns (bytes32, uint256);
/// @notice Retries routing the payload to the specific address on destination chain using specified GMPs
/// @param chainId Destination chain (Glacis chain ID)
/// @param to Destination address on remote chain
/// @param payload Payload to be routed
/// @param adapters An array of custom adapters to be used for the routing
/// @param fees Payment for each GMP to cover source and destination gas fees (excess will be refunded)
/// @param refundAddress Address to refund excess gas payment
/// @param messageId The message ID of the message to retry
/// @param nonce The nonce emitted by the original message routing
/// @param token Token (implementing XERC20 standard) to be sent to remote contract
/// @param tokenAmount Amount of token to send to remote contract
/// @return A tuple with a bytes32 messageId and a uint256 nonce
function routeRetry(
uint256 chainId,
bytes32 to,
bytes memory payload,
address[] memory adapters,
GlacisCommons.CrossChainGas[] memory fees,
address refundAddress,
bytes32 messageId,
uint256 nonce,
address token,
uint256 tokenAmount
) external payable returns (bytes32, uint256);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @title IGovernable Interface
* @notice This is an interface used by the AxelarGateway contract to manage governance and mint limiter roles.
*/
interface IGovernable {
error NotGovernance();
error NotMintLimiter();
error InvalidGovernance();
error InvalidMintLimiter();
event GovernanceTransferred(address indexed previousGovernance, address indexed newGovernance);
event MintLimiterTransferred(address indexed previousGovernance, address indexed newGovernance);
/**
* @notice Returns the governance address.
* @return address of the governance
*/
function governance() external view returns (address);
/**
* @notice Returns the mint limiter address.
* @return address of the mint limiter
*/
function mintLimiter() external view returns (address);
/**
* @notice Transfer the governance role to another address.
* @param newGovernance The new governance address
*/
function transferGovernance(address newGovernance) external;
/**
* @notice Transfer the mint limiter role to another address.
* @param newGovernance The new mint limiter address
*/
function transferMintLimiter(address newGovernance) external;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import { IContractIdentifier } from './IContractIdentifier.sol';
interface IImplementation is IContractIdentifier {
error NotProxy();
function setup(bytes calldata data) external;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import { GasEstimationType, GasInfo } from '../types/GasEstimationTypes.sol';
/**
* @title IInterchainGasEstimation Interface
* @notice This is an interface for the InterchainGasEstimation contract
* which allows for estimating gas fees for cross-chain communication on the Axelar network.
*/
interface IInterchainGasEstimation {
error UnsupportedEstimationType(GasEstimationType gasEstimationType);
/**
* @notice Event emitted when the gas price for a specific chain is updated.
* @param chain The name of the chain
* @param info The gas info for the chain
*/
event GasInfoUpdated(string chain, GasInfo info);
/**
* @notice Returns the gas price for a specific chain.
* @param chain The name of the chain
* @return gasInfo The gas info for the chain
*/
function getGasInfo(string calldata chain) external view returns (GasInfo memory);
/**
* @notice Estimates the gas fee for a cross-chain contract call.
* @param destinationChain Axelar registered name of the destination chain
* @param destinationAddress Destination contract address being called
* @param executionGasLimit The gas limit to be used for the destination contract execution,
* e.g. pass in 200k if your app consumes needs upto 200k for this contract call
* @param params Additional parameters for the gas estimation
* @return gasEstimate The cross-chain gas estimate, in terms of source chain's native gas token that should be forwarded to the gas service.
*/
function estimateGasFee(
string calldata destinationChain,
string calldata destinationAddress,
bytes calldata payload,
uint256 executionGasLimit,
bytes calldata params
) external view returns (uint256 gasEstimate);
}
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.11;
interface IInterchainSecurityModule {
enum Types {
UNUSED,
ROUTING,
AGGREGATION,
LEGACY_MULTISIG,
MERKLE_ROOT_MULTISIG,
MESSAGE_ID_MULTISIG,
NULL, // used with relayer carrying no metadata
CCIP_READ
}
/**
* @notice Returns an enum that represents the type of security model
* encoded by this ISM.
* @dev Relayers infer how to fetch and format metadata.
*/
function moduleType() external view returns (uint8);
/**
* @notice Defines a security model responsible for verifying interchain
* messages based on the provided metadata.
* @param _metadata Off-chain metadata provided by a relayer, specific to
* the security model encoded by the module (e.g. validator signatures)
* @param _message Hyperlane encoded interchain message
* @return True if the message was verified
*/
function verify(
bytes calldata _metadata,
bytes calldata _message
) external returns (bool);
}
interface ISpecifiesInterchainSecurityModule {
function interchainSecurityModule()
external
view
returns (IInterchainSecurityModule);
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0;
import "./ILayerZeroUserApplicationConfig.sol";
interface ILayerZeroEndpoint is ILayerZeroUserApplicationConfig {
// @notice send a LayerZero message to the specified address at a LayerZero endpoint.
// @param _dstChainId - the destination chain identifier
// @param _destination - the address on destination chain (in bytes). address length/format may vary by chains
// @param _payload - a custom bytes payload to send to the destination contract
// @param _refundAddress - if the source transaction is cheaper than the amount of value passed, refund the additional amount to this address
// @param _zroPaymentAddress - the address of the ZRO token holder who would pay for the transaction
// @param _adapterParams - parameters for custom functionality. e.g. receive airdropped native gas from the relayer on destination
function send(
uint16 _dstChainId,
bytes calldata _destination,
bytes calldata _payload,
address payable _refundAddress,
address _zroPaymentAddress,
bytes calldata _adapterParams
) external payable;
// @notice used by the messaging library to publish verified payload
// @param _srcChainId - the source chain identifier
// @param _srcAddress - the source contract (as bytes) at the source chain
// @param _dstAddress - the address on destination chain
// @param _nonce - the unbound message ordering nonce
// @param _gasLimit - the gas limit for external contract execution
// @param _payload - verified payload to send to the destination contract
function receivePayload(
uint16 _srcChainId,
bytes calldata _srcAddress,
address _dstAddress,
uint64 _nonce,
uint _gasLimit,
bytes calldata _payload
) external;
// @notice get the inboundNonce of a lzApp from a source chain which could be EVM or non-EVM chain
// @param _srcChainId - the source chain identifier
// @param _srcAddress - the source chain contract address
function getInboundNonce(uint16 _srcChainId, bytes calldata _srcAddress) external view returns (uint64);
// @notice get the outboundNonce from this source chain which, consequently, is always an EVM
// @param _srcAddress - the source chain contract address
function getOutboundNonce(uint16 _dstChainId, address _srcAddress) external view returns (uint64);
// @notice gets a quote in source native gas, for the amount that send() requires to pay for message delivery
// @param _dstChainId - the destination chain identifier
// @param _userApplication - the user app address on this EVM chain
// @param _payload - the custom message to send over LayerZero
// @param _payInZRO - if false, user app pays the protocol fee in native token
// @param _adapterParam - parameters for the adapter service, e.g. send some dust native token to dstChain
function estimateFees(
uint16 _dstChainId,
address _userApplication,
bytes calldata _payload,
bool _payInZRO,
bytes calldata _adapterParam
) external view returns (uint nativeFee, uint zroFee);
// @notice get this Endpoint's immutable source identifier
function getChainId() external view returns (uint16);
// @notice the interface to retry failed message on this Endpoint destination
// @param _srcChainId - the source chain identifier
// @param _srcAddress - the source chain contract address
// @param _payload - the payload to be retried
function retryPayload(
uint16 _srcChainId,
bytes calldata _srcAddress,
bytes calldata _payload
) external;
// @notice query if any STORED payload (message blocking) at the endpoint.
// @param _srcChainId - the source chain identifier
// @param _srcAddress - the source chain contract address
function hasStoredPayload(uint16 _srcChainId, bytes calldata _srcAddress) external view returns (bool);
// @notice query if the _libraryAddress is valid for sending msgs.
// @param _userApplication - the user app address on this EVM chain
function getSendLibraryAddress(address _userApplication) external view returns (address);
// @notice query if the _libraryAddress is valid for receiving msgs.
// @param _userApplication - the user app address on this EVM chain
function getReceiveLibraryAddress(address _userApplication) external view returns (address);
// @notice query if the non-reentrancy guard for send() is on
// @return true if the guard is on. false otherwise
function isSendingPayload() external view returns (bool);
// @notice query if the non-reentrancy guard for receive() is on
// @return true if the guard is on. false otherwise
function isReceivingPayload() external view returns (bool);
// @notice get the configuration of the LayerZero messaging library of the specified version
// @param _version - messaging library version
// @param _chainId - the chainId for the pending config change
// @param _userApplication - the contract address of the user application
// @param _configType - type of configuration. every messaging library has its own convention.
function getConfig(
uint16 _version,
uint16 _chainId,
address _userApplication,
uint _configType
) external view returns (bytes memory);
// @notice get the send() LayerZero messaging library version
// @param _userApplication - the contract address of the user application
function getSendVersion(address _userApplication) external view returns (uint16);
// @notice get the lzReceive() LayerZero messaging library version
// @param _userApplication - the contract address of the user application
function getReceiveVersion(address _userApplication) external view returns (uint16);
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import { IMessageLibManager } from "./IMessageLibManager.sol";
import { IMessagingComposer } from "./IMessagingComposer.sol";
import { IMessagingChannel } from "./IMessagingChannel.sol";
import { IMessagingContext } from "./IMessagingContext.sol";
struct MessagingParams {
uint32 dstEid;
bytes32 receiver;
bytes message;
bytes options;
bool payInLzToken;
}
struct MessagingReceipt {
bytes32 guid;
uint64 nonce;
MessagingFee fee;
}
struct MessagingFee {
uint256 nativeFee;
uint256 lzTokenFee;
}
struct Origin {
uint32 srcEid;
bytes32 sender;
uint64 nonce;
}
interface ILayerZeroEndpointV2 is IMessageLibManager, IMessagingComposer, IMessagingChannel, IMessagingContext {
event PacketSent(bytes encodedPayload, bytes options, address sendLibrary);
event PacketVerified(Origin origin, address receiver, bytes32 payloadHash);
event PacketDelivered(Origin origin, address receiver);
event LzReceiveAlert(
address indexed receiver,
address indexed executor,
Origin origin,
bytes32 guid,
uint256 gas,
uint256 value,
bytes message,
bytes extraData,
bytes reason
);
event LzTokenSet(address token);
event DelegateSet(address sender, address delegate);
function quote(MessagingParams calldata _params, address _sender) external view returns (MessagingFee memory);
function send(
MessagingParams calldata _params,
address _refundAddress
) external payable returns (MessagingReceipt memory);
function verify(Origin calldata _origin, address _receiver, bytes32 _payloadHash) external;
function verifiable(Origin calldata _origin, address _receiver) external view returns (bool);
function initializable(Origin calldata _origin, address _receiver) external view returns (bool);
function lzReceive(
Origin calldata _origin,
address _receiver,
bytes32 _guid,
bytes calldata _message,
bytes calldata _extraData
) external payable;
// oapp can burn messages partially by calling this function with its own business logic if messages are verified in order
function clear(address _oapp, Origin calldata _origin, bytes32 _guid, bytes calldata _message) external;
function setLzToken(address _lzToken) external;
function lzToken() external view returns (address);
function nativeToken() external view returns (address);
function setDelegate(address _delegate) external;
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import { Origin } from "./ILayerZeroEndpointV2.sol";
interface ILayerZeroReceiver {
function allowInitializePath(Origin calldata _origin) external view returns (bool);
function nextNonce(uint32 _eid, bytes32 _sender) external view returns (uint64);
function lzReceive(
Origin calldata _origin,
bytes32 _guid,
bytes calldata _message,
address _executor,
bytes calldata _extraData
) external payable;
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0;
interface ILayerZeroUserApplicationConfig {
// @notice set the configuration of the LayerZero messaging library of the specified version
// @param _version - messaging library version
// @param _chainId - the chainId for the pending config change
// @param _configType - type of configuration. every messaging library has its own convention.
// @param _config - configuration in the bytes. can encode arbitrary content.
function setConfig(
uint16 _version,
uint16 _chainId,
uint _configType,
bytes calldata _config
) external;
// @notice set the send() LayerZero messaging library version to _version
// @param _version - new messaging library version
function setSendVersion(uint16 _version) external;
// @notice set the lzReceive() LayerZero messaging library version to _version
// @param _version - new messaging library version
function setReceiveVersion(uint16 _version) external;
// @notice Only when the UA needs to resume the message flow in blocking mode and clear the stored payload
// @param _srcChainId - the chainId of the source chain
// @param _srcAddress - the contract address of the source contract at the source chain
function forceResumeReceive(uint16 _srcChainId, bytes calldata _srcAddress) external;
}
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.8.0;
import {IInterchainSecurityModule} from "./IInterchainSecurityModule.sol";
import {IPostDispatchHook} from "./hooks/IPostDispatchHook.sol";
interface IMailbox {
// ============ Events ============
/**
* @notice Emitted when a new message is dispatched via Hyperlane
* @param sender The address that dispatched the message
* @param destination The destination domain of the message
* @param recipient The message recipient address on `destination`
* @param message Raw bytes of message
*/
event Dispatch(
address indexed sender,
uint32 indexed destination,
bytes32 indexed recipient,
bytes message
);
/**
* @notice Emitted when a new message is dispatched via Hyperlane
* @param messageId The unique message identifier
*/
event DispatchId(bytes32 indexed messageId);
/**
* @notice Emitted when a Hyperlane message is processed
* @param messageId The unique message identifier
*/
event ProcessId(bytes32 indexed messageId);
/**
* @notice Emitted when a Hyperlane message is delivered
* @param origin The origin domain of the message
* @param sender The message sender address on `origin`
* @param recipient The address that handled the message
*/
event Process(
uint32 indexed origin,
bytes32 indexed sender,
address indexed recipient
);
function localDomain() external view returns (uint32);
function delivered(bytes32 messageId) external view returns (bool);
function defaultIsm() external view returns (IInterchainSecurityModule);
function defaultHook() external view returns (IPostDispatchHook);
function requiredHook() external view returns (IPostDispatchHook);
function latestDispatchedId() external view returns (bytes32);
function dispatch(
uint32 destinationDomain,
bytes32 recipientAddress,
bytes calldata messageBody
) external payable returns (bytes32 messageId);
function quoteDispatch(
uint32 destinationDomain,
bytes32 recipientAddress,
bytes calldata messageBody
) external view returns (uint256 fee);
function dispatch(
uint32 destinationDomain,
bytes32 recipientAddress,
bytes calldata body,
bytes calldata defaultHookMetadata
) external payable returns (bytes32 messageId);
function quoteDispatch(
uint32 destinationDomain,
bytes32 recipientAddress,
bytes calldata messageBody,
bytes calldata defaultHookMetadata
) external view returns (uint256 fee);
function dispatch(
uint32 destinationDomain,
bytes32 recipientAddress,
bytes calldata body,
bytes calldata customHookMetadata,
IPostDispatchHook customHook
) external payable returns (bytes32 messageId);
function quoteDispatch(
uint32 destinationDomain,
bytes32 recipientAddress,
bytes calldata messageBody,
bytes calldata customHookMetadata,
IPostDispatchHook customHook
) external view returns (uint256 fee);
function process(
bytes calldata metadata,
bytes calldata message
) external payable;
function recipientIsm(
address recipient
) external view returns (IInterchainSecurityModule module);
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
struct SetConfigParam {
uint32 eid;
uint32 configType;
bytes config;
}
interface IMessageLibManager {
struct Timeout {
address lib;
uint256 expiry;
}
event LibraryRegistered(address newLib);
event DefaultSendLibrarySet(uint32 eid, address newLib);
event DefaultReceiveLibrarySet(uint32 eid, address newLib);
event DefaultReceiveLibraryTimeoutSet(uint32 eid, address oldLib, uint256 expiry);
event SendLibrarySet(address sender, uint32 eid, address newLib);
event ReceiveLibrarySet(address receiver, uint32 eid, address newLib);
event ReceiveLibraryTimeoutSet(address receiver, uint32 eid, address oldLib, uint256 timeout);
function registerLibrary(address _lib) external;
function isRegisteredLibrary(address _lib) external view returns (bool);
function getRegisteredLibraries() external view returns (address[] memory);
function setDefaultSendLibrary(uint32 _eid, address _newLib) external;
function defaultSendLibrary(uint32 _eid) external view returns (address);
function setDefaultReceiveLibrary(uint32 _eid, address _newLib, uint256 _timeout) external;
function defaultReceiveLibrary(uint32 _eid) external view returns (address);
function setDefaultReceiveLibraryTimeout(uint32 _eid, address _lib, uint256 _expiry) external;
function defaultReceiveLibraryTimeout(uint32 _eid) external view returns (address lib, uint256 expiry);
function isSupportedEid(uint32 _eid) external view returns (bool);
function isValidReceiveLibrary(address _receiver, uint32 _eid, address _lib) external view returns (bool);
/// ------------------- OApp interfaces -------------------
function setSendLibrary(address _oapp, uint32 _eid, address _newLib) external;
function getSendLibrary(address _sender, uint32 _eid) external view returns (address lib);
function isDefaultSendLibrary(address _sender, uint32 _eid) external view returns (bool);
function setReceiveLibrary(address _oapp, uint32 _eid, address _newLib, uint256 _gracePeriod) external;
function getReceiveLibrary(address _receiver, uint32 _eid) external view returns (address lib, bool isDefault);
function setReceiveLibraryTimeout(address _oapp, uint32 _eid, address _lib, uint256 _gracePeriod) external;
function receiveLibraryTimeout(address _receiver, uint32 _eid) external view returns (address lib, uint256 expiry);
function setConfig(address _oapp, address _lib, SetConfigParam[] calldata _params) external;
function getConfig(
address _oapp,
address _lib,
uint32 _eid,
uint32 _configType
) external view returns (bytes memory config);
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
interface IMessagingChannel {
event InboundNonceSkipped(uint32 srcEid, bytes32 sender, address receiver, uint64 nonce);
event PacketNilified(uint32 srcEid, bytes32 sender, address receiver, uint64 nonce, bytes32 payloadHash);
event PacketBurnt(uint32 srcEid, bytes32 sender, address receiver, uint64 nonce, bytes32 payloadHash);
function eid() external view returns (uint32);
// this is an emergency function if a message cannot be verified for some reasons
// required to provide _nextNonce to avoid race condition
function skip(address _oapp, uint32 _srcEid, bytes32 _sender, uint64 _nonce) external;
function nilify(address _oapp, uint32 _srcEid, bytes32 _sender, uint64 _nonce, bytes32 _payloadHash) external;
function burn(address _oapp, uint32 _srcEid, bytes32 _sender, uint64 _nonce, bytes32 _payloadHash) external;
function nextGuid(address _sender, uint32 _dstEid, bytes32 _receiver) external view returns (bytes32);
function inboundNonce(address _receiver, uint32 _srcEid, bytes32 _sender) external view returns (uint64);
function outboundNonce(address _sender, uint32 _dstEid, bytes32 _receiver) external view returns (uint64);
function inboundPayloadHash(
address _receiver,
uint32 _srcEid,
bytes32 _sender,
uint64 _nonce
) external view returns (bytes32);
function lazyInboundNonce(address _receiver, uint32 _srcEid, bytes32 _sender) external view returns (uint64);
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
interface IMessagingComposer {
event ComposeSent(address from, address to, bytes32 guid, uint16 index, bytes message);
event ComposeDelivered(address from, address to, bytes32 guid, uint16 index);
event LzComposeAlert(
address indexed from,
address indexed to,
address indexed executor,
bytes32 guid,
uint16 index,
uint256 gas,
uint256 value,
bytes message,
bytes extraData,
bytes reason
);
function composeQueue(
address _from,
address _to,
bytes32 _guid,
uint16 _index
) external view returns (bytes32 messageHash);
function sendCompose(address _to, bytes32 _guid, uint16 _index, bytes calldata _message) external;
function lzCompose(
address _from,
address _to,
bytes32 _guid,
uint16 _index,
bytes calldata _message,
bytes calldata _extraData
) external payable;
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
interface IMessagingContext {
function isSendingMessage() external view returns (bool);
function getSendContext() external view returns (uint32 dstEid, address sender);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import { ILayerZeroEndpointV2 } from "@layerzerolabs/lz-evm-protocol-v2/contracts/interfaces/ILayerZeroEndpointV2.sol";
/**
* @title IOAppCore
*/
interface IOAppCore {
// Custom error messages
error OnlyPeer(uint32 eid, bytes32 sender);
error NoPeer(uint32 eid);
error InvalidEndpointCall();
error InvalidDelegate();
// Event emitted when a peer (OApp) is set for a corresponding endpoint
event PeerSet(uint32 eid, bytes32 peer);
/**
* @notice Retrieves the OApp version information.
* @return senderVersion The version of the OAppSender.sol contract.
* @return receiverVersion The version of the OAppReceiver.sol contract.
*/
function oAppVersion() external view returns (uint64 senderVersion, uint64 receiverVersion);
/**
* @notice Retrieves the LayerZero endpoint associated with the OApp.
* @return iEndpoint The LayerZero endpoint as an interface.
*/
function endpoint() external view returns (ILayerZeroEndpointV2 iEndpoint);
/**
* @notice Retrieves the peer (OApp) associated with a corresponding endpoint.
* @param _eid The endpoint ID.
* @return peer The peer address (OApp instance) associated with the corresponding endpoint.
*/
function peers(uint32 _eid) external view returns (bytes32 peer);
/**
* @notice Sets the peer address (OApp instance) for a corresponding endpoint.
* @param _eid The endpoint ID.
* @param _peer The address of the peer to be associated with the corresponding endpoint.
*/
function setPeer(uint32 _eid, bytes32 _peer) external;
/**
* @notice Sets the delegate address for the OApp Core.
* @param _delegate The address of the delegate to be set.
*/
function setDelegate(address _delegate) external;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import { ILayerZeroReceiver, Origin } from "@layerzerolabs/lz-evm-protocol-v2/contracts/interfaces/ILayerZeroReceiver.sol";
interface IOAppReceiver is ILayerZeroReceiver {
/**
* @notice Indicates whether an address is an approved composeMsg sender to the Endpoint.
* @param _origin The origin information containing the source endpoint and sender address.
* - srcEid: The source chain endpoint ID.
* - sender: The sender address on the src chain.
* - nonce: The nonce of the message.
* @param _message The lzReceive payload.
* @param _sender The sender address.
* @return isSender Is a valid sender.
*
* @dev Applications can optionally choose to implement a separate composeMsg sender that is NOT the bridging layer.
* @dev The default sender IS the OAppReceiver implementer.
*/
function isComposeMsgSender(
Origin calldata _origin,
bytes calldata _message,
address _sender
) external view returns (bool isSender);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @title IOwnable Interface
* @notice IOwnable is an interface that abstracts the implementation of a
* contract with ownership control features. It's commonly used in upgradable
* contracts and includes the functionality to get current owner, transfer
* ownership, and propose and accept ownership.
*/
interface IOwnable {
error NotOwner();
error InvalidOwner();
error InvalidOwnerAddress();
event OwnershipTransferStarted(address indexed newOwner);
event OwnershipTransferred(address indexed newOwner);
/**
* @notice Returns the current owner of the contract.
* @return address The address of the current owner
*/
function owner() external view returns (address);
/**
* @notice Returns the address of the pending owner of the contract.
* @return address The address of the pending owner
*/
function pendingOwner() external view returns (address);
/**
* @notice Transfers ownership of the contract to a new address
* @param newOwner The address to transfer ownership to
*/
function transferOwnership(address newOwner) external;
/**
* @notice Proposes to transfer the contract's ownership to a new address.
* The new owner needs to accept the ownership explicitly.
* @param newOwner The address to transfer ownership to
*/
function proposeOwnership(address newOwner) external;
/**
* @notice Transfers ownership to the pending owner.
* @dev Can only be called by the pending owner
*/
function acceptOwnership() external;
}
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.8.0;
/*@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@ HYPERLANE @@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@*/
interface IPostDispatchHook {
enum Types {
UNUSED,
ROUTING,
AGGREGATION,
MERKLE_TREE,
INTERCHAIN_GAS_PAYMASTER,
FALLBACK_ROUTING,
ID_AUTH_ISM,
PAUSABLE,
PROTOCOL_FEE,
LAYER_ZERO_V1,
Rate_Limited_Hook
}
/**
* @notice Returns an enum that represents the type of hook
*/
function hookType() external view returns (uint8);
/**
* @notice Returns whether the hook supports metadata
* @param metadata metadata
* @return Whether the hook supports metadata
*/
function supportsMetadata(
bytes calldata metadata
) external view returns (bool);
/**
* @notice Post action after a message is dispatched via the Mailbox
* @param metadata The metadata required for the hook
* @param message The message passed from the Mailbox.dispatch() call
*/
function postDispatch(
bytes calldata metadata,
bytes calldata message
) external payable;
/**
* @notice Compute the payment required by the postDispatch call
* @param metadata The metadata required for the hook
* @param message The message passed from the Mailbox.dispatch() call
* @return Quoted payment for the postDispatch call
*/
function quoteDispatch(
bytes calldata metadata,
bytes calldata message
) external view returns (uint256);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {Client} from "../libraries/Client.sol";
interface IRouterClient {
error UnsupportedDestinationChain(uint64 destChainSelector);
error InsufficientFeeTokenAmount();
error InvalidMsgValue();
/// @notice Checks if the given chain ID is supported for sending/receiving.
/// @param chainSelector The chain to check.
/// @return supported is true if it is supported, false if not.
function isChainSupported(uint64 chainSelector) external view returns (bool supported);
/// @notice Gets a list of all supported tokens which can be sent or received
/// to/from a given chain id.
/// @param chainSelector The chainSelector.
/// @return tokens The addresses of all tokens that are supported.
function getSupportedTokens(uint64 chainSelector) external view returns (address[] memory tokens);
/// @param destinationChainSelector The destination chainSelector
/// @param message The cross-chain CCIP message including data and/or tokens
/// @return fee returns execution fee for the message
/// delivery to destination chain, denominated in the feeToken specified in the message.
/// @dev Reverts with appropriate reason upon invalid message.
function getFee(
uint64 destinationChainSelector,
Client.EVM2AnyMessage memory message
) external view returns (uint256 fee);
/// @notice Request a message to be sent to the destination chain
/// @param destinationChainSelector The destination chain ID
/// @param message The cross-chain CCIP message including data and/or tokens
/// @return messageId The message ID
/// @dev Note if msg.value is larger than the required fee (from getFee) we accept
/// the overpayment with no refund.
/// @dev Reverts with appropriate reason upon invalid message.
function ccipSend(
uint64 destinationChainSelector,
Client.EVM2AnyMessage calldata message
) external payable returns (bytes32);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import { IOwnable } from './IOwnable.sol';
import { IImplementation } from './IImplementation.sol';
// General interface for upgradable contracts
interface IUpgradable is IOwnable, IImplementation {
error InvalidCodeHash();
error InvalidImplementation();
error SetupFailed();
event Upgraded(address indexed newImplementation);
function implementation() external view returns (address);
function upgrade(
address newImplementation,
bytes32 newImplementationCodeHash,
bytes calldata params
) external;
}
// SPDX-License-Identifier: Apache 2
pragma solidity 0.8.18;
/**
* @notice Interface for a contract which can receive Wormhole messages.
*/
interface IWormholeReceiver {
/**
* @notice When a `send` is performed with this contract as the target, this function will be
* invoked by the WormholeRelayer contract
*
* NOTE: This function should be restricted such that only the Wormhole Relayer contract can call it.
*
* We also recommend that this function:
* - Stores all received `deliveryHash`s in a mapping `(bytes32 => bool)`, and
* on every call, checks that deliveryHash has not already been stored in the
* map (This is to prevent other users maliciously trying to relay the same message)
* - Checks that `sourceChain` and `sourceAddress` are indeed who
* you expect to have requested the calling of `send` on the source chain
*
* The invocation of this function corresponding to the `send` request will have msg.value equal
* to the receiverValue specified in the send request.
*
* If the invocation of this function reverts or exceeds the gas limit
* specified by the send requester, this delivery will result in a `ReceiverFailure`.
*
* @param payload - an arbitrary message which was included in the delivery by the
* requester.
* @param additionalVaas - Additional VAAs which were requested to be included in this delivery.
* They are guaranteed to all be included and in the same order as was specified in the
* delivery request.
* @param sourceAddress - the (wormhole format) address on the sending chain which requested
* this delivery.
* @param sourceChain - the wormhole chain ID where this delivery was requested.
* @param deliveryHash - the VAA hash of the deliveryVAA.
*
* NOTE: These signedVaas are NOT verified by the Wormhole core contract prior to being provided
* to this call. Always make sure `parseAndVerify()` is called on the Wormhole core contract
* before trusting the content of a raw VAA, otherwise the VAA may be invalid or malicious.
*/
function receiveWormholeMessages(
bytes memory payload,
bytes[] memory additionalVaas,
bytes32 sourceAddress,
uint16 sourceChain,
bytes32 deliveryHash
) external payable;
}
// SPDX-License-Identifier: Apache 2
pragma solidity 0.8.18;
/**
* @title WormholeRelayer
* @author
* @notice This project allows developers to build cross-chain applications powered by Wormhole without needing to
* write and run their own relaying infrastructure
*
* We implement the IWormholeRelayer interface that allows users to request a delivery provider to relay a payload (and/or additional VAAs)
* to a chain and address of their choice.
*/
/**
* @notice VaaKey identifies a wormhole message
*
* @custom:member chainId Wormhole chain ID of the chain where this VAA was emitted from
* @custom:member emitterAddress Address of the emitter of the VAA, in Wormhole bytes32 format
* @custom:member sequence Sequence number of the VAA
*/
struct VaaKey {
uint16 chainId;
bytes32 emitterAddress;
uint64 sequence;
}
interface IWormholeRelayerBase {
event SendEvent(
uint64 indexed sequence,
uint256 deliveryQuote,
uint256 paymentForExtraReceiverValue
);
function getRegisteredWormholeRelayerContract(
uint16 chainId
) external view returns (bytes32);
}
/**
* @title IWormholeRelayerSend
* @notice The interface to request deliveries
*/
interface IWormholeRelayerSend is IWormholeRelayerBase {
/**
* @notice Publishes an instruction for the default delivery provider
* to relay a payload to the address `targetAddress` on chain `targetChain`
* with gas limit `gasLimit` and `msg.value` equal to `receiverValue`
*
* `targetAddress` must implement the IWormholeReceiver interface
*
* This function must be called with `msg.value` equal to `quoteEVMDeliveryPrice(targetChain, receiverValue, gasLimit)`
*
* Any refunds (from leftover gas) will be paid to the delivery provider. In order to receive the refunds, use the `sendPayloadToEvm` function
* with `refundChain` and `refundAddress` as parameters
*
* @param targetChain in Wormhole Chain ID format
* @param targetAddress address to call on targetChain (that implements IWormholeReceiver)
* @param payload arbitrary bytes to pass in as parameter in call to `targetAddress`
* @param receiverValue msg.value that delivery provider should pass in for call to `targetAddress` (in targetChain currency units)
* @param gasLimit gas limit with which to call `targetAddress`.
* @return sequence sequence number of published VAA containing delivery instructions
*/
function sendPayloadToEvm(
uint16 targetChain,
address targetAddress,
bytes memory payload,
uint256 receiverValue,
uint256 gasLimit
) external payable returns (uint64 sequence);
/**
* @notice Publishes an instruction for the default delivery provider
* to relay a payload to the address `targetAddress` on chain `targetChain`
* with gas limit `gasLimit` and `msg.value` equal to `receiverValue`
*
* Any refunds (from leftover gas) will be sent to `refundAddress` on chain `refundChain`
* `targetAddress` must implement the IWormholeReceiver interface
*
* This function must be called with `msg.value` equal to `quoteEVMDeliveryPrice(targetChain, receiverValue, gasLimit)`
*
* @param targetChain in Wormhole Chain ID format
* @param targetAddress address to call on targetChain (that implements IWormholeReceiver)
* @param payload arbitrary bytes to pass in as parameter in call to `targetAddress`
* @param receiverValue msg.value that delivery provider should pass in for call to `targetAddress` (in targetChain currency units)
* @param gasLimit gas limit with which to call `targetAddress`. Any units of gas unused will be refunded according to the
* `targetChainRefundPerGasUnused` rate quoted by the delivery provider
* @param refundChain The chain to deliver any refund to, in Wormhole Chain ID format
* @param refundAddress The address on `refundChain` to deliver any refund to
* @return sequence sequence number of published VAA containing delivery instructions
*/
function sendPayloadToEvm(
uint16 targetChain,
address targetAddress,
bytes memory payload,
uint256 receiverValue,
uint256 gasLimit,
uint16 refundChain,
address refundAddress
) external payable returns (uint64 sequence);
/**
* @notice Publishes an instruction for the default delivery provider
* to relay a payload and VAAs specified by `vaaKeys` to the address `targetAddress` on chain `targetChain`
* with gas limit `gasLimit` and `msg.value` equal to `receiverValue`
*
* `targetAddress` must implement the IWormholeReceiver interface
*
* This function must be called with `msg.value` equal to `quoteEVMDeliveryPrice(targetChain, receiverValue, gasLimit)`
*
* Any refunds (from leftover gas) will be paid to the delivery provider. In order to receive the refunds, use the `sendVaasToEvm` function
* with `refundChain` and `refundAddress` as parameters
*
* @param targetChain in Wormhole Chain ID format
* @param targetAddress address to call on targetChain (that implements IWormholeReceiver)
* @param payload arbitrary bytes to pass in as parameter in call to `targetAddress`
* @param receiverValue msg.value that delivery provider should pass in for call to `targetAddress` (in targetChain currency units)
* @param gasLimit gas limit with which to call `targetAddress`.
* @param vaaKeys Additional VAAs to pass in as parameter in call to `targetAddress`
* @return sequence sequence number of published VAA containing delivery instructions
*/
function sendVaasToEvm(
uint16 targetChain,
address targetAddress,
bytes memory payload,
uint256 receiverValue,
uint256 gasLimit,
VaaKey[] memory vaaKeys
) external payable returns (uint64 sequence);
/**
* @notice Publishes an instruction for the default delivery provider
* to relay a payload and VAAs specified by `vaaKeys` to the address `targetAddress` on chain `targetChain`
* with gas limit `gasLimit` and `msg.value` equal to `receiverValue`
*
* Any refunds (from leftover gas) will be sent to `refundAddress` on chain `refundChain`
* `targetAddress` must implement the IWormholeReceiver interface
*
* This function must be called with `msg.value` equal to `quoteEVMDeliveryPrice(targetChain, receiverValue, gasLimit)`
*
* @param targetChain in Wormhole Chain ID format
* @param targetAddress address to call on targetChain (that implements IWormholeReceiver)
* @param payload arbitrary bytes to pass in as parameter in call to `targetAddress`
* @param receiverValue msg.value that delivery provider should pass in for call to `targetAddress` (in targetChain currency units)
* @param gasLimit gas limit with which to call `targetAddress`. Any units of gas unused will be refunded according to the
* `targetChainRefundPerGasUnused` rate quoted by the delivery provider
* @param vaaKeys Additional VAAs to pass in as parameter in call to `targetAddress`
* @param refundChain The chain to deliver any refund to, in Wormhole Chain ID format
* @param refundAddress The address on `refundChain` to deliver any refund to
* @return sequence sequence number of published VAA containing delivery instructions
*/
function sendVaasToEvm(
uint16 targetChain,
address targetAddress,
bytes memory payload,
uint256 receiverValue,
uint256 gasLimit,
VaaKey[] memory vaaKeys,
uint16 refundChain,
address refundAddress
) external payable returns (uint64 sequence);
/**
* @notice Publishes an instruction for the delivery provider at `deliveryProviderAddress`
* to relay a payload and VAAs specified by `vaaKeys` to the address `targetAddress` on chain `targetChain`
* with gas limit `gasLimit` and `msg.value` equal to
* receiverValue + (arbitrary amount that is paid for by paymentForExtraReceiverValue of this chain's wei) in targetChain wei.
*
* Any refunds (from leftover gas) will be sent to `refundAddress` on chain `refundChain`
* `targetAddress` must implement the IWormholeReceiver interface
*
* This function must be called with `msg.value` equal to
* quoteEVMDeliveryPrice(targetChain, receiverValue, gasLimit, deliveryProviderAddress) + paymentForExtraReceiverValue
*
* @param targetChain in Wormhole Chain ID format
* @param targetAddress address to call on targetChain (that implements IWormholeReceiver)
* @param payload arbitrary bytes to pass in as parameter in call to `targetAddress`
* @param receiverValue msg.value that delivery provider should pass in for call to `targetAddress` (in targetChain currency units)
* @param paymentForExtraReceiverValue amount (in current chain currency units) to spend on extra receiverValue
* (in addition to the `receiverValue` specified)
* @param gasLimit gas limit with which to call `targetAddress`. Any units of gas unused will be refunded according to the
* `targetChainRefundPerGasUnused` rate quoted by the delivery provider
* @param refundChain The chain to deliver any refund to, in Wormhole Chain ID format
* @param refundAddress The address on `refundChain` to deliver any refund to
* @param deliveryProviderAddress The address of the desired delivery provider's implementation of IDeliveryProvider
* @param vaaKeys Additional VAAs to pass in as parameter in call to `targetAddress`
* @param consistencyLevel Consistency level with which to publish the delivery instructions - see
* https://book.wormhole.com/wormhole/3_coreLayerContracts.html?highlight=consistency#consistency-levels
* @return sequence sequence number of published VAA containing delivery instructions
*/
function sendToEvm(
uint16 targetChain,
address targetAddress,
bytes memory payload,
uint256 receiverValue,
uint256 paymentForExtraReceiverValue,
uint256 gasLimit,
uint16 refundChain,
address refundAddress,
address deliveryProviderAddress,
VaaKey[] memory vaaKeys,
uint8 consistencyLevel
) external payable returns (uint64 sequence);
/**
* @notice Publishes an instruction for the delivery provider at `deliveryProviderAddress`
* to relay a payload and VAAs specified by `vaaKeys` to the address `targetAddress` on chain `targetChain`
* with `msg.value` equal to
* receiverValue + (arbitrary amount that is paid for by paymentForExtraReceiverValue of this chain's wei) in targetChain wei.
*
* Any refunds (from leftover gas) will be sent to `refundAddress` on chain `refundChain`
* `targetAddress` must implement the IWormholeReceiver interface
*
* This function must be called with `msg.value` equal to
* quoteDeliveryPrice(targetChain, receiverValue, encodedExecutionParameters, deliveryProviderAddress) + paymentForExtraReceiverValue
*
* @param targetChain in Wormhole Chain ID format
* @param targetAddress address to call on targetChain (that implements IWormholeReceiver), in Wormhole bytes32 format
* @param payload arbitrary bytes to pass in as parameter in call to `targetAddress`
* @param receiverValue msg.value that delivery provider should pass in for call to `targetAddress` (in targetChain currency units)
* @param paymentForExtraReceiverValue amount (in current chain currency units) to spend on extra receiverValue
* (in addition to the `receiverValue` specified)
* @param encodedExecutionParameters encoded information on how to execute delivery that may impact pricing
* e.g. for version EVM_V1, this is a struct that encodes the `gasLimit` with which to call `targetAddress`
* @param refundChain The chain to deliver any refund to, in Wormhole Chain ID format
* @param refundAddress The address on `refundChain` to deliver any refund to, in Wormhole bytes32 format
* @param deliveryProviderAddress The address of the desired delivery provider's implementation of IDeliveryProvider
* @param vaaKeys Additional VAAs to pass in as parameter in call to `targetAddress`
* @param consistencyLevel Consistency level with which to publish the delivery instructions - see
* https://book.wormhole.com/wormhole/3_coreLayerContracts.html?highlight=consistency#consistency-levels
* @return sequence sequence number of published VAA containing delivery instructions
*/
function send(
uint16 targetChain,
bytes32 targetAddress,
bytes memory payload,
uint256 receiverValue,
uint256 paymentForExtraReceiverValue,
bytes memory encodedExecutionParameters,
uint16 refundChain,
bytes32 refundAddress,
address deliveryProviderAddress,
VaaKey[] memory vaaKeys,
uint8 consistencyLevel
) external payable returns (uint64 sequence);
/**
* @notice Requests a previously published delivery instruction to be redelivered
* (e.g. with a different delivery provider)
*
* This function must be called with `msg.value` equal to
* quoteEVMDeliveryPrice(targetChain, newReceiverValue, newGasLimit, newDeliveryProviderAddress)
*
* @notice *** This will only be able to succeed if the following is true **
* - newGasLimit >= gas limit of the old instruction
* - newReceiverValue >= receiver value of the old instruction
* - newDeliveryProvider's `targetChainRefundPerGasUnused` >= old relay provider's `targetChainRefundPerGasUnused`
*
* @param deliveryVaaKey VaaKey identifying the wormhole message containing the
* previously published delivery instructions
* @param targetChain The target chain that the original delivery targeted. Must match targetChain from original delivery instructions
* @param newReceiverValue new msg.value that delivery provider should pass in for call to `targetAddress` (in targetChain currency units)
* @param newGasLimit gas limit with which to call `targetAddress`. Any units of gas unused will be refunded according to the
* `targetChainRefundPerGasUnused` rate quoted by the delivery provider, to the refund chain and address specified in the original request
* @param newDeliveryProviderAddress The address of the desired delivery provider's implementation of IDeliveryProvider
* @return sequence sequence number of published VAA containing redelivery instructions
*
* @notice *** This will only be able to succeed if the following is true **
* - newGasLimit >= gas limit of the old instruction
* - newReceiverValue >= receiver value of the old instruction
* - newDeliveryProvider's `targetChainRefundPerGasUnused` >= old relay provider's `targetChainRefundPerGasUnused`
*/
function resendToEvm(
VaaKey memory deliveryVaaKey,
uint16 targetChain,
uint256 newReceiverValue,
uint256 newGasLimit,
address newDeliveryProviderAddress
) external payable returns (uint64 sequence);
/**
* @notice Requests a previously published delivery instruction to be redelivered
*
*
* This function must be called with `msg.value` equal to
* quoteDeliveryPrice(targetChain, newReceiverValue, newEncodedExecutionParameters, newDeliveryProviderAddress)
*
* @param deliveryVaaKey VaaKey identifying the wormhole message containing the
* previously published delivery instructions
* @param targetChain The target chain that the original delivery targeted. Must match targetChain from original delivery instructions
* @param newReceiverValue new msg.value that delivery provider should pass in for call to `targetAddress` (in targetChain currency units)
* @param newEncodedExecutionParameters new encoded information on how to execute delivery that may impact pricing
* e.g. for version EVM_V1, this is a struct that encodes the `gasLimit` with which to call `targetAddress`
* @param newDeliveryProviderAddress The address of the desired delivery provider's implementation of IDeliveryProvider
* @return sequence sequence number of published VAA containing redelivery instructions
*
* @notice *** This will only be able to succeed if the following is true **
* - (For EVM_V1) newGasLimit >= gas limit of the old instruction
* - newReceiverValue >= receiver value of the old instruction
* - (For EVM_V1) newDeliveryProvider's `targetChainRefundPerGasUnused` >= old relay provider's `targetChainRefundPerGasUnused`
*/
function resend(
VaaKey memory deliveryVaaKey,
uint16 targetChain,
uint256 newReceiverValue,
bytes memory newEncodedExecutionParameters,
address newDeliveryProviderAddress
) external payable returns (uint64 sequence);
/**
* @notice Returns the price to request a relay to chain `targetChain`, using the default delivery provider
*
* @param targetChain in Wormhole Chain ID format
* @param receiverValue msg.value that delivery provider should pass in for call to `targetAddress` (in targetChain currency units)
* @param gasLimit gas limit with which to call `targetAddress`.
* @return nativePriceQuote Price, in units of current chain currency, that the delivery provider charges to perform the relay
* @return targetChainRefundPerGasUnused amount of target chain currency that will be refunded per unit of gas unused,
* if a refundAddress is specified
*/
function quoteEVMDeliveryPrice(
uint16 targetChain,
uint256 receiverValue,
uint256 gasLimit
)
external
view
returns (
uint256 nativePriceQuote,
uint256 targetChainRefundPerGasUnused
);
/**
* @notice Returns the price to request a relay to chain `targetChain`, using delivery provider `deliveryProviderAddress`
*
* @param targetChain in Wormhole Chain ID format
* @param receiverValue msg.value that delivery provider should pass in for call to `targetAddress` (in targetChain currency units)
* @param gasLimit gas limit with which to call `targetAddress`.
* @param deliveryProviderAddress The address of the desired delivery provider's implementation of IDeliveryProvider
* @return nativePriceQuote Price, in units of current chain currency, that the delivery provider charges to perform the relay
* @return targetChainRefundPerGasUnused amount of target chain currency that will be refunded per unit of gas unused,
* if a refundAddress is specified
*/
function quoteEVMDeliveryPrice(
uint16 targetChain,
uint256 receiverValue,
uint256 gasLimit,
address deliveryProviderAddress
)
external
view
returns (
uint256 nativePriceQuote,
uint256 targetChainRefundPerGasUnused
);
/**
* @notice Returns the price to request a relay to chain `targetChain`, using delivery provider `deliveryProviderAddress`
*
* @param targetChain in Wormhole Chain ID format
* @param receiverValue msg.value that delivery provider should pass in for call to `targetAddress` (in targetChain currency units)
* @param encodedExecutionParameters encoded information on how to execute delivery that may impact pricing
* e.g. for version EVM_V1, this is a struct that encodes the `gasLimit` with which to call `targetAddress`
* @param deliveryProviderAddress The address of the desired delivery provider's implementation of IDeliveryProvider
* @return nativePriceQuote Price, in units of current chain currency, that the delivery provider charges to perform the relay
* @return encodedExecutionInfo encoded information on how the delivery will be executed
* e.g. for version EVM_V1, this is a struct that encodes the `gasLimit` and `targetChainRefundPerGasUnused`
* (which is the amount of target chain currency that will be refunded per unit of gas unused,
* if a refundAddress is specified)
*/
function quoteDeliveryPrice(
uint16 targetChain,
uint256 receiverValue,
bytes memory encodedExecutionParameters,
address deliveryProviderAddress
)
external
view
returns (uint256 nativePriceQuote, bytes memory encodedExecutionInfo);
/**
* @notice Returns the (extra) amount of target chain currency that `targetAddress`
* will be called with, if the `paymentForExtraReceiverValue` field is set to `currentChainAmount`
*
* @param targetChain in Wormhole Chain ID format
* @param currentChainAmount The value that `paymentForExtraReceiverValue` will be set to
* @param deliveryProviderAddress The address of the desired delivery provider's implementation of IDeliveryProvider
* @return targetChainAmount The amount such that if `targetAddress` will be called with `msg.value` equal to
* receiverValue + targetChainAmount
*/
function quoteNativeForChain(
uint16 targetChain,
uint256 currentChainAmount,
address deliveryProviderAddress
) external view returns (uint256 targetChainAmount);
/**
* @notice Returns the address of the current default delivery provider
* @return deliveryProvider The address of (the default delivery provider)'s contract on this source
* chain. This must be a contract that implements IDeliveryProvider.
*/
function getDefaultDeliveryProvider()
external
view
returns (address deliveryProvider);
}
/**
* @title IWormholeRelayerDelivery
* @notice The interface to execute deliveries. Only relevant for Delivery Providers
*/
interface IWormholeRelayerDelivery is IWormholeRelayerBase {
enum DeliveryStatus {
SUCCESS,
RECEIVER_FAILURE
}
enum RefundStatus {
REFUND_SENT,
REFUND_FAIL,
CROSS_CHAIN_REFUND_SENT,
CROSS_CHAIN_REFUND_FAIL_PROVIDER_NOT_SUPPORTED,
CROSS_CHAIN_REFUND_FAIL_NOT_ENOUGH
}
/**
* @custom:member recipientContract - The target contract address
* @custom:member sourceChain - The chain which this delivery was requested from (in wormhole
* ChainID format)
* @custom:member sequence - The wormhole sequence number of the delivery VAA on the source chain
* corresponding to this delivery request
* @custom:member deliveryVaaHash - The hash of the delivery VAA corresponding to this delivery
* request
* @custom:member gasUsed - The amount of gas that was used to call your target contract
* @custom:member status:
* - RECEIVER_FAILURE, if the target contract reverts
* - SUCCESS, if the target contract doesn't revert
* @custom:member additionalStatusInfo:
* - If status is SUCCESS, then this is empty.
* - If status is RECEIVER_FAILURE, this is `RETURNDATA_TRUNCATION_THRESHOLD` bytes of the
* return data (i.e. potentially truncated revert reason information).
* @custom:member refundStatus - Result of the refund. REFUND_SUCCESS or REFUND_FAIL are for
* refunds where targetChain=refundChain; the others are for targetChain!=refundChain,
* where a cross chain refund is necessary
* @custom:member overridesInfo:
* - If not an override: empty bytes array
* - Otherwise: An encoded `DeliveryOverride`
*/
event Delivery(
address indexed recipientContract,
uint16 indexed sourceChain,
uint64 indexed sequence,
bytes32 deliveryVaaHash,
DeliveryStatus status,
uint256 gasUsed,
RefundStatus refundStatus,
bytes additionalStatusInfo,
bytes overridesInfo
);
/**
* @notice The delivery provider calls `deliver` to relay messages as described by one delivery instruction
*
* The delivery provider must pass in the specified (by VaaKeys[]) signed wormhole messages (VAAs) from the source chain
* as well as the signed wormhole message with the delivery instructions (the delivery VAA)
*
* The messages will be relayed to the target address (with the specified gas limit and receiver value) iff the following checks are met:
* - the delivery VAA has a valid signature
* - the delivery VAA's emitter is one of these WormholeRelayer contracts
* - the delivery provider passed in at least enough of this chain's currency as msg.value (enough meaning the maximum possible refund)
* - the instruction's target chain is this chain
* - the relayed signed VAAs match the descriptions in container.messages (the VAA hashes match, or the emitter address, sequence number pair matches, depending on the description given)
*
* @param encodedVMs - An array of signed wormhole messages (all from the same source chain
* transaction)
* @param encodedDeliveryVAA - Signed wormhole message from the source chain's WormholeRelayer
* contract with payload being the encoded delivery instruction container
* @param relayerRefundAddress - The address to which any refunds to the delivery provider
* should be sent
* @param deliveryOverrides - Optional overrides field which must be either an empty bytes array or
* an encoded DeliveryOverride struct
*/
function deliver(
bytes[] memory encodedVMs,
bytes memory encodedDeliveryVAA,
address payable relayerRefundAddress,
bytes memory deliveryOverrides
) external payable;
}
interface IWormholeRelayer is IWormholeRelayerDelivery, IWormholeRelayerSend {}
/*
* Errors thrown by IWormholeRelayer contract
*/
// Bound chosen by the following formula: `memoryWord * 4 + selectorSize`.
// This means that an error identifier plus four fixed size arguments should be available to developers.
// In the case of a `require` revert with error message, this should provide 2 memory word's worth of data.
uint256 constant RETURNDATA_TRUNCATION_THRESHOLD = 132;
//When msg.value was not equal to `delivery provider's quoted delivery price` + `paymentForExtraReceiverValue`
error InvalidMsgValue(uint256 msgValue, uint256 totalFee);
error RequestedGasLimitTooLow();
error DeliveryProviderDoesNotSupportTargetChain(
address relayer,
uint16 chainId
);
error DeliveryProviderCannotReceivePayment();
//When calling `delivery()` a second time even though a delivery is already in progress
error ReentrantDelivery(address msgSender, address lockedBy);
error InvalidPayloadId(uint8 parsed, uint8 expected);
error InvalidPayloadLength(uint256 received, uint256 expected);
error InvalidVaaKeyType(uint8 parsed);
error InvalidDeliveryVaa(string reason);
//When the delivery VAA (signed wormhole message with delivery instructions) was not emitted by the
// registered WormholeRelayer contract
error InvalidEmitter(bytes32 emitter, bytes32 registered, uint16 chainId);
error VaaKeysLengthDoesNotMatchVaasLength(uint256 keys, uint256 vaas);
error VaaKeysDoNotMatchVaas(uint8 index);
//When someone tries to call an external function of the WormholeRelayer that is only intended to be
// called by the WormholeRelayer itself (to allow retroactive reverts for atomicity)
error RequesterNotWormholeRelayer();
//When trying to relay a `DeliveryInstruction` to any other chain but the one it was specified for
error TargetChainIsNotThisChain(uint16 targetChain);
//When a `DeliveryOverride` contains a gas limit that's less than the original
error InvalidOverrideGasLimit();
//When a `DeliveryOverride` contains a receiver value that's less than the original
error InvalidOverrideReceiverValue();
//When a `DeliveryOverride` contains a 'refund per unit of gas unused' that's less than the original
error InvalidOverrideRefundPerGasUnused();
//When the delivery provider doesn't pass in sufficient funds (i.e. msg.value does not cover the
// maximum possible refund to the user)
error InsufficientRelayerFunds(uint256 msgValue, uint256 minimum);
//When a bytes32 field can't be converted into a 20 byte EVM address, because the 12 padding bytes
// are non-zero (duplicated from Utils.sol)
error NotAnEvmAddress(bytes32);
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.18;
interface IXERC20 {
/**
* @notice Emits when a lockbox is set
*
* @param _lockbox The address of the lockbox
*/
event LockboxSet(address _lockbox);
/**
* @notice Emits when a limit is set
*
* @param _mintingLimit The updated minting limit we are setting to the bridge
* @param _burningLimit The updated burning limit we are setting to the bridge
* @param _bridge The address of the bridge we are setting the limit too
*/
event BridgeLimitsSet(
uint256 _mintingLimit,
uint256 _burningLimit,
address indexed _bridge
);
/**
* @notice Reverts when a user with too low of a limit tries to call mint/burn
*/
error IXERC20_NotHighEnoughLimits();
/**
* @notice Reverts when caller is not the factory
*/
error IXERC20_NotFactory();
struct Bridge {
BridgeParameters minterParams;
BridgeParameters burnerParams;
}
struct BridgeParameters {
uint256 timestamp;
uint256 ratePerSecond;
uint256 maxLimit;
uint256 currentLimit;
}
/**
* @notice Sets the lockbox address
*
* @param _lockbox The address of the lockbox
*/
function setLockbox(address _lockbox) external;
/**
* @notice Updates the limits of any bridge
* @dev Can only be called by the owner
* @param _mintingLimit The updated minting limit we are setting to the bridge
* @param _burningLimit The updated burning limit we are setting to the bridge
* @param _bridge The address of the bridge we are setting the limits too
*/
function setLimits(
address _bridge,
uint256 _mintingLimit,
uint256 _burningLimit
) external;
/**
* @notice Returns the max limit of a minter
*
* @param _minter The minter we are viewing the limits of
* @return _limit The limit the minter has
*/
function mintingMaxLimitOf(
address _minter
) external view returns (uint256 _limit);
/**
* @notice Returns the max limit of a bridge
*
* @param _bridge the bridge we are viewing the limits of
* @return _limit The limit the bridge has
*/
function burningMaxLimitOf(
address _bridge
) external view returns (uint256 _limit);
/**
* @notice Returns the current limit of a minter
*
* @param _minter The minter we are viewing the limits of
* @return _limit The limit the minter has
*/
function mintingCurrentLimitOf(
address _minter
) external view returns (uint256 _limit);
/**
* @notice Returns the current limit of a bridge
*
* @param _bridge the bridge we are viewing the limits of
* @return _limit The limit the bridge has
*/
function burningCurrentLimitOf(
address _bridge
) external view returns (uint256 _limit);
/**
* @notice Mints tokens for a user
* @dev Can only be called by a minter
* @param _user The address of the user who needs tokens minted
* @param _amount The amount of tokens being minted
*/
function mint(address _user, uint256 _amount) external;
/**
* @notice Burns tokens for a user
* @dev Can only be called by a minter
* @param _user The address of the user who needs tokens burned
* @param _amount The amount of tokens being burned
*/
function burn(address _user, uint256 _amount) external;
}
/**
* An optional extension to IXERC20 that the GlacisTokenMediator will query for.
* It allows developers to have XERC20 tokens that have different addresses on
* different chains.
*/
interface IXERC20GlacisExtension {
/**
* @notice Returns a token variant for a specific chainId if it exists.
*
* @param chainId The chainId of the token variant.
*/
function getTokenVariant(uint256 chainId) external view returns (bytes32);
/**
* @notice Sets a token variant for a specific chainId.
*
* @param chainId The chainId of the token variant.
* @param variant The address of the token variant.
*/
function setTokenVariant(uint256 chainId, bytes32 variant) external;
}
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.18;
interface IXERC20Lockbox {
/**
* @notice Emitted when tokens are deposited into the lockbox
*/
event Deposit(address _sender, uint256 _amount);
/**
* @notice Emitted when tokens are withdrawn from the lockbox
*/
event Withdraw(address _sender, uint256 _amount);
/**
* @notice Reverts when a user tries to deposit native tokens on a non-native lockbox
*/
error IXERC20Lockbox_NotNative();
/**
* @notice Reverts when a user tries to deposit non-native tokens on a native lockbox
*/
error IXERC20Lockbox_Native();
/**
* @notice Reverts when a user tries to withdraw and the call fails
*/
error IXERC20Lockbox_WithdrawFailed();
/**
* @notice Deposit ERC20 tokens into the lockbox
*
* @param _amount The amount of tokens to deposit
*/
function deposit(uint256 _amount) external;
/**
* @notice Deposit ERC20 tokens into the lockbox, and send the XERC20 to a user
*
* @param _user The user to send the XERC20 to
* @param _amount The amount of tokens to deposit
*/
function depositTo(address _user, uint256 _amount) external;
/**
* @notice Deposit the native asset into the lockbox, and send the XERC20 to a user
*
* @param _user The user to send the XERC20 to
*/
function depositNativeTo(address _user) external payable;
/**
* @notice Withdraw ERC20 tokens from the lockbox
*
* @param _amount The amount of tokens to withdraw
*/
function withdraw(uint256 _amount) external;
/**
* @notice Withdraw ERC20 tokens from the lockbox
*
* @param _user The user to withdraw to
* @param _amount The amount of tokens to withdraw
*/
function withdrawTo(address _user, uint256 _amount) external;
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/math/Math.sol)
pragma solidity ^0.8.0;
/**
* @dev Standard math utilities missing in the Solidity language.
*/
library Math {
enum Rounding {
Down, // Toward negative infinity
Up, // Toward infinity
Zero // Toward zero
}
/**
* @dev Returns the largest of two numbers.
*/
function max(uint256 a, uint256 b) internal pure returns (uint256) {
return a > b ? a : b;
}
/**
* @dev Returns the smallest of two numbers.
*/
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
/**
* @dev Returns the average of two numbers. The result is rounded towards
* zero.
*/
function average(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b) / 2 can overflow.
return (a & b) + (a ^ b) / 2;
}
/**
* @dev Returns the ceiling of the division of two numbers.
*
* This differs from standard division with `/` in that it rounds up instead
* of rounding down.
*/
function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b - 1) / b can overflow on addition, so we distribute.
return a == 0 ? 0 : (a - 1) / b + 1;
}
/**
* @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
* @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv)
* with further edits by Uniswap Labs also under MIT license.
*/
function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
unchecked {
// 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
// use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
// variables such that product = prod1 * 2^256 + prod0.
uint256 prod0; // Least significant 256 bits of the product
uint256 prod1; // Most significant 256 bits of the product
assembly {
let mm := mulmod(x, y, not(0))
prod0 := mul(x, y)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
// Handle non-overflow cases, 256 by 256 division.
if (prod1 == 0) {
// Solidity will revert if denominator == 0, unlike the div opcode on its own.
// The surrounding unchecked block does not change this fact.
// See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.
return prod0 / denominator;
}
// Make sure the result is less than 2^256. Also prevents denominator == 0.
require(denominator > prod1, "Math: mulDiv overflow");
///////////////////////////////////////////////
// 512 by 256 division.
///////////////////////////////////////////////
// Make division exact by subtracting the remainder from [prod1 prod0].
uint256 remainder;
assembly {
// Compute remainder using mulmod.
remainder := mulmod(x, y, denominator)
// Subtract 256 bit number from 512 bit number.
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
// Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1.
// See https://cs.stackexchange.com/q/138556/92363.
// Does not overflow because the denominator cannot be zero at this stage in the function.
uint256 twos = denominator & (~denominator + 1);
assembly {
// Divide denominator by twos.
denominator := div(denominator, twos)
// Divide [prod1 prod0] by twos.
prod0 := div(prod0, twos)
// Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.
twos := add(div(sub(0, twos), twos), 1)
}
// Shift in bits from prod1 into prod0.
prod0 |= prod1 * twos;
// Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
// that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
// four bits. That is, denominator * inv = 1 mod 2^4.
uint256 inverse = (3 * denominator) ^ 2;
// Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also works
// in modular arithmetic, doubling the correct bits in each step.
inverse *= 2 - denominator * inverse; // inverse mod 2^8
inverse *= 2 - denominator * inverse; // inverse mod 2^16
inverse *= 2 - denominator * inverse; // inverse mod 2^32
inverse *= 2 - denominator * inverse; // inverse mod 2^64
inverse *= 2 - denominator * inverse; // inverse mod 2^128
inverse *= 2 - denominator * inverse; // inverse mod 2^256
// Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
// This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is
// less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1
// is no longer required.
result = prod0 * inverse;
return result;
}
}
/**
* @notice Calculates x * y / denominator with full precision, following the selected rounding direction.
*/
function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
uint256 result = mulDiv(x, y, denominator);
if (rounding == Rounding.Up && mulmod(x, y, denominator) > 0) {
result += 1;
}
return result;
}
/**
* @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded down.
*
* Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11).
*/
function sqrt(uint256 a) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
// For our first guess, we get the biggest power of 2 which is smaller than the square root of the target.
//
// We know that the "msb" (most significant bit) of our target number `a` is a power of 2 such that we have
// `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`.
//
// This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)`
// → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))`
// → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)`
//
// Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit.
uint256 result = 1 << (log2(a) >> 1);
// At this point `result` is an estimation with one bit of precision. We know the true value is a uint128,
// since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at
// every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision
// into the expected uint128 result.
unchecked {
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
return min(result, a / result);
}
}
/**
* @notice Calculates sqrt(a), following the selected rounding direction.
*/
function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = sqrt(a);
return result + (rounding == Rounding.Up && result * result < a ? 1 : 0);
}
}
/**
* @dev Return the log in base 2, rounded down, of a positive value.
* Returns 0 if given 0.
*/
function log2(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 128;
}
if (value >> 64 > 0) {
value >>= 64;
result += 64;
}
if (value >> 32 > 0) {
value >>= 32;
result += 32;
}
if (value >> 16 > 0) {
value >>= 16;
result += 16;
}
if (value >> 8 > 0) {
value >>= 8;
result += 8;
}
if (value >> 4 > 0) {
value >>= 4;
result += 4;
}
if (value >> 2 > 0) {
value >>= 2;
result += 2;
}
if (value >> 1 > 0) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 2, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log2(value);
return result + (rounding == Rounding.Up && 1 << result < value ? 1 : 0);
}
}
/**
* @dev Return the log in base 10, rounded down, of a positive value.
* Returns 0 if given 0.
*/
function log10(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >= 10 ** 64) {
value /= 10 ** 64;
result += 64;
}
if (value >= 10 ** 32) {
value /= 10 ** 32;
result += 32;
}
if (value >= 10 ** 16) {
value /= 10 ** 16;
result += 16;
}
if (value >= 10 ** 8) {
value /= 10 ** 8;
result += 8;
}
if (value >= 10 ** 4) {
value /= 10 ** 4;
result += 4;
}
if (value >= 10 ** 2) {
value /= 10 ** 2;
result += 2;
}
if (value >= 10 ** 1) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 10, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log10(value);
return result + (rounding == Rounding.Up && 10 ** result < value ? 1 : 0);
}
}
/**
* @dev Return the log in base 256, rounded down, of a positive value.
* Returns 0 if given 0.
*
* Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
*/
function log256(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 16;
}
if (value >> 64 > 0) {
value >>= 64;
result += 8;
}
if (value >> 32 > 0) {
value >>= 32;
result += 4;
}
if (value >> 16 > 0) {
value >>= 16;
result += 2;
}
if (value >> 8 > 0) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 256, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log256(value);
return result + (rounding == Rounding.Up && 1 << (result << 3) < value ? 1 : 0);
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import { Ownable2Step } from "@openzeppelin/contracts/access/Ownable2Step.sol";
import { IOAppCore, ILayerZeroEndpointV2 } from "@layerzerolabs/oapp-evm/contracts/oapp/interfaces/IOAppCore.sol";
/**
* @title OAppCoreNoPeer
* @dev Abstract contract implementing the IOAppCore interface with basic OApp configurations.
*/
abstract contract OAppCoreNoPeer is IOAppCore, Ownable2Step {
// The LayerZero endpoint associated with the given OApp
ILayerZeroEndpointV2 public immutable endpoint;
/**
* @dev Constructor to initialize the OAppCore with the provided endpoint and delegate.
* @param _endpoint The address of the LOCAL Layer Zero endpoint.
* @param _delegate The delegate capable of making OApp configurations inside of the endpoint.
*
* @dev The delegate typically should be set as the owner of the contract.
*/
constructor(address _endpoint, address _delegate) {
endpoint = ILayerZeroEndpointV2(_endpoint);
if (_delegate == address(0)) revert InvalidDelegate();
endpoint.setDelegate(_delegate);
}
/**
* @notice Sets the delegate address for the OApp.
* @param _delegate The address of the delegate to be set.
*
* @dev Only the owner/admin of the OApp can call this function.
* @dev Provides the ability for a delegate to set configs, on behalf of the OApp, directly on the Endpoint contract.
*/
function setDelegate(address _delegate) public onlyOwner {
endpoint.setDelegate(_delegate);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
// @dev Import the 'MessagingFee' and 'MessagingReceipt' so it's exposed to OApp implementers
// solhint-disable-next-line no-unused-import
// @dev Import the 'Origin' so it's exposed to OApp implementers
// solhint-disable-next-line no-unused-import
import { OAppReceiverNoPeer, Origin } from "./OAppReceiverNoPeer.sol";
import { OAppCoreNoPeer } from "./OAppCoreNoPeer.sol";
/**
* @title OAppNoPeer
* @dev Abstract contract serving as the base for OApp implementation, combining OAppSender and OAppReceiver functionality.
*/
abstract contract OAppNoPeer is OAppReceiverNoPeer {
/**
* @dev Constructor to initialize the OApp with the provided endpoint and owner.
* @param _endpoint The address of the LOCAL LayerZero endpoint.
* @param _delegate The delegate capable of making OApp configurations inside of the endpoint.
*/
constructor(address _endpoint, address _delegate) OAppCoreNoPeer(_endpoint, _delegate) {}
/**
* @notice Retrieves the OApp version information.
* @return senderVersion The version of the OAppSender.sol implementation.
* @return receiverVersion The version of the OAppReceiver.sol implementation.
*/
function oAppVersion()
public
pure
virtual
override(OAppReceiverNoPeer)
returns (uint64 senderVersion, uint64 receiverVersion)
{
return (0, RECEIVER_VERSION);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import { IOAppReceiver, Origin } from "@layerzerolabs/oapp-evm/contracts/oapp/interfaces/IOAppReceiver.sol";
import { OAppCoreNoPeer } from "./OAppCoreNoPeer.sol";
/**
* @title OAppReceiverNoPeer
* @dev Abstract contract implementing the ILayerZeroReceiver interface and extending OAppCore for OApp receivers.
*/
abstract contract OAppReceiverNoPeer is IOAppReceiver, OAppCoreNoPeer {
// Custom error message for when the caller is not the registered endpoint/
error OnlyEndpoint(address addr);
// @dev The version of the OAppReceiver implementation.
// @dev Version is bumped when changes are made to this contract.
uint64 internal constant RECEIVER_VERSION = 2;
/**
* @notice Retrieves the OApp version information.
* @return senderVersion The version of the OAppSender.sol contract.
* @return receiverVersion The version of the OAppReceiver.sol contract.
*
* @dev Providing 0 as the default for OAppSender version. Indicates that the OAppSender is not implemented.
* ie. this is a RECEIVE only OApp.
* @dev If the OApp uses both OAppSender and OAppReceiver, then this needs to be override returning the correct versions.
*/
function oAppVersion() public view virtual returns (uint64 senderVersion, uint64 receiverVersion) {
return (0, RECEIVER_VERSION);
}
/**
* @notice Indicates whether an address is an approved composeMsg sender to the Endpoint.
* @dev _origin The origin information containing the source endpoint and sender address.
* - srcEid: The source chain endpoint ID.
* - sender: The sender address on the src chain.
* - nonce: The nonce of the message.
* @dev _message The lzReceive payload.
* @param _sender The sender address.
* @return isSender Is a valid sender.
*
* @dev Applications can optionally choose to implement separate composeMsg senders that are NOT the bridging layer.
* @dev The default sender IS the OAppReceiver implementer.
*/
function isComposeMsgSender(
Origin calldata /*_origin*/,
bytes calldata /*_message*/,
address _sender
) public view virtual returns (bool) {
return _sender == address(this);
}
/**
* @notice Checks if the path initialization is allowed based on the provided origin.
* @return Whether the path has been initialized.
*
* @dev This indicates to the endpoint that the OApp has enabled msgs for this particular path to be received.
* @dev This defaults to assuming if a peer has been set, its initialized.
* Can be overridden by the OApp if there is other logic to determine this.
*/
function allowInitializePath(Origin calldata) public view virtual returns (bool) {
return false;
}
/**
* @notice Retrieves the next nonce for a given source endpoint and sender address.
* @dev _srcEid The source endpoint ID.
* @dev _sender The sender address.
* @return nonce The next nonce.
*
* @dev The path nonce starts from 1. If 0 is returned it means that there is NO nonce ordered enforcement.
* @dev Is required by the off-chain executor to determine the OApp expects msg execution is ordered.
* @dev This is also enforced by the OApp.
* @dev By default this is NOT enabled. ie. nextNonce is hardcoded to return 0.
*/
function nextNonce(uint32 /*_srcEid*/, bytes32 /*_sender*/) public view virtual returns (uint64 nonce) {
return 0;
}
/**
* @dev Entry point for receiving messages or packets from the endpoint.
* @param _origin The origin information containing the source endpoint and sender address.
* - srcEid: The source chain endpoint ID.
* - sender: The sender address on the src chain.
* - nonce: The nonce of the message.
* @param _guid The unique identifier for the received LayerZero message.
* @param _message The payload of the received message.
* @param _executor The address of the executor for the received message.
* @param _extraData Additional arbitrary data provided by the corresponding executor.
*
* @dev Entry point for receiving msg/packet from the LayerZero endpoint.
*/
function lzReceive(
Origin calldata _origin,
bytes32 _guid,
bytes calldata _message,
address _executor,
bytes calldata _extraData
) public payable virtual {
// Ensures that only the endpoint can attempt to lzReceive() messages to this OApp.
if (address(endpoint) != msg.sender) revert OnlyEndpoint(msg.sender);
// Peer was removed in lieu of GlacisCounterpartManager
// Call the internal OApp implementation of lzReceive.
_lzReceive(_origin, _guid, _message, _executor, _extraData);
}
/**
* @dev Internal function to implement lzReceive logic without needing to copy the basic parameter validation.
*/
function _lzReceive(
Origin calldata _origin,
bytes32 _guid,
bytes calldata _message,
address _executor,
bytes calldata _extraData
) internal virtual;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import { BytesLib } from "solidity-bytes-utils/contracts/BytesLib.sol";
import { SafeCast } from "@openzeppelin/contracts/utils/math/SafeCast.sol";
import { ExecutorOptions } from "@layerzerolabs/lz-evm-protocol-v2/contracts/messagelib/libs/ExecutorOptions.sol";
import { DVNOptions } from "@layerzerolabs/lz-evm-messagelib-v2/contracts/uln/libs/DVNOptions.sol";
/**
* @title OptionsBuilder
* @dev Library for building and encoding various message options.
*/
library OptionsBuilder {
using SafeCast for uint256;
using BytesLib for bytes;
// Constants for options types
uint16 internal constant TYPE_1 = 1; // legacy options type 1
uint16 internal constant TYPE_2 = 2; // legacy options type 2
uint16 internal constant TYPE_3 = 3;
// Custom error message
error InvalidSize(uint256 max, uint256 actual);
error InvalidOptionType(uint16 optionType);
// Modifier to ensure only options of type 3 are used
modifier onlyType3(bytes memory _options) {
if (_options.toUint16(0) != TYPE_3) revert InvalidOptionType(_options.toUint16(0));
_;
}
/**
* @dev Creates a new options container with type 3.
* @return options The newly created options container.
*/
function newOptions() internal pure returns (bytes memory) {
return abi.encodePacked(TYPE_3);
}
/**
* @dev Adds an executor LZ receive option to the existing options.
* @param _options The existing options container.
* @param _gas The gasLimit used on the lzReceive() function in the OApp.
* @param _value The msg.value passed to the lzReceive() function in the OApp.
* @return options The updated options container.
*
* @dev When multiples of this option are added, they are summed by the executor
* eg. if (_gas: 200k, and _value: 1 ether) AND (_gas: 100k, _value: 0.5 ether) are sent in an option to the LayerZeroEndpoint,
* that becomes (300k, 1.5 ether) when the message is executed on the remote lzReceive() function.
*/
function addExecutorLzReceiveOption(
bytes memory _options,
uint128 _gas,
uint128 _value
) internal pure onlyType3(_options) returns (bytes memory) {
bytes memory option = ExecutorOptions.encodeLzReceiveOption(_gas, _value);
return addExecutorOption(_options, ExecutorOptions.OPTION_TYPE_LZRECEIVE, option);
}
/**
* @dev Adds an executor native drop option to the existing options.
* @param _options The existing options container.
* @param _amount The amount for the native value that is airdropped to the 'receiver'.
* @param _receiver The receiver address for the native drop option.
* @return options The updated options container.
*
* @dev When multiples of this option are added, they are summed by the executor on the remote chain.
*/
function addExecutorNativeDropOption(
bytes memory _options,
uint128 _amount,
bytes32 _receiver
) internal pure onlyType3(_options) returns (bytes memory) {
bytes memory option = ExecutorOptions.encodeNativeDropOption(_amount, _receiver);
return addExecutorOption(_options, ExecutorOptions.OPTION_TYPE_NATIVE_DROP, option);
}
/**
* @dev Adds an executor LZ compose option to the existing options.
* @param _options The existing options container.
* @param _index The index for the lzCompose() function call.
* @param _gas The gasLimit for the lzCompose() function call.
* @param _value The msg.value for the lzCompose() function call.
* @return options The updated options container.
*
* @dev When multiples of this option are added, they are summed PER index by the executor on the remote chain.
* @dev If the OApp sends N lzCompose calls on the remote, you must provide N incremented indexes starting with 0.
* ie. When your remote OApp composes (N = 3) messages, you must set this option for index 0,1,2
*/
function addExecutorLzComposeOption(
bytes memory _options,
uint16 _index,
uint128 _gas,
uint128 _value
) internal pure onlyType3(_options) returns (bytes memory) {
bytes memory option = ExecutorOptions.encodeLzComposeOption(_index, _gas, _value);
return addExecutorOption(_options, ExecutorOptions.OPTION_TYPE_LZCOMPOSE, option);
}
/**
* @dev Adds an executor ordered execution option to the existing options.
* @param _options The existing options container.
* @return options The updated options container.
*/
function addExecutorOrderedExecutionOption(
bytes memory _options
) internal pure onlyType3(_options) returns (bytes memory) {
return addExecutorOption(_options, ExecutorOptions.OPTION_TYPE_ORDERED_EXECUTION, bytes(""));
}
/**
* @dev Adds a DVN pre-crime option to the existing options.
* @param _options The existing options container.
* @param _dvnIdx The DVN index for the pre-crime option.
* @return options The updated options container.
*/
function addDVNPreCrimeOption(
bytes memory _options,
uint8 _dvnIdx
) internal pure onlyType3(_options) returns (bytes memory) {
return addDVNOption(_options, _dvnIdx, DVNOptions.OPTION_TYPE_PRECRIME, bytes(""));
}
/**
* @dev Adds an executor option to the existing options.
* @param _options The existing options container.
* @param _optionType The type of the executor option.
* @param _option The encoded data for the executor option.
* @return options The updated options container.
*/
function addExecutorOption(
bytes memory _options,
uint8 _optionType,
bytes memory _option
) internal pure onlyType3(_options) returns (bytes memory) {
return
abi.encodePacked(
_options,
ExecutorOptions.WORKER_ID,
_option.length.toUint16() + 1, // +1 for optionType
_optionType,
_option
);
}
/**
* @dev Adds a DVN option to the existing options.
* @param _options The existing options container.
* @param _dvnIdx The DVN index for the DVN option.
* @param _optionType The type of the DVN option.
* @param _option The encoded data for the DVN option.
* @return options The updated options container.
*/
function addDVNOption(
bytes memory _options,
uint8 _dvnIdx,
uint8 _optionType,
bytes memory _option
) internal pure onlyType3(_options) returns (bytes memory) {
return
abi.encodePacked(
_options,
DVNOptions.WORKER_ID,
_option.length.toUint16() + 2, // +2 for optionType and dvnIdx
_dvnIdx,
_optionType,
_option
);
}
/**
* @dev Encodes legacy options of type 1.
* @param _executionGas The gasLimit value passed to lzReceive().
* @return legacyOptions The encoded legacy options.
*/
function encodeLegacyOptionsType1(uint256 _executionGas) internal pure returns (bytes memory) {
if (_executionGas > type(uint128).max) revert InvalidSize(type(uint128).max, _executionGas);
return abi.encodePacked(TYPE_1, _executionGas);
}
/**
* @dev Encodes legacy options of type 2.
* @param _executionGas The gasLimit value passed to lzReceive().
* @param _nativeForDst The amount of native air dropped to the receiver.
* @param _receiver The _nativeForDst receiver address.
* @return legacyOptions The encoded legacy options of type 2.
*/
function encodeLegacyOptionsType2(
uint256 _executionGas,
uint256 _nativeForDst,
bytes memory _receiver // @dev Use bytes instead of bytes32 in legacy type 2 for _receiver.
) internal pure returns (bytes memory) {
if (_executionGas > type(uint128).max) revert InvalidSize(type(uint128).max, _executionGas);
if (_nativeForDst > type(uint128).max) revert InvalidSize(type(uint128).max, _nativeForDst);
if (_receiver.length > 32) revert InvalidSize(32, _receiver.length);
return abi.encodePacked(TYPE_2, _executionGas, _nativeForDst, _receiver);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (access/Ownable.sol)
pragma solidity ^0.8.0;
import "../utils/Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor() {
_transferOwnership(_msgSender());
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (access/Ownable2Step.sol)
pragma solidity ^0.8.0;
import "./Ownable.sol";
/**
* @dev Contract module which provides access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership} and {acceptOwnership}.
*
* This module is used through inheritance. It will make available all functions
* from parent (Ownable).
*/
abstract contract Ownable2Step is Ownable {
address private _pendingOwner;
event OwnershipTransferStarted(address indexed previousOwner, address indexed newOwner);
/**
* @dev Returns the address of the pending owner.
*/
function pendingOwner() public view virtual returns (address) {
return _pendingOwner;
}
/**
* @dev Starts the ownership transfer of the contract to a new account. Replaces the pending transfer if there is one.
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual override onlyOwner {
_pendingOwner = newOwner;
emit OwnershipTransferStarted(owner(), newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`) and deletes any pending owner.
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual override {
delete _pendingOwner;
super._transferOwnership(newOwner);
}
/**
* @dev The new owner accepts the ownership transfer.
*/
function acceptOwnership() public virtual {
address sender = _msgSender();
require(pendingOwner() == sender, "Ownable2Step: caller is not the new owner");
_transferOwnership(sender);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/math/SafeCast.sol)
// This file was procedurally generated from scripts/generate/templates/SafeCast.js.
pragma solidity ^0.8.0;
/**
* @dev Wrappers over Solidity's uintXX/intXX casting operators with added overflow
* checks.
*
* Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
* easily result in undesired exploitation or bugs, since developers usually
* assume that overflows raise errors. `SafeCast` restores this intuition by
* reverting the transaction when such an operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*
* Can be combined with {SafeMath} and {SignedSafeMath} to extend it to smaller types, by performing
* all math on `uint256` and `int256` and then downcasting.
*/
library SafeCast {
/**
* @dev Returns the downcasted uint248 from uint256, reverting on
* overflow (when the input is greater than largest uint248).
*
* Counterpart to Solidity's `uint248` operator.
*
* Requirements:
*
* - input must fit into 248 bits
*
* _Available since v4.7._
*/
function toUint248(uint256 value) internal pure returns (uint248) {
require(value <= type(uint248).max, "SafeCast: value doesn't fit in 248 bits");
return uint248(value);
}
/**
* @dev Returns the downcasted uint240 from uint256, reverting on
* overflow (when the input is greater than largest uint240).
*
* Counterpart to Solidity's `uint240` operator.
*
* Requirements:
*
* - input must fit into 240 bits
*
* _Available since v4.7._
*/
function toUint240(uint256 value) internal pure returns (uint240) {
require(value <= type(uint240).max, "SafeCast: value doesn't fit in 240 bits");
return uint240(value);
}
/**
* @dev Returns the downcasted uint232 from uint256, reverting on
* overflow (when the input is greater than largest uint232).
*
* Counterpart to Solidity's `uint232` operator.
*
* Requirements:
*
* - input must fit into 232 bits
*
* _Available since v4.7._
*/
function toUint232(uint256 value) internal pure returns (uint232) {
require(value <= type(uint232).max, "SafeCast: value doesn't fit in 232 bits");
return uint232(value);
}
/**
* @dev Returns the downcasted uint224 from uint256, reverting on
* overflow (when the input is greater than largest uint224).
*
* Counterpart to Solidity's `uint224` operator.
*
* Requirements:
*
* - input must fit into 224 bits
*
* _Available since v4.2._
*/
function toUint224(uint256 value) internal pure returns (uint224) {
require(value <= type(uint224).max, "SafeCast: value doesn't fit in 224 bits");
return uint224(value);
}
/**
* @dev Returns the downcasted uint216 from uint256, reverting on
* overflow (when the input is greater than largest uint216).
*
* Counterpart to Solidity's `uint216` operator.
*
* Requirements:
*
* - input must fit into 216 bits
*
* _Available since v4.7._
*/
function toUint216(uint256 value) internal pure returns (uint216) {
require(value <= type(uint216).max, "SafeCast: value doesn't fit in 216 bits");
return uint216(value);
}
/**
* @dev Returns the downcasted uint208 from uint256, reverting on
* overflow (when the input is greater than largest uint208).
*
* Counterpart to Solidity's `uint208` operator.
*
* Requirements:
*
* - input must fit into 208 bits
*
* _Available since v4.7._
*/
function toUint208(uint256 value) internal pure returns (uint208) {
require(value <= type(uint208).max, "SafeCast: value doesn't fit in 208 bits");
return uint208(value);
}
/**
* @dev Returns the downcasted uint200 from uint256, reverting on
* overflow (when the input is greater than largest uint200).
*
* Counterpart to Solidity's `uint200` operator.
*
* Requirements:
*
* - input must fit into 200 bits
*
* _Available since v4.7._
*/
function toUint200(uint256 value) internal pure returns (uint200) {
require(value <= type(uint200).max, "SafeCast: value doesn't fit in 200 bits");
return uint200(value);
}
/**
* @dev Returns the downcasted uint192 from uint256, reverting on
* overflow (when the input is greater than largest uint192).
*
* Counterpart to Solidity's `uint192` operator.
*
* Requirements:
*
* - input must fit into 192 bits
*
* _Available since v4.7._
*/
function toUint192(uint256 value) internal pure returns (uint192) {
require(value <= type(uint192).max, "SafeCast: value doesn't fit in 192 bits");
return uint192(value);
}
/**
* @dev Returns the downcasted uint184 from uint256, reverting on
* overflow (when the input is greater than largest uint184).
*
* Counterpart to Solidity's `uint184` operator.
*
* Requirements:
*
* - input must fit into 184 bits
*
* _Available since v4.7._
*/
function toUint184(uint256 value) internal pure returns (uint184) {
require(value <= type(uint184).max, "SafeCast: value doesn't fit in 184 bits");
return uint184(value);
}
/**
* @dev Returns the downcasted uint176 from uint256, reverting on
* overflow (when the input is greater than largest uint176).
*
* Counterpart to Solidity's `uint176` operator.
*
* Requirements:
*
* - input must fit into 176 bits
*
* _Available since v4.7._
*/
function toUint176(uint256 value) internal pure returns (uint176) {
require(value <= type(uint176).max, "SafeCast: value doesn't fit in 176 bits");
return uint176(value);
}
/**
* @dev Returns the downcasted uint168 from uint256, reverting on
* overflow (when the input is greater than largest uint168).
*
* Counterpart to Solidity's `uint168` operator.
*
* Requirements:
*
* - input must fit into 168 bits
*
* _Available since v4.7._
*/
function toUint168(uint256 value) internal pure returns (uint168) {
require(value <= type(uint168).max, "SafeCast: value doesn't fit in 168 bits");
return uint168(value);
}
/**
* @dev Returns the downcasted uint160 from uint256, reverting on
* overflow (when the input is greater than largest uint160).
*
* Counterpart to Solidity's `uint160` operator.
*
* Requirements:
*
* - input must fit into 160 bits
*
* _Available since v4.7._
*/
function toUint160(uint256 value) internal pure returns (uint160) {
require(value <= type(uint160).max, "SafeCast: value doesn't fit in 160 bits");
return uint160(value);
}
/**
* @dev Returns the downcasted uint152 from uint256, reverting on
* overflow (when the input is greater than largest uint152).
*
* Counterpart to Solidity's `uint152` operator.
*
* Requirements:
*
* - input must fit into 152 bits
*
* _Available since v4.7._
*/
function toUint152(uint256 value) internal pure returns (uint152) {
require(value <= type(uint152).max, "SafeCast: value doesn't fit in 152 bits");
return uint152(value);
}
/**
* @dev Returns the downcasted uint144 from uint256, reverting on
* overflow (when the input is greater than largest uint144).
*
* Counterpart to Solidity's `uint144` operator.
*
* Requirements:
*
* - input must fit into 144 bits
*
* _Available since v4.7._
*/
function toUint144(uint256 value) internal pure returns (uint144) {
require(value <= type(uint144).max, "SafeCast: value doesn't fit in 144 bits");
return uint144(value);
}
/**
* @dev Returns the downcasted uint136 from uint256, reverting on
* overflow (when the input is greater than largest uint136).
*
* Counterpart to Solidity's `uint136` operator.
*
* Requirements:
*
* - input must fit into 136 bits
*
* _Available since v4.7._
*/
function toUint136(uint256 value) internal pure returns (uint136) {
require(value <= type(uint136).max, "SafeCast: value doesn't fit in 136 bits");
return uint136(value);
}
/**
* @dev Returns the downcasted uint128 from uint256, reverting on
* overflow (when the input is greater than largest uint128).
*
* Counterpart to Solidity's `uint128` operator.
*
* Requirements:
*
* - input must fit into 128 bits
*
* _Available since v2.5._
*/
function toUint128(uint256 value) internal pure returns (uint128) {
require(value <= type(uint128).max, "SafeCast: value doesn't fit in 128 bits");
return uint128(value);
}
/**
* @dev Returns the downcasted uint120 from uint256, reverting on
* overflow (when the input is greater than largest uint120).
*
* Counterpart to Solidity's `uint120` operator.
*
* Requirements:
*
* - input must fit into 120 bits
*
* _Available since v4.7._
*/
function toUint120(uint256 value) internal pure returns (uint120) {
require(value <= type(uint120).max, "SafeCast: value doesn't fit in 120 bits");
return uint120(value);
}
/**
* @dev Returns the downcasted uint112 from uint256, reverting on
* overflow (when the input is greater than largest uint112).
*
* Counterpart to Solidity's `uint112` operator.
*
* Requirements:
*
* - input must fit into 112 bits
*
* _Available since v4.7._
*/
function toUint112(uint256 value) internal pure returns (uint112) {
require(value <= type(uint112).max, "SafeCast: value doesn't fit in 112 bits");
return uint112(value);
}
/**
* @dev Returns the downcasted uint104 from uint256, reverting on
* overflow (when the input is greater than largest uint104).
*
* Counterpart to Solidity's `uint104` operator.
*
* Requirements:
*
* - input must fit into 104 bits
*
* _Available since v4.7._
*/
function toUint104(uint256 value) internal pure returns (uint104) {
require(value <= type(uint104).max, "SafeCast: value doesn't fit in 104 bits");
return uint104(value);
}
/**
* @dev Returns the downcasted uint96 from uint256, reverting on
* overflow (when the input is greater than largest uint96).
*
* Counterpart to Solidity's `uint96` operator.
*
* Requirements:
*
* - input must fit into 96 bits
*
* _Available since v4.2._
*/
function toUint96(uint256 value) internal pure returns (uint96) {
require(value <= type(uint96).max, "SafeCast: value doesn't fit in 96 bits");
return uint96(value);
}
/**
* @dev Returns the downcasted uint88 from uint256, reverting on
* overflow (when the input is greater than largest uint88).
*
* Counterpart to Solidity's `uint88` operator.
*
* Requirements:
*
* - input must fit into 88 bits
*
* _Available since v4.7._
*/
function toUint88(uint256 value) internal pure returns (uint88) {
require(value <= type(uint88).max, "SafeCast: value doesn't fit in 88 bits");
return uint88(value);
}
/**
* @dev Returns the downcasted uint80 from uint256, reverting on
* overflow (when the input is greater than largest uint80).
*
* Counterpart to Solidity's `uint80` operator.
*
* Requirements:
*
* - input must fit into 80 bits
*
* _Available since v4.7._
*/
function toUint80(uint256 value) internal pure returns (uint80) {
require(value <= type(uint80).max, "SafeCast: value doesn't fit in 80 bits");
return uint80(value);
}
/**
* @dev Returns the downcasted uint72 from uint256, reverting on
* overflow (when the input is greater than largest uint72).
*
* Counterpart to Solidity's `uint72` operator.
*
* Requirements:
*
* - input must fit into 72 bits
*
* _Available since v4.7._
*/
function toUint72(uint256 value) internal pure returns (uint72) {
require(value <= type(uint72).max, "SafeCast: value doesn't fit in 72 bits");
return uint72(value);
}
/**
* @dev Returns the downcasted uint64 from uint256, reverting on
* overflow (when the input is greater than largest uint64).
*
* Counterpart to Solidity's `uint64` operator.
*
* Requirements:
*
* - input must fit into 64 bits
*
* _Available since v2.5._
*/
function toUint64(uint256 value) internal pure returns (uint64) {
require(value <= type(uint64).max, "SafeCast: value doesn't fit in 64 bits");
return uint64(value);
}
/**
* @dev Returns the downcasted uint56 from uint256, reverting on
* overflow (when the input is greater than largest uint56).
*
* Counterpart to Solidity's `uint56` operator.
*
* Requirements:
*
* - input must fit into 56 bits
*
* _Available since v4.7._
*/
function toUint56(uint256 value) internal pure returns (uint56) {
require(value <= type(uint56).max, "SafeCast: value doesn't fit in 56 bits");
return uint56(value);
}
/**
* @dev Returns the downcasted uint48 from uint256, reverting on
* overflow (when the input is greater than largest uint48).
*
* Counterpart to Solidity's `uint48` operator.
*
* Requirements:
*
* - input must fit into 48 bits
*
* _Available since v4.7._
*/
function toUint48(uint256 value) internal pure returns (uint48) {
require(value <= type(uint48).max, "SafeCast: value doesn't fit in 48 bits");
return uint48(value);
}
/**
* @dev Returns the downcasted uint40 from uint256, reverting on
* overflow (when the input is greater than largest uint40).
*
* Counterpart to Solidity's `uint40` operator.
*
* Requirements:
*
* - input must fit into 40 bits
*
* _Available since v4.7._
*/
function toUint40(uint256 value) internal pure returns (uint40) {
require(value <= type(uint40).max, "SafeCast: value doesn't fit in 40 bits");
return uint40(value);
}
/**
* @dev Returns the downcasted uint32 from uint256, reverting on
* overflow (when the input is greater than largest uint32).
*
* Counterpart to Solidity's `uint32` operator.
*
* Requirements:
*
* - input must fit into 32 bits
*
* _Available since v2.5._
*/
function toUint32(uint256 value) internal pure returns (uint32) {
require(value <= type(uint32).max, "SafeCast: value doesn't fit in 32 bits");
return uint32(value);
}
/**
* @dev Returns the downcasted uint24 from uint256, reverting on
* overflow (when the input is greater than largest uint24).
*
* Counterpart to Solidity's `uint24` operator.
*
* Requirements:
*
* - input must fit into 24 bits
*
* _Available since v4.7._
*/
function toUint24(uint256 value) internal pure returns (uint24) {
require(value <= type(uint24).max, "SafeCast: value doesn't fit in 24 bits");
return uint24(value);
}
/**
* @dev Returns the downcasted uint16 from uint256, reverting on
* overflow (when the input is greater than largest uint16).
*
* Counterpart to Solidity's `uint16` operator.
*
* Requirements:
*
* - input must fit into 16 bits
*
* _Available since v2.5._
*/
function toUint16(uint256 value) internal pure returns (uint16) {
require(value <= type(uint16).max, "SafeCast: value doesn't fit in 16 bits");
return uint16(value);
}
/**
* @dev Returns the downcasted uint8 from uint256, reverting on
* overflow (when the input is greater than largest uint8).
*
* Counterpart to Solidity's `uint8` operator.
*
* Requirements:
*
* - input must fit into 8 bits
*
* _Available since v2.5._
*/
function toUint8(uint256 value) internal pure returns (uint8) {
require(value <= type(uint8).max, "SafeCast: value doesn't fit in 8 bits");
return uint8(value);
}
/**
* @dev Converts a signed int256 into an unsigned uint256.
*
* Requirements:
*
* - input must be greater than or equal to 0.
*
* _Available since v3.0._
*/
function toUint256(int256 value) internal pure returns (uint256) {
require(value >= 0, "SafeCast: value must be positive");
return uint256(value);
}
/**
* @dev Returns the downcasted int248 from int256, reverting on
* overflow (when the input is less than smallest int248 or
* greater than largest int248).
*
* Counterpart to Solidity's `int248` operator.
*
* Requirements:
*
* - input must fit into 248 bits
*
* _Available since v4.7._
*/
function toInt248(int256 value) internal pure returns (int248 downcasted) {
downcasted = int248(value);
require(downcasted == value, "SafeCast: value doesn't fit in 248 bits");
}
/**
* @dev Returns the downcasted int240 from int256, reverting on
* overflow (when the input is less than smallest int240 or
* greater than largest int240).
*
* Counterpart to Solidity's `int240` operator.
*
* Requirements:
*
* - input must fit into 240 bits
*
* _Available since v4.7._
*/
function toInt240(int256 value) internal pure returns (int240 downcasted) {
downcasted = int240(value);
require(downcasted == value, "SafeCast: value doesn't fit in 240 bits");
}
/**
* @dev Returns the downcasted int232 from int256, reverting on
* overflow (when the input is less than smallest int232 or
* greater than largest int232).
*
* Counterpart to Solidity's `int232` operator.
*
* Requirements:
*
* - input must fit into 232 bits
*
* _Available since v4.7._
*/
function toInt232(int256 value) internal pure returns (int232 downcasted) {
downcasted = int232(value);
require(downcasted == value, "SafeCast: value doesn't fit in 232 bits");
}
/**
* @dev Returns the downcasted int224 from int256, reverting on
* overflow (when the input is less than smallest int224 or
* greater than largest int224).
*
* Counterpart to Solidity's `int224` operator.
*
* Requirements:
*
* - input must fit into 224 bits
*
* _Available since v4.7._
*/
function toInt224(int256 value) internal pure returns (int224 downcasted) {
downcasted = int224(value);
require(downcasted == value, "SafeCast: value doesn't fit in 224 bits");
}
/**
* @dev Returns the downcasted int216 from int256, reverting on
* overflow (when the input is less than smallest int216 or
* greater than largest int216).
*
* Counterpart to Solidity's `int216` operator.
*
* Requirements:
*
* - input must fit into 216 bits
*
* _Available since v4.7._
*/
function toInt216(int256 value) internal pure returns (int216 downcasted) {
downcasted = int216(value);
require(downcasted == value, "SafeCast: value doesn't fit in 216 bits");
}
/**
* @dev Returns the downcasted int208 from int256, reverting on
* overflow (when the input is less than smallest int208 or
* greater than largest int208).
*
* Counterpart to Solidity's `int208` operator.
*
* Requirements:
*
* - input must fit into 208 bits
*
* _Available since v4.7._
*/
function toInt208(int256 value) internal pure returns (int208 downcasted) {
downcasted = int208(value);
require(downcasted == value, "SafeCast: value doesn't fit in 208 bits");
}
/**
* @dev Returns the downcasted int200 from int256, reverting on
* overflow (when the input is less than smallest int200 or
* greater than largest int200).
*
* Counterpart to Solidity's `int200` operator.
*
* Requirements:
*
* - input must fit into 200 bits
*
* _Available since v4.7._
*/
function toInt200(int256 value) internal pure returns (int200 downcasted) {
downcasted = int200(value);
require(downcasted == value, "SafeCast: value doesn't fit in 200 bits");
}
/**
* @dev Returns the downcasted int192 from int256, reverting on
* overflow (when the input is less than smallest int192 or
* greater than largest int192).
*
* Counterpart to Solidity's `int192` operator.
*
* Requirements:
*
* - input must fit into 192 bits
*
* _Available since v4.7._
*/
function toInt192(int256 value) internal pure returns (int192 downcasted) {
downcasted = int192(value);
require(downcasted == value, "SafeCast: value doesn't fit in 192 bits");
}
/**
* @dev Returns the downcasted int184 from int256, reverting on
* overflow (when the input is less than smallest int184 or
* greater than largest int184).
*
* Counterpart to Solidity's `int184` operator.
*
* Requirements:
*
* - input must fit into 184 bits
*
* _Available since v4.7._
*/
function toInt184(int256 value) internal pure returns (int184 downcasted) {
downcasted = int184(value);
require(downcasted == value, "SafeCast: value doesn't fit in 184 bits");
}
/**
* @dev Returns the downcasted int176 from int256, reverting on
* overflow (when the input is less than smallest int176 or
* greater than largest int176).
*
* Counterpart to Solidity's `int176` operator.
*
* Requirements:
*
* - input must fit into 176 bits
*
* _Available since v4.7._
*/
function toInt176(int256 value) internal pure returns (int176 downcasted) {
downcasted = int176(value);
require(downcasted == value, "SafeCast: value doesn't fit in 176 bits");
}
/**
* @dev Returns the downcasted int168 from int256, reverting on
* overflow (when the input is less than smallest int168 or
* greater than largest int168).
*
* Counterpart to Solidity's `int168` operator.
*
* Requirements:
*
* - input must fit into 168 bits
*
* _Available since v4.7._
*/
function toInt168(int256 value) internal pure returns (int168 downcasted) {
downcasted = int168(value);
require(downcasted == value, "SafeCast: value doesn't fit in 168 bits");
}
/**
* @dev Returns the downcasted int160 from int256, reverting on
* overflow (when the input is less than smallest int160 or
* greater than largest int160).
*
* Counterpart to Solidity's `int160` operator.
*
* Requirements:
*
* - input must fit into 160 bits
*
* _Available since v4.7._
*/
function toInt160(int256 value) internal pure returns (int160 downcasted) {
downcasted = int160(value);
require(downcasted == value, "SafeCast: value doesn't fit in 160 bits");
}
/**
* @dev Returns the downcasted int152 from int256, reverting on
* overflow (when the input is less than smallest int152 or
* greater than largest int152).
*
* Counterpart to Solidity's `int152` operator.
*
* Requirements:
*
* - input must fit into 152 bits
*
* _Available since v4.7._
*/
function toInt152(int256 value) internal pure returns (int152 downcasted) {
downcasted = int152(value);
require(downcasted == value, "SafeCast: value doesn't fit in 152 bits");
}
/**
* @dev Returns the downcasted int144 from int256, reverting on
* overflow (when the input is less than smallest int144 or
* greater than largest int144).
*
* Counterpart to Solidity's `int144` operator.
*
* Requirements:
*
* - input must fit into 144 bits
*
* _Available since v4.7._
*/
function toInt144(int256 value) internal pure returns (int144 downcasted) {
downcasted = int144(value);
require(downcasted == value, "SafeCast: value doesn't fit in 144 bits");
}
/**
* @dev Returns the downcasted int136 from int256, reverting on
* overflow (when the input is less than smallest int136 or
* greater than largest int136).
*
* Counterpart to Solidity's `int136` operator.
*
* Requirements:
*
* - input must fit into 136 bits
*
* _Available since v4.7._
*/
function toInt136(int256 value) internal pure returns (int136 downcasted) {
downcasted = int136(value);
require(downcasted == value, "SafeCast: value doesn't fit in 136 bits");
}
/**
* @dev Returns the downcasted int128 from int256, reverting on
* overflow (when the input is less than smallest int128 or
* greater than largest int128).
*
* Counterpart to Solidity's `int128` operator.
*
* Requirements:
*
* - input must fit into 128 bits
*
* _Available since v3.1._
*/
function toInt128(int256 value) internal pure returns (int128 downcasted) {
downcasted = int128(value);
require(downcasted == value, "SafeCast: value doesn't fit in 128 bits");
}
/**
* @dev Returns the downcasted int120 from int256, reverting on
* overflow (when the input is less than smallest int120 or
* greater than largest int120).
*
* Counterpart to Solidity's `int120` operator.
*
* Requirements:
*
* - input must fit into 120 bits
*
* _Available since v4.7._
*/
function toInt120(int256 value) internal pure returns (int120 downcasted) {
downcasted = int120(value);
require(downcasted == value, "SafeCast: value doesn't fit in 120 bits");
}
/**
* @dev Returns the downcasted int112 from int256, reverting on
* overflow (when the input is less than smallest int112 or
* greater than largest int112).
*
* Counterpart to Solidity's `int112` operator.
*
* Requirements:
*
* - input must fit into 112 bits
*
* _Available since v4.7._
*/
function toInt112(int256 value) internal pure returns (int112 downcasted) {
downcasted = int112(value);
require(downcasted == value, "SafeCast: value doesn't fit in 112 bits");
}
/**
* @dev Returns the downcasted int104 from int256, reverting on
* overflow (when the input is less than smallest int104 or
* greater than largest int104).
*
* Counterpart to Solidity's `int104` operator.
*
* Requirements:
*
* - input must fit into 104 bits
*
* _Available since v4.7._
*/
function toInt104(int256 value) internal pure returns (int104 downcasted) {
downcasted = int104(value);
require(downcasted == value, "SafeCast: value doesn't fit in 104 bits");
}
/**
* @dev Returns the downcasted int96 from int256, reverting on
* overflow (when the input is less than smallest int96 or
* greater than largest int96).
*
* Counterpart to Solidity's `int96` operator.
*
* Requirements:
*
* - input must fit into 96 bits
*
* _Available since v4.7._
*/
function toInt96(int256 value) internal pure returns (int96 downcasted) {
downcasted = int96(value);
require(downcasted == value, "SafeCast: value doesn't fit in 96 bits");
}
/**
* @dev Returns the downcasted int88 from int256, reverting on
* overflow (when the input is less than smallest int88 or
* greater than largest int88).
*
* Counterpart to Solidity's `int88` operator.
*
* Requirements:
*
* - input must fit into 88 bits
*
* _Available since v4.7._
*/
function toInt88(int256 value) internal pure returns (int88 downcasted) {
downcasted = int88(value);
require(downcasted == value, "SafeCast: value doesn't fit in 88 bits");
}
/**
* @dev Returns the downcasted int80 from int256, reverting on
* overflow (when the input is less than smallest int80 or
* greater than largest int80).
*
* Counterpart to Solidity's `int80` operator.
*
* Requirements:
*
* - input must fit into 80 bits
*
* _Available since v4.7._
*/
function toInt80(int256 value) internal pure returns (int80 downcasted) {
downcasted = int80(value);
require(downcasted == value, "SafeCast: value doesn't fit in 80 bits");
}
/**
* @dev Returns the downcasted int72 from int256, reverting on
* overflow (when the input is less than smallest int72 or
* greater than largest int72).
*
* Counterpart to Solidity's `int72` operator.
*
* Requirements:
*
* - input must fit into 72 bits
*
* _Available since v4.7._
*/
function toInt72(int256 value) internal pure returns (int72 downcasted) {
downcasted = int72(value);
require(downcasted == value, "SafeCast: value doesn't fit in 72 bits");
}
/**
* @dev Returns the downcasted int64 from int256, reverting on
* overflow (when the input is less than smallest int64 or
* greater than largest int64).
*
* Counterpart to Solidity's `int64` operator.
*
* Requirements:
*
* - input must fit into 64 bits
*
* _Available since v3.1._
*/
function toInt64(int256 value) internal pure returns (int64 downcasted) {
downcasted = int64(value);
require(downcasted == value, "SafeCast: value doesn't fit in 64 bits");
}
/**
* @dev Returns the downcasted int56 from int256, reverting on
* overflow (when the input is less than smallest int56 or
* greater than largest int56).
*
* Counterpart to Solidity's `int56` operator.
*
* Requirements:
*
* - input must fit into 56 bits
*
* _Available since v4.7._
*/
function toInt56(int256 value) internal pure returns (int56 downcasted) {
downcasted = int56(value);
require(downcasted == value, "SafeCast: value doesn't fit in 56 bits");
}
/**
* @dev Returns the downcasted int48 from int256, reverting on
* overflow (when the input is less than smallest int48 or
* greater than largest int48).
*
* Counterpart to Solidity's `int48` operator.
*
* Requirements:
*
* - input must fit into 48 bits
*
* _Available since v4.7._
*/
function toInt48(int256 value) internal pure returns (int48 downcasted) {
downcasted = int48(value);
require(downcasted == value, "SafeCast: value doesn't fit in 48 bits");
}
/**
* @dev Returns the downcasted int40 from int256, reverting on
* overflow (when the input is less than smallest int40 or
* greater than largest int40).
*
* Counterpart to Solidity's `int40` operator.
*
* Requirements:
*
* - input must fit into 40 bits
*
* _Available since v4.7._
*/
function toInt40(int256 value) internal pure returns (int40 downcasted) {
downcasted = int40(value);
require(downcasted == value, "SafeCast: value doesn't fit in 40 bits");
}
/**
* @dev Returns the downcasted int32 from int256, reverting on
* overflow (when the input is less than smallest int32 or
* greater than largest int32).
*
* Counterpart to Solidity's `int32` operator.
*
* Requirements:
*
* - input must fit into 32 bits
*
* _Available since v3.1._
*/
function toInt32(int256 value) internal pure returns (int32 downcasted) {
downcasted = int32(value);
require(downcasted == value, "SafeCast: value doesn't fit in 32 bits");
}
/**
* @dev Returns the downcasted int24 from int256, reverting on
* overflow (when the input is less than smallest int24 or
* greater than largest int24).
*
* Counterpart to Solidity's `int24` operator.
*
* Requirements:
*
* - input must fit into 24 bits
*
* _Available since v4.7._
*/
function toInt24(int256 value) internal pure returns (int24 downcasted) {
downcasted = int24(value);
require(downcasted == value, "SafeCast: value doesn't fit in 24 bits");
}
/**
* @dev Returns the downcasted int16 from int256, reverting on
* overflow (when the input is less than smallest int16 or
* greater than largest int16).
*
* Counterpart to Solidity's `int16` operator.
*
* Requirements:
*
* - input must fit into 16 bits
*
* _Available since v3.1._
*/
function toInt16(int256 value) internal pure returns (int16 downcasted) {
downcasted = int16(value);
require(downcasted == value, "SafeCast: value doesn't fit in 16 bits");
}
/**
* @dev Returns the downcasted int8 from int256, reverting on
* overflow (when the input is less than smallest int8 or
* greater than largest int8).
*
* Counterpart to Solidity's `int8` operator.
*
* Requirements:
*
* - input must fit into 8 bits
*
* _Available since v3.1._
*/
function toInt8(int256 value) internal pure returns (int8 downcasted) {
downcasted = int8(value);
require(downcasted == value, "SafeCast: value doesn't fit in 8 bits");
}
/**
* @dev Converts an unsigned uint256 into a signed int256.
*
* Requirements:
*
* - input must be less than or equal to maxInt256.
*
* _Available since v3.0._
*/
function toInt256(uint256 value) internal pure returns (int256) {
// Note: Unsafe cast below is okay because `type(int256).max` is guaranteed to be positive
require(value <= uint256(type(int256).max), "SafeCast: value doesn't fit in an int256");
return int256(value);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.3) (token/ERC20/utils/SafeERC20.sol)
pragma solidity ^0.8.0;
import "../IERC20.sol";
import "../extensions/IERC20Permit.sol";
import "../../../utils/Address.sol";
/**
* @title SafeERC20
* @dev Wrappers around ERC20 operations that throw on failure (when the token
* contract returns false). Tokens that return no value (and instead revert or
* throw on failure) are also supported, non-reverting calls are assumed to be
* successful.
* To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
*/
library SafeERC20 {
using Address for address;
/**
* @dev Transfer `value` amount of `token` from the calling contract to `to`. If `token` returns no value,
* non-reverting calls are assumed to be successful.
*/
function safeTransfer(IERC20 token, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
/**
* @dev Transfer `value` amount of `token` from `from` to `to`, spending the approval given by `from` to the
* calling contract. If `token` returns no value, non-reverting calls are assumed to be successful.
*/
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
/**
* @dev Deprecated. This function has issues similar to the ones found in
* {IERC20-approve}, and its usage is discouraged.
*
* Whenever possible, use {safeIncreaseAllowance} and
* {safeDecreaseAllowance} instead.
*/
function safeApprove(IERC20 token, address spender, uint256 value) internal {
// safeApprove should only be called when setting an initial allowance,
// or when resetting it to zero. To increase and decrease it, use
// 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
require(
(value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
/**
* @dev Increase the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful.
*/
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 oldAllowance = token.allowance(address(this), spender);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance + value));
}
/**
* @dev Decrease the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful.
*/
function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
unchecked {
uint256 oldAllowance = token.allowance(address(this), spender);
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance - value));
}
}
/**
* @dev Set the calling contract's allowance toward `spender` to `value`. If `token` returns no value,
* non-reverting calls are assumed to be successful. Meant to be used with tokens that require the approval
* to be set to zero before setting it to a non-zero value, such as USDT.
*/
function forceApprove(IERC20 token, address spender, uint256 value) internal {
bytes memory approvalCall = abi.encodeWithSelector(token.approve.selector, spender, value);
if (!_callOptionalReturnBool(token, approvalCall)) {
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, 0));
_callOptionalReturn(token, approvalCall);
}
}
/**
* @dev Use a ERC-2612 signature to set the `owner` approval toward `spender` on `token`.
* Revert on invalid signature.
*/
function safePermit(
IERC20Permit token,
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
uint256 nonceBefore = token.nonces(owner);
token.permit(owner, spender, value, deadline, v, r, s);
uint256 nonceAfter = token.nonces(owner);
require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed");
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*/
function _callOptionalReturn(IERC20 token, bytes memory data) private {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We use {Address-functionCall} to perform this call, which verifies that
// the target address contains contract code and also asserts for success in the low-level call.
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
require(returndata.length == 0 || abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*
* This is a variant of {_callOptionalReturn} that silents catches all reverts and returns a bool instead.
*/
function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We cannot use {Address-functionCall} here since this should return false
// and not revert is the subcall reverts.
(bool success, bytes memory returndata) = address(token).call(data);
return
success && (returndata.length == 0 || abi.decode(returndata, (bool))) && Address.isContract(address(token));
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/ShortStrings.sol)
pragma solidity ^0.8.8;
import "./StorageSlot.sol";
// | string | 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
// | length | 0x BB |
type ShortString is bytes32;
/**
* @dev This library provides functions to convert short memory strings
* into a `ShortString` type that can be used as an immutable variable.
*
* Strings of arbitrary length can be optimized using this library if
* they are short enough (up to 31 bytes) by packing them with their
* length (1 byte) in a single EVM word (32 bytes). Additionally, a
* fallback mechanism can be used for every other case.
*
* Usage example:
*
* ```solidity
* contract Named {
* using ShortStrings for *;
*
* ShortString private immutable _name;
* string private _nameFallback;
*
* constructor(string memory contractName) {
* _name = contractName.toShortStringWithFallback(_nameFallback);
* }
*
* function name() external view returns (string memory) {
* return _name.toStringWithFallback(_nameFallback);
* }
* }
* ```
*/
library ShortStrings {
// Used as an identifier for strings longer than 31 bytes.
bytes32 private constant _FALLBACK_SENTINEL = 0x00000000000000000000000000000000000000000000000000000000000000FF;
error StringTooLong(string str);
error InvalidShortString();
/**
* @dev Encode a string of at most 31 chars into a `ShortString`.
*
* This will trigger a `StringTooLong` error is the input string is too long.
*/
function toShortString(string memory str) internal pure returns (ShortString) {
bytes memory bstr = bytes(str);
if (bstr.length > 31) {
revert StringTooLong(str);
}
return ShortString.wrap(bytes32(uint256(bytes32(bstr)) | bstr.length));
}
/**
* @dev Decode a `ShortString` back to a "normal" string.
*/
function toString(ShortString sstr) internal pure returns (string memory) {
uint256 len = byteLength(sstr);
// using `new string(len)` would work locally but is not memory safe.
string memory str = new string(32);
/// @solidity memory-safe-assembly
assembly {
mstore(str, len)
mstore(add(str, 0x20), sstr)
}
return str;
}
/**
* @dev Return the length of a `ShortString`.
*/
function byteLength(ShortString sstr) internal pure returns (uint256) {
uint256 result = uint256(ShortString.unwrap(sstr)) & 0xFF;
if (result > 31) {
revert InvalidShortString();
}
return result;
}
/**
* @dev Encode a string into a `ShortString`, or write it to storage if it is too long.
*/
function toShortStringWithFallback(string memory value, string storage store) internal returns (ShortString) {
if (bytes(value).length < 32) {
return toShortString(value);
} else {
StorageSlot.getStringSlot(store).value = value;
return ShortString.wrap(_FALLBACK_SENTINEL);
}
}
/**
* @dev Decode a string that was encoded to `ShortString` or written to storage using {setWithFallback}.
*/
function toStringWithFallback(ShortString value, string storage store) internal pure returns (string memory) {
if (ShortString.unwrap(value) != _FALLBACK_SENTINEL) {
return toString(value);
} else {
return store;
}
}
/**
* @dev Return the length of a string that was encoded to `ShortString` or written to storage using {setWithFallback}.
*
* WARNING: This will return the "byte length" of the string. This may not reflect the actual length in terms of
* actual characters as the UTF-8 encoding of a single character can span over multiple bytes.
*/
function byteLengthWithFallback(ShortString value, string storage store) internal view returns (uint256) {
if (ShortString.unwrap(value) != _FALLBACK_SENTINEL) {
return byteLength(value);
} else {
return bytes(store).length;
}
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/math/SignedMath.sol)
pragma solidity ^0.8.0;
/**
* @dev Standard signed math utilities missing in the Solidity language.
*/
library SignedMath {
/**
* @dev Returns the largest of two signed numbers.
*/
function max(int256 a, int256 b) internal pure returns (int256) {
return a > b ? a : b;
}
/**
* @dev Returns the smallest of two signed numbers.
*/
function min(int256 a, int256 b) internal pure returns (int256) {
return a < b ? a : b;
}
/**
* @dev Returns the average of two signed numbers without overflow.
* The result is rounded towards zero.
*/
function average(int256 a, int256 b) internal pure returns (int256) {
// Formula from the book "Hacker's Delight"
int256 x = (a & b) + ((a ^ b) >> 1);
return x + (int256(uint256(x) >> 255) & (a ^ b));
}
/**
* @dev Returns the absolute unsigned value of a signed value.
*/
function abs(int256 n) internal pure returns (uint256) {
unchecked {
// must be unchecked in order to support `n = type(int256).min`
return uint256(n >= 0 ? n : -n);
}
}
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
import {Ownable2Step} from "@openzeppelin/contracts/access/Ownable2Step.sol";
import "@layerzerolabs/solidity-examples/contracts/lzApp/interfaces/ILayerZeroReceiver.sol";
import "@layerzerolabs/solidity-examples/contracts/lzApp/interfaces/ILayerZeroUserApplicationConfig.sol";
import "@layerzerolabs/solidity-examples/contracts/lzApp/interfaces/ILayerZeroEndpoint.sol";
import "@layerzerolabs/solidity-examples/contracts/libraries/BytesLib.sol";
import "@layerzerolabs/solidity-examples/contracts/libraries/ExcessivelySafeCall.sol";
/// @title Nonblocking LzApp Events
/// @notice The events interface for LayerZero nonblocking apps, which are necessary to listen for
/// in forge failure tests
interface SimpleNonblockingLzAppEvents {
event MessageFailed(
uint16 _srcChainId,
bytes _srcAddress,
uint64 _nonce,
bytes _payload,
bytes _reason
);
event RetryMessageSuccess(
uint16 _srcChainId,
bytes _srcAddress,
uint64 _nonce,
bytes32 _payloadHash
);
}
/// @title Simple Nonblocking LzApp
/// @notice A generic LzReceiver implementation that removes the base reference to the blocking LzApp
abstract contract SimpleNonblockingLzApp is
Ownable2Step,
ILayerZeroReceiver,
ILayerZeroUserApplicationConfig,
SimpleNonblockingLzAppEvents
{
using BytesLib for bytes;
using ExcessivelySafeCall for address;
// ua can not send payload larger than this by default, but it can be changed by the ua owner
uint public constant DEFAULT_PAYLOAD_SIZE_LIMIT = 10000;
ILayerZeroEndpoint public immutable lzEndpoint;
mapping(uint16 => mapping(uint16 => uint)) public minDstGasLookup;
mapping(uint16 => uint) public payloadSizeLimitLookup;
address public precrime;
event SetPrecrime(address precrime);
event SetMinDstGas(uint16 _dstChainId, uint16 _type, uint _minDstGas);
constructor(address _endpoint) {
lzEndpoint = ILayerZeroEndpoint(_endpoint);
}
function lzReceive(
uint16 _srcChainId,
bytes calldata _srcAddress,
uint64 _nonce,
bytes calldata _payload
) public virtual override {
// lzReceive must be called by the endpoint for security
require(
_msgSender() == address(lzEndpoint),
"LzApp: invalid endpoint caller"
);
(bool success, bytes memory reason) = address(this).excessivelySafeCall(
gasleft(),
150,
abi.encodeWithSelector(
this.nonblockingLzReceive.selector,
_srcChainId,
_srcAddress,
_nonce,
_payload
)
);
// try-catch all errors/exceptions
if (!success) {
_storeFailedMessage(
_srcChainId,
_srcAddress,
_nonce,
_payload,
reason
);
}
}
function _lzSend(
uint16 _dstChainId,
address _dstChainAddress,
bytes memory _payload,
address payable _refundAddress,
address _zroPaymentAddress,
bytes memory _adapterParams,
uint _nativeFee
) internal virtual {
_checkPayloadSize(_dstChainId, _payload.length);
lzEndpoint.send{value: _nativeFee}(
_dstChainId,
abi.encodePacked(_dstChainAddress, address(this)),
_payload,
_refundAddress,
_zroPaymentAddress,
_adapterParams
);
}
// function _checkGasLimit(
// uint16 _dstChainId,
// uint16 _type,
// bytes memory _adapterParams,
// uint _extraGas
// ) internal view virtual {
// uint providedGasLimit = _getGasLimit(_adapterParams);
// uint minGasLimit = minDstGasLookup[_dstChainId][_type] + _extraGas;
// require(minGasLimit > 0, "LzApp: minGasLimit not set");
// require(providedGasLimit >= minGasLimit, "LzApp: gas limit is too low");
// }
// function _getGasLimit(
// bytes memory _adapterParams
// ) internal pure virtual returns (uint gasLimit) {
// require(_adapterParams.length >= 34, "LzApp: invalid adapterParams");
// assembly {
// gasLimit := mload(add(_adapterParams, 34))
// }
// }
function _checkPayloadSize(
uint16 _dstChainId,
uint _payloadSize
) internal view virtual {
uint payloadSizeLimit = payloadSizeLimitLookup[_dstChainId];
if (payloadSizeLimit == 0) {
// use default if not set
payloadSizeLimit = DEFAULT_PAYLOAD_SIZE_LIMIT;
}
require(
_payloadSize <= payloadSizeLimit,
"LzApp: payload size is too large"
);
}
//---------------------------UserApplication config----------------------------------------
function getConfig(
uint16 _version,
uint16 _chainId,
address,
uint _configType
) external view returns (bytes memory) {
return
lzEndpoint.getConfig(
_version,
_chainId,
address(this),
_configType
);
}
// generic config for LayerZero user Application
function setConfig(
uint16 _version,
uint16 _chainId,
uint _configType,
bytes calldata _config
) external override onlyOwner {
lzEndpoint.setConfig(_version, _chainId, _configType, _config);
}
function setSendVersion(uint16 _version) external override onlyOwner {
lzEndpoint.setSendVersion(_version);
}
function setReceiveVersion(uint16 _version) external override onlyOwner {
lzEndpoint.setReceiveVersion(_version);
}
function forceResumeReceive(
uint16 _srcChainId,
bytes calldata _srcAddress
) external override onlyOwner {
lzEndpoint.forceResumeReceive(_srcChainId, _srcAddress);
}
function setPrecrime(address _precrime) external onlyOwner {
precrime = _precrime;
emit SetPrecrime(_precrime);
}
function setMinDstGas(
uint16 _dstChainId,
uint16 _packetType,
uint _minGas
) external onlyOwner {
require(_minGas > 0, "LzApp: invalid minGas");
minDstGasLookup[_dstChainId][_packetType] = _minGas;
emit SetMinDstGas(_dstChainId, _packetType, _minGas);
}
// if the size is 0, it means default size limit
function setPayloadSizeLimit(
uint16 _dstChainId,
uint _size
) external onlyOwner {
payloadSizeLimitLookup[_dstChainId] = _size;
}
// ======================= NONBLOCKING =======================
mapping(uint16 => mapping(bytes => mapping(uint64 => bytes32)))
public failedMessages;
function _storeFailedMessage(
uint16 _srcChainId,
bytes memory _srcAddress,
uint64 _nonce,
bytes memory _payload,
bytes memory _reason
) internal virtual {
failedMessages[_srcChainId][_srcAddress][_nonce] = keccak256(_payload);
emit MessageFailed(_srcChainId, _srcAddress, _nonce, _payload, _reason);
}
function nonblockingLzReceive(
uint16 _srcChainId,
bytes calldata _srcAddress,
uint64 _nonce,
bytes calldata _payload
) public virtual {
// only internal transaction
require(
_msgSender() == address(this),
"NonblockingLzApp: caller must be LzApp"
);
_nonblockingLzReceive(_srcChainId, _srcAddress, _nonce, _payload);
}
//@notice override this function
function _nonblockingLzReceive(
uint16 _srcChainId,
bytes memory _srcAddress,
uint64 _nonce,
bytes memory _payload
) internal virtual;
function retryMessage(
uint16 _srcChainId,
bytes calldata _srcAddress,
uint64 _nonce,
bytes calldata _payload
) public payable virtual {
// assert there is message to retry
bytes32 payloadHash = failedMessages[_srcChainId][_srcAddress][_nonce];
require(
payloadHash != bytes32(0),
"NonblockingLzApp: no stored message"
);
require(
keccak256(_payload) == payloadHash,
"NonblockingLzApp: invalid payload"
);
// clear the stored message
failedMessages[_srcChainId][_srcAddress][_nonce] = bytes32(0);
// execute the message. revert if it fails again
_nonblockingLzReceive(_srcChainId, _srcAddress, _nonce, _payload);
emit RetryMessageSuccess(_srcChainId, _srcAddress, _nonce, payloadHash);
}
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.18;
import {IGlacisRouter} from "../interfaces/IGlacisRouter.sol";
import {GlacisClient} from "../client/GlacisClient.sol";
import {IXERC20} from "../interfaces/IXERC20.sol";
import {GlacisCommons} from "../commons/GlacisCommons.sol";
import {GlacisRemoteCounterpartManager} from "../managers/GlacisRemoteCounterpartManager.sol";
import {AddressBytes32} from "../libraries/AddressBytes32.sol";
error SimpleTokenMediator__DestinationChainUnavailable();
/// @title Simple Token Mediator
/// @notice This contract burns and mints XERC-20 tokens without additional
/// features. There is no additional Glacis XERC-20 interface, tokens cannot
/// be sent with a payload, and there is no special interface for a client to
/// inherit from.
/// The `route` function has been replaced with a `sendCrossChain`
/// function to differentiate it from the routing with payload that the
/// GlacisTokenMediator has. Similarly, the retry function has been replaced
/// with a `sendCrossChainRetry`.
/// Developers using this must ensure that their token has the same address on
/// each chain.
contract SimpleTokenMediator is GlacisRemoteCounterpartManager, GlacisClient {
using AddressBytes32 for address;
using AddressBytes32 for bytes32;
event SimpleTokenMediator__TokensMinted(address indexed, address indexed, uint256);
event SimpleTokenMediator__TokensBurnt(address indexed, address indexed, uint256);
constructor(
address _glacisRouter,
uint256 _quorum,
address _owner
) GlacisClient(_glacisRouter, _quorum) {
_transferOwnership(_owner);
}
address public xERC20Token;
/// @notice Allows the owner to set the single xERC20 that this mediator sends
/// @param _xERC20Token The address of the token that this mediator sends
function setXERC20(address _xERC20Token) public onlyOwner {
xERC20Token = _xERC20Token;
}
/// @notice Routes the payload to the specific address on destination chain through GlacisRouter using GMPs
/// specified in gmps array
/// @param chainId Destination chain (Glacis chain ID)
/// @param to Destination address on remote chain
/// @param adapters The GMP Adapters to use for routing
/// @param fees Payment for each GMP to cover source and destination gas fees (excess will be refunded)
/// @param refundAddress Address to refund excess gas payment
/// @param tokenAmount Amount of token to send to remote contract
function sendCrossChain(
uint256 chainId,
bytes32 to,
address[] memory adapters,
CrossChainGas[] memory fees,
address refundAddress,
uint256 tokenAmount
) public payable virtual returns (bytes32, uint256) {
bytes32 destinationTokenMediator = remoteCounterpart[chainId];
if (destinationTokenMediator == bytes32(0))
revert SimpleTokenMediator__DestinationChainUnavailable();
IXERC20(xERC20Token).burn(msg.sender, tokenAmount);
bytes memory tokenPayload = packTokenPayload(to, tokenAmount);
emit SimpleTokenMediator__TokensBurnt(
msg.sender,
xERC20Token,
tokenAmount
);
return
IGlacisRouter(GLACIS_ROUTER).route{value: msg.value}(
chainId,
destinationTokenMediator,
tokenPayload,
adapters,
fees,
refundAddress,
true // Token Mediator always enables retry
);
}
/// @notice Retries routing the payload to the specific address on destination chain using specified GMPs
/// @param chainId Destination chain (Glacis chain ID)
/// @param to Destination address on remote chain
/// @param adapters The GMP Adapters to use for routing
/// @param fees Payment for each GMP to cover source and destination gas fees (excess will be refunded)
/// @param refundAddress Address to refund excess gas payment
/// @param messageId The message ID of the message to retry
/// @param nonce The nonce emitted by the original message routing
/// @param tokenAmount Amount of token to send to remote contract
/// @return A tuple with a bytes32 messageId and a uint256 nonce
function sendCrossChainRetry(
uint256 chainId,
bytes32 to,
address[] memory adapters,
CrossChainGas[] memory fees,
address refundAddress,
bytes32 messageId,
uint256 nonce,
uint256 tokenAmount
) public payable virtual returns (bytes32, uint256) {
// Pack with a function
bytes memory tokenPayload = packTokenPayload(to, tokenAmount);
// Use helper function (otherwise stack too deep)
return
_routeRetry(
chainId,
tokenPayload,
adapters,
fees,
refundAddress,
messageId,
nonce
);
}
/// A private function to help with stack to deep during retries.
function _routeRetry(
uint256 chainId,
bytes memory tokenPayload,
address[] memory adapters,
CrossChainGas[] memory fees,
address refundAddress,
bytes32 messageId,
uint256 nonce
) private returns (bytes32, uint256) {
bytes32 destinationTokenMediator = remoteCounterpart[chainId];
if (destinationTokenMediator == bytes32(0))
revert SimpleTokenMediator__DestinationChainUnavailable();
return
IGlacisRouter(GLACIS_ROUTER).routeRetry{value: msg.value}(
chainId,
destinationTokenMediator,
tokenPayload,
adapters,
fees,
refundAddress,
messageId,
nonce
);
}
/// @notice Receives a cross chain message from an IGlacisAdapter.
/// @param payload Received payload from Glacis Router
function _receiveMessage(
address[] memory, // fromAdapters
uint256, // fromChainId
bytes32, // fromAddress
bytes memory payload
) internal override {
// Access control security is handled by allowed routes. No need to check for remoteCounterpart
(bytes32 to, uint256 tokenAmount) = decodeTokenPayload(payload);
// Mint
address toAddress = to.toAddress();
IXERC20(xERC20Token).mint(toAddress, tokenAmount);
emit SimpleTokenMediator__TokensMinted(
toAddress,
xERC20Token,
tokenAmount
);
}
/// Packs a token payload into this contract's standard.
function packTokenPayload(
bytes32 to,
uint256 tokenAmount
) internal pure returns (bytes memory) {
return abi.encode(to, tokenAmount);
}
/// Decodes a token payload into this contract's standard.
function decodeTokenPayload(
bytes memory payload
) internal pure returns (bytes32 to, uint256 tokenAmount) {
(to, tokenAmount) = abi.decode(payload, (bytes32, uint256));
}
/// @notice Add an allowed route for this client
/// @param allowedRoute Route to be added
function addAllowedRoute(
GlacisCommons.GlacisRoute memory allowedRoute
) external onlyOwner {
_addAllowedRoute(allowedRoute);
}
/// @notice Removes an allowed route for this client
/// @param route Allowed route to be removed
function removeAllowedRoute(
GlacisCommons.GlacisRoute calldata route
) external onlyOwner {
_removeAllowedRoute(route);
}
}
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.8.0;
/*@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@ HYPERLANE @@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@*/
/**
* Format of metadata:
*
* [0:2] variant
* [2:34] msg.value
* [34:66] Gas limit for message (IGP)
* [66:86] Refund address for message (IGP)
* [86:] Custom metadata
*/
library StandardHookMetadata {
struct Metadata {
uint16 variant;
uint256 msgValue;
uint256 gasLimit;
address refundAddress;
}
uint8 private constant VARIANT_OFFSET = 0;
uint8 private constant MSG_VALUE_OFFSET = 2;
uint8 private constant GAS_LIMIT_OFFSET = 34;
uint8 private constant REFUND_ADDRESS_OFFSET = 66;
uint256 private constant MIN_METADATA_LENGTH = 86;
uint16 public constant VARIANT = 1;
/**
* @notice Returns the variant of the metadata.
* @param _metadata ABI encoded standard hook metadata.
* @return variant of the metadata as uint8.
*/
function variant(bytes calldata _metadata) internal pure returns (uint16) {
if (_metadata.length < VARIANT_OFFSET + 2) return 0;
return uint16(bytes2(_metadata[VARIANT_OFFSET:VARIANT_OFFSET + 2]));
}
/**
* @notice Returns the specified value for the message.
* @param _metadata ABI encoded standard hook metadata.
* @param _default Default fallback value.
* @return Value for the message as uint256.
*/
function msgValue(
bytes calldata _metadata,
uint256 _default
) internal pure returns (uint256) {
if (_metadata.length < MSG_VALUE_OFFSET + 32) return _default;
return
uint256(bytes32(_metadata[MSG_VALUE_OFFSET:MSG_VALUE_OFFSET + 32]));
}
/**
* @notice Returns the specified gas limit for the message.
* @param _metadata ABI encoded standard hook metadata.
* @param _default Default fallback gas limit.
* @return Gas limit for the message as uint256.
*/
function gasLimit(
bytes calldata _metadata,
uint256 _default
) internal pure returns (uint256) {
if (_metadata.length < GAS_LIMIT_OFFSET + 32) return _default;
return
uint256(bytes32(_metadata[GAS_LIMIT_OFFSET:GAS_LIMIT_OFFSET + 32]));
}
/**
* @notice Returns the specified refund address for the message.
* @param _metadata ABI encoded standard hook metadata.
* @param _default Default fallback refund address.
* @return Refund address for the message as address.
*/
function refundAddress(
bytes calldata _metadata,
address _default
) internal pure returns (address) {
if (_metadata.length < REFUND_ADDRESS_OFFSET + 20) return _default;
return
address(
bytes20(
_metadata[REFUND_ADDRESS_OFFSET:REFUND_ADDRESS_OFFSET + 20]
)
);
}
/**
* @notice Returns any custom metadata.
* @param _metadata ABI encoded standard hook metadata.
* @return Custom metadata.
*/
function getCustomMetadata(
bytes calldata _metadata
) internal pure returns (bytes calldata) {
if (_metadata.length < MIN_METADATA_LENGTH) return _metadata[0:0];
return _metadata[MIN_METADATA_LENGTH:];
}
/**
* @notice Formats the specified gas limit and refund address into standard hook metadata.
* @param _msgValue msg.value for the message.
* @param _gasLimit Gas limit for the message.
* @param _refundAddress Refund address for the message.
* @param _customMetadata Additional metadata to include in the standard hook metadata.
* @return ABI encoded standard hook metadata.
*/
function formatMetadata(
uint256 _msgValue,
uint256 _gasLimit,
address _refundAddress,
bytes memory _customMetadata
) internal pure returns (bytes memory) {
return
abi.encodePacked(
VARIANT,
_msgValue,
_gasLimit,
_refundAddress,
_customMetadata
);
}
/**
* @notice Formats the specified gas limit and refund address into standard hook metadata.
* @param _msgValue msg.value for the message.
* @return ABI encoded standard hook metadata.
*/
function overrideMsgValue(
uint256 _msgValue
) internal view returns (bytes memory) {
return formatMetadata(_msgValue, uint256(0), msg.sender, "");
}
/**
* @notice Formats the specified gas limit and refund address into standard hook metadata.
* @param _gasLimit Gas limit for the message.
* @return ABI encoded standard hook metadata.
*/
function overrideGasLimit(
uint256 _gasLimit
) internal view returns (bytes memory) {
return formatMetadata(uint256(0), _gasLimit, msg.sender, "");
}
/**
* @notice Formats the specified refund address into standard hook metadata.
* @param _refundAddress Refund address for the message.
* @return ABI encoded standard hook metadata.
*/
function overrideRefundAddress(
address _refundAddress
) internal pure returns (bytes memory) {
return formatMetadata(uint256(0), uint256(0), _refundAddress, "");
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/StorageSlot.sol)
// This file was procedurally generated from scripts/generate/templates/StorageSlot.js.
pragma solidity ^0.8.0;
/**
* @dev Library for reading and writing primitive types to specific storage slots.
*
* Storage slots are often used to avoid storage conflict when dealing with upgradeable contracts.
* This library helps with reading and writing to such slots without the need for inline assembly.
*
* The functions in this library return Slot structs that contain a `value` member that can be used to read or write.
*
* Example usage to set ERC1967 implementation slot:
* ```solidity
* contract ERC1967 {
* bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
*
* function _getImplementation() internal view returns (address) {
* return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
* }
*
* function _setImplementation(address newImplementation) internal {
* require(Address.isContract(newImplementation), "ERC1967: new implementation is not a contract");
* StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
* }
* }
* ```
*
* _Available since v4.1 for `address`, `bool`, `bytes32`, `uint256`._
* _Available since v4.9 for `string`, `bytes`._
*/
library StorageSlot {
struct AddressSlot {
address value;
}
struct BooleanSlot {
bool value;
}
struct Bytes32Slot {
bytes32 value;
}
struct Uint256Slot {
uint256 value;
}
struct StringSlot {
string value;
}
struct BytesSlot {
bytes value;
}
/**
* @dev Returns an `AddressSlot` with member `value` located at `slot`.
*/
function getAddressSlot(bytes32 slot) internal pure returns (AddressSlot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := slot
}
}
/**
* @dev Returns an `BooleanSlot` with member `value` located at `slot`.
*/
function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := slot
}
}
/**
* @dev Returns an `Bytes32Slot` with member `value` located at `slot`.
*/
function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := slot
}
}
/**
* @dev Returns an `Uint256Slot` with member `value` located at `slot`.
*/
function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := slot
}
}
/**
* @dev Returns an `StringSlot` with member `value` located at `slot`.
*/
function getStringSlot(bytes32 slot) internal pure returns (StringSlot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := slot
}
}
/**
* @dev Returns an `StringSlot` representation of the string storage pointer `store`.
*/
function getStringSlot(string storage store) internal pure returns (StringSlot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := store.slot
}
}
/**
* @dev Returns an `BytesSlot` with member `value` located at `slot`.
*/
function getBytesSlot(bytes32 slot) internal pure returns (BytesSlot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := slot
}
}
/**
* @dev Returns an `BytesSlot` representation of the bytes storage pointer `store`.
*/
function getBytesSlot(bytes storage store) internal pure returns (BytesSlot storage r) {
/// @solidity memory-safe-assembly
assembly {
r.slot := store.slot
}
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/Strings.sol)
pragma solidity ^0.8.0;
import "./math/Math.sol";
import "./math/SignedMath.sol";
/**
* @dev String operations.
*/
library Strings {
bytes16 private constant _SYMBOLS = "0123456789abcdef";
uint8 private constant _ADDRESS_LENGTH = 20;
/**
* @dev Converts a `uint256` to its ASCII `string` decimal representation.
*/
function toString(uint256 value) internal pure returns (string memory) {
unchecked {
uint256 length = Math.log10(value) + 1;
string memory buffer = new string(length);
uint256 ptr;
/// @solidity memory-safe-assembly
assembly {
ptr := add(buffer, add(32, length))
}
while (true) {
ptr--;
/// @solidity memory-safe-assembly
assembly {
mstore8(ptr, byte(mod(value, 10), _SYMBOLS))
}
value /= 10;
if (value == 0) break;
}
return buffer;
}
}
/**
* @dev Converts a `int256` to its ASCII `string` decimal representation.
*/
function toString(int256 value) internal pure returns (string memory) {
return string(abi.encodePacked(value < 0 ? "-" : "", toString(SignedMath.abs(value))));
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
*/
function toHexString(uint256 value) internal pure returns (string memory) {
unchecked {
return toHexString(value, Math.log256(value) + 1);
}
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
*/
function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
bytes memory buffer = new bytes(2 * length + 2);
buffer[0] = "0";
buffer[1] = "x";
for (uint256 i = 2 * length + 1; i > 1; --i) {
buffer[i] = _SYMBOLS[value & 0xf];
value >>= 4;
}
require(value == 0, "Strings: hex length insufficient");
return string(buffer);
}
/**
* @dev Converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal representation.
*/
function toHexString(address addr) internal pure returns (string memory) {
return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH);
}
/**
* @dev Returns true if the two strings are equal.
*/
function equal(string memory a, string memory b) internal pure returns (bool) {
return keccak256(bytes(a)) == keccak256(bytes(b));
}
}
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.11;
library TypeCasts {
// alignment preserving cast
function addressToBytes32(address _addr) internal pure returns (bytes32) {
return bytes32(uint256(uint160(_addr)));
}
// alignment preserving cast
function bytes32ToAddress(bytes32 _buf) internal pure returns (address) {
return address(uint160(uint256(_buf)));
}
}
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.4 <0.9.0;
import {IXERC20, IXERC20GlacisExtension} from "../interfaces/IXERC20.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {ERC20Permit} from "@openzeppelin/contracts/token/ERC20/extensions/draft-ERC20Permit.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
contract XERC20Basic is ERC20, Ownable, IXERC20, ERC20Permit {
/**
* @notice The duration it takes for the limits to fully replenish
*/
uint256 private constant _DURATION = 1 days;
/**
* @notice The address of the factory which deployed this contract
*/
address public immutable FACTORY;
/**
* @notice The address of the lockbox contract
*/
address public lockbox;
/**
* @notice Maps bridge address to bridge configurations
*/
mapping(address => Bridge) public bridges;
/**
* @notice Constructs the initial config of the XERC20
*
* @param _name The name of the token
* @param _symbol The symbol of the token
* @param _factory The factory which deployed this contract
*/
constructor(
string memory _name,
string memory _symbol,
address _factory
) ERC20(_name, _symbol) ERC20Permit(_name) {
_transferOwnership(_factory);
FACTORY = _factory;
}
/**
* @notice Mints tokens for a user
* @dev Can only be called by a bridge or lockbox
* @param _user The address of the user who needs tokens minted
* @param _amount The amount of tokens being minted
*/
function mint(address _user, uint256 _amount) public {
_mintWithCaller(msg.sender, _user, _amount);
}
/**
* @notice Burns tokens for a user
* @dev Can only be called by a bridge or lockbox
* @param _user The address of the user who needs tokens burned
* @param _amount The amount of tokens being burned
*/
function burn(address _user, uint256 _amount) public {
if (msg.sender != _user) {
_spendAllowance(_user, msg.sender, _amount);
}
_burnWithCaller(msg.sender, _user, _amount);
}
/**
* @notice Sets the lockbox address
*
* @param _lockbox The address of the lockbox
*/
function setLockbox(address _lockbox) public {
if (msg.sender != FACTORY) revert IXERC20_NotFactory();
lockbox = _lockbox;
emit LockboxSet(_lockbox);
}
/**
* @notice Updates the limits of any bridge
* @dev Can only be called by the owner
* @param _mintingLimit The updated minting limit we are setting to the bridge
* @param _burningLimit The updated burning limit we are setting to the bridge
* @param _bridge The address of the bridge we are setting the limits to
*/
function setLimits(
address _bridge,
uint256 _mintingLimit,
uint256 _burningLimit
) external onlyOwner {
_changeMinterLimit(_bridge, _mintingLimit);
_changeBurnerLimit(_bridge, _burningLimit);
emit BridgeLimitsSet(_mintingLimit, _burningLimit, _bridge);
}
/**
* @notice Returns the max limit of a bridge
*
* @param _bridge the bridge we are viewing the limits of
* @return _limit The limit the bridge has
*/
function mintingMaxLimitOf(
address _bridge
) public view returns (uint256 _limit) {
_limit = bridges[_bridge].minterParams.maxLimit;
}
/**
* @notice Returns the max limit of a bridge
*
* @param _bridge the bridge we are viewing the limits of
* @return _limit The limit the bridge has
*/
function burningMaxLimitOf(
address _bridge
) public view returns (uint256 _limit) {
_limit = bridges[_bridge].burnerParams.maxLimit;
}
/**
* @notice Returns the current limit of a bridge
*
* @param _bridge the bridge we are viewing the limits of
* @return _limit The limit the bridge has
*/
function mintingCurrentLimitOf(
address _bridge
) public view returns (uint256 _limit) {
_limit = _getCurrentLimit(
bridges[_bridge].minterParams.currentLimit,
bridges[_bridge].minterParams.maxLimit,
bridges[_bridge].minterParams.timestamp,
bridges[_bridge].minterParams.ratePerSecond
);
}
/**
* @notice Returns the current limit of a bridge
*
* @param _bridge the bridge we are viewing the limits of
* @return _limit The limit the bridge has
*/
function burningCurrentLimitOf(
address _bridge
) public view returns (uint256 _limit) {
_limit = _getCurrentLimit(
bridges[_bridge].burnerParams.currentLimit,
bridges[_bridge].burnerParams.maxLimit,
bridges[_bridge].burnerParams.timestamp,
bridges[_bridge].burnerParams.ratePerSecond
);
}
/**
* @notice Uses the limit of any bridge
* @param _bridge The address of the bridge that is being changed
* @param _change The change in the limit
*/
function _useMinterLimits(address _bridge, uint256 _change) internal {
uint256 _currentLimit = mintingCurrentLimitOf(_bridge);
bridges[_bridge].minterParams.timestamp = block.timestamp;
bridges[_bridge].minterParams.currentLimit = _currentLimit - _change;
}
/**
* @notice Uses the limit of any bridge
* @param _bridge The address of the bridge that is being changed
* @param _change The change in the limit
*/
function _useBurnerLimits(address _bridge, uint256 _change) internal {
uint256 _currentLimit = burningCurrentLimitOf(_bridge);
bridges[_bridge].burnerParams.timestamp = block.timestamp;
bridges[_bridge].burnerParams.currentLimit = _currentLimit - _change;
}
/**
* @notice Updates the limit of any bridge
* @dev Can only be called by the owner
* @param _bridge The address of the bridge we are setting the limit to
* @param _limit The updated limit we are setting to the bridge
*/
function _changeMinterLimit(address _bridge, uint256 _limit) internal {
uint256 _oldLimit = bridges[_bridge].minterParams.maxLimit;
uint256 _currentLimit = mintingCurrentLimitOf(_bridge);
bridges[_bridge].minterParams.maxLimit = _limit;
bridges[_bridge].minterParams.currentLimit = _calculateNewCurrentLimit(
_limit,
_oldLimit,
_currentLimit
);
bridges[_bridge].minterParams.ratePerSecond = _limit / _DURATION;
bridges[_bridge].minterParams.timestamp = block.timestamp;
}
/**
* @notice Updates the limit of any bridge
* @dev Can only be called by the owner
* @param _bridge The address of the bridge we are setting the limit to
* @param _limit The updated limit we are setting to the bridge
*/
function _changeBurnerLimit(address _bridge, uint256 _limit) internal {
uint256 _oldLimit = bridges[_bridge].burnerParams.maxLimit;
uint256 _currentLimit = burningCurrentLimitOf(_bridge);
bridges[_bridge].burnerParams.maxLimit = _limit;
bridges[_bridge].burnerParams.currentLimit = _calculateNewCurrentLimit(
_limit,
_oldLimit,
_currentLimit
);
bridges[_bridge].burnerParams.ratePerSecond = _limit / _DURATION;
bridges[_bridge].burnerParams.timestamp = block.timestamp;
}
/**
* @notice Updates the current limit
*
* @param _limit The new limit
* @param _oldLimit The old limit
* @param _currentLimit The current limit
*/
function _calculateNewCurrentLimit(
uint256 _limit,
uint256 _oldLimit,
uint256 _currentLimit
) internal pure returns (uint256 _newCurrentLimit) {
uint256 _difference;
if (_oldLimit > _limit) {
_difference = _oldLimit - _limit;
_newCurrentLimit = _currentLimit > _difference
? _currentLimit - _difference
: 0;
} else {
_difference = _limit - _oldLimit;
_newCurrentLimit = _currentLimit + _difference;
}
}
/**
* @notice Gets the current limit
*
* @param _currentLimit The current limit
* @param _maxLimit The max limit
* @param _timestamp The timestamp of the last update
* @param _ratePerSecond The rate per second
*/
function _getCurrentLimit(
uint256 _currentLimit,
uint256 _maxLimit,
uint256 _timestamp,
uint256 _ratePerSecond
) internal view returns (uint256 _limit) {
_limit = _currentLimit;
if (_limit == _maxLimit) {
return _limit;
} else if (_timestamp + _DURATION <= block.timestamp) {
_limit = _maxLimit;
} else if (_timestamp + _DURATION > block.timestamp) {
uint256 _timePassed = block.timestamp - _timestamp;
uint256 _calculatedLimit = _limit + (_timePassed * _ratePerSecond);
_limit = _calculatedLimit > _maxLimit
? _maxLimit
: _calculatedLimit;
}
}
/**
* @notice Internal function for burning tokens
*
* @param _caller The caller address
* @param _user The user address
* @param _amount The amount to burn
*/
function _burnWithCaller(
address _caller,
address _user,
uint256 _amount
) internal {
if (_caller != lockbox) {
uint256 _currentLimit = burningCurrentLimitOf(_caller);
if (_currentLimit < _amount) revert IXERC20_NotHighEnoughLimits();
_useBurnerLimits(_caller, _amount);
}
_burn(_user, _amount);
}
/**
* @notice Internal function for minting tokens
*
* @param _caller The caller address
* @param _user The user address
* @param _amount The amount to mint
*/
function _mintWithCaller(
address _caller,
address _user,
uint256 _amount
) internal {
if (_caller != lockbox) {
uint256 _currentLimit = mintingCurrentLimitOf(_caller);
if (_currentLimit < _amount) revert IXERC20_NotHighEnoughLimits();
_useMinterLimits(_caller, _amount);
}
_mint(_user, _amount);
}
}
contract XERC20 is XERC20Basic, IXERC20GlacisExtension {
/**
* @notice Constructs the initial config of the XERC20
*
* @param _name The name of the token
* @param _symbol The symbol of the token
* @param _factory The factory which deployed this contract
*/
constructor(
string memory _name,
string memory _symbol,
address _factory
) XERC20Basic(_name, _symbol, _factory) {}
mapping(uint256 => bytes32) private chainIdToTokenVariant;
/**
* @notice Returns a token variant for a specific chainId if it exists.
*
* @param chainId The chainId of the token variant.
*/
function getTokenVariant(
uint256 chainId
) external view override returns (bytes32) {
return chainIdToTokenVariant[chainId];
}
/**
* @notice Sets a token variant for a specific chainId.
*
* @param chainId The chainId of the token variant.
* @param variant The address of the token variant.
*/
function setTokenVariant(
uint256 chainId,
bytes32 variant
) external onlyOwner {
chainIdToTokenVariant[chainId] = variant;
}
/**
* @notice Removes a token variant for a specific chainId.
*
* @param chainId The chainId of the token variant.
*/
function removeTokenVariant(uint256 chainId) external onlyOwner {
delete chainIdToTokenVariant[chainId];
}
}
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.4 <0.9.0;
import {IXERC20} from "../interfaces/IXERC20.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";
import {IXERC20Lockbox} from "../interfaces/IXERC20Lockbox.sol";
contract XERC20Lockbox is IXERC20Lockbox {
using SafeERC20 for IERC20;
using SafeCast for uint256;
/**
* @notice The XERC20 token of this contract
*/
IXERC20 public immutable XERC20;
/**
* @notice The ERC20 token of this contract
*/
IERC20 public immutable ERC20;
/**
* @notice Whether the ERC20 token is the native gas token of this chain
*/
bool public immutable IS_NATIVE;
/**
* @notice Constructor
*
* @param _xerc20 The address of the XERC20 contract
* @param _erc20 The address of the ERC20 contract
*/
constructor(address _xerc20, address _erc20, bool _isNative) {
XERC20 = IXERC20(_xerc20);
ERC20 = IERC20(_erc20);
IS_NATIVE = _isNative;
}
/**
* @notice Deposit native tokens into the lockbox
*/
function depositNative() public payable {
if (!IS_NATIVE) revert IXERC20Lockbox_NotNative();
_deposit(msg.sender, msg.value);
}
/**
* @notice Deposit ERC20 tokens into the lockbox
*
* @param _amount The amount of tokens to deposit
*/
function deposit(uint256 _amount) external {
if (IS_NATIVE) revert IXERC20Lockbox_Native();
_deposit(msg.sender, _amount);
}
/**
* @notice Deposit ERC20 tokens into the lockbox, and send the XERC20 to a user
*
* @param _to The user to send the XERC20 to
* @param _amount The amount of tokens to deposit
*/
function depositTo(address _to, uint256 _amount) external {
if (IS_NATIVE) revert IXERC20Lockbox_Native();
_deposit(_to, _amount);
}
/**
* @notice Deposit the native asset into the lockbox, and send the XERC20 to a user
*
* @param _to The user to send the XERC20 to
*/
function depositNativeTo(address _to) public payable {
if (!IS_NATIVE) revert IXERC20Lockbox_NotNative();
_deposit(_to, msg.value);
}
/**
* @notice Withdraw ERC20 tokens from the lockbox
*
* @param _amount The amount of tokens to withdraw
*/
function withdraw(uint256 _amount) external {
_withdraw(msg.sender, _amount);
}
/**
* @notice Withdraw tokens from the lockbox
*
* @param _to The user to withdraw to
* @param _amount The amount of tokens to withdraw
*/
function withdrawTo(address _to, uint256 _amount) external {
_withdraw(_to, _amount);
}
/**
* @notice Withdraw tokens from the lockbox
*
* @param _to The user to withdraw to
* @param _amount The amount of tokens to withdraw
*/
function _withdraw(address _to, uint256 _amount) internal {
emit Withdraw(_to, _amount);
XERC20.burn(msg.sender, _amount);
if (IS_NATIVE) {
(bool _success, ) = payable(_to).call{value: _amount}("");
if (!_success) revert IXERC20Lockbox_WithdrawFailed();
} else {
ERC20.safeTransfer(_to, _amount);
}
}
/**
* @notice Deposit tokens into the lockbox
*
* @param _to The address to send the XERC20 to
* @param _amount The amount of tokens to deposit
*/
function _deposit(address _to, uint256 _amount) internal {
if (!IS_NATIVE) {
ERC20.safeTransferFrom(msg.sender, address(this), _amount);
}
XERC20.mint(_to, _amount);
emit Deposit(_to, _amount);
}
receive() external payable {
depositNative();
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/extensions/draft-ERC20Permit.sol)
pragma solidity ^0.8.0;
// EIP-2612 is Final as of 2022-11-01. This file is deprecated.
import "./ERC20Permit.sol";
{
"compilationTarget": {
"contracts/adapters/LayerZero/GlacisLayerZeroV2Adapter.sol": "GlacisLayerZeroV2Adapter"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_glacisRouter","type":"address"},{"internalType":"address","name":"_lzEndpoint","type":"address"},{"internalType":"address","name":"_owner","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"uint256","name":"toChainId","type":"uint256"}],"name":"GlacisAbstractAdapter__ChainIsNotAvailable","type":"error"},{"inputs":[],"name":"GlacisAbstractAdapter__DestinationChainIdNotValid","type":"error"},{"inputs":[],"name":"GlacisAbstractAdapter__IDArraysMustBeSameLength","type":"error"},{"inputs":[{"internalType":"uint256","name":"chainId","type":"uint256"}],"name":"GlacisAbstractAdapter__NoRemoteAdapterForChainId","type":"error"},{"inputs":[],"name":"GlacisAbstractAdapter__OnlyAdapterAllowed","type":"error"},{"inputs":[],"name":"GlacisAbstractAdapter__OnlyGlacisRouterAllowed","type":"error"},{"inputs":[],"name":"GlacisLayerZeroV2Adapter__PeersDisabledUseCounterpartInstead","type":"error"},{"inputs":[],"name":"GlacisRemoteCounterpartManager__CounterpartsAndChainIDsMustHaveSameLength","type":"error"},{"inputs":[],"name":"GlacisRemoteCounterpartManager__RemoteCounterpartCannotHaveChainIdZero","type":"error"},{"inputs":[],"name":"InvalidDelegate","type":"error"},{"inputs":[],"name":"InvalidEndpointCall","type":"error"},{"inputs":[{"internalType":"uint16","name":"optionType","type":"uint16"}],"name":"InvalidOptionType","type":"error"},{"inputs":[{"internalType":"uint32","name":"eid","type":"uint32"}],"name":"NoPeer","type":"error"},{"inputs":[{"internalType":"address","name":"addr","type":"address"}],"name":"OnlyEndpoint","type":"error"},{"inputs":[{"internalType":"uint32","name":"eid","type":"uint32"},{"internalType":"bytes32","name":"sender","type":"bytes32"}],"name":"OnlyPeer","type":"error"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256[]","name":"chainIDs","type":"uint256[]"},{"indexed":false,"internalType":"uint32[]","name":"lzIDs","type":"uint32[]"}],"name":"GlacisLayerZeroV2Adapter__SetGlacisChainIDs","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferStarted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint32","name":"eid","type":"uint32"},{"indexed":false,"internalType":"bytes32","name":"peer","type":"bytes32"}],"name":"PeerSet","type":"event"},{"inputs":[],"name":"GLACIS_ROUTER","outputs":[{"internalType":"contract IGlacisRouter","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"acceptOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"chainId","type":"uint256"}],"name":"adapterChainID","outputs":[{"internalType":"uint32","name":"","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint32","name":"","type":"uint32"}],"name":"adapterChainIdToGlacisChainId","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256[]","name":"chainIDs","type":"uint256[]"},{"internalType":"bytes32[]","name":"counterpart","type":"bytes32[]"}],"name":"addRemoteCounterparts","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint32","name":"srcEid","type":"uint32"},{"internalType":"bytes32","name":"sender","type":"bytes32"},{"internalType":"uint64","name":"nonce","type":"uint64"}],"internalType":"struct Origin","name":"origin","type":"tuple"}],"name":"allowInitializePath","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"chainId","type":"uint256"}],"name":"chainIsAvailable","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"endpoint","outputs":[{"internalType":"contract ILayerZeroEndpointV2","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"chainId","type":"uint256"}],"name":"getRemoteCounterpart","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"uint32","name":"srcEid","type":"uint32"},{"internalType":"bytes32","name":"sender","type":"bytes32"},{"internalType":"uint64","name":"nonce","type":"uint64"}],"internalType":"struct Origin","name":"","type":"tuple"},{"internalType":"bytes","name":"","type":"bytes"},{"internalType":"address","name":"_sender","type":"address"}],"name":"isComposeMsgSender","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"uint32","name":"srcEid","type":"uint32"},{"internalType":"bytes32","name":"sender","type":"bytes32"},{"internalType":"uint64","name":"nonce","type":"uint64"}],"internalType":"struct Origin","name":"_origin","type":"tuple"},{"internalType":"bytes32","name":"_guid","type":"bytes32"},{"internalType":"bytes","name":"_message","type":"bytes"},{"internalType":"address","name":"_executor","type":"address"},{"internalType":"bytes","name":"_extraData","type":"bytes"}],"name":"lzReceive","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint32","name":"","type":"uint32"},{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"nextNonce","outputs":[{"internalType":"uint64","name":"nonce","type":"uint64"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"oAppVersion","outputs":[{"internalType":"uint64","name":"senderVersion","type":"uint64"},{"internalType":"uint64","name":"receiverVersion","type":"uint64"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint32","name":"_eid","type":"uint32"}],"name":"peers","outputs":[{"internalType":"bytes32","name":"peer","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pendingOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"chainId","type":"uint256"}],"name":"removeRemoteCounterpart","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"toChainId","type":"uint256"},{"internalType":"address","name":"refundAddress","type":"address"},{"components":[{"internalType":"uint128","name":"gasLimit","type":"uint128"},{"internalType":"uint128","name":"nativeCurrencyValue","type":"uint128"}],"internalType":"struct GlacisCommons.CrossChainGas","name":"incentives","type":"tuple"},{"internalType":"bytes","name":"payload","type":"bytes"}],"name":"sendMessage","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"_delegate","type":"address"}],"name":"setDelegate","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256[]","name":"chainIDs","type":"uint256[]"},{"internalType":"uint32[]","name":"lzIDs","type":"uint32[]"}],"name":"setGlacisChainIds","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint32","name":"","type":"uint32"},{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"setPeer","outputs":[],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"}]