-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibAsset.sol
179 lines (160 loc) · 5.42 KB
/
LibAsset.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/utils/introspection/ERC165CheckerUpgradeable.sol";
import "../interfaces/IERC721Royalties.sol";
import "../interfaces/IERC20Permit.sol";
import "../interfaces/IERC721Permit.sol";
import "../interfaces/IERC1155Permit.sol";
library LibAsset {
using SafeERC20Upgradeable for IERC20Permit;
using ERC165CheckerUpgradeable for address;
uint256 public constant MAX_ROYALTY_VALUE = 2000;
/// @notice Order hash for EIP712
bytes32 private constant ASSET_TYPEHASH =
keccak256("Asset(uint8 assetType,address token,uint256 id,uint256 amount)");
enum AssetType {
ERC20,
ERC721,
ERC1155,
ETH
}
struct Asset {
AssetType assetType;
address token;
uint256 id;
uint256 amount;
}
struct Signature {
uint8 v;
bytes32 r;
bytes32 s;
}
struct SignatureERC20 {
uint256 amount;
uint256 deadline;
Signature sig;
}
struct SignatureERC721 {
bool forAll;
uint256 tokenId;
uint256 deadline;
Signature sig;
}
struct SignatureERC1155 {
uint256 deadline;
Signature sig;
}
function hashAsset(Asset memory asset) internal pure returns (bytes32) {
return
keccak256(
abi.encode(ASSET_TYPEHASH, asset.assetType, asset.token, asset.id, asset.amount)
);
}
function isCommodity(Asset memory asset) internal pure returns (bool) {
return asset.assetType == AssetType.ERC721 || asset.assetType == AssetType.ERC1155;
}
function isPayment(Asset memory asset, bool buyOrder) internal pure returns (bool) {
if (buyOrder) {
return asset.assetType == AssetType.ERC20;
} else {
return asset.assetType == AssetType.ERC20 || asset.assetType == AssetType.ETH;
}
}
function withAmount(Asset memory asset, uint256 amount) internal pure returns (Asset memory) {
return
Asset({assetType: asset.assetType, token: asset.token, amount: amount, id: asset.id});
}
function getRoyalty(Asset memory asset)
internal
view
returns (address receiver, uint256 value)
{
if (
asset.assetType == AssetType.ERC721 &&
asset.token.supportsInterface(type(IERC721Royalties).interfaceId)
) {
(receiver, value) = IERC721Royalties(asset.token).getRoyalty(asset.id);
require(value <= MAX_ROYALTY_VALUE, "LibAsset: invalid royalty value");
}
}
function getCurrency(Asset memory asset) internal pure returns (address) {
return (asset.assetType == AssetType.ETH) ? address(0) : asset.token;
}
function permit(
Asset memory asset,
bytes memory permitSig,
address from
) internal {
if (asset.assetType == AssetType.ERC20) {
SignatureERC20 memory signature = abi.decode(permitSig, (SignatureERC20));
IERC20Permit(asset.token).permit(
from,
address(this),
signature.amount,
signature.deadline,
signature.sig.v,
signature.sig.r,
signature.sig.s
);
} else if (asset.assetType == AssetType.ERC721) {
SignatureERC721 memory signature = abi.decode(permitSig, (SignatureERC721));
if (signature.forAll) {
IERC721Permit(asset.token).permitAll(
from,
address(this),
signature.deadline,
signature.sig.v,
signature.sig.r,
signature.sig.s
);
} else {
IERC721Permit(asset.token).permit(
from,
address(this),
signature.tokenId,
signature.deadline,
signature.sig.v,
signature.sig.r,
signature.sig.s
);
}
} else {
SignatureERC1155 memory signature = abi.decode(permitSig, (SignatureERC1155));
IERC1155Permit(asset.token).permit(
from,
address(this),
signature.deadline,
signature.sig.v,
signature.sig.r,
signature.sig.s
);
}
}
function transfer(
Asset memory asset,
address from,
address to
) internal {
if (asset.assetType == AssetType.ERC20) {
IERC20Permit(asset.token).safeTransferFrom(from, to, asset.amount);
} else if (asset.assetType == AssetType.ERC721) {
IERC721Permit(asset.token).safeTransferFrom(from, to, asset.id);
} else if (asset.assetType == AssetType.ERC1155) {
IERC1155Permit(asset.token).safeTransferFrom(from, to, asset.id, asset.amount, "");
} else {
payable(to).send(asset.amount);
}
}
function permitTransfer(
Asset memory asset,
bytes memory permitSig,
address from,
address to
) internal {
if (permitSig.length != 0) {
permit(asset, permitSig, from);
}
transfer(asset, from, to);
}
}