Skip to content

Commit

Permalink
events
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielZlotin committed Oct 31, 2024
1 parent f0e5430 commit 07120ae
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 62 deletions.
36 changes: 18 additions & 18 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ AdminWithdrawTest:test_tokens() (gas: 879919)
LiquidityHubAccessTest:test_revert_execute_onlyAllowed() (gas: 15345)
LiquidityHubAccessTest:test_revert_reactorCallback_onlyReactor() (gas: 10576)
LiquidityHubAccessTest:test_revert_validationCallback_onlySelf() (gas: 11914)
LiquidityHubExecuteTest:test_decayOnNegativeSlippage() (gas: 340186)
LiquidityHubExecuteTest:test_emitEvents() (gas: 350678)
LiquidityHubExecuteTest:test_gas() (gas: 235042)
LiquidityHubExecuteTest:test_gasToAdmin() (gas: 337151)
LiquidityHubExecuteTest:test_inTokenSlippage() (gas: 375726)
LiquidityHubExecuteTest:test_longLimit() (gas: 339689)
LiquidityHubExecuteTest:test_nativeOutput() (gas: 300821)
LiquidityHubExecuteTest:test_nativeOutputSwapperLimit() (gas: 300898)
LiquidityHubExecuteTest:test_nativeSlippageToRef() (gas: 300492)
LiquidityHubExecuteTest:test_optionalRef() (gas: 299694)
LiquidityHubExecuteTest:test_optionalRefshare() (gas: 299964)
LiquidityHubExecuteTest:test_revert_swapperLimit() (gas: 330265)
LiquidityHubExecuteTest:test_slippageToRef() (gas: 339820)
LiquidityHubExecuteTest:test_swapperLimit() (gas: 337680)
LiquidityHubExecuteTest:test_swapperLimitRespectsSurplus() (gas: 342395)
PartialOrderE2ETest:test_e2e_exactMirrorMatch() (gas: 451079)
PartialOrderE2ETest:test_e2e_multiplePartialInputs() (gas: 623248)
PartialOrderE2ETest:test_e2e_partialInputMatch() (gas: 470935)
LiquidityHubExecuteTest:test_decayOnNegativeSlippage() (gas: 341784)
LiquidityHubExecuteTest:test_emitEvents() (gas: 355157)
LiquidityHubExecuteTest:test_gas() (gas: 236423)
LiquidityHubExecuteTest:test_gasToAdmin() (gas: 338749)
LiquidityHubExecuteTest:test_inTokenSlippage() (gas: 377108)
LiquidityHubExecuteTest:test_longLimit() (gas: 341287)
LiquidityHubExecuteTest:test_nativeOutput() (gas: 302419)
LiquidityHubExecuteTest:test_nativeOutputSwapperLimit() (gas: 302496)
LiquidityHubExecuteTest:test_nativeSlippageToRef() (gas: 302090)
LiquidityHubExecuteTest:test_optionalRef() (gas: 301076)
LiquidityHubExecuteTest:test_optionalRefshare() (gas: 301346)
LiquidityHubExecuteTest:test_revert_swapperLimit() (gas: 332086)
LiquidityHubExecuteTest:test_slippageToRef() (gas: 341418)
LiquidityHubExecuteTest:test_swapperLimit() (gas: 339278)
LiquidityHubExecuteTest:test_swapperLimitRespectsSurplus() (gas: 343993)
PartialOrderE2ETest:test_e2e_exactMirrorMatch() (gas: 452677)
PartialOrderE2ETest:test_e2e_multiplePartialInputs() (gas: 624846)
PartialOrderE2ETest:test_e2e_partialInputMatch() (gas: 472533)
PartialOrderReactorTest:test_execute_swapFullAmount() (gas: 199131)
PartialOrderReactorTest:test_execute_swapPartial() (gas: 309736)
PartialOrderReactorTest:test_execute_swapPartialOdd() (gas: 199121)
Expand Down
2 changes: 1 addition & 1 deletion out/LiquidityHub.sol/LiquidityHub.json

Large diffs are not rendered by default.

