-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEncoderLib.sol
173 lines (145 loc) · 5.62 KB
/
EncoderLib.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {Ops} from "../Ops.sol";
/// @author philogy <https://github.com/philogy>
library EncoderLib {
function init(uint256 hashMapSize) internal pure returns (bytes memory program) {
require(hashMapSize <= 0xffff);
assembly {
program := mload(0x40)
mstore(0x40, add(program, 0x22))
mstore(add(program, 2), hashMapSize)
mstore(program, 2)
}
}
function appendSwap(bytes memory self, address token0, address token1, bool zeroForOne, uint256 amount)
internal
pure
returns (bytes memory)
{
uint256 op = Ops.SWAP | (zeroForOne ? Ops.SWAP_DIR : 0);
assembly {
let length := mload(self)
mstore(self, add(length, 57))
let initialOffset := add(add(self, 0x20), length)
mstore(initialOffset, shl(248, op))
mstore(add(initialOffset, 1), shl(96, token0))
mstore(add(initialOffset, 21), shl(96, token1))
mstore(add(initialOffset, 41), shl(128, amount))
}
return self;
}
function appendAddLiquidity(
bytes memory self,
address token0,
address token1,
address to,
uint256 maxAmount0,
uint256 maxAmount1
) internal pure returns (bytes memory) {
uint256 op = Ops.ADD_LIQ;
(token0, token1, maxAmount0, maxAmount1) =
token0 < token1 ? (token0, token1, maxAmount0, maxAmount1) : (token1, token0, maxAmount1, maxAmount0);
assembly {
let length := mload(self)
mstore(self, add(length, 93))
let initialOffset := add(add(self, 0x20), length)
mstore(initialOffset, shl(248, op))
mstore(add(initialOffset, 1), shl(96, token0))
mstore(add(initialOffset, 21), shl(96, token1))
mstore(add(initialOffset, 41), shl(96, to))
mstore(add(initialOffset, 61), shl(128, maxAmount0))
mstore(add(initialOffset, 77), shl(128, maxAmount1))
}
return self;
}
function appendRemoveLiquidity(bytes memory self, address token0, address token1, uint256 liquidity)
internal
pure
returns (bytes memory)
{
uint256 op = Ops.RM_LIQ;
(token0, token1) = sort(token0, token1);
assembly {
let length := mload(self)
mstore(self, add(length, 73))
let initialOffset := add(add(self, 0x20), length)
mstore(initialOffset, shl(248, op))
mstore(add(initialOffset, 1), shl(96, token0))
mstore(add(initialOffset, 21), shl(96, token1))
mstore(add(initialOffset, 41), liquidity)
}
return self;
}
function appendSend(bytes memory self, address token, address to, uint256 amount)
internal
pure
returns (bytes memory)
{
uint256 op = Ops.SEND;
assembly {
let length := mload(self)
mstore(self, add(length, 57))
let initialOffset := add(add(self, 0x20), length)
mstore(initialOffset, shl(248, op))
mstore(add(initialOffset, 1), shl(96, token))
mstore(add(initialOffset, 21), shl(96, to))
mstore(add(initialOffset, 41), shl(128, amount))
}
return self;
}
function appendReceive(bytes memory self, address token, uint256 amount) internal pure returns (bytes memory) {
uint256 op = Ops.RECEIVE;
assembly {
let length := mload(self)
mstore(self, add(length, 37))
let initialOffset := add(add(self, 0x20), length)
mstore(initialOffset, shl(248, op))
mstore(add(initialOffset, 1), shl(96, token))
mstore(add(initialOffset, 21), shl(128, amount))
}
return self;
}
function appendSwapHead(bytes memory self, address token0, address token1, uint256 amount, bool zeroForOne)
internal
pure
returns (bytes memory)
{
(token0, token1, zeroForOne) = sort(token0, token1, zeroForOne);
uint256 op = Ops.SWAP_HEAD | (zeroForOne ? Ops.SWAP_DIR : 0);
assembly {
let length := mload(self)
mstore(self, add(length, 57))
let initialOffset := add(add(self, 0x20), length)
mstore(initialOffset, shl(248, op))
mstore(add(initialOffset, 1), shl(96, token0))
mstore(add(initialOffset, 21), shl(96, token1))
mstore(add(initialOffset, 41), shl(128, amount))
}
return self;
}
function appendSwapHop(bytes memory self, address nextToken) internal pure returns (bytes memory) {
uint256 op = Ops.SWAP_HOP;
assembly {
let length := mload(self)
mstore(self, add(length, 21))
let initialOffset := add(add(self, 0x20), length)
mstore(initialOffset, shl(248, op))
mstore(add(initialOffset, 1), shl(96, nextToken))
}
return self;
}
function done(bytes memory self) internal pure returns (bytes memory) {
assembly {
let freeMem := mload(0x40)
mstore(0x40, add(freeMem, mload(self)))
}
return self;
}
function sort(address token0, address token1, bool zeroForOne) internal pure returns (address, address, bool) {
return token1 > token0 ? (token0, token1, zeroForOne) : (token1, token0, !zeroForOne);
}
function sort(address token0, address token1) internal pure returns (address, address) {
return token1 > token0 ? (token0, token1) : (token1, token0);
}
}