@@ -10,116 +10,75 @@ import {IDEX} from "./IDEX.sol";
10
10
/// @dev This contracts will interact with dex pallet
11
11
contract DEX is IDEX {
12
12
/// @dev The DEX precompile address.
13
- address private constant PRECOMPILE =
14
- address (0x0000000000000000000000000000000000000405 );
13
+ address internal constant PRECOMPILE = address (0x0000000000000000000000000000000000000405 );
15
14
16
15
/// @inheritdoc IDEX
17
- function getLiquidityPool (
18
- address tokenA ,
19
- address tokenB
20
- ) public view override returns (uint256 , uint256 ) {
16
+ function getLiquidityPool (address tokenA , address tokenB ) public view override returns (uint256 , uint256 ) {
21
17
require (tokenA != address (0 ), "DEX: tokenA is zero address " );
22
18
require (tokenB != address (0 ), "DEX: tokenB is zero address " );
23
19
24
- (bool success , bytes memory returnData ) = PRECOMPILE.staticcall (
25
- abi.encodeWithSignature (
26
- "getLiquidityPool(address,address) " ,
27
- tokenA,
28
- tokenB
29
- )
30
- );
20
+ (bool success , bytes memory returnData ) =
21
+ PRECOMPILE.staticcall (abi.encodeWithSignature ("getLiquidityPool(address,address) " , tokenA, tokenB));
31
22
assembly {
32
- if eq (success, 0 ) {
33
- revert (add (returnData, 0x20 ), returndatasize ())
34
- }
23
+ if eq (success, 0 ) { revert (add (returnData, 0x20 ), returndatasize ()) }
35
24
}
36
25
37
26
return abi.decode (returnData, (uint256 , uint256 ));
38
27
}
39
28
40
29
/// @inheritdoc IDEX
41
- function getLiquidityTokenAddress (
42
- address tokenA ,
43
- address tokenB
44
- ) public view override returns (address ) {
30
+ function getLiquidityTokenAddress (address tokenA , address tokenB ) public view override returns (address ) {
45
31
require (tokenA != address (0 ), "DEX: tokenA is zero address " );
46
32
require (tokenB != address (0 ), "DEX: tokenB is zero address " );
47
33
48
- (bool success , bytes memory returnData ) = PRECOMPILE.staticcall (
49
- abi.encodeWithSignature (
50
- "getLiquidityTokenAddress(address,address) " ,
51
- tokenA,
52
- tokenB
53
- )
54
- );
34
+ (bool success , bytes memory returnData ) =
35
+ PRECOMPILE.staticcall (abi.encodeWithSignature ("getLiquidityTokenAddress(address,address) " , tokenA, tokenB));
55
36
assembly {
56
- if eq (success, 0 ) {
57
- revert (add (returnData, 0x20 ), returndatasize ())
58
- }
37
+ if eq (success, 0 ) { revert (add (returnData, 0x20 ), returndatasize ()) }
59
38
}
60
39
61
40
return abi.decode (returnData, (address ));
62
41
}
63
42
64
43
/// @inheritdoc IDEX
65
- function getSwapTargetAmount (
66
- address [] memory path ,
67
- uint256 supplyAmount
68
- ) public view override returns (uint256 ) {
69
- for (uint i = 0 ; i < path.length ; i++ ) {
44
+ function getSwapTargetAmount (address [] memory path , uint256 supplyAmount ) public view override returns (uint256 ) {
45
+ for (uint256 i = 0 ; i < path.length ; i++ ) {
70
46
require (path[i] != address (0 ), "DEX: token is zero address " );
71
47
}
72
48
require (supplyAmount != 0 , "DEX: supplyAmount is zero " );
73
49
74
- (bool success , bytes memory returnData ) = PRECOMPILE.staticcall (
75
- abi.encodeWithSignature (
76
- "getSwapTargetAmount(address[],uint256) " ,
77
- path,
78
- supplyAmount
79
- )
80
- );
50
+ (bool success , bytes memory returnData ) =
51
+ PRECOMPILE.staticcall (abi.encodeWithSignature ("getSwapTargetAmount(address[],uint256) " , path, supplyAmount));
81
52
assembly {
82
- if eq (success, 0 ) {
83
- revert (add (returnData, 0x20 ), returndatasize ())
84
- }
53
+ if eq (success, 0 ) { revert (add (returnData, 0x20 ), returndatasize ()) }
85
54
}
86
55
87
56
return abi.decode (returnData, (uint256 ));
88
57
}
89
58
90
59
/// @inheritdoc IDEX
91
- function getSwapSupplyAmount (
92
- address [] memory path ,
93
- uint256 targetAmount
94
- ) public view override returns (uint256 ) {
95
- for (uint i = 0 ; i < path.length ; i++ ) {
60
+ function getSwapSupplyAmount (address [] memory path , uint256 targetAmount ) public view override returns (uint256 ) {
61
+ for (uint256 i = 0 ; i < path.length ; i++ ) {
96
62
require (path[i] != address (0 ), "DEX: token is zero address " );
97
63
}
98
64
require (targetAmount != 0 , "DEX: targetAmount is zero " );
99
65
100
- (bool success , bytes memory returnData ) = PRECOMPILE.staticcall (
101
- abi.encodeWithSignature (
102
- "getSwapSupplyAmount(address[],uint256) " ,
103
- path,
104
- targetAmount
105
- )
106
- );
66
+ (bool success , bytes memory returnData ) =
67
+ PRECOMPILE.staticcall (abi.encodeWithSignature ("getSwapSupplyAmount(address[],uint256) " , path, targetAmount));
107
68
assembly {
108
- if eq (success, 0 ) {
109
- revert (add (returnData, 0x20 ), returndatasize ())
110
- }
69
+ if eq (success, 0 ) { revert (add (returnData, 0x20 ), returndatasize ()) }
111
70
}
112
71
113
72
return abi.decode (returnData, (uint256 ));
114
73
}
115
74
116
75
/// @inheritdoc IDEX
117
- function swapWithExactSupply (
118
- address [] memory path ,
119
- uint256 supplyAmount ,
120
- uint256 minTargetAmount
121
- ) public override returns ( bool ) {
122
- for (uint i = 0 ; i < path.length ; i++ ) {
76
+ function swapWithExactSupply (address [] memory path , uint256 supplyAmount , uint256 minTargetAmount )
77
+ public
78
+ override
79
+ returns ( bool )
80
+ {
81
+ for (uint256 i = 0 ; i < path.length ; i++ ) {
123
82
require (path[i] != address (0 ), "DEX: token is zero address " );
124
83
}
125
84
require (supplyAmount != 0 , "DEX: supplyAmount is zero " );
@@ -134,27 +93,20 @@ contract DEX is IDEX {
134
93
)
135
94
);
136
95
assembly {
137
- if eq (success, 0 ) {
138
- revert (add (returnData, 0x20 ), returndatasize ())
139
- }
96
+ if eq (success, 0 ) { revert (add (returnData, 0x20 ), returndatasize ()) }
140
97
}
141
98
142
- emit Swaped (
143
- msg .sender ,
144
- path,
145
- supplyAmount,
146
- abi.decode (returnData, (uint256 ))
147
- );
99
+ emit Swaped (msg .sender , path, supplyAmount, abi.decode (returnData, (uint256 )));
148
100
return true ;
149
101
}
150
102
151
103
/// @inheritdoc IDEX
152
- function swapWithExactTarget (
153
- address [] memory path ,
154
- uint256 targetAmount ,
155
- uint256 maxSupplyAmount
156
- ) public override returns ( bool ) {
157
- for (uint i = 0 ; i < path.length ; i++ ) {
104
+ function swapWithExactTarget (address [] memory path , uint256 targetAmount , uint256 maxSupplyAmount )
105
+ public
106
+ override
107
+ returns ( bool )
108
+ {
109
+ for (uint256 i = 0 ; i < path.length ; i++ ) {
158
110
require (path[i] != address (0 ), "DEX: token is zero address " );
159
111
}
160
112
require (targetAmount != 0 , "DEX: targetAmount is zero " );
@@ -169,17 +121,10 @@ contract DEX is IDEX {
169
121
)
170
122
);
171
123
assembly {
172
- if eq (success, 0 ) {
173
- revert (add (returnData, 0x20 ), returndatasize ())
174
- }
124
+ if eq (success, 0 ) { revert (add (returnData, 0x20 ), returndatasize ()) }
175
125
}
176
126
177
- emit Swaped (
178
- msg .sender ,
179
- path,
180
- abi.decode (returnData, (uint256 )),
181
- targetAmount
182
- );
127
+ emit Swaped (msg .sender , path, abi.decode (returnData, (uint256 )), targetAmount);
183
128
return true ;
184
129
}
185
130
@@ -208,9 +153,7 @@ contract DEX is IDEX {
208
153
)
209
154
);
210
155
assembly {
211
- if eq (success, 0 ) {
212
- revert (add (returnData, 0x20 ), returndatasize ())
213
- }
156
+ if eq (success, 0 ) { revert (add (returnData, 0x20 ), returndatasize ()) }
214
157
}
215
158
216
159
emit AddedLiquidity (msg .sender , tokenA, tokenB, maxAmountA, maxAmountB);
@@ -241,9 +184,7 @@ contract DEX is IDEX {
241
184
)
242
185
);
243
186
assembly {
244
- if eq (success, 0 ) {
245
- revert (add (returnData, 0x20 ), returndatasize ())
246
- }
187
+ if eq (success, 0 ) { revert (add (returnData, 0x20 ), returndatasize ()) }
247
188
}
248
189
249
190
emit RemovedLiquidity (msg .sender , tokenA, tokenB, removeShare);
0 commit comments