44 changes: 33 additions & 11 deletions src/LiquidityHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,31 @@ import {ResolvedOrder, SignedOrder} from "uniswapx/src/base/ReactorStructs.sol";
import {ExclusiveDutchOrder} from "uniswapx/src/lib/ExclusiveDutchOrderLib.sol";

import {Consts} from "./Consts.sol";
import {IAllowed, LiquidityHubLib} from "./LiquidityHubLib.sol";

/**
* LiquidityHub Executor
*/
contract LiquidityHub is IReactorCallback, IValidationCallback {
using SafeERC20 for IERC20;

uint8 public constant VERSION = 5;
error InvalidSender(address sender);
error InvalidOrder();

event Resolved(
bytes32 indexed orderHash,
address indexed swapper,
address indexed ref,
address inToken,
address outToken,
uint256 inAmount,
uint256 outAmount
);

event ExtraOut(address indexed recipient, address token, uint256 amount);

event Surplus(address indexed ref, address swapper, address token, uint256 amount, uint256 refshare);

uint8 public constant VERSION = 6;
address public constant INVALID_ADDRESS = address(1);

IReactor public immutable reactor;
Expand All @@ -33,12 +49,12 @@ contract LiquidityHub is IReactorCallback, IValidationCallback {
}

modifier onlyAllowed() {
if (!allowed.allowed(msg.sender)) revert LiquidityHubLib.InvalidSender(msg.sender);
if (!allowed.allowed(msg.sender)) revert InvalidSender(msg.sender);
_;
}

modifier onlyReactor() {
if (msg.sender != address(reactor)) revert LiquidityHubLib.InvalidSender(msg.sender);
if (msg.sender != address(reactor)) revert InvalidSender(msg.sender);
_;
}

Expand All @@ -54,9 +70,9 @@ contract LiquidityHub is IReactorCallback, IValidationCallback {
ExclusiveDutchOrder memory o = abi.decode(order.order, (ExclusiveDutchOrder));
(address ref, uint8 share) = abi.decode(o.info.additionalValidationData, (address, uint8));

_surplus(o.info.swapper, ref, address(o.input.token), share);
_surplus(ref, o.info.swapper, address(o.input.token), share);
for (uint256 i = 0; i < o.outputs.length; i++) {
_surplus(o.info.swapper, ref, address(o.outputs[i].token), share);
_surplus(ref, o.info.swapper, address(o.outputs[i].token), share);
}
}

Expand All @@ -76,7 +92,7 @@ contract LiquidityHub is IReactorCallback, IValidationCallback {

address ref = abi.decode(order.info.additionalValidationData, (address));

emit LiquidityHubLib.Resolved(
emit Resolved(
order.hash, order.info.swapper, ref, address(order.input.token), outToken, order.input.amount, outAmount
);
}
Expand All @@ -97,15 +113,17 @@ contract LiquidityHub is IReactorCallback, IValidationCallback {
_outputReactor(token, amount);

if (order.outputs[i].recipient == order.info.swapper) {
if (outToken != INVALID_ADDRESS && outToken != token) revert LiquidityHubLib.InvalidOrder();
if (outToken != INVALID_ADDRESS && outToken != token) revert InvalidOrder();
outToken = token;
outAmount += amount;
} else {
emit ExtraOut(order.outputs[i].recipient, token, amount);
}
}
}
}

