Skip to content

Commit

Permalink
add documentation for Router and update the MEV section in the docs t…
Browse files Browse the repository at this point in the history
…o reflect new design
  • Loading branch information
jankjr committed Jan 9, 2024
1 parent 69c1bc9 commit 5cf82e1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
7 changes: 4 additions & 3 deletions contracts/plugins/trading/DutchTrade.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import "../../interfaces/ITrade.sol";

interface IDutchTradeCallee {
function dutchTradeCallback(
address caller,
address buyToken,
// {qBuyTok}
uint256 buyAmount,
Expand Down Expand Up @@ -209,7 +208,7 @@ contract DutchTrade is ITrade {
// {qBuyTok}
amountIn = _bidAmount(price);

// Transfer in buy tokens
// Mark bidder
bidder = msg.sender;

// status must begin OPEN
Expand All @@ -219,9 +218,11 @@ contract DutchTrade is ITrade {
if (price > bestPrice.mul(ONE_POINT_FIVE, CEIL)) {
broker.reportViolation();
}

// Transfer buy tokens from bidder
sell.safeTransfer(bidder, lot()); // {qSellTok}
uint256 balanceBefore = buy.balanceOf(address(this)); // {qBuyTok}
IDutchTradeCallee(bidder).dutchTradeCallback(bidder, address(buy), amountIn, data);
IDutchTradeCallee(bidder).dutchTradeCallback(address(buy), amountIn, data);
require(
amountIn <= buy.balanceOf(address(this)) - balanceBefore,
"insufficient buy tokens"
Expand Down
8 changes: 5 additions & 3 deletions contracts/plugins/trading/DutchTradeRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { IDutchTradeCallee, TradeStatus, DutchTrade } from "../trading/DutchTrade.sol";
import { IMain } from "../../interfaces/IMain.sol";

/** @title DutchTradeRouter
* @notice Utility contract for placing bids on DutchTrade auctions
* @dev This contract is needed as end user wallets cannot call DutchTrade.bid directly anymore,
tests and UI need to be updated to use this contract
*/
contract DutchTradeRouter is IDutchTradeCallee {
using SafeERC20 for IERC20;
struct Bid {
Expand Down Expand Up @@ -53,17 +58,14 @@ contract DutchTradeRouter is IDutchTradeCallee {
}

/// @notice Callback for DutchTrade
/// @param caller The caller of the callback, should be the router
/// @param buyToken The token DutchTrade is expecting to receive
/// @param buyAmount The amt the DutchTrade is expecting to receive {qBuyToken}
/// @notice Data is not used here
function dutchTradeCallback(
address caller,
address buyToken,
uint256 buyAmount,
bytes calldata
) external {
require(caller == address(this), "Invalid caller");
require(msg.sender == address(_currentTrade), "Incorrect callee");
IERC20(buyToken).safeTransfer(msg.sender, buyAmount); // {qBuyToken}
}
Expand Down
4 changes: 3 additions & 1 deletion docs/mev.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Bidding instructions from the `DutchTrade` contract:
`DutchTrade` (relevant) interface:

```solidity
function bid() external; // execute a bid at the current block number
function bid(bytes memory data) external; // execute a bid at the current block number
function sell() external view returns (IERC20);
Expand All @@ -43,6 +43,8 @@ function bidAmount(uint256 blockNumber) external view returns (uint256); // {qBu

To participate:

Make sure calling contract implements the `IDutchTradeCallee` interface. It contains a single method function `dutchTradeCallbac(address buyToken,uint256 buyAmount,bytes calldata data) external;`. This method will be called by the `DutchTrade` as a callback after calling `bid` and before the trade has been resolved. The trader is expected to pay for the trade during the callback. See `DutchTradeRouter.sol` for an example.

1. Call `status()` view; the auction is ongoing if return value is 1
2. Call `lot()` to see the number of tokens being sold
3. Call `bidAmount()` to see the number of tokens required to buy the lot, at various block numbers
Expand Down

0 comments on commit 5cf82e1

Please sign in to comment.