本文档介绍了OpenZeppelin社区合约中的跨链通信相关合约,特别是基于ERC-7786标准的网关和适配器。
在 https://docs.openzeppelin.com/community-contracts/api/crosschain 查看本文档效果更佳 |
网关是实现跨链通信的合约。根据 ERC-7786,它们既可以是消息源,也可以是消息目的地。
ERC7786Receiver
: ERC-7786 跨链消息接收器。
ERC7786OpenBridge
: ERC-7786 "M 中取 N" 网关。通过 M 个网关发送消息,如果 N 个网关收到消息,则在目的地执行。
开发者可以通过网关适配器访问互操作性协议。该库包括以下网关适配器:
AxelarGatewayBase
: Axelar 适配器的核心网关逻辑。
AxelarGatewaySource
: Axelar 的 ERC-7786 源网关适配器(发送端)。
AxelarGatewayDestination
: Axelar 的 ERC-7786 目标网关适配器(接收端)。
AxelarGatewayDuplex
: ERC-7786 网关适配器,它使用 Axelar 网络在两个方向(即发送和接收消息)上运行。
ERC7786OpenBridge
import "@openzeppelin/community-contracts/crosschain/ERC7786OpenBridge.sol";
M 中取 N 网关:通过 M 个独立网关发送你的消息。 如果 M 个网关中的 N 个达成一致,它将通过目标链上的等效桥传递给接收者。
函数
constructor(owner_, gateways_, threshold_)
supportsAttribute()
sendMessage(recipient, payload, attributes)
executeMessage(, sender, payload, attributes)
getGateways()
getThreshold()
getRemoteBridge(chain)
getRemoteBridge(chainType, chainReference)
addGateway(gateway)
removeGateway(gateway)
setThreshold(newThreshold)
registerRemoteBridge(bridge)
pause()
unpause()
sweep(to)
_addGateway(gateway)
_removeGateway(gateway)
_setThreshold(newThreshold)
_registerRemoteBridge(bridge)
可暂停的
paused()
_requireNotPaused()
_requirePaused()
_pause()
_unpause()
可拥有的
owner()
_checkOwner()
renounceOwnership()
transferOwnership(newOwner)
_transferOwnership(newOwner)
事件
OutboxDetails(sendId, outbox)
Received(receiveId, gateway)
ExecutionSuccess(receiveId)
ExecutionFailed(receiveId)
GatewayAdded(gateway)
GatewayRemoved(gateway)
ThresholdUpdated(threshold)
RemoteRegistered(remote)
可暂停的
Paused(account)
Unpaused(account)
可拥有的
OwnershipTransferred(previousOwner, newOwner)
IERC7786GatewaySource
MessageSent(sendId, sender, receiver, payload, value, attributes)
错误
UnsupportedNativeTransfer()
ERC7786OpenBridgeInvalidCrosschainSender()
ERC7786OpenBridgeAlreadyExecuted()
ERC7786OpenBridgeRemoteNotRegistered(chainType, chainReference)
ERC7786OpenBridgeGatewayAlreadyRegistered(gateway)
ERC7786OpenBridgeGatewayNotRegistered(gateway)
ERC7786OpenBridgeThresholdViolation()
ERC7786OpenBridgeInvalidExecutionReturnValue()
RemoteAlreadyRegistered(remote)
可暂停的
EnforcedPause()
ExpectedPause()
可拥有的
OwnableUnauthorizedAccount(account)
OwnableInvalidOwner(owner)
IERC7786GatewaySource
UnsupportedAttribute(selector)
constructor(address owner_, address[] gateways_, uint8 threshold_)
publicsupportsAttribute(bytes4) → bool
publicGetter 用于检查是否支持属性。
sendMessage(bytes recipient, bytes payload, bytes[] attributes) → bytes32 sendId
public使用内存而不是 calldata 可以避免堆栈太深错误
executeMessage(bytes32, bytes sender, bytes payload, bytes[] attributes) → bytes4
public此函数具有双重用途:
它将被 ERC-7786 网关调用,这些网关的消息来自源链上相应的桥。 这些“信号”被跟踪,直到达到阈值。 届时,消息将发送到目的地。
它也可以被任何人(包括 ERC-7786 网关)调用来重试执行。 如果自动执行(在达到阈值时触发)失败,并且有人想要重试,这可能很有用。
当消息由已知网关转发时,会发出 Received
事件。 如果已知网关多次调用此函数(对于给定消息),则只有第一次调用会计入阈值并发出 Received
事件。
在以下情况下,此函数将恢复:
消息格式不正确或并非来自源链上注册的桥。
有人试图重新执行已成功传递的消息。 这包括第二次使用已执行的消息调用此函数的网关。
(在 IERC7786Receiver
接收器上) 消息的执行成功但未能返回已执行的值。
在以下情况下,此函数不会恢复:
已知网关首次传递消息,并且该消息已执行。 在这种情况下,消息不会重新执行,并且会返回正确的“魔术值”。
(在 IERC7786Receiver
接收器上) 消息的执行恢复。 在这种情况下,会发出 ExecutionFailed
事件。
此函数发出:
Received
,当已知 ERC-7786 网关首次传递消息时。
ExecutionSuccess
,当消息成功传递到接收者时。
ExecutionFailed
,当消息传递到接收者恢复时(例如,由于 OOG 错误)。
接口需要此函数可支付。 即使我们不期望任何值,网关也可能会因未知原因传递<br>一些值。 在这种情况下,我们希望注册已传递消息的网关,并且<br>不恢复。 以这种方式累积的任何值都可以由管理员使用 sweep 函数恢复。 |
getGateways() → address[]
publicgetThreshold() → uint8
publicgetRemoteBridge(bytes chain) → bytes
publicgetRemoteBridge(bytes2 chainType, bytes chainReference) → bytes
publicaddGateway(address gateway)
publicremoveGateway(address gateway)
publicsetThreshold(uint8 newThreshold)
publicregisterRemoteBridge(bytes bridge)
publicpause()
publicunpause()
publicsweep(address payable to)
public如果通过 executeMessage
收到值,则使用恢复方法
_addGateway(address gateway)
internal_removeGateway(address gateway)
internal_setThreshold(uint8 newThreshold)
internal_registerRemoteBridge(bytes bridge)
internalOutboxDetails(bytes32 indexed sendId, struct ERC7786OpenBridge.Outbox[] outbox)
eventReceived(bytes32 indexed receiveId, address gateway)
eventExecutionSuccess(bytes32 indexed receiveId)
eventExecutionFailed(bytes32 indexed receiveId)
eventGatewayAdded(address indexed gateway)
eventGatewayRemoved(address indexed gateway)
eventThresholdUpdated(uint8 threshold)
eventRemoteRegistered(bytes remote)
eventUnsupportedNativeTransfer()
errorERC7786OpenBridgeInvalidCrosschainSender()
errorERC7786OpenBridgeAlreadyExecuted()
errorERC7786OpenBridgeRemoteNotRegistered(bytes2 chainType, bytes chainReference)
errorERC7786OpenBridgeGatewayAlreadyRegistered(address gateway)
errorERC7786OpenBridgeGatewayNotRegistered(address gateway)
errorERC7786OpenBridgeThresholdViolation()
errorERC7786OpenBridgeInvalidExecutionReturnValue()
errorRemoteAlreadyRegistered(bytes remote)
errorERC7786Receiver
import "@openzeppelin/community-contracts/crosschain/utils/ERC7786Receiver.sol";
符合 ERC-7786 的跨链消息接收器的基本实现。
此抽象合约公开了 executeMessage
函数,该函数用于与(一个或多个)目标网关进行通信。 此合约保留了两个未实现的函数:
_isKnownGateway
,一个内部 getter,用于验证合约是否将地址识别为有效的 ERC-7786 目标网关。 可以支持一个或多个网关。 请注意,任何为此函数返回 true 的恶意地址都能够模拟任何其他链上的任何帐户发送任何消息。
_processMessage
,内部函数,将在已验证的任何消息上调用。
函数
executeMessage(receiveId, sender, payload, attributes)
_isKnownGateway(instance)
_processMessage(gateway, receiveId, sender, payload, attributes)
错误
ERC7786ReceiverInvalidGateway(gateway)
ERC7786ReceivePassiveModeValue()
executeMessage(bytes32 receiveId, bytes sender, bytes payload, bytes[] attributes) → bytes4
public用于接收跨链消息的端点。
此函数可能由网关直接调用。
_isKnownGateway(address instance) → bool
internal虚拟 getter,返回地址是否为有效的 ERC-7786 网关。
_processMessage(address gateway, bytes32 receiveId, bytes sender, bytes payload, bytes[] attributes)
internal虚拟函数,应包含在收到跨链消息时要执行的逻辑。
ERC7786ReceiverInvalidGateway(address gateway)
errorERC7786ReceivePassiveModeValue()
errorAxelarGatewayBase
import "@openzeppelin/community-contracts/crosschain/axelar/AxelarGatewayBase.sol";
用于 Axelar 网络的跨链网关适配器的基本实现。
此合约允许开发人员注册链之间的等效性(即 ERC-7930 链类型和对 Axelar 链标识符的引用)和远程网关(即其他链上的网关)以促进跨链通信。
函数
constructor(_gateway)
getAxelarChain(input)
getErc7930Chain(input)
getRemoteGateway(chain)
getRemoteGateway(chainType, chainReference)
registerChainEquivalence(chain, axelar)
registerRemoteGateway(remote)
可拥有的
owner()
_checkOwner()
renounceOwnership()
transferOwnership(newOwner)
_transferOwnership(newOwner)
事件
RegisteredRemoteGateway(remote)
RegisteredChainEquivalence(erc7930binary, axelar)
可拥有的
OwnershipTransferred(previousOwner, newOwner)
错误
UnsupportedERC7930Chain(erc7930binary)
UnsupportedAxelarChain(axelar)
InvalidChainIdentifier(erc7930binary)
ChainEquivalenceAlreadyRegistered(erc7930binary, axelar)
RemoteGatewayAlreadyRegistered(chainType, chainReference)
可拥有的
OwnableUnauthorizedAccount(account)
OwnableInvalidOwner(owner)
内部变量
contract IAxelarGateway _axelarGateway
constructor(contract IAxelarGateway _gateway)
internal设置本地网关地址(即 Axelar 当前链的官方网关)。
getAxelarChain(bytes input) → string output
public返回等效链,给定一个 id,该 id 可以是二进制可互操作地址或 Axelar 网络标识符。
getErc7930Chain(string input) → bytes output
publicgetRemoteGateway(bytes chain) → bytes
public返回给定 chainType 和 chainReference 的远程网关的地址。
getRemoteGateway(bytes2 chainType, bytes chainReference) → bytes
publicregisterChainEquivalence(bytes chain, string axelar)
public注册二进制可互操作地址和 Axelar 网络标识符之间的链等效性。
registerRemoteGateway(bytes remote)
public注册远程网关的地址。
RegisteredRemoteGateway(bytes remote)
event已为链注册了远程网关。
RegisteredChainEquivalence(bytes erc7930binary, string axelar)
event已注册链等效性。
UnsupportedERC7930Chain(bytes erc7930binary)
error查询不受支持的链时发出的错误。
UnsupportedAxelarChain(string axelar)
errorInvalidChainIdentifier(bytes erc7930binary)
errorChainEquivalenceAlreadyRegistered(bytes erc7930binary, string axelar)
errorRemoteGatewayAlreadyRegistered(bytes2 chainType, bytes chainReference)
errorcontract IAxelarGateway _axelarGateway
internalAxelar 当前链的官方网关。
AxelarGatewaySource
import "@openzeppelin/community-contracts/crosschain/axelar/AxelarGatewaySource.sol";
用于 Axelar 网络的 ERC-7786 网关源适配器的实现。
该合约提供了一种使用 sendMessage
函数通过 Axelar 网络将消息发送到远程链的方法。
函数
supportsAttribute()
sendMessage(recipient, payload, attributes)
AxelarGatewayBase
getAxelarChain(input)
getErc7930Chain(input)
getRemoteGateway(chain)
getRemoteGateway(chainType, chainReference)
registerChainEquivalence(chain, axelar)
registerRemoteGateway(remote)
可拥有的
owner()
_checkOwner()
renounceOwnership()
transferOwnership(newOwner)
_transferOwnership(newOwner)
事件
AxelarGatewayBase
RegisteredRemoteGateway(remote)
RegisteredChainEquivalence(erc7930binary, axelar)
可拥有的
OwnershipTransferred(previousOwner, newOwner)
IERC7786GatewaySource
MessageSent(sendId, sender, receiver, payload, value, attributes)
错误
UnsupportedNativeTransfer()
AxelarGatewayBase
UnsupportedERC7930Chain(erc7930binary)
UnsupportedAxelarChain(axelar)
InvalidChainIdentifier(erc7930binary)
ChainEquivalenceAlreadyRegistered(erc7930binary, axelar)
RemoteGatewayAlreadyRegistered(chainType, chainReference)
可拥有的
OwnableUnauthorizedAccount(account)
OwnableInvalidOwner(owner)
IERC7786GatewaySource
UnsupportedAttribute(selector)
supportsAttribute(bytes4) → bool
publicGetter 用于检查是否支持属性。
sendMessage(bytes recipient, bytes payload, bytes[] attributes) → bytes32 sendId
external用于创建新消息的端点。 如果消息在可以发送到目标链之前需要进一步的(网关特定的)处理,则必须返回非零的 outboxId
。 否则,必须发送消息,并且此函数必须返回 0。
如果任何 attributes
不受支持,则此函数应恢复并出现 {UnsupportedAttribute} 错误。 其他错误应恢复并出现 ERC-7786 中未指定的错误。
UnsupportedNativeTransfer()
errorAxelarGatewayDestination
import "@openzeppelin/community-contracts/crosschain/axelar/AxelarGatewayDestination.sol";
用于双模 Axelar 网络的 ERC-7786 网关目标适配器的实现。
该合约实现了 AxelarExecutable 的 _execute
函数来执行消息,将 Axelar 的本机工作流转换为标准 ERC-7786。
函数
_execute(commandId, axelarSourceChain, axelarSourceAddress, adapterPayload)
AxelarExecutable
execute(commandId, sourceChain, sourceAddress, payload)
gateway()
AxelarGatewayBase
getAxelarChain(input)
getErc7930Chain(input)
getRemoteGateway(chain)
getRemoteGateway(chainType, chainReference)
registerChainEquivalence(chain, axelar)
registerRemoteGateway(remote)
可拥有的
owner()
_checkOwner()
renounceOwnership()
transferOwnership(newOwner)
_transferOwnership(newOwner)
事件
AxelarGatewayBase
RegisteredRemoteGateway(remote)
RegisteredChainEquivalence(erc7930binary, axelar)
可拥有的
OwnershipTransferred(previousOwner, newOwner)
错误
InvalidOriginGateway(axelarSourceChain, axelarSourceAddress)
ReceiverExecutionFailed()
IAxelarExecutable
InvalidAddress()
NotApprovedByGateway()
AxelarGatewayBase
UnsupportedERC7930Chain(erc7930binary)
UnsupportedAxelarChain(axelar)
InvalidChainIdentifier(erc7930binary)
ChainEquivalenceAlreadyRegistered(erc7930binary, axelar)
RemoteGatewayAlreadyRegistered(chainType, chainReference)
可拥有的
OwnableUnauthorizedAccount(account)
OwnableInvalidOwner(owner)
_execute(bytes32 commandId, string axelarSourceChain, string axelarSourceAddress, bytes adapterPayload)
internal执行跨链消息。
在此函数中:
axelarSourceChain
是 Axelar 格式。 不应期望它是正确的 ERC-7930 格式
axelarSourceAddress
是 Axelar 消息的发送者。 这应该是消息源自的链上的远程网关。 它不是 ERC-7786 跨链消息的发送者。
跨链消息发送者的正确 ERC-7930 编码可以在消息中找到
InvalidOriginGateway(string axelarSourceChain, string axelarSourceAddress)
errorReceiverExecutionFailed()
errorAxelarGatewayDuplex
import "@openzeppelin/community-contracts/crosschain/axelar/AxelarGatewayDuplex.sol";
一个合约,它结合了 Axelar 网络的源和目标网关适配器的功能。 允许跨链发送或接收消息。
函数
constructor(gateway, initialOwner)
AxelarGatewayDestination
_execute(commandId, axelarSourceChain, axelarSourceAddress, adapterPayload)
AxelarExecutable
execute(commandId, sourceChain, sourceAddress, payload)
gateway()
AxelarGatewaySource
supportsAttribute()
sendMessage(recipient, payload, attributes)
AxelarGatewayBase
getAxelarChain(input)
getErc7930Chain(input)
getRemoteGateway(chain)
getRemoteGateway(chainType, chainReference)
registerChainEquivalence(chain, axelar)
registerRemoteGateway(remote)
可拥有的
owner()
_checkOwner()
renounceOwnership()
transferOwnership(newOwner)
_transferOwnership(newOwner)
事件
AxelarGatewayBase
RegisteredRemoteGateway(remote)
RegisteredChainEquivalence(erc7930binary, axelar)
可拥有的
OwnershipTransferred(previousOwner, newOwner)
IERC7786GatewaySource
MessageSent(sendId, sender, receiver, payload, value, attributes)
错误
AxelarGatewayDestination
InvalidOriginGateway(axelarSourceChain, axelarSourceAddress)
ReceiverExecutionFailed()
IAxelarExecutable
InvalidAddress()
NotApprovedByGateway()
AxelarGatewaySource
UnsupportedNativeTransfer()
AxelarGatewayBase
UnsupportedERC7930Chain(erc7930binary)
UnsupportedAxelarChain(axelar)
InvalidChainIdentifier(erc7930binary)
ChainEquivalenceAlreadyRegistered(erc7930binary, axelar)
RemoteGatewayAlreadyRegistered(chainType, chainReference)
可拥有的
OwnableUnauthorizedAccount(account)
OwnableInvalidOwner(owner)
IERC7786GatewaySource
UnsupportedAttribute(selector)
constructor(contract IAxelarGateway gateway, address initialOwner)
public使用 Axelar 网关和初始所有者初始化合约。
- 原文链接: docs.openzeppelin.com/co...
- 登链社区 AI 助手,为大家转译优秀英文文章,如有翻译不通的地方,还请包涵~
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!