-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathLayerZeroAdapter.sol
177 lines (157 loc) · 6.35 KB
/
LayerZeroAdapter.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.8;
import {ILayerZeroReceiver} from './interfaces/ILayerZeroReceiver.sol';
import {MessagingParams, Origin, MessagingFee, MessagingReceipt} from './interfaces/ILayerZeroEndpointV2.sol';
import {SafeCast} from 'solidity-utils/contracts/oz-common/SafeCast.sol';
import {OptionsBuilder} from './libs/OptionsBuilder.sol';
import {BaseAdapter, IBaseAdapter} from '../BaseAdapter.sol';
import {ILayerZeroAdapter, ILayerZeroEndpointV2} from './ILayerZeroAdapter.sol';
import {ChainIds} from 'solidity-utils/contracts/utils/ChainHelpers.sol';
import {Errors} from '../../libs/Errors.sol';
/**
* @title LayerZeroAdapter
* @author BGD Labs
* @notice LayerZero bridge adapter. Used to send and receive messages cross chain
* @dev it uses the eth balance of CrossChainController contract to pay for message bridging as the method to bridge
is called via delegate call
*/
contract LayerZeroAdapter is BaseAdapter, ILayerZeroAdapter, ILayerZeroReceiver {
/// @inheritdoc ILayerZeroAdapter
ILayerZeroEndpointV2 public immutable LZ_ENDPOINT;
/// @notice modifier to check that caller is LayerZero endpoint
modifier onlyLZEndpoint() {
require(msg.sender == address(LZ_ENDPOINT), Errors.CALLER_NOT_LZ_ENDPOINT);
_;
}
/**
* @notice constructor for the Layer Zero adapter
* @param crossChainController address of the contract that manages cross chain infrastructure
* @param lzEndpoint address of the layer zero endpoint on the current chain where adapter is deployed
* @param providerGasLimit base gas limit used by the bridge adapter
* @param trustedRemotes array of objects with chain id and origin addresses which will be allowed to send messages to this adapter
*/
constructor(
address crossChainController,
address lzEndpoint,
uint256 providerGasLimit,
TrustedRemotesConfig[] memory trustedRemotes
) BaseAdapter(crossChainController, providerGasLimit, 'LayerZero adapter', trustedRemotes) {
require(lzEndpoint != address(0), Errors.INVALID_LZ_ENDPOINT);
LZ_ENDPOINT = ILayerZeroEndpointV2(lzEndpoint);
}
/// @inheritdoc ILayerZeroReceiver
function nextNonce(uint32 /*_srcEid*/, bytes32 /*_sender*/) public pure returns (uint64) {
return 0;
}
/// @inheritdoc ILayerZeroReceiver
function lzReceive(
Origin calldata _origin,
bytes32,
bytes calldata _message,
address,
bytes calldata
) external payable onlyLZEndpoint {
uint256 originChainId = nativeToInfraChainId(_origin.srcEid);
require(allowInitializePath(_origin), Errors.REMOTE_NOT_TRUSTED);
_registerReceivedMessage(_message, originChainId);
}
/// @inheritdoc ILayerZeroReceiver
function allowInitializePath(Origin calldata origin) public view returns (bool) {
uint256 originChainId = nativeToInfraChainId(origin.srcEid);
address srcAddress = address(uint160(uint256(origin.sender)));
return _trustedRemotes[originChainId] == srcAddress && srcAddress != address(0);
}
/// @inheritdoc IBaseAdapter
function forwardMessage(
address receiver,
uint256 executionGasLimit,
uint256 destinationChainId,
bytes calldata message
) external returns (address, uint256) {
uint32 nativeChainId = SafeCast.toUint32(infraToNativeChainId(destinationChainId));
require(nativeChainId != uint32(0), Errors.DESTINATION_CHAIN_ID_NOT_SUPPORTED);
require(receiver != address(0), Errors.RECEIVER_NOT_SET);
bytes32 receiverAddress = bytes32(uint256(uint160(receiver)));
uint256 totalGasLimit = executionGasLimit + BASE_GAS_LIMIT;
bytes memory options = _generateOptions(SafeCast.toUint128(totalGasLimit));
MessagingFee memory fee = LZ_ENDPOINT.quote(
MessagingParams(nativeChainId, receiverAddress, message, options, false),
address(this)
);
require(fee.nativeFee <= address(this).balance, Errors.NOT_ENOUGH_VALUE_TO_PAY_BRIDGE_FEES);
MessagingReceipt memory receipt = LZ_ENDPOINT.send{value: fee.nativeFee}(
MessagingParams(nativeChainId, receiverAddress, message, options, false),
address(this)
);
return (address(LZ_ENDPOINT), uint256(receipt.nonce));
}
/// @inheritdoc IBaseAdapter
function nativeToInfraChainId(
uint256 nativeChainId
) public pure virtual override returns (uint256) {
if (nativeChainId == 30101) {
return ChainIds.ETHEREUM;
} else if (nativeChainId == 30106) {
return ChainIds.AVALANCHE;
} else if (nativeChainId == 30109) {
return ChainIds.POLYGON;
} else if (nativeChainId == 30110) {
return ChainIds.ARBITRUM;
} else if (nativeChainId == 30111) {
return ChainIds.OPTIMISM;
} else if (nativeChainId == 30112) {
return ChainIds.FANTOM;
} else if (nativeChainId == 30116) {
return ChainIds.HARMONY;
} else if (nativeChainId == 30102) {
return ChainIds.BNB;
} else if (nativeChainId == 30151) {
return ChainIds.METIS;
} else if (nativeChainId == 30145) {
return ChainIds.GNOSIS;
} else if (nativeChainId == 30125) {
return ChainIds.CELO;
} else {
return 0;
}
}
/// @inheritdoc IBaseAdapter
function infraToNativeChainId(
uint256 infraChainId
) public pure virtual override returns (uint256) {
if (infraChainId == ChainIds.ETHEREUM) {
return 30101;
} else if (infraChainId == ChainIds.AVALANCHE) {
return 30106;
} else if (infraChainId == ChainIds.POLYGON) {
return 30109;
} else if (infraChainId == ChainIds.ARBITRUM) {
return 30110;
} else if (infraChainId == ChainIds.OPTIMISM) {
return 30111;
} else if (infraChainId == ChainIds.FANTOM) {
return 30112;
} else if (infraChainId == ChainIds.HARMONY) {
return 30116;
} else if (infraChainId == ChainIds.METIS) {
return 30151;
} else if (infraChainId == ChainIds.BNB) {
return 30102;
} else if (infraChainId == ChainIds.GNOSIS) {
return 30145;
} else if (infraChainId == ChainIds.CELO) {
return 30125;
} else {
return uint16(0);
}
}
/**
* @notice method to generate LayerZero options
* @param gasLimit the gas limit to use on destination chain
* @return bytes with the packed options
*/
function _generateOptions(uint128 gasLimit) internal pure returns (bytes memory) {
bytes memory options = OptionsBuilder.newOptions();
return OptionsBuilder.addExecutorLzReceiveOption(options, gasLimit, 0);
}
}