function _surplus(address swapper, address ref, address token, uint8 share) private {
function _surplus(address ref, address swapper, address token, uint8 share) private {
uint256 balance = _balanceOf(token, address(this));
if (balance == 0) return;

Expand All @@ -114,7 +132,7 @@ contract LiquidityHub is IReactorCallback, IValidationCallback {
if (refshare > 0) _transfer(token, ref, refshare);
_transfer(token, swapper, balance - refshare);

emit LiquidityHubLib.Surplus(swapper, ref, token, balance, refshare);
emit Surplus(ref, swapper, token, balance, refshare);
}

function _outputReactor(address token, uint256 amount) private {
Expand All @@ -135,10 +153,14 @@ contract LiquidityHub is IReactorCallback, IValidationCallback {
* @dev IValidationCallback
*/
function validate(address filler, ResolvedOrder calldata) external view override {
if (filler != address(this)) revert LiquidityHubLib.InvalidSender(filler);
if (filler != address(this)) revert InvalidSender(filler);
}

receive() external payable {
// accept ETH
}
}

interface IAllowed {
function allowed(address) external view returns (bool);
}
25 changes: 0 additions & 25 deletions src/LiquidityHubLib.sol

This file was deleted.

8 changes: 4 additions & 4 deletions test/LiquidityHub.access.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "forge-std/Test.sol";

import {BaseTest} from "test/base/BaseTest.sol";

import {LiquidityHub, LiquidityHubLib, IReactor, ResolvedOrder, SignedOrder, IMulticall3} from "src/LiquidityHub.sol";
import {LiquidityHub, IReactor, ResolvedOrder, SignedOrder, IMulticall3} from "src/LiquidityHub.sol";

contract LiquidityHubAccessTest is BaseTest {
address public owner;
Expand All @@ -22,19 +22,19 @@ contract LiquidityHubAccessTest is BaseTest {

function test_revert_execute_onlyAllowed() public {
SignedOrder memory order;
vm.expectRevert(abi.encodeWithSelector(LiquidityHubLib.InvalidSender.selector, address(this)));
vm.expectRevert(abi.encodeWithSelector(LiquidityHub.InvalidSender.selector, address(this)));
uut.execute(order, new IMulticall3.Call[](0), 0);
}

function test_revert_validationCallback_onlySelf() public {
ResolvedOrder memory order;
address filler = makeAddr("unknown filler");
vm.expectRevert(abi.encodeWithSelector(LiquidityHubLib.InvalidSender.selector, filler));
vm.expectRevert(abi.encodeWithSelector(LiquidityHub.InvalidSender.selector, filler));
config.executor.validate(filler, order);
}

function test_revert_reactorCallback_onlyReactor() public {
vm.expectRevert(abi.encodeWithSelector(LiquidityHubLib.InvalidSender.selector, address(this)));
vm.expectRevert(abi.encodeWithSelector(LiquidityHub.InvalidSender.selector, address(this)));
config.executor.reactorCallback(new ResolvedOrder[](0), abi.encode(new IMulticall3.Call[](0)));
}
}
25 changes: 22 additions & 3 deletions test/LiquidityHub.execute.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "forge-std/Test.sol";

import {BaseTest, ERC20Mock, IERC20} from "test/base/BaseTest.sol";

import {LiquidityHubLib, SignedOrder, IMulticall3} from "src/LiquidityHub.sol";
import {SignedOrder, IMulticall3} from "src/LiquidityHub.sol";
import {ExclusiveDutchOrder, ExclusiveDutchOrderLib} from "uniswapx/src/lib/ExclusiveDutchOrderLib.sol";

contract LiquidityHubExecuteTest is BaseTest {
Expand Down Expand Up @@ -248,7 +248,10 @@ contract LiquidityHubExecuteTest is BaseTest {
hoax(config.admin.owner());

vm.expectEmit(address(config.executor));
emit LiquidityHubLib.Resolved(
emit ExtraOut(address(config.admin), address(outToken), gasAmount);

vm.expectEmit(address(config.executor));
emit Resolved(
ExclusiveDutchOrderLib.hash(o),
o.info.swapper,
ref,
Expand All @@ -257,8 +260,24 @@ contract LiquidityHubExecuteTest is BaseTest {
inAmount,
outAmount
);
emit LiquidityHubLib.Surplus(o.info.swapper, ref, address(outToken), slippage, refshare);

vm.expectEmit(address(config.executor));
emit Surplus(ref, o.info.swapper, address(outToken), slippage, slippage * refshare / 100);

config.executor.execute(order, calls, 0);
}

event Resolved(
bytes32 indexed orderHash,
address indexed swapper,
address indexed ref,
address inToken,
address outToken,
uint256 inAmount,
uint256 outAmount
);

event Surplus(address indexed ref, address swapper, address token, uint256 amount, uint256 refshare);

event ExtraOut(address indexed recipient, address token, uint256 amount);
}

0 comments on commit 07120ae

Please sign in to comment.