@@ -7,18 +7,18 @@ import "@rari-capital/solmate/src/tokens/ERC20.sol";
77import "./HyperLiquidity.sol " ;
88import "./HyperSwap.sol " ;
99
10- /// @title Enigma Compiler
10+ /// @title Enigma Decompiler
1111/// @notice Main contract of the Enigma that implements instruction processing.
1212/// @dev Eliminates the use of function signatures. Expects encoded bytes as msg.data in the fallback.
13- contract Compiler is HyperLiquidity , HyperSwap {
13+ contract Decompiler is HyperLiquidity , HyperSwap {
1414 // --- Fallback --- //
1515
1616 /// @notice Main touchpoint for receiving calls.
1717 /// @dev Critical: data must be encoded properly to be processed.
1818 /// @custom:security Critical. Guarded against re-entrancy. This is like the bank vault door.
1919 /// @custom:mev Higher level security checks must be implemented by calling contract.
2020 fallback () external payable lock {
21- if (msg .data [0 ] != INSTRUCTION_JUMP) _process (msg .data );
21+ if (msg .data [0 ] != Instructions. INSTRUCTION_JUMP) _process (msg .data );
2222 else _jumpProcess (msg .data );
2323 _settleBalances ();
2424 }
@@ -30,10 +30,13 @@ contract Compiler is HyperLiquidity, HyperSwap {
3030 /// @custom:security High. Without pairIds to loop through, no token amounts are settled.
3131 uint16 [] internal _tempPairIds;
3232
33+ /// @dev Token -> Touched Flag. Stored temporary to signal which token reserves were tapped.
34+ mapping (address => bool ) internal _addressCache;
35+
3336 /// @dev Flag set to `true` during `_process`. Set to `false` during `_settleToken`.
3437 /// @custom:security High. Referenced in settlement to pay for tokens due.
3538 function _cacheAddress (address token , bool flag ) internal {
36- addressCache [token] = flag;
39+ _addressCache [token] = flag;
3740 }
3841
3942 // --- Internal --- //
@@ -61,66 +64,40 @@ contract Compiler is HyperLiquidity, HyperSwap {
6164 emit Debit (token, amount);
6265 }
6366
64- /// @notice First byte should always be the INSTRUCTION_JUMP Enigma code.
65- /// @dev Expects a special encoding method for multiple instructions.
66- /// @param data Includes opcode as byte at index 0. First byte should point to next instruction.
67- /// @custom:security Critical. Processes multiple instructions. Data must be encoded perfectly.
68- function _jumpProcess (bytes calldata data ) internal {
69- uint8 length = uint8 (data[1 ]);
70- uint8 pointer = JUMP_PROCESS_START_POINTER; // note: [opcode, length, pointer, ...instruction, pointer, ...etc]
71- uint256 start;
72-
73- // For each instruction set...
74- for (uint256 i; i != length; ++ i) {
75- // Start at the index of the first byte of the next instruction.
76- start = pointer;
77-
78- // Set the new pointer to the next instruction, located at the pointer.
79- pointer = uint8 (data[pointer]);
80-
81- // The `start:` includes the pointer byte, while the `:end` `pointer` is excluded.
82- if (pointer > data.length ) revert JumpError (pointer);
83- bytes calldata instruction = data[start:pointer];
84-
85- // Process the instruction.
86- _process (instruction[1 :]); // note: Removes the pointer to the next instruction.
87- }
88- }
89-
9067 /// @notice Single instruction processor that will forward instruction to appropriate function.
9168 /// @dev Critical: Every token of every pair interacted with is cached to be settled later.
9269 /// @param data Encoded Enigma data. First byte must be an Enigma instruction.
9370 /// @custom:security Critical. Directly sends instructions to be executed.
94- function _process (bytes calldata data ) internal returns ( uint16 pairId ) {
71+ function _process (bytes calldata data ) internal override {
9572 uint48 poolId;
9673 bytes1 instruction = bytes1 (data[0 ] & 0x0f );
97- if (instruction == UNKNOWN) revert UnknownInstruction ();
74+ if (instruction == Instructions. UNKNOWN) revert UnknownInstruction ();
9875
99- if (instruction == ADD_LIQUIDITY) {
76+ if (instruction == Instructions. ADD_LIQUIDITY) {
10077 (poolId, ) = _addLiquidity (data);
101- } else if (instruction == REMOVE_LIQUIDITY) {
78+ } else if (instruction == Instructions. REMOVE_LIQUIDITY) {
10279 (poolId, , ) = _removeLiquidity (data);
103- } else if (instruction == SWAP) {
80+ } else if (instruction == Instructions. SWAP) {
10481 (poolId, ) = _swapExactForExact (data);
105- } else if (instruction == CREATE_POOL) {
82+ } else if (instruction == Instructions. CREATE_POOL) {
10683 (poolId, , ) = _createPool (data);
107- } else if (instruction == CREATE_CURVE) {
84+ } else if (instruction == Instructions. CREATE_CURVE) {
10885 _createCurve (data);
109- } else if (instruction == CREATE_PAIR) {
86+ } else if (instruction == Instructions. CREATE_PAIR) {
11087 _createPair (data);
11188 } else {
11289 revert UnknownInstruction ();
11390 }
11491
11592 // note: Only pool interactions have a non-zero poolId.
11693 if (poolId != 0 ) {
117- pairId = uint16 (poolId >> 32 );
94+ uint16 pairId = uint16 (poolId >> 32 );
11895 // Add the pair to the array to track all the pairs that have been interacted with.
11996 _tempPairIds.push (pairId); // note: critical to push the tokens interacted with.
12097 // Caching the addresses to settle the pools interacted with in the fallback function.
12198 Pair memory pair = pairs[pairId]; // note: pairIds start at 1 because nonce is incremented first.
122- if (! addressCache [pair.tokenBase]) _cacheAddress (pair.tokenBase, true );
123- if (! addressCache [pair.tokenQuote]) _cacheAddress (pair.tokenQuote, true );
99+ if (! _addressCache [pair.tokenBase]) _cacheAddress (pair.tokenBase, true );
100+ if (! _addressCache [pair.tokenQuote]) _cacheAddress (pair.tokenQuote, true );
124101 }
125102 }
126103
@@ -144,7 +121,7 @@ contract Compiler is HyperLiquidity, HyperSwap {
144121 /// @param token Target token to pay or credit.
145122 /// @custom:security Critical. Handles crediting accounts or requesting payment for debits.
146123 function _settleToken (address token ) internal {
147- if (! addressCache [token]) return ; // note: Early short circuit, since attempting to settle twice is common for big orders.
124+ if (! _addressCache [token]) return ; // note: Early short circuit, since attempting to settle twice is common for big orders.
148125
149126 uint256 global = globalReserves[token];
150127 uint256 actual = _balanceOf (token, address (this ));
@@ -178,4 +155,6 @@ contract Compiler is HyperLiquidity, HyperSwap {
178155 _applyCredit (token, amount);
179156 SafeTransferLib.safeTransferFrom (ERC20 (token), msg .sender , address (this ), amount);
180157 }
158+
159+ // --- View --- //
181160}
0 commit comments