LayerZeroGatewayAdapterLib
Title: LayerZeroGatewayAdapterLib
Authors: David Dada <daveproxy80@gmail.com> (https://github.com/dadadave80), Modified from LayerZero v2 (https://github.com/LayerZero-Labs/LayerZero-v2)
Logic + ERC-7201 storage for the LayerZero v2 ERC-7786 gateway adapter. sendMessage builds a
LayerZero MessagingParams, quotes the native fee via endpoint.quote, and dispatches via
endpoint.send (native fee only, excess refunded); lzReceive is the endpoint-gated delivery
callback that validates the source eid + trusted peer, de-duplicates per (chainId, guid), and
delivers to the ERC-7930 recipient. EVM chains only. LayerZero routes by uint32 eid, not chainId.
The adapter is its own OApp. Wire message = abi.encode(senderInteropAddr, recipientInteropAddr, innerPayload); the LayerZero receiver field targets the trusted 32-byte peer, which forwards to the
final recipient. options is a Type-3 executor lzReceive blob synthesized INLINE from the admin
per-dest gas + msg.value (no OptionsBuilder dependency).
Constants
OPTIONS_TYPE_3
LayerZero Type-3 options container prefix.
uint16 private constant OPTIONS_TYPE_3 = 3;EXECUTOR_WORKER_ID
Executor worker id inside a Type-3 options blob.
uint8 private constant EXECUTOR_WORKER_ID = 1;OPTION_TYPE_LZRECEIVE
Executor option type for the destination lzReceive call.
uint8 private constant OPTION_TYPE_LZRECEIVE = 1;Functions
layerZeroGatewayAdapterStorage
function layerZeroGatewayAdapterStorage() internal pure returns (LayerZeroGatewayAdapterStorage storage $);Returns
| Name | Type | Description |
|---|---|---|
| $ | LayerZeroGatewayAdapterStorage |
__LayerZeroGatewayAdapter_init
Stores the endpoint and registers the gateway-source ERC-165 id. The adapter is its own OApp;
setDelegate is NOT called at init (only needed to reconfigure LayerZero libs later).
function __LayerZeroGatewayAdapter_init(address endpoint_) internal;Parameters
| Name | Type | Description |
|---|---|---|
| endpoint_ | address |
registerInterface
Writes true to the SHARED IERC7786GatewaySource ERC-165 map slot (0x11967553 → ...).
Same slot the CCIP/Wormhole/Axelar adapters register; a Diamond mounts at most one gateway.
function registerInterface() internal;endpoint
function endpoint() internal view returns (address);Returns
| Name | Type | Description |
|---|---|---|
| <none> | address |
getEid
function getEid(uint256 chainId) internal view returns (uint32);Parameters
| Name | Type | Description |
|---|---|---|
| chainId | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint32 |
getChainId
function getChainId(uint32 eid) internal view returns (uint256);Parameters
| Name | Type | Description |
|---|---|---|
| eid | uint32 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint256 |
getPeer
function getPeer(uint256 chainId) internal view returns (bytes32);Parameters
| Name | Type | Description |
|---|---|---|
| chainId | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | bytes32 |
getDestinationGas
function getDestinationGas(uint256 chainId) internal view returns (uint128);Parameters
| Name | Type | Description |
|---|---|---|
| chainId | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint128 |
getDestinationMsgValue
function getDestinationMsgValue(uint256 chainId) internal view returns (uint128);Parameters
| Name | Type | Description |
|---|---|---|
| chainId | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint128 |
supportsAttribute
No sendMessage attributes are supported by this adapter (per-dest gas is admin-configured).
function supportsAttribute(bytes4) internal pure returns (bool);Parameters
| Name | Type | Description |
|---|---|---|
| _ | bytes4 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | bool |
quoteFee
Quotes the LayerZero native fee to send payload to recipient (ERC-7930).
function quoteFee(bytes calldata recipient, bytes calldata payload) internal view returns (uint256);Parameters
| Name | Type | Description |
|---|---|---|
| recipient | bytes | |
| payload | bytes |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint256 |
allowInitializePath
LayerZero receiver policy: whether the OApp accepts a new path from _origin (peer must match).
function allowInitializePath(Origin calldata origin) internal view returns (bool);Parameters
| Name | Type | Description |
|---|---|---|
| origin | Origin |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | bool |
nextNonce
LayerZero receiver policy: unordered delivery, so no nonce is enforced (always 0).
function nextNonce(uint32, bytes32) internal pure returns (uint64);Parameters
| Name | Type | Description |
|---|---|---|
| _ | uint32 | |
| _ | bytes32 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint64 |
registerEid
function registerEid(uint256 chainId, uint32 eid) internal;Parameters
| Name | Type | Description |
|---|---|---|
| chainId | uint256 | |
| eid | uint32 |
registerPeer
function registerPeer(uint256 chainId, bytes32 peer) internal;Parameters
| Name | Type | Description |
|---|---|---|
| chainId | uint256 | |
| peer | bytes32 |
configureDestination
function configureDestination(uint256 chainId, uint128 gas, uint128 msgValue) internal;Parameters
| Name | Type | Description |
|---|---|---|
| chainId | uint256 | |
| gas | uint128 | |
| msgValue | uint128 |
sendMessage
ERC-7786 source. Quotes the LayerZero native fee and dispatches the message immediately via the
endpoint. The fee is paid by msg.sender via msg.value (excess refunded); LayerZero refunds
any protocol-side native surplus to msg.sender too. Diamond funds are never spent.
No attributes are supported — any attribute reverts UnsupportedAttribute. Returns the LayerZero
guid as the ERC-7786 sendId.
function sendMessage(bytes calldata recipient, bytes calldata payload, bytes[] calldata attributes)
internal
returns (bytes32);Parameters
| Name | Type | Description |
|---|---|---|
| recipient | bytes | |
| payload | bytes | |
| attributes | bytes[] |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | bytes32 |
lzReceive
LayerZero delivery callback: DUAL AUTH — msg.sender must be the endpoint AND origin.sender
must equal the trusted peer registered for the source eid's chain. De-dups per (chainId, guid),
marking the guid executed BEFORE the external delivery (checks-effects-interactions), then
delivers to the ERC-7930 recipient encoded in the message.
function lzReceive(Origin calldata origin, bytes32 guid, bytes calldata message) internal;Parameters
| Name | Type | Description |
|---|---|---|
| origin | Origin | |
| guid | bytes32 | |
| message | bytes |
_buildParams
Builds the LayerZero MessagingParams for recipient/payload.
Reverts UnknownDestinationChain if the eid/peer is unset, DestinationNotConfigured if the
executor gas is unset. Wraps the final ERC-7930 recipient inside the wire message and synthesizes
the Type-3 options from the admin per-dest gas + msg.value.
function _buildParams(bytes calldata recipient, bytes calldata payload)
private
view
returns (MessagingParams memory params);Parameters
| Name | Type | Description |
|---|---|---|
| recipient | bytes | |
| payload | bytes |
Returns
| Name | Type | Description |
|---|---|---|
| params | MessagingParams |
_buildOptions
Synthesizes a LayerZero Type-3 options blob carrying a single executor lzReceive option with
gas (and value when non-zero). Built inline to avoid an OptionsBuilder dependency.
Layout: type(uint16) || workerId(uint8) || optionLen(uint16) || optionType(uint8) || gas(uint128) [|| value(uint128)]. optionLen counts optionType (1) + the option data (16, or 32 with value).
function _buildOptions(uint128 gas, uint128 value) private pure returns (bytes memory);Parameters
| Name | Type | Description |
|---|---|---|
| gas | uint128 | |
| value | uint128 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | bytes |