-
Notifications
You must be signed in to change notification settings - Fork 544
/
Copy pathOFTCoreV2.sol
243 lines (188 loc) · 10.9 KB
/
OFTCoreV2.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../../../lzApp/NonblockingLzApp.sol";
import "../../../util/ExcessivelySafeCall.sol";
import "./ICommonOFT.sol";
import "./IOFTReceiverV2.sol";
abstract contract OFTCoreV2 is NonblockingLzApp {
using BytesLib for bytes;
using ExcessivelySafeCall for address;
uint public constant NO_EXTRA_GAS = 0;
// packet type
uint8 public constant PT_SEND = 0;
uint8 public constant PT_SEND_AND_CALL = 1;
uint8 public immutable sharedDecimals;
bool public useCustomAdapterParams;
mapping(uint16 => mapping(bytes => mapping(uint64 => bool))) public creditedPackets;
/**
* @dev Emitted when `_amount` tokens are moved from the `_sender` to (`_dstChainId`, `_toAddress`)
* `_nonce` is the outbound nonce
*/
event SendToChain(uint16 indexed _dstChainId, address indexed _from, bytes32 indexed _toAddress, uint _amount);
/**
* @dev Emitted when `_amount` tokens are received from `_srcChainId` into the `_toAddress` on the local chain.
* `_nonce` is the inbound nonce.
*/
event ReceiveFromChain(uint16 indexed _srcChainId, address indexed _to, uint _amount);
event SetUseCustomAdapterParams(bool _useCustomAdapterParams);
event CallOFTReceivedSuccess(uint16 indexed _srcChainId, bytes _srcAddress, uint64 _nonce, bytes32 _hash);
event NonContractAddress(address _address);
// _sharedDecimals should be the minimum decimals on all chains
constructor(uint8 _sharedDecimals, address _lzEndpoint) NonblockingLzApp(_lzEndpoint) {
sharedDecimals = _sharedDecimals;
}
/************************************************************************
* public functions
************************************************************************/
function callOnOFTReceived(uint16 _srcChainId, bytes calldata _srcAddress, uint64 _nonce, bytes32 _from, address _to, uint _amount, bytes calldata _payload, uint _gasForCall) public virtual {
require(_msgSender() == address(this), "OFTCore: caller must be OFTCore");
// send
_amount = _transferFrom(address(this), _to, _amount);
emit ReceiveFromChain(_srcChainId, _to, _amount);
// call
IOFTReceiverV2(_to).onOFTReceived{gas: _gasForCall}(_srcChainId, _srcAddress, _nonce, _from, _amount, _payload);
}
function setUseCustomAdapterParams(bool _useCustomAdapterParams) public virtual onlyOwner {
useCustomAdapterParams = _useCustomAdapterParams;
emit SetUseCustomAdapterParams(_useCustomAdapterParams);
}
/************************************************************************
* internal functions
************************************************************************/
function _estimateSendFee(uint16 _dstChainId, bytes32 _toAddress, uint _amount, bool _useZro, bytes memory _adapterParams) internal view virtual returns (uint nativeFee, uint zroFee) {
// mock the payload for sendFrom()
bytes memory payload = _encodeSendPayload(_toAddress, _ld2sd(_amount));
return lzEndpoint.estimateFees(_dstChainId, address(this), payload, _useZro, _adapterParams);
}
function _estimateSendAndCallFee(uint16 _dstChainId, bytes32 _toAddress, uint _amount, bytes memory _payload, uint64 _dstGasForCall, bool _useZro, bytes memory _adapterParams) internal view virtual returns (uint nativeFee, uint zroFee) {
// mock the payload for sendAndCall()
bytes memory payload = _encodeSendAndCallPayload(msg.sender, _toAddress, _ld2sd(_amount), _payload, _dstGasForCall);
return lzEndpoint.estimateFees(_dstChainId, address(this), payload, _useZro, _adapterParams);
}
function _nonblockingLzReceive(uint16 _srcChainId, bytes memory _srcAddress, uint64 _nonce, bytes memory _payload) internal virtual override {
uint8 packetType = _payload.toUint8(0);
if (packetType == PT_SEND) {
_sendAck(_srcChainId, _srcAddress, _nonce, _payload);
} else if (packetType == PT_SEND_AND_CALL) {
_sendAndCallAck(_srcChainId, _srcAddress, _nonce, _payload);
} else {
revert("OFTCore: unknown packet type");
}
}
function _send(address _from, uint16 _dstChainId, bytes32 _toAddress, uint _amount, address payable _refundAddress, address _zroPaymentAddress, bytes memory _adapterParams) internal virtual returns (uint amount) {
_checkAdapterParams(_dstChainId, PT_SEND, _adapterParams, NO_EXTRA_GAS);
(amount,) = _removeDust(_amount);
amount = _debitFrom(_from, _dstChainId, _toAddress, amount); // amount returned should not have dust
require(amount > 0, "OFTCore: amount too small");
bytes memory lzPayload = _encodeSendPayload(_toAddress, _ld2sd(amount));
_lzSend(_dstChainId, lzPayload, _refundAddress, _zroPaymentAddress, _adapterParams, msg.value);
emit SendToChain(_dstChainId, _from, _toAddress, amount);
}
function _sendAck(uint16 _srcChainId, bytes memory, uint64, bytes memory _payload) internal virtual {
(address to, uint64 amountSD) = _decodeSendPayload(_payload);
if (to == address(0)) {
to = address(0xdead);
}
uint amount = _sd2ld(amountSD);
amount = _creditTo(_srcChainId, to, amount);
emit ReceiveFromChain(_srcChainId, to, amount);
}
function _sendAndCall(address _from, uint16 _dstChainId, bytes32 _toAddress, uint _amount, bytes memory _payload, uint64 _dstGasForCall, address payable _refundAddress, address _zroPaymentAddress, bytes memory _adapterParams) internal virtual returns (uint amount) {
_checkAdapterParams(_dstChainId, PT_SEND_AND_CALL, _adapterParams, _dstGasForCall);
(amount,) = _removeDust(_amount);
amount = _debitFrom(_from, _dstChainId, _toAddress, amount);
require(amount > 0, "OFTCore: amount too small");
// encode the msg.sender into the payload instead of _from
bytes memory lzPayload = _encodeSendAndCallPayload(msg.sender, _toAddress, _ld2sd(amount), _payload, _dstGasForCall);
_lzSend(_dstChainId, lzPayload, _refundAddress, _zroPaymentAddress, _adapterParams, msg.value);
emit SendToChain(_dstChainId, _from, _toAddress, amount);
}
function _sendAndCallAck(uint16 _srcChainId, bytes memory _srcAddress, uint64 _nonce, bytes memory _payload) internal virtual {
(bytes32 from, address to, uint64 amountSD, bytes memory payloadForCall, uint64 gasForCall) = _decodeSendAndCallPayload(_payload);
bool credited = creditedPackets[_srcChainId][_srcAddress][_nonce];
uint amount = _sd2ld(amountSD);
// credit to this contract first, and then transfer to receiver only if callOnOFTReceived() succeeds
if (!credited) {
amount = _creditTo(_srcChainId, address(this), amount);
creditedPackets[_srcChainId][_srcAddress][_nonce] = true;
}
if (!_isContract(to)) {
emit NonContractAddress(to);
return;
}
// workaround for stack too deep
uint16 srcChainId = _srcChainId;
bytes memory srcAddress = _srcAddress;
uint64 nonce = _nonce;
bytes memory payload = _payload;
bytes32 from_ = from;
address to_ = to;
uint amount_ = amount;
bytes memory payloadForCall_ = payloadForCall;
// no gas limit for the call if retry
uint gas = credited ? gasleft() : gasForCall;
(bool success, bytes memory reason) = address(this).excessivelySafeCall(gasleft(), 150, abi.encodeWithSelector(this.callOnOFTReceived.selector, srcChainId, srcAddress, nonce, from_, to_, amount_, payloadForCall_, gas));
if (success) {
bytes32 hash = keccak256(payload);
emit CallOFTReceivedSuccess(srcChainId, srcAddress, nonce, hash);
} else {
// store the failed message into the nonblockingLzApp
_storeFailedMessage(srcChainId, srcAddress, nonce, payload, reason);
}
}
function _isContract(address _account) internal view returns (bool) {
return _account.code.length > 0;
}
function _checkAdapterParams(uint16 _dstChainId, uint16 _pkType, bytes memory _adapterParams, uint _extraGas) internal virtual {
if (useCustomAdapterParams) {
_checkGasLimit(_dstChainId, _pkType, _adapterParams, _extraGas);
} else {
require(_adapterParams.length == 0, "OFTCore: _adapterParams must be empty.");
}
}
function _ld2sd(uint _amount) internal virtual view returns (uint64) {
uint amountSD = _amount / _ld2sdRate();
require(amountSD <= type(uint64).max, "OFTCore: amountSD overflow");
return uint64(amountSD);
}
function _sd2ld(uint64 _amountSD) internal virtual view returns (uint) {
return _amountSD * _ld2sdRate();
}
function _removeDust(uint _amount) internal virtual view returns (uint amountAfter, uint dust) {
dust = _amount % _ld2sdRate();
amountAfter = _amount - dust;
}
function _encodeSendPayload(bytes32 _toAddress, uint64 _amountSD) internal virtual view returns (bytes memory) {
return abi.encodePacked(PT_SEND, _toAddress, _amountSD);
}
function _decodeSendPayload(bytes memory _payload) internal virtual view returns (address to, uint64 amountSD) {
require(_payload.toUint8(0) == PT_SEND && _payload.length == 41, "OFTCore: invalid payload");
to = _payload.toAddress(13); // drop the first 12 bytes of bytes32
amountSD = _payload.toUint64(33);
}
function _encodeSendAndCallPayload(address _from, bytes32 _toAddress, uint64 _amountSD, bytes memory _payload, uint64 _dstGasForCall) internal virtual view returns (bytes memory) {
return abi.encodePacked(
PT_SEND_AND_CALL,
_toAddress,
_amountSD,
_addressToBytes32(_from),
_dstGasForCall,
_payload
);
}
function _decodeSendAndCallPayload(bytes memory _payload) internal virtual view returns (bytes32 from, address to, uint64 amountSD, bytes memory payload, uint64 dstGasForCall) {
require(_payload.toUint8(0) == PT_SEND_AND_CALL, "OFTCore: invalid payload");
to = _payload.toAddress(13); // drop the first 12 bytes of bytes32
amountSD = _payload.toUint64(33);
from = _payload.toBytes32(41);
dstGasForCall = _payload.toUint64(73);
payload = _payload.slice(81, _payload.length - 81);
}
function _addressToBytes32(address _address) internal pure virtual returns (bytes32) {
return bytes32(uint(uint160(_address)));
}
function _debitFrom(address _from, uint16 _dstChainId, bytes32 _toAddress, uint _amount) internal virtual returns (uint);
function _creditTo(uint16 _srcChainId, address _toAddress, uint _amount) internal virtual returns (uint);
function _transferFrom(address _from, address _to, uint _amount) internal virtual returns (uint);
function _ld2sdRate() internal view virtual returns (uint);
}