bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 74쪽
"},{"inputs":[{"name":"_is_enabled","type":"bool"},{"name":"_read_channel","type":"uint32"},{"name":"_mainnet_eid","type":"uint32"},{"name":"_mainnet_view","type":"address"}],"name":"set_read_config","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"_eids","type":"uint32[]"},{"name":"_peers","type":"address[]"}],"name":"set_peers","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"_oracle","type":"address"}],"name":"set_block_oracle","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"_amount","type":"uint256"}],"name":"withdraw_eth","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"fallback"},{"inputs":[{"name":"_read_gas_limit","type":"uint128"},{"name":"_value","type":"uint128"}],"name":"quote_read_fee","outputs":[{"name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"name":"_target_eids","type":"uint32[]"},{"name":"_lz_receive_gas_limit","type":"uint128"}],"name":"quote_broadcast_fees","outputs":[{"name":"","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"name":"_target_eids","type":"uint32[]"},{"name":"_target_fees","type":"uint256[]"},{"name":"_lz_receive_gas_limit","type":"uint128"},{"name":"_read_gas_limit","type":"uint128"}],"name":"request_block_hash","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"name":"_target_eids","type":"uint32[]"},{"name":"_target_fees","type":"uint256[]"},{"name":"_lz_receive_gas_limit","type":"uint128"},{"name":"_read_gas_limit","type":"uint128"},{"name":"_block_number","type":"uint256"}],"name":"request_block_hash","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"name":"_target_eids","type":"uint32[]"},{"name":"_target_fees","type":"uint256[]"},{"name":"_lz_receive_gas_limit","type":"uint128"}],"name":"broadcast_latest_block","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"name":"srcEid","type":"uint32"},{"name":"sender","type":"bytes32"},{"name":"nonce","type":"uint64"}],"name":"_origin","type":"tuple"},{"name":"_guid","type":"bytes32"},{"name":"_message","type":"bytes"},{"name":"_executor","type":"address"},{"name":"_extraData","type":"bytes"}],"name":"lzReceive","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"read_enabled","outputs":[{"name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"read_channel","outputs":[{"name":"","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"mainnet_eid","outputs":[{"name":"","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"mainnet_block_view","outputs":[{"name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"block_oracle","outputs":[{"name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"name":"_endpoint","type":"address"}],"outputs":[],"stateMutability":"nonpayable","type":"constructor"}]
```
* * *
<a id="configuration--ownership"></a>
## Configuration & Ownership
This section covers owner-only functions for configuring LayerZero channels, peers, delegates, and the block oracle. These functions are critical for secure cross-chain operation and must be managed by the contract owner (DAO).
<a id="set_read_config"></a>
### `set_read_config`
`LZBlockRelay.set_read_config(_is_enabled: bool, _read_channel: uint32, _mainnet_eid: uint32, _mainnet_view: address)`
Guarded Method by [Snekmate](https://github.com/pcaversaccio/snekmate)
This contract makes use of a Snekmate module to manage roles and permissions. This specific function can only be called by the current `owner` of the contract.
Function to configure read functionality.
| Input | Type | Description |
| --- | --- | --- |
| `_is_enabled` | `bool` | Whether this contract can initiate reads |
| `_read_channel` | `uint32` | LZ read channel ID |
| `_mainnet_eid` | `uint32` | Mainnet endpoint ID |
| `_mainnet_view` | `address` | MainnetBlockView contract address |
Emits: `PeerSet` event.
**<>Source code▼**
```vyper
read_enabled: public(bool)
read_channel: public(uint32)
mainnet_eid: public(uint32)
mainnet_block_view: public(address)
@external
def set_read_config(
_is_enabled: bool, _read_channel: uint32, _mainnet_eid: uint32, _mainnet_view: address
):
"""
@notice Configure read functionality
@param _is_enabled Whether this contract can initiate reads
@param _read_channel LZ read channel ID
@param _mainnet_eid Mainnet endpoint ID
@param _mainnet_view MainnetBlockView contract address
"""
ownable._check_owner()
assert _read_channel > OApp.READ_CHANNEL_THRESHOLD, "Invalid read channel"
assert (_is_enabled and _mainnet_eid != 0 and _mainnet_view != empty(address)) or (
not _is_enabled and _mainnet_eid == 0 and _mainnet_view == empty(address)
), "Invalid read config"
# Clean up old peer if switching channels while read is enabled
# This prevents leaving stale peer mappings when changing read channels
if self.read_enabled and self.read_channel != _read_channel:
OApp._setPeer(self.read_channel, convert(empty(address), bytes32))
self.read_enabled = _is_enabled
self.read_channel = _read_channel
self.mainnet_eid = _mainnet_eid
self.mainnet_block_view = _mainnet_view
peer: bytes32 = convert(self, bytes32) if _is_enabled else convert(empty(address), bytes32)
OApp._setPeer(_read_channel, peer)
```
```vyper
@internal
def _check_owner():
"""
@dev Throws if the sender is not the owner.
"""
assert msg.sender == self.owner, "ownable: caller is not the owner"
```
```vyper
event PeerSet:
eid: uint32
peer: bytes32
# Mapping to store peers associated with corresponding endpoints
peers: public(HashMap[uint32, bytes32])
@internal
def _setPeer(_eid: uint32, _peer: bytes32):
"""
@notice Internal function to set peer address
@param _eid The endpoint ID.
@param _peer The address of the peer to be associated with the corresponding endpoint.
"""
self.peers[_eid] = _peer
log PeerSet(eid=_eid, peer=_peer)
```
**▶Example▼**
```shell
>>> LZBlockRelay.set_read_config(True, 30101, 30101, '0xb10cface00696B1390875DB2a0113B3ab99752a4')
```
<a id="set_block_oracle"></a>
### `set_block_oracle`
`LZBlockRelay.set_block_oracle(_oracle: address)`
Guarded Method by [Snekmate](https://github.com/pcaversaccio/snekmate)
This contract makes use of a Snekmate module to manage roles and permissions. This specific function can only be called by the current `owner` of the contract.
Sets the BlockOracle address for this contract.
| Input | Type | Description |
| --- | --- | --- |
| `_oracle` | `address` | The BlockOracle address to set. |
**<>Source code▼**
```vyper
interface IBlockOracle:
def commit_block(block_number: uint256, block_hash: bytes32) -> bool: nonpayable
def last_confirmed_block_number() -> uint256: view
def get_block_hash(block_number: uint256) -> bytes32: view
from snekmate.auth import ownable
@external
def set_block_oracle(_oracle: address):
"""
@notice Set the block oracle address
@param _oracle Block oracle address
"""
ownable._check_owner()
self.block_oracle = IBlockOracle(_oracle)
```
```vyper
@internal
def _check_owner():
"""
@dev Throws if the sender is not the owner.
"""
assert msg.sender == self.owner, "ownable: caller is not the owner"
```
**▶Example▼**
```shell
>>> LZBlockRelay.set_block_oracle('0xb10cface698eBbEeda6Fd1aC3e1687a8a3f5c5Df')
```
<a id="withdraw_eth"></a>
### `withdraw_eth`
`LZBlockRelay.withdraw_eth(_amount: uint256)`
Guarded Method by [Snekmate](https://github.com/pcaversaccio/snekmate)
This contract makes use of a Snekmate module to manage roles and permissions. This specific function can only be called by the current `owner` of the contract.
Withdraws ETH from the contract. ETH can be accumulated from LayerZero refunds.
| Input | Type | Description |
| --- | --- | --- |
| `_amount` | `uint256` | Amount of ETH to withdraw. |
**<>Source code▼**
```vyper
from snekmate.auth import ownable
@external
def withdraw_eth(_amount: uint256):
"""
@notice Withdraw ETH from contract
@dev ETH can be accumulated from LZ refunds
@param _amount Amount to withdraw
"""
ownable._check_owner()
assert self.balance >= _amount, "Insufficient balance"
send(msg.sender, _amount)
```
```vyper
@internal
def _check_owner():
"""
@dev Throws if the sender is not the owner.
"""
assert msg.sender == self.owner, "ownable: caller is not the owner"
```
**▶Example▼**
```shell
>>> LZBlockRelay.withdraw_eth(1000000000000000000)
```
* * *
<a id="layerzero-messaging--peers"></a>
## LayerZero Messaging & Peers
This section documents LayerZero-specific configuration and peer management. These functions are critical for secure cross-chain communication. Only trusted peers should be set to avoid malicious message injection.
<a id="endpoint"></a>
### `endpoint`
`LZBlockRelay.endpoint() -> address: view`
Getter for the LayerZero endpoint.
Returns: Lz Endpoint (`address`).
**<>Source code▼**
```vyper
from ..modules.oapp_vyper.src import OApp # main module
exports: (
OApp.endpoint,
OApp.peers,
OApp.setPeer,
OApp.setDelegate,
OApp.isComposeMsgSender,
OApp.allowInitializePath,
OApp.nextNonce,
)
```
```vyper
# LayerZero EndpointV2 interface
interface ILayerZeroEndpointV2:
def quote(_params: MessagingParams, _sender: address) -> MessagingFee: view
def send(_params: MessagingParams, _refundAddress: address) -> MessagingReceipt: payable
def setDelegate(_delegate: address): nonpayable
def eid() -> uint32: view
def lzToken() -> address: view
# The LayerZero endpoint associated with the given OApp
endpoint: public(immutable(ILayerZeroEndpointV2))
```
**▶Example▼**
endpoint on Arbitrum
```shell
>>> LZBlockRelay.endpoint()
'0x1a44076050125825900e736c501f859c50fE728c'
```
<a id="peers"></a>
### `peers`
`LZBlockRelay.peers(_eid: uint32) -> bytes32: view`
Getter for the peer address (OApp instance) for a given endpoint ID.
| Input | Type | Description |
| --- | --- | --- |
| `_eid` | `uint32` | The endpoint ID. |
Returns: peer address for the given endpoint ID (`bytes32`).
**<>Source code▼**
```vyper
from ..modules.oapp_vyper.src import OApp # main module
exports: (
OApp.endpoint,
OApp.peers,
OApp.setPeer,
OApp.setDelegate,
OApp.isComposeMsgSender,
OApp.allowInitializePath,
OApp.nextNonce,
)
```
```vyper
# Mapping to store peers associated with corresponding endpoints
peers: public(HashMap[uint32, bytes32])
```
**▶Example▼**
```shell
>>> LZBlockRelay.peers(0)
'0x0000000000000000000000000000000000000000000000000000000000000000'
```
<a id="setpeer"></a>
### `setPeer`
`LZBlockRelay.setPeer(_eid: uint32, _peer: bytes32)`
Guarded Method by [Snekmate](https://github.com/pcaversaccio/snekmate)
This contract makes use of a Snekmate module to manage roles and permissions. This specific function can only be called by the current `owner` of the contract.
Sets the peer address (OApp instance) for a corresponding endpoint. This establishes a trusted cross-chain communication channel.
| Input | Type | Description |
| --- | --- | --- |
| `_eid` | `uint32` | The endpoint ID. |
| `_peer` | `bytes32` | The peer address (OApp instance) |
Emits: `PeerSet` event.
**<>Source code▼**
```vyper
from ..modules.oapp_vyper.src import OApp # main module
exports: (
OApp.endpoint,
OApp.peers,
OApp.setPeer,
OApp.setDelegate,
OApp.isComposeMsgSender,
OApp.allowInitializePath,
OApp.nextNonce,
)
```
```vyper
event PeerSet:
eid: uint32
peer: bytes32
# Mapping to store peers associated with corresponding endpoints
peers: public(HashMap[uint32, bytes32])
@external
def setPeer(_eid: uint32, _peer: bytes32):
"""
@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.
@dev Only the owner/admin of the OApp can call this function.
@dev Indicates that the peer is trusted to send LayerZero messages to this OApp.
@dev Set this to bytes32(0) to remove the peer address.
@dev Peer is a bytes32 to accommodate non-evm chains.
"""
ownable._check_owner()
self._setPeer(_eid, _peer)
@internal
def _setPeer(_eid: uint32, _peer: bytes32):
"""
@notice Internal function to set peer address
@param _eid The endpoint ID.
@param _peer The address of the peer to be associated with the corresponding endpoint.
"""
self.peers[_eid] = _peer
log PeerSet(eid=_eid, peer=_peer)
```
```vyper
@internal
def _check_owner():
"""
@dev Throws if the sender is not the owner.
"""
assert msg.sender == self.owner, "ownable: caller is not the owner"
```
**▶Example▼**
```shell
>>> LZBlockRelay.setPeer(30110, 0x000000000000000000000000FacEFeeD696BFC0ebe7EaD3FFBb9a56290d31752)
```
<a id="set_peers"></a>
### `set_peers`
`LZBlockRelay.set_peers(_eids: DynArray[uint32, MAX_N_BROADCAST], _peers: DynArray[address, MAX_N_BROADCAST])`
Guarded Method by [Snekmate](https://github.com/pcaversaccio/snekmate)
This contract makes use of a Snekmate module to manage roles and permissions. This specific function can only be called by the current `owner` of the contract.
Function to set peers for a corresponding endpoints. This is a batched version of the `OApp.setPeer` that accepts EVM addresses only.
| Input | Type | Description |
| --- | --- | --- |
| `_eids` | `DynArray[uint32, MAX_N_BROADCAST]` | The endpoint IDs |
| `_peers` | `DynArray[address, MAX_N_BROADCAST]` | Addresses of the peers to associate |
Emits: `PeerSet` event.
**<>Source code▼**
```vyper
from ..modules.oapp_vyper.src import OApp # main module
exports: (
OApp.endpoint,
OApp.peers,
OApp.setPeer,
OApp.setDelegate,
OApp.isComposeMsgSender,
OApp.allowInitializePath,
OApp.nextNonce,
)
@external
def set_peers(_eids: DynArray[uint32, MAX_N_BROADCAST], _peers: DynArray[address, MAX_N_BROADCAST]):
"""
@notice Set peers for a corresponding endpoints. Batched version of OApp.setPeer that accept address (EVM only).
@param _eids The endpoint IDs.
@param _peers Addresses of the peers to be associated with the corresponding endpoints.
"""
ownable._check_owner()
assert len(_eids) == len(_peers), "Invalid peer arrays"
for i: uint256 in range(0, len(_eids), bound=MAX_N_BROADCAST):
OApp._setPeer(_eids[i], convert(_peers[i], bytes32))
```
```vyper
@internal
def _check_owner():
"""
@dev Throws if the sender is not the owner.
"""
assert msg.sender == self.owner, "ownable: caller is not the owner"
```
```vyper
event PeerSet:
eid: uint32
peer: bytes32
# Mapping to store peers associated with corresponding endpoints
peers: public(HashMap[uint32, bytes32])
@internal
def _setPeer(_eid: uint32, _peer: bytes32):
"""
@notice Internal function to set peer address
@param _eid The endpoint ID.
@param _peer The address of the peer to be associated with the corresponding endpoint.
"""
self.peers[_eid] = _peer
log PeerSet(eid=_eid, peer=_peer)
```
**▶Example▼**
```shell
>>> LZBlockRelay.set_peers([30110, 30111], ['0xfacefeed69e0eb9dB6Ad8Cb0883fC45Df7561Dc2', '0xfacefeed69e0eb9dB6Ad8Cb0883fC45Df7561Dc2'])
```
<a id="setdelegate"></a>
### `setDelegate`
`LZBlockRelay.setDelegate(_delegate: address)`
Guarded Method by [Snekmate](https://github.com/pcaversaccio/snekmate)
This contract makes use of a Snekmate module to manage roles and permissions. This specific function can only be called by the current `owner` of the contract.
Sets the delegate address for the OApp. The delegate can manage LayerZero configurations on behalf of the contract.
| Input | Type | Description |
| --- | --- | --- |
| `_delegate` | `address` | The address of the delegate to set. |
**<>Source code▼**
```vyper
from ..modules.oapp_vyper.src import OApp # main module
exports: (
OApp.endpoint,
OApp.peers,
OApp.setPeer,
OApp.setDelegate,
OApp.isComposeMsgSender,
OApp.allowInitializePath,
OApp.nextNonce,
)
```
```vyper
# LayerZero EndpointV2 interface
interface ILayerZeroEndpointV2:
def quote(_params: MessagingParams, _sender: address) -> MessagingFee: view
def send(_params: MessagingParams, _refundAddress: address) -> MessagingReceipt: payable
def setDelegate(_delegate: address): nonpayable
def eid() -> uint32: view
def lzToken() -> address: view
@external
def setDelegate(_delegate: address):
"""
@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.
"""
ownable._check_owner()
extcall endpoint.setDelegate(_delegate)
```
```vyper
@internal
def _check_owner():
"""
@dev Throws if the sender is not the owner.
"""
assert msg.sender == self.owner, "ownable: caller is not the owner"
```
**▶Example▼**
```shell
>>> LZBlockRelay.setDelegate('0x1234567890123456789012345678901234567890')
```
<a id="iscomposemsgsender"></a>
### `isComposeMsgSender`
`LZBlockRelay.isComposeMsgSender(_origin: Origin, _message: Bytes[MAX_MESSAGE_SIZE], _sender: address) -> bool: view`
Function to check whether an address is an approved composeMsg sender to the Endpoint.
| Input | Type | Description |
| --- | --- | --- |
| `_origin` | `Origin` | Struct containing of srcEid, sender and nonce |
| `_message` | `Bytes[MAX_MESSAGE_SIZE]` | The lzReceive payload |
| `_sender` | `address` | The sender address |
Returns: true or false (`bool`).
**<>Source code▼**
```vyper
from ..modules.oapp_vyper.src import OApp # main module
exports: (
OApp.endpoint,
OApp.peers,
OApp.setPeer,
OApp.setDelegate,
OApp.isComposeMsgSender,
OApp.allowInitializePath,
OApp.nextNonce,
)
```
```vyper
struct Origin:
srcEid: uint32
sender: bytes32
nonce: uint64
@external
@view
def isComposeMsgSender(
_origin: Origin, _message: Bytes[MAX_MESSAGE_SIZE], _sender: address
) -> bool:
"""
@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.
@param _message The lzReceive payload.
@param _sender The sender address.
@return isSender Is a valid sender.
"""
return _sender == self
```
**▶Example▼**
```shell
>>> LZBlockRelay.isComposeMsgSender(origin, message, '0xfacefeed69e0eb9dB6Ad8Cb0883fC45Df7561Dc2')
True
```
<a id="allowinitializepath"></a>
### `allowInitializePath`
`LZBlockRelay.allowInitializePath(_origin: Origin) -> bool: view`
Function to check if the path initialization is allowed based on the provided origin.
| Input | Type | Description |
| --- | --- | --- |
| `_origin` | `Origin` | Struct containing of srcEid, sender and nonce |
Returns: true or false (`bool`).
**<>Source code▼**
```vyper
from ..modules.oapp_vyper.src import OApp # main module
exports: (
OApp.endpoint,
OApp.peers,
OApp.setPeer,
OApp.setDelegate,
OApp.isComposeMsgSender,
OApp.allowInitializePath,
OApp.nextNonce,
)
```
```vyper
struct Origin:
srcEid: uint32
sender: bytes32
nonce: uint64
@external
@view
def allowInitializePath(_origin: Origin) -> bool:
"""
@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.
"""
return self.peers[_origin.srcEid] == _origin.sender
```
**▶Example▼**
```shell
>>> LZBlockRelay.allowInitializePath(origin)
True
```
<a id="nextnonce"></a>
### `nextNonce`
`LZBlockRelay.nextNonce(_srcEid: uint32, _sender: bytes32) -> uint64: pure`
warning
Vyper-specific: If your app relies on o프로젝트로 돌아가기