-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathLayerZeroLib.sol
More file actions
104 lines (84 loc) · 3.5 KB
/
LayerZeroLib.sol
File metadata and controls
104 lines (84 loc) · 3.5 KB
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
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.21;
import {
ILayerZero,
SendParam,
OFTReceipt,
MessagingFee,
OFTLimit,
OFTFeeDetail
} from "../interfaces/ILayerZero.sol";
import { IRateLimits } from "../interfaces/IRateLimits.sol";
import { IALMProxy } from "../interfaces/IALMProxy.sol";
import { ApproveLib } from "./ApproveLib.sol";
import { OptionsBuilder } from "layerzerolabs/oapp-evm/contracts/oapp/libs/OptionsBuilder.sol";
library LayerZeroLib {
using OptionsBuilder for bytes;
bytes32 public constant LIMIT_LAYERZERO_TRANSFER = keccak256("LIMIT_LAYERZERO_TRANSFER");
/**********************************************************************************************/
/*** External functions ***/
/**********************************************************************************************/
function transferTokenLayerZero(
IALMProxy proxy,
IRateLimits rateLimits,
address oftAddress,
uint256 amount,
uint32 destinationEndpointId,
bytes32 layerZeroRecipient
) external {
_rateLimited(
rateLimits,
keccak256(
abi.encode(
LIMIT_LAYERZERO_TRANSFER,
oftAddress,
destinationEndpointId
)
),
amount
);
require(layerZeroRecipient != bytes32(0), "recipient-not-set");
// NOTE: Full integration testing of this logic is not possible without OFTs with
// approvalRequired == false. Add integration testing for this case before
// using in production.
if (ILayerZero(oftAddress).approvalRequired()) {
ApproveLib.approve(
ILayerZero(oftAddress).token(),
address(proxy),
oftAddress,
amount
);
}
bytes memory options = OptionsBuilder.newOptions().addExecutorLzReceiveOption(200_000, 0);
SendParam memory sendParams = SendParam({
dstEid : destinationEndpointId,
to : layerZeroRecipient,
amountLD : amount,
minAmountLD : 0,
extraOptions : options,
composeMsg : "",
oftCmd : ""
});
// Query the min amount received on the destination chain and set it.
( ,, OFTReceipt memory receipt ) = abi.decode(
proxy.doCall(
oftAddress,
abi.encodeCall(ILayerZero.quoteOFT, (sendParams))
),
(OFTLimit, OFTFeeDetail[], OFTReceipt)
);
sendParams.minAmountLD = receipt.amountReceivedLD;
MessagingFee memory fee = ILayerZero(oftAddress).quoteSend(sendParams, false);
proxy.doCallWithValue{value: fee.nativeFee}(
oftAddress,
abi.encodeCall(ILayerZero.send, (sendParams, fee, address(proxy))),
fee.nativeFee
);
}
/**********************************************************************************************/
/*** Rate Limit helper functions ***/
/**********************************************************************************************/
function _rateLimited(IRateLimits rateLimits, bytes32 key, uint256 amount) internal {
rateLimits.triggerRateLimitDecrease(key, amount);
}
}