Skip to content

Commit

Permalink
Split up solidity files and added README
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Rodrigues Lordello committed Sep 4, 2022
1 parent 99cb3c0 commit 4b4f88b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 27 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# DEXag Trader

This repo contains a daemon that monitors the CoW Protocol orderbook and, for each new order, simulates trading the order amounts on various DEX aggregators.
The simulation records both the expected traded amounts as well as the actual executed amounts.

## Simulation Details

Simulation is done with an `eth_call` with state overrides.
This allows simulations to work even for traders that haven't approved the required contracts.

## Output

The daemon writes to an SQLite database `orders.db`.
It includes details of the original CoW Protocol order and trade simulation results for each DEX aggregator.
30 changes: 3 additions & 27 deletions contracts/Trader.sol
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;

library Caller {
function doCall(address self, bytes memory cdata) internal returns (bytes memory rdata) {
bool success;
(success, rdata) = self.call(cdata);
if (!success) {
assembly { revert(add(rdata, 32), mload(rdata)) }
}
}
}

interface IERC20 {
function balanceOf(address holder) external view returns (uint256);
function approve(address target, uint256 amount) external returns (bool);
}

library SafeERC20 {
using Caller for address;

function safeApprove(IERC20 self, address target, uint256 amount) internal {
bytes memory cdata = abi.encodeCall(self.approve, (target, amount));
bytes memory rdata = address(self).doCall(cdata);
require(
rdata.length == 0 || abi.decode(rdata, (bool)),
"SafeERC20: approval failed"
);
}
}
import { IERC20 } from "./interfaces/IERC20.sol";
import { Caller } from "./libraries/Caller.sol";
import { SafeERC20 } from "./libraries/SafeERC20.sol";

contract Trader {
using Caller for address;
Expand Down
7 changes: 7 additions & 0 deletions contracts/interfaces/IERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;

interface IERC20 {
function balanceOf(address holder) external view returns (uint256);
function approve(address target, uint256 amount) external returns (bool);
}
12 changes: 12 additions & 0 deletions contracts/libraries/Caller.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;

library Caller {
function doCall(address self, bytes memory cdata) internal returns (bytes memory rdata) {
bool success;
(success, rdata) = self.call(cdata);
if (!success) {
assembly { revert(add(rdata, 32), mload(rdata)) }
}
}
}
18 changes: 18 additions & 0 deletions contracts/libraries/SafeERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;

import { IERC20 } from "../interfaces/IERC20.sol";
import { Caller } from "./Caller.sol";

library SafeERC20 {
using Caller for address;

function safeApprove(IERC20 self, address target, uint256 amount) internal {
bytes memory cdata = abi.encodeCall(self.approve, (target, amount));
bytes memory rdata = address(self).doCall(cdata);
require(
rdata.length == 0 || abi.decode(rdata, (bool)),
"SafeERC20: approval failed"
);
}
}

0 comments on commit 4b4f88b

Please sign in to comment.