-
Notifications
You must be signed in to change notification settings - Fork 539
/
LzLib.sol
90 lines (77 loc) · 3.19 KB
/
LzLib.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity >=0.6.0;
pragma experimental ABIEncoderV2;
library LzLib {
// LayerZero communication
struct CallParams {
address payable refundAddress;
address zroPaymentAddress;
}
//---------------------------------------------------------------------------
// Address type handling
struct AirdropParams {
uint airdropAmount;
bytes32 airdropAddress;
}
function buildAdapterParams(LzLib.AirdropParams memory _airdropParams, uint _uaGasLimit) internal pure returns (bytes memory adapterParams) {
if (_airdropParams.airdropAmount == 0 && _airdropParams.airdropAddress == bytes32(0x0)) {
adapterParams = buildDefaultAdapterParams(_uaGasLimit);
} else {
adapterParams = buildAirdropAdapterParams(_uaGasLimit, _airdropParams);
}
}
// Build Adapter Params
function buildDefaultAdapterParams(uint _uaGas) internal pure returns (bytes memory) {
// txType 1
// bytes [2 32 ]
// fields [txType extraGas]
return abi.encodePacked(uint16(1), _uaGas);
}
function buildAirdropAdapterParams(uint _uaGas, AirdropParams memory _params) internal pure returns (bytes memory) {
require(_params.airdropAmount > 0, "Airdrop amount must be greater than 0");
require(_params.airdropAddress != bytes32(0x0), "Airdrop address must be set");
// txType 2
// bytes [2 32 32 bytes[] ]
// fields [txType extraGas dstNativeAmt dstNativeAddress]
return abi.encodePacked(uint16(2), _uaGas, _params.airdropAmount, _params.airdropAddress);
}
function getGasLimit(bytes memory _adapterParams) internal pure returns (uint gasLimit) {
require(_adapterParams.length == 34 || _adapterParams.length > 66, "Invalid adapterParams");
assembly {
gasLimit := mload(add(_adapterParams, 34))
}
}
// Decode Adapter Params
function decodeAdapterParams(bytes memory _adapterParams)
internal
pure
returns (
uint16 txType,
uint uaGas,
uint airdropAmount,
address payable airdropAddress
)
{
require(_adapterParams.length == 34 || _adapterParams.length > 66, "Invalid adapterParams");
assembly {
txType := mload(add(_adapterParams, 2))
uaGas := mload(add(_adapterParams, 34))
}
require(txType == 1 || txType == 2, "Unsupported txType");
require(uaGas > 0, "Gas too low");
if (txType == 2) {
assembly {
airdropAmount := mload(add(_adapterParams, 66))
airdropAddress := mload(add(_adapterParams, 86))
}
}
}
//---------------------------------------------------------------------------
// Address type handling
function bytes32ToAddress(bytes32 _bytes32Address) internal pure returns (address _address) {
return address(uint160(uint(_bytes32Address)));
}
function addressToBytes32(address _address) internal pure returns (bytes32 _bytes32Address) {
return bytes32(uint(uint160(_address)));
}
}