forked from Dapp-Learning-DAO/Dapp-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoswap.lib.sol
280 lines (254 loc) · 15.9 KB
/
sudoswap.lib.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {ERC20} from "solmate/tokens/ERC20.sol";
import {ICurve} from "../bonding-curves/ICurve.sol";
import {ILSSVMPairFactoryLike} from "../ILSSVMPairFactoryLike.sol";
library LSSVMPairCloner {
/**
* @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
*
* This function uses the create opcode, which should never revert.
*
* During the delegate call, extra data is copied into the calldata which can then be
* accessed by the implementation contract.
*/
function cloneETHPair(
address implementation,
ILSSVMPairFactoryLike factory,
ICurve bondingCurve,
IERC721 nft,
uint8 poolType
) internal returns (address instance) {
assembly {
let ptr := mload(0x40)
// -------------------------------------------------------------------------------------------------------------
// CREATION (9 bytes)
// -------------------------------------------------------------------------------------------------------------
// creation size = 09
// runtime size = 72
// 60 runtime | PUSH1 runtime (r) | r | –
// 3d | RETURNDATASIZE | 0 r | –
// 81 | DUP2 | r 0 r | –
// 60 creation | PUSH1 creation (c) | c r 0 r | –
// 3d | RETURNDATASIZE | 0 c r 0 r | –
// 39 | CODECOPY | 0 r | [0-runSize): runtime code
// f3 | RETURN | | [0-runSize): runtime code
// -------------------------------------------------------------------------------------------------------------
// RUNTIME (53 bytes of code + 61 bytes of extra data = 114 bytes)
// -------------------------------------------------------------------------------------------------------------
// extra data size = 3d
// 3d | RETURNDATASIZE | 0 | –
// 3d | RETURNDATASIZE | 0 0 | –
// 3d | RETURNDATASIZE | 0 0 0 | –
// 3d | RETURNDATASIZE | 0 0 0 0 | –
// 36 | CALLDATASIZE | cds 0 0 0 0 | –
// 3d | RETURNDATASIZE | 0 cds 0 0 0 0 | –
// 3d | RETURNDATASIZE | 0 0 cds 0 0 0 0 | –
// 37 | CALLDATACOPY | 0 0 0 0 | [0, cds) = calldata
// 60 extra | PUSH1 extra | extra 0 0 0 0 | [0, cds) = calldata
// 60 0x35 | PUSH1 0x35 | 0x35 extra 0 0 0 0 | [0, cds) = calldata // 0x35 (53) is runtime size - data
// 36 | CALLDATASIZE | cds 0x35 extra 0 0 0 0 | [0, cds) = calldata
// 39 | CODECOPY | 0 0 0 0 | [0, cds) = calldata, [cds, cds+0x35) = extraData
// 36 | CALLDATASIZE | cds 0 0 0 0 | [0, cds) = calldata, [cds, cds+0x35) = extraData
// 60 extra | PUSH1 extra | extra cds 0 0 0 0 | [0, cds) = calldata, [cds, cds+0x35) = extraData
// 01 | ADD | cds+extra 0 0 0 0 | [0, cds) = calldata, [cds, cds+0x35) = extraData
// 3d | RETURNDATASIZE | 0 cds 0 0 0 0 | [0, cds) = calldata, [cds, cds+0x35) = extraData
// 73 addr | PUSH20 0x123… | addr 0 cds 0 0 0 0 | [0, cds) = calldata, [cds, cds+0x35) = extraData
mstore(
ptr,
hex"60_72_3d_81_60_09_3d_39_f3_3d_3d_3d_3d_36_3d_3d_37_60_3d_60_35_36_39_36_60_3d_01_3d_73_00_00_00"
)
mstore(add(ptr, 0x1d), shl(0x60, implementation))
// 5a | GAS | gas addr 0 cds 0 0 0 0 | [0, cds) = calldata, [cds, cds+0x35) = extraData
// f4 | DELEGATECALL | success 0 0 | [0, cds) = calldata, [cds, cds+0x35) = extraData
// 3d | RETURNDATASIZE | rds success 0 0 | [0, cds) = calldata, [cds, cds+0x35) = extraData
// 3d | RETURNDATASIZE | rds rds success 0 0 | [0, cds) = calldata, [cds, cds+0x35) = extraData
// 93 | SWAP4 | 0 rds success 0 rds | [0, cds) = calldata, [cds, cds+0x35) = extraData
// 80 | DUP1 | 0 0 rds success 0 rds | [0, cds) = calldata, [cds, cds+0x35) = extraData
// 3e | RETURNDATACOPY | success 0 rds | [0, rds) = return data (there might be some irrelevant leftovers in memory [rds, cds+0x37) when rds < cds+0x37)
// 60 0x33 | PUSH1 0x33 | 0x33 sucess 0 rds | [0, rds) = return data
// 57 | JUMPI | 0 rds | [0, rds) = return data
// fd | REVERT | – | [0, rds) = return data
// 5b | JUMPDEST | 0 rds | [0, rds) = return data
// f3 | RETURN | – | [0, rds) = return data
mstore(
add(ptr, 0x31),
hex"5a_f4_3d_3d_93_80_3e_60_33_57_fd_5b_f3_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00"
)
// -------------------------------------------------------------------------------------------------------------
// EXTRA DATA (61 bytes)
// -------------------------------------------------------------------------------------------------------------
mstore(add(ptr, 0x3e), shl(0x60, factory))
mstore(add(ptr, 0x52), shl(0x60, bondingCurve))
mstore(add(ptr, 0x66), shl(0x60, nft))
mstore8(add(ptr, 0x7a), poolType)
instance := create(0, ptr, 0x7b)
}
}
/**
* @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
*
* This function uses the create opcode, which should never revert.
*
* During the delegate call, extra data is copied into the calldata which can then be
* accessed by the implementation contract.
*/
function cloneERC20Pair(
address implementation,
ILSSVMPairFactoryLike factory,
ICurve bondingCurve,
IERC721 nft,
uint8 poolType,
ERC20 token
) internal returns (address instance) {
assembly {
let ptr := mload(0x40)
// -------------------------------------------------------------------------------------------------------------
// CREATION (9 bytes)
// -------------------------------------------------------------------------------------------------------------
// creation size = 09
// runtime size = 86
// 60 runtime | PUSH1 runtime (r) | r | –
// 3d | RETURNDATASIZE | 0 r | –
// 81 | DUP2 | r 0 r | –
// 60 creation | PUSH1 creation (c) | c r 0 r | –
// 3d | RETURNDATASIZE | 0 c r 0 r | –
// 39 | CODECOPY | 0 r | [0-runSize): runtime code
// f3 | RETURN | | [0-runSize): runtime code
// -------------------------------------------------------------------------------------------------------------
// RUNTIME (53 bytes of code + 81 bytes of extra data = 134 bytes)
// -------------------------------------------------------------------------------------------------------------
// extra data size = 51
// 3d | RETURNDATASIZE | 0 | –
// 3d | RETURNDATASIZE | 0 0 | –
// 3d | RETURNDATASIZE | 0 0 0 | –
// 3d | RETURNDATASIZE | 0 0 0 0 | –
// 36 | CALLDATASIZE | cds 0 0 0 0 | –
// 3d | RETURNDATASIZE | 0 cds 0 0 0 0 | –
// 3d | RETURNDATASIZE | 0 0 cds 0 0 0 0 | –
// 37 | CALLDATACOPY | 0 0 0 0 | [0, cds) = calldata
// 60 extra | PUSH1 extra | extra 0 0 0 0 | [0, cds) = calldata
// 60 0x35 | PUSH1 0x35 | 0x35 extra 0 0 0 0 | [0, cds) = calldata // 0x35 (53) is runtime size - data
// 36 | CALLDATASIZE | cds 0x35 extra 0 0 0 0 | [0, cds) = calldata
// 39 | CODECOPY | 0 0 0 0 | [0, cds) = calldata, [cds, cds+0x35) = extraData
// 36 | CALLDATASIZE | cds 0 0 0 0 | [0, cds) = calldata, [cds, cds+0x35) = extraData
// 60 extra | PUSH1 extra | extra cds 0 0 0 0 | [0, cds) = calldata, [cds, cds+0x35) = extraData
// 01 | ADD | cds+extra 0 0 0 0 | [0, cds) = calldata, [cds, cds+0x35) = extraData
// 3d | RETURNDATASIZE | 0 cds 0 0 0 0 | [0, cds) = calldata, [cds, cds+0x35) = extraData
// 73 addr | PUSH20 0x123… | addr 0 cds 0 0 0 0 | [0, cds) = calldata, [cds, cds+0x35) = extraData
mstore(
ptr,
hex"60_86_3d_81_60_09_3d_39_f3_3d_3d_3d_3d_36_3d_3d_37_60_51_60_35_36_39_36_60_51_01_3d_73_00_00_00"
)
mstore(add(ptr, 0x1d), shl(0x60, implementation))
// 5a | GAS | gas addr 0 cds 0 0 0 0 | [0, cds) = calldata, [cds, cds+0x35) = extraData
// f4 | DELEGATECALL | success 0 0 | [0, cds) = calldata, [cds, cds+0x35) = extraData
// 3d | RETURNDATASIZE | rds success 0 0 | [0, cds) = calldata, [cds, cds+0x35) = extraData
// 3d | RETURNDATASIZE | rds rds success 0 0 | [0, cds) = calldata, [cds, cds+0x35) = extraData
// 93 | SWAP4 | 0 rds success 0 rds | [0, cds) = calldata, [cds, cds+0x35) = extraData
// 80 | DUP1 | 0 0 rds success 0 rds | [0, cds) = calldata, [cds, cds+0x35) = extraData
// 3e | RETURNDATACOPY | success 0 rds | [0, rds) = return data (there might be some irrelevant leftovers in memory [rds, cds+0x37) when rds < cds+0x37)
// 60 0x33 | PUSH1 0x33 | 0x33 sucess 0 rds | [0, rds) = return data
// 57 | JUMPI | 0 rds | [0, rds) = return data
// fd | REVERT | – | [0, rds) = return data
// 5b | JUMPDEST | 0 rds | [0, rds) = return data
// f3 | RETURN | – | [0, rds) = return data
mstore(
add(ptr, 0x31),
hex"5a_f4_3d_3d_93_80_3e_60_33_57_fd_5b_f3_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00"
)
// -------------------------------------------------------------------------------------------------------------
// EXTRA DATA (81 bytes)
// -------------------------------------------------------------------------------------------------------------
mstore(add(ptr, 0x3e), shl(0x60, factory))
mstore(add(ptr, 0x52), shl(0x60, bondingCurve))
mstore(add(ptr, 0x66), shl(0x60, nft))
mstore8(add(ptr, 0x7a), poolType)
mstore(add(ptr, 0x7b), shl(0x60, token))
instance := create(0, ptr, 0x8f)
}
}
/**
* @notice Checks if a contract is a clone of a LSSVMPairETH.
* @dev Only checks the runtime bytecode, does not check the extra data.
* @param factory the factory that deployed the clone
* @param implementation the LSSVMPairETH implementation contract
* @param query the contract to check
* @return result True if the contract is a clone, false otherwise
*/
function isETHPairClone(
address factory,
address implementation,
address query
) internal view returns (bool result) {
// solhint-disable-next-line no-inline-assembly
assembly {
let ptr := mload(0x40)
mstore(
ptr,
hex"3d_3d_3d_3d_36_3d_3d_37_60_3d_60_35_36_39_36_60_3d_01_3d_73_00_00_00_00_00_00_00_00_00_00_00_00"
)
mstore(add(ptr, 0x14), shl(0x60, implementation))
mstore(
add(ptr, 0x28),
hex"5a_f4_3d_3d_93_80_3e_60_33_57_fd_5b_f3_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00"
)
mstore(add(ptr, 0x35), shl(0x60, factory))
// compare expected bytecode with that of the queried contract
let other := add(ptr, 0x49)
extcodecopy(query, other, 0, 0x49)
result := and(
eq(mload(ptr), mload(other)),
and(
eq(mload(add(ptr, 0x20)), mload(add(other, 0x20))),
eq(mload(add(ptr, 0x29)), mload(add(other, 0x29)))
)
)
}
// bytes memory data = abi.encodePacked(
// hex"3d_3d_3d_3d_36_3d_3d_37_60_3d_60_35_36_39_36_60_3d_01_3d_73",
// implementation,
// hex"5a_f4_3d_3d_93_80_3e_60_33_57_fd_5b_f3",
// factory);
// bytes memory queryData = address(query).code;
// return keccak256(data) == keccak256(queryData);
}
/**
* @notice Checks if a contract is a clone of a LSSVMPairERC20.
* @dev Only checks the runtime bytecode, does not check the extra data.
* @param implementation the LSSVMPairERC20 implementation contract
* @param query the contract to check
* @return result True if the contract is a clone, false otherwise
*/
function isERC20PairClone(
address factory,
address implementation,
address query
) internal view returns (bool result) {
// solhint-disable-next-line no-inline-assembly
assembly {
let ptr := mload(0x40)
mstore(
ptr,
hex"3d_3d_3d_3d_36_3d_3d_37_60_51_60_35_36_39_36_60_51_01_3d_73_00_00_00_00_00_00_00_00_00_00_00_00"
)
mstore(add(ptr, 0x14), shl(0x60, implementation))
mstore(
add(ptr, 0x28),
hex"5a_f4_3d_3d_93_80_3e_60_33_57_fd_5b_f3_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00"
)
mstore(add(ptr, 0x35), shl(0x60, factory))
// compare expected bytecode with that of the queried contract
let other := add(ptr, 0x49)
extcodecopy(query, other, 0, 0x49)
result := and(
eq(mload(ptr), mload(other)),
and(
eq(mload(add(ptr, 0x20)), mload(add(other, 0x20))),
eq(mload(add(ptr, 0x29)), mload(add(other, 0x29)))
)
)
}
}
}