Solidity API
EndpointV2
lzToken
address lzToken
This stores the address of the LayerZero token, which may be used for paying messaging fees. It enables applications to settle cross-chain communication costs using LayerZero's native token, where applicable.
delegates
mapping(address => address) delegates
A mapping that allows address-based delegation. Applications (OApps) can delegate certain privileges to another address, authorizing the delegate to perform tasks on behalf of the original sender.
constructor
constructor(uint32 _eid, address _owner) public
The constructor initializes the LayerZero endpoint on a specific chain. It assigns a unique Endpoint ID (_eid
) to this instance, ensuring each chain has a distinct identifier for cross-chain messaging.
Parameters
Name | Type | Description |
---|---|---|
_eid | uint32 | the unique Endpoint Id for this deploy that all other Endpoints can use to send to it |
_owner | address |
quote
function quote(struct MessagingParams _params, address _sender) external view returns (struct MessagingFee)
This function returns a fee estimate for sending a cross-chain message, based on the parameters specified in _params
. The fee quote takes into account the current messaging cost, which might vary over time. Note that the actual messaging cost could differ if the fees change between the quote and the message send operation.
MESSAGING STEP 0
Parameters
Name | Type | Description |
---|---|---|
_params | struct MessagingParams | the messaging parameters |
_sender | address | the sender of the message |
send
function send(struct MessagingParams _params, address _refundAddress) external payable returns (struct MessagingReceipt)
This function sends a message to a destination chain through the LayerZero network. It also processes the associated fees, which can be either in native tokens or LayerZero tokens (lzToken
). If excess fees are supplied, the surplus is refunded to the provided _refundAddress
.
MESSAGING STEP 1 - OApp need to transfer the fees to the endpoint before sending the message
Parameters
Name | Type | Description |
---|---|---|
_params | struct MessagingParams | the messaging parameters |
_refundAddress | address | the address to refund both the native and lzToken |
_send
function _send(address _sender, struct MessagingParams _params) internal returns (struct MessagingReceipt, address)
An internal version of the send function that handles the underlying mechanics of sending a message. This function is called by external message-sending methods and ensures the message is routed to the appropriate destination with the correct fee management.
internal function for sending the messages used by all external send methods
Parameters
Name | Type | Description |
---|---|---|
_sender | address | the address of the application sending the message to the destination chain |
_params | struct MessagingParams | the messaging parameters |
verify
function verify(struct Origin _origin, address _receiver, bytes32 _payloadHash) external
On the destination chain, the message needs to be verified before being processed. This function checks the validity of the incoming message by comparing its origin and payload hash with the expected values.
MESSAGING STEP 2 - on the destination chain configured receive library verifies a message
Parameters
Name | Type | Description |
---|---|---|
_origin | struct Origin | a struct holding the srcEid, nonce, and sender of the message |
_receiver | address | the receiver of the message |
_payloadHash | bytes32 | the payload hash of the message |
lzReceive
function lzReceive(struct Origin _origin, address _receiver, bytes32 _guid, bytes _message, bytes _extraData) external payable
This is the final step in the message execution process. After the message has been verified, it is delivered to the intended recipient address. The function can pass additional extraData
if needed for execution.
MESSAGING STEP 3 - the last step execute a verified message to the designated receiver the execution provides the execution context (caller, extraData) to the receiver. the receiver can optionally assert the caller and validate the untrusted extraData cant reentrant because the payload is cleared before execution
Parameters
Name | Type | Description |
---|---|---|
_origin | struct Origin | the origin of the message |
_receiver | address | the receiver of the message |
_guid | bytes32 | the guid of the message |
_message | bytes | the message |
_extraData | bytes | the extra data provided by the executor. this data is untrusted and should be validated. |
lzReceiveAlert
function lzReceiveAlert(struct Origin _origin, address _receiver, bytes32 _guid, uint256 _gas, uint256 _value, bytes _message, bytes _extraData, bytes _reason) external
This function handles a failure in message delivery and provides an alert to the application. It logs the reason for the failure and the state of the message, allowing developers to debug message processing errors.
Parameters
Name | Type | Description |
---|---|---|
_origin | struct Origin | the origin of the message |
_receiver | address | the receiver of the message |
_guid | bytes32 | the guid of the message |
_gas | uint256 | |
_value | uint256 | |
_message | bytes | the message |
_extraData | bytes | the extra data provided by the executor. |
_reason | bytes | the reason for failure |
clear
function clear(address _oapp, struct Origin _origin, bytes32 _guid, bytes _message) external
This function allows an OApp (Omnichain Application) to clear a pending message manually. Instead of pushing the message through the standard delivery flow, the message is cleared from the queue, effectively marking it as processed or ignored.
_Oapp
uses this interface to clear a message.
this is a PULL mode versus the PUSH mode of lzReceive
the cleared message can be ignored by the app (effectively burnt)
authenticated by oapp_
Parameters
Name | Type | Description |
---|---|---|
_oapp | address | |
_origin | struct Origin | the origin of the message |
_guid | bytes32 | the guid of the message |
_message | bytes | the message |
setLzToken
function setLzToken(address _lzToken) public virtual
This function allows the owner to set or change the LayerZero token (lzToken
). This token may be used to pay for messaging fees. The function is designed to provide flexibility in case the initial configuration of the token was incorrect or needs to be updated. It should only be called by the contract owner.
Users should avoid approving non-LayerZero tokens to be spent by the EndpointV2
contract, as this function can override the token used for fees.
allows reconfiguration to recover from wrong configurations users should never approve the EndpointV2 contract to spend their non-layerzero tokens override this function if the endpoint is charging ERC20 tokens as native only owner
Parameters
Name | Type | Description |
---|---|---|
_lzToken | address | the new layer zero token address |
recoverToken
function recoverToken(address _token, address _to, uint256 _amount) external
This function allows the owner to recover tokens that were mistakenly sent to the EndpointV2
contract. It supports both native tokens (if _token
is set to 0x0
) and ERC20
tokens. This ensures that tokens accidentally locked in the contract can be safely retrieved by the owner.
recover the token sent to this contract by mistake only owner
Parameters
Name | Type | Description |
---|---|---|
_token | address | the token to recover. if 0x0 then it is native token |
_to | address | the address to send the token to |
_amount | uint256 | the amount to send |
_payToken
function _payToken(address _token, uint256 _required, uint256 _supplied, address _receiver, address _refundAddress) internal
This internal function handles payments in ERC20 tokens. It ensures that the sender has approved the endpoint to spend the specified tokens and processes the payment. If the supplied token amount exceeds the required amount, the excess is refunded to the specified _refundAddress
.
handling token payments on endpoint. the sender must approve the endpoint to spend the token internal function
Parameters
Name | Type | Description |
---|---|---|
_token | address | the token to pay |
_required | uint256 | the amount required |
_supplied | uint256 | the amount supplied |
_receiver | address | the receiver of the token |
_refundAddress | address |
_payNative
function _payNative(uint256 _required, uint256 _supplied, address _receiver, address _refundAddress) internal virtual
This internal function manages payments in native tokens (such as ETH). It processes the payment and refunds any excess amount to the _refundAddress
. If the endpoint charges ERC20 tokens as native, this function can be overridden.
handling native token payments on endpoint override this if the endpoint is charging ERC20 tokens as native internal function
Parameters
Name | Type | Description |
---|---|---|
_required | uint256 | the amount required |
_supplied | uint256 | the amount supplied |
_receiver | address | the receiver of the native token |
_refundAddress | address | the address to refund the excess to |
_suppliedLzToken
function _suppliedLzToken(bool _payInLzToken) internal view returns (uint256 supplied)
This internal view function returns the amount of LayerZero tokens (lzToken
) supplied for payment, but only if _payInLzToken
is set to true. It checks the balance of the lzToken
used to pay for the messaging fee.
get the balance of the lzToken as the supplied lzToken fee if payInLzToken is true
_suppliedNative
function _suppliedNative() internal view virtual returns (uint256)
This internal function returns the amount of native tokens supplied for the payment. If the endpoint charges ERC20 tokens as native tokens, this function can be overridden to handle such cases.
override this if the endpoint is charging ERC20 tokens as native
_assertMessagingFee
function _assertMessagingFee(struct MessagingFee _required, uint256 _suppliedNativeFee, uint256 _suppliedLzTokenFee) internal pure
This internal function verifies that the supplied fees (both native and lzToken
) are sufficient to cover the required messaging fees. If the supplied fees are insufficient, the function will assert an error.
Assert the required fees and the supplied fees are enough
nativeToken
function nativeToken() external view virtual returns (address)
This external view function returns the address of the native ERC20 token used by the endpoint if it charges ERC20 tokens as native tokens. If the contract uses actual native tokens (like ETH), it returns 0x0
.
override this if the endpoint is charging ERC20 tokens as native
Return Values
Name | Type | Description |
---|---|---|
[0] | address | 0x0 if using native. otherwise the address of the native ERC20 token |
setDelegate
function setDelegate(address _delegate) external
This function allows an OApp to authorize a delegate to act on its behalf. The delegate can configure settings or perform other operations related to the LayerZero endpoint, effectively giving another address certain administrative permissions over the OApp's endpoint interaction.
delegate is authorized by the oapp to configure anything in layerzero
_initializable
function _initializable(struct Origin _origin, address _receiver, uint64 _lazyInboundNonce) internal view returns (bool)
This internal view function checks whether a message from a specific origin can be initialized for delivery to the receiver. The function verifies that the message can be safely processed based on the lazyInboundNonce
, which controls the message order and flow.
_verifiable
function _verifiable(struct Origin _origin, address _receiver, uint64 _lazyInboundNonce) internal view returns (bool)
This internal function checks whether a message from the given origin is verifiable
for the receiver. It ensures that the message payload is valid and ready for execution based on the provided nonce and other checks.
A payload with a hash of bytes(0)
can never be submitted.
bytes(0) payloadHash can never be submitted
_assertAuthorized
function _assertAuthorized(address _oapp) internal view
This internal function ensures that the caller is either the OApp or its authorized delegate. It acts as an access control check to verify that only trusted entities can configure or interact with the OApp's LayerZero-related settings.
assert the caller to either be the oapp or the delegate
initializable
function initializable(struct Origin _origin, address _receiver) external view returns (bool)
This external view function checks whether a message from the given origin is ready to be initialized and processed for the specified receiver. It returns true if the message can be initialized.
verifiable
function verifiable(struct Origin _origin, address _receiver) external view returns (bool)
This external view function checks whether a message from the given origin is verifiable for the receiver. It confirms that the message payload has been received and validated.
EndpointV2Alt
EndpointV2Alt
is the LayerZero V2 endpoint contract designed for blockchain networks where ERC20
tokens are used as native tokens (instead of standard native tokens like ETH or BNB). This contract supports altFeeTokens, which are ERC20 tokens that can be used for paying messaging fees. The architecture is optimized to reduce gas costs by making certain configurations immutable.
LZ_OnlyAltToken
error LZ_OnlyAltToken()
This error is thrown when a non-ERC20 token is used in a context where only the altFeeToken
(an ERC20 token) is allowed. It enforces that only the designated ERC20 token is used for certain operations in the contract.
nativeErc20
address nativeErc20
This holds the address of the ERC20 token used as the native currency in this contract. The nativeErc20
token is immutable, meaning that once it's set, it cannot be changed. This saves gas by preventing unnecessary updates and checks. This token is used for paying fees when the chain doesn't have a standard native token.
the altFeeToken is used for fees when the native token has no value it is immutable for gas saving. only 1 endpoint for such chains
constructor
constructor(uint32 _eid, address _owner, address _altToken) public
The constructor initializes the EndpointV2Alt
contract, associating it with a unique Endpoint ID (_eid
). It also specifies the owner of the contract and the altFeeToken
(an ERC20 token) used for fees on the chain.
_payNative
function _payNative(uint256 _required, uint256 _supplied, address _receiver, address _refundAddress) internal
This internal function handles native payments in the context of this contract. Since the contract operates on chains using ERC20 tokens as native tokens, _payNative
processes payments in those tokens. If the supplied amount exceeds the required amount, the excess is refunded to the _refundAddress
.
handling native token payments on endpoint internal function
Parameters
Name | Type | Description |
---|---|---|
_required | uint256 | the amount required |
_supplied | uint256 | the amount supplied |
_receiver | address | the receiver of the native token |
_refundAddress | address | the address to refund the excess to |
_suppliedNative
function _suppliedNative() internal view returns (uint256)
This internal view function returns the amount of native tokens (ERC20 tokens, in this case) supplied for the payment. It is used to track the exact amount of tokens provided for a transaction, ensuring that the necessary fees are met.
return the balance of the native token
setLzToken
function setLzToken(address _lzToken) public
This function allows the contract owner to set or change the LayerZero token (lzToken
). The function checks if the new token address matches the current one before applying changes. The lzToken can be used for paying fees when applicable.
check if lzToken is set to the same address
nativeToken
function nativeToken() external view returns (address)
This external view function returns the address of the native ERC20 token used by the contract. If the contract uses actual native tokens, it returns 0x0
. Otherwise, it returns the address of the ERC20 token acting as the native currency on the chain.
override this if the endpoint is charging ERC20 tokens as native
Return Values
Name | Type | Description |
---|---|---|
[0] | address | 0x0 if using native. otherwise the address of the native ERC20 token |
EndpointV2View
EndpointV2View
is a contract used for viewing the state of LayerZero V2 messages, particularly related to whether a message is verifiable, executable, or initializable. This contract is typically used by other contracts or off-chain services that need to check the status of cross-chain messages.
initialize
function initialize(address _endpoint) external
The initialize function sets the reference to the LayerZero endpoint (_endpoint
). This endpoint is used for all subsequent verifications and message status checks. This function must be called before the contract can be used.
ExecutionState
enum ExecutionState {
NotExecutable,
VerifiedButNotExecutable,
Executable,
Executed
}
This enum defines the possible execution states for a message within the LayerZero system:
NotExecutable
: The message is not yet ready for execution.
VerifiedButNotExecutable
: The message has been verified, but something is preventing its execution (e.g., not enough gas).
Executable
: The message is ready to be executed.
Executed
: The message has been successfully executed.
EndpointV2ViewUpgradeable
EndpointV2ViewUpgradeable
is an upgradeable version of the EndpointV2View
, adding support for certain upgrades while maintaining compatibility with existing state.
EMPTY_PAYLOAD_HASH
bytes32 EMPTY_PAYLOAD_HASH
This constant represents an empty payload hash, which can be used to signal that no payload is associated with a message.
NIL_PAYLOAD_HASH
bytes32 NIL_PAYLOAD_HASH
This constant represents a "nil" payload hash, often used to indicate that a payload has been intentionally left out or invalidated.
endpoint
contract ILayerZeroEndpointV2 endpoint
This contract reference stores the LayerZero endpoint that the EndpointV2ViewUpgradeable
is interacting with. It is used for all message-related queries and verifications.
__EndpointV2View_init
function __EndpointV2View_init(address _endpoint) internal
This internal initialization function sets the reference to the LayerZero endpoint. This function must be called during the deployment process to initialize the contract.
__EndpointV2View_init_unchained
function __EndpointV2View_init_unchained(address _endpoint) internal
This is a version of the initialization function that is not chained. It can be used in scenarios where the contract needs to be initialized without triggering additional logic.
initializable
function initializable(struct Origin _origin, address _receiver) public view returns (bool)
This function checks if a message from a specific origin can be initialized for the provided receiver. It returns true if the message is ready to be processed (initialized).
verifiable
function verifiable(struct Origin _origin, address _receiver, address _receiveLib, bytes32 _payloadHash) public view returns (bool)
This function checks if a message from the given origin is verifiable for the provided receiver. It ensures that the payload hash matches and the message has been validated by the correct messaging library.
check if a message is verifiable.
executable
function executable(struct Origin _origin, address _receiver) public view returns (enum ExecutionState)
This function checks the execution state of a message for a given origin and receiver. It returns the current execution state, whether the message is NotExecutable
, VerifiedButNotExecutable
, Executable
, or Executed
.
check if a message is executable.
Return Values
Name | Type | Description |
---|---|---|
[0] | enum ExecutionState | ExecutionState of Executed, Executable, or NotExecutable |
MessageLibManager
MessageLibManager
manages the messaging libraries (msgLib
) that are used for sending and receiving messages in the LayerZero protocol. It controls the libraries that each application (OApp
) can use, either by directly assigning libraries or defaulting to LayerZero's settings.
blockedLibrary
address blockedLibrary
The blockedLibrary
is a specific library that is no longer allowed for sending or receiving messages.
registeredLibraries
address[] registeredLibraries
An array storing the addresses of all libraries that are registered and can be used for message sending or receiving.
isRegisteredLibrary
mapping(address => bool) isRegisteredLibrary
This mapping tracks whether a given library is registered, providing a quick way to verify if a library is eligible for use.
sendLibrary
mapping(address => mapping(uint32 => address)) sendLibrary
A mapping that stores the send libraries for each OApp (address
) and endpoint ID (uint32
). Each OApp can specify a library it wants to use for sending messages to a specific endpoint.
receiveLibrary
mapping(address => mapping(uint32 => address)) receiveLibrary
This mapping stores the receive libraries for each OApp (address
) and endpoint ID (uint32
). Each OApp can specify a library for handling received messages.
receiveLibraryTimeout
mapping(address => mapping(uint32 => struct IMessageLibManager.Timeout)) receiveLibraryTimeout
This mapping tracks the timeout period for a receive library. After a timeout period, the receive library may need to be updated or replaced. This helps manage library versioning and ensure that OApps can handle breaking changes in a safe manner.
defaultSendLibrary
mapping(uint32 => address) defaultSendLibrary
This mapping holds the default send library for each endpoint (uint32
). If an OApp does not specify a send library, the default send library for that endpoint is used.
defaultReceiveLibrary
mapping(uint32 => address) defaultReceiveLibrary
The default receive library for each endpoint is stored here. If an OApp does not specify a receive library, the system defaults to the configured library for that endpoint.
defaultReceiveLibraryTimeout
mapping(uint32 => struct IMessageLibManager.Timeout) defaultReceiveLibraryTimeout
This mapping tracks the timeout period for default receive libraries. After this period, the default library may need to be updated or retired.
constructor
constructor() internal
The constructor is internal and initializes the MessageLibManager. It ensures that all the necessary mappings and configurations are properly set up when the contract is deployed.
onlyRegistered
modifier onlyRegistered(address _lib)
This modifier ensures that only libraries registered with MessageLibManager
can call certain functions. It restricts access to unregistered libraries, safeguarding the system from misuse.
isSendLib
modifier isSendLib(address _lib)
This modifier ensures that only valid send libraries can call specific functions. It checks if the library is properly configured for sending messages.
isReceiveLib
modifier isReceiveLib(address _lib)
This modifier ensures that only valid receive libraries can call certain functions. It verifies the library's eligibility for processing received messages.
onlyRegisteredOrDefault
modifier onlyRegisteredOrDefault(address _lib)
This modifier allows both registered libraries and default libraries to access certain functions, ensuring that the system works even when custom libraries are not defined.
onlySupportedEid
modifier onlySupportedEid(address _lib, uint32 _eid)
This modifier ensures that a library supports a specific endpoint ID (_eid
). It checks if the library has been configured to handle messages for that endpoint.
check if the library supported the eid.
getRegisteredLibraries
function getRegisteredLibraries() external view returns (address[])
This function returns a list of all registered libraries. It allows users and applications to query which libraries are available for use.
getSendLibrary
function getSendLibrary(address _sender, uint32 _dstEid) public view returns (address lib)
This function retrieves the send library for a specific OApp (_sender
) and destination endpoint (_dstEid
). If the OApp has not specified a library, the default one is used.
If the Oapp does not have a selected Send Library, this function will resolve to the default library configured by LayerZero
Parameters
Name | Type | Description |
---|---|---|
_sender | address | The address of the Oapp that is sending the message |
_dstEid | uint32 | The destination endpoint id |
Return Values
Name | Type | Description |
---|---|---|
lib | address | address of the Send Library |
isDefaultSendLibrary
function isDefaultSendLibrary(address _sender, uint32 _dstEid) public view returns (bool)
This function checks if the send library in use for a specific OApp and endpoint is the default one.
getReceiveLibrary
function getReceiveLibrary(address _receiver, uint32 _srcEid) public view returns (address lib, bool isDefault)
This function retrieves the receive library for a specific OApp (_receiver
) and source endpoint (_srcEid
). If the OApp has not specified a library, the default one is used.
the receiveLibrary can be lazily resolved that if not set it will point to the default configured by LayerZero
isValidReceiveLibrary
function isValidReceiveLibrary(address _receiver, uint32 _srcEid, address _actualReceiveLib) public view returns (bool)
This function checks if the specified receive library is valid for a given OApp, ensuring that the OApp can trust the message verification and processing done by the library.
called when the endpoint checks if the msgLib attempting to verify the msg is the configured msgLib of the Oapp this check provides the ability for Oapp to lock in a trusted msgLib it will fist check if the msgLib is the currently configured one. then check if the msgLib is the one in grace period of msgLib versioning upgrade
registerLibrary
function registerLibrary(address _lib) public
This function registers a new library with the MessageLibManager
. Only the contract owner can register new libraries.
all libraries have to implement the erc165 interface to prevent wrong configurations only owner
setDefaultSendLibrary
function setDefaultSendLibrary(uint32 _eid, address _newLib) external
The contract owner sets the default send library for a specific endpoint. The new library must be registered and have support for the endpoint.
owner setting the defaultSendLibrary can set to the blockedLibrary, which is a registered library the msgLib must enable the support before they can be registered to the endpoint as the default only owner
setDefaultReceiveLibrary
function setDefaultReceiveLibrary(uint32 _eid, address _newLib, uint256 _gracePeriod) external
The contract owner sets the default receive library for a specific endpoint and may define a grace period during which the old library can still be used.
owner setting the defaultSendLibrary must be a registered library (including blockLibrary) with the eid support enabled in version migration, it can add a grace period to the old library. if the grace period is 0, it will delete the timeout configuration. only owner
setDefaultReceiveLibraryTimeout
function setDefaultReceiveLibraryTimeout(uint32 _eid, address _lib, uint256 _expiry) external
This function allows the contract owner to set a timeout for the default receive library for a given endpoint. After the timeout, the library may need to be updated or retired.
owner setting the defaultSendLibrary must be a registered library (including blockLibrary) with the eid support enabled can used to (1) extend the current configuration (2) force remove the current configuration (3) change to a new configuration
Parameters
Name | Type | Description |
---|---|---|
_eid | uint32 | |
_lib | address | |
_expiry | uint256 | the block number when lib expires |
isSupportedEid
function isSupportedEid(uint32 _eid) external view returns (bool)
This function checks if an endpoint is supported, returning true only if both the default send and receive libraries are set.
returns true only if both the default send/receive libraries